First working revision
This commit is contained in:
parent
0926a4ccd8
commit
ec6cc83bd1
2 changed files with 7 additions and 5 deletions
11
ringbuf.c
11
ringbuf.c
|
@ -14,6 +14,9 @@ rb_init(struct RingBuf *rb, rb_size_t capacity, ALLOC_T malloc_fn,
|
||||||
rb->capacity = capacity;
|
rb->capacity = capacity;
|
||||||
rb->buffer = malloc_fn(capacity * struct_size); /* Calloc? */
|
rb->buffer = malloc_fn(capacity * struct_size); /* Calloc? */
|
||||||
rb->buffer_end = rb->buffer + (capacity * struct_size);
|
rb->buffer_end = rb->buffer + (capacity * struct_size);
|
||||||
|
rb->count = 0;
|
||||||
|
rb->write_head = rb->buffer;
|
||||||
|
rb->read_head = rb->buffer;
|
||||||
|
|
||||||
// Read from buffer at max position to force a segfault if theres an issue
|
// Read from buffer at max position to force a segfault if theres an issue
|
||||||
printf("Reading from buffer at position %d\n",
|
printf("Reading from buffer at position %d\n",
|
||||||
|
@ -37,9 +40,9 @@ rb_push_back(struct RingBuf *rb, const void *item, MEMCPY_T memcpy_fn)
|
||||||
if(rb->count == rb->capacity)
|
if(rb->count == rb->capacity)
|
||||||
return Full;
|
return Full;
|
||||||
|
|
||||||
memcpy_fn(rb->write_head, item, rb->size);
|
memcpy_fn(rb->write_head, item, rb->struct_size);
|
||||||
|
|
||||||
rb->write_head = (char *)rb->write_head + rb->size;
|
rb->write_head = (char *)rb->write_head + rb->struct_size;
|
||||||
|
|
||||||
if(rb->write_head == rb->buffer_end)
|
if(rb->write_head == rb->buffer_end)
|
||||||
rb->write_head = rb->buffer;
|
rb->write_head = rb->buffer;
|
||||||
|
@ -54,9 +57,9 @@ rb_pop_front(struct RingBuf *rb, void *item)
|
||||||
if(rb->count == 0)
|
if(rb->count == 0)
|
||||||
return Empty;
|
return Empty;
|
||||||
|
|
||||||
memcpy(item, rb->read_head, rb->size);
|
memcpy(item, rb->read_head, rb->struct_size);
|
||||||
|
|
||||||
rb->read_head = (char *)rb->read_head + rb->size;
|
rb->read_head = (char *)rb->read_head + rb->struct_size;
|
||||||
|
|
||||||
if(rb->read_head == rb->buffer_end)
|
if(rb->read_head == rb->buffer_end)
|
||||||
rb->read_head = rb->buffer;
|
rb->read_head = rb->buffer;
|
||||||
|
|
|
@ -16,7 +16,6 @@ typedef void *(*MEMCPY_T)(void *, const void *, rb_size_t);
|
||||||
struct RingBuf {
|
struct RingBuf {
|
||||||
rb_size_t struct_size; /* Size of the struct */
|
rb_size_t struct_size; /* Size of the struct */
|
||||||
rb_size_t capacity; /* The physical capacity of the entire ringbuf */
|
rb_size_t capacity; /* The physical capacity of the entire ringbuf */
|
||||||
rb_size_t size; /* The current size of the ring buffer */
|
|
||||||
rb_size_t count; /* The number of elements in the buffer */
|
rb_size_t count; /* The number of elements in the buffer */
|
||||||
void *write_head; /* Address of the write head */
|
void *write_head; /* Address of the write head */
|
||||||
void *read_head; /* Address of the read head */
|
void *read_head; /* Address of the read head */
|
||||||
|
|
Loading…
Reference in a new issue