Draft
This commit is contained in:
parent
23273d09e2
commit
6a9b169b7d
2 changed files with 48 additions and 0 deletions
24
ringbuf.c
Normal file
24
ringbuf.c
Normal file
|
@ -0,0 +1,24 @@
|
|||
/* SPDX-License-Identifier: MIT */
|
||||
|
||||
#include "ringbuf.h"
|
||||
|
||||
void rb_init(struct RingBuf *rb, int capacity, void *(*alloc)(int),
|
||||
int struct_size) {
|
||||
rb->struct_size = struct_size;
|
||||
rb->capacity = capacity;
|
||||
rb->write_idx = 0;
|
||||
rb->read_idx = 0;
|
||||
rb->buffer = alloc(capacity * struct_size); /* Calloc? */
|
||||
}
|
||||
|
||||
void rb_destroy(struct RingBuf *rb, int(free)(void *)) { free(rb->buffer); }
|
||||
|
||||
enum WriteResult rb_push(struct RingBuf *rb, void *data[], int amount) {
|
||||
if (rb->write_idx == rb->read_idx) {
|
||||
return CollisionError;
|
||||
}
|
||||
|
||||
// Iterate over the data and copy it to the buffer
|
||||
|
||||
return Ok;
|
||||
}
|
24
ringbuf.h
Normal file
24
ringbuf.h
Normal file
|
@ -0,0 +1,24 @@
|
|||
/* SPDX-License-Identifier: MIT */
|
||||
|
||||
/**
|
||||
* Ring buffer, also known as circular buffer.
|
||||
*/
|
||||
struct RingBuf {
|
||||
int struct_size; /* Size of the struct */
|
||||
int capacity; /* The physical capacity of the entire ringbuf */
|
||||
int write_idx; /* The write head */
|
||||
int read_idx; /* THe read head */
|
||||
void **buffer; /* The actual data */
|
||||
};
|
||||
|
||||
enum WriteResult { CollisionError, Ok };
|
||||
|
||||
/** Initialize the ring buffer */
|
||||
void rb_init(struct RingBuf *rb, int capacity, void *(*alloc)(int),
|
||||
int struct_size);
|
||||
|
||||
/** Insert data to the ring buffer */
|
||||
enum WriteResult rb_push(struct RingBuf *rb, void *data[], int amount);
|
||||
|
||||
/** Read data from the ring buffer */
|
||||
// void *rb_read(struct RingBuf *rb, int amount);
|
Loading…
Reference in a new issue