From ec6cc83bd1997144fd2faa137f5086689ce02d37 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Sun, 30 Jun 2024 20:06:30 +0200 Subject: [PATCH] First working revision --- ringbuf.c | 11 +++++++---- ringbuf.h | 1 - 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/ringbuf.c b/ringbuf.c index b5ca393..ae79968 100644 --- a/ringbuf.c +++ b/ringbuf.c @@ -14,6 +14,9 @@ rb_init(struct RingBuf *rb, rb_size_t capacity, ALLOC_T malloc_fn, rb->capacity = capacity; rb->buffer = malloc_fn(capacity * struct_size); /* Calloc? */ 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 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) 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) rb->write_head = rb->buffer; @@ -54,9 +57,9 @@ rb_pop_front(struct RingBuf *rb, void *item) if(rb->count == 0) 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) rb->read_head = rb->buffer; diff --git a/ringbuf.h b/ringbuf.h index 482c1b8..bffc0f2 100644 --- a/ringbuf.h +++ b/ringbuf.h @@ -16,7 +16,6 @@ typedef void *(*MEMCPY_T)(void *, const void *, rb_size_t); struct RingBuf { rb_size_t struct_size; /* Size of the struct */ 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 */ void *write_head; /* Address of the write head */ void *read_head; /* Address of the read head */