From 6a9b169b7d33e13fe4589368d4a98e8744d607b4 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Sun, 23 Jun 2024 14:08:57 +0200 Subject: [PATCH] Draft --- ringbuf.c | 24 ++++++++++++++++++++++++ ringbuf.h | 24 ++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 ringbuf.c create mode 100644 ringbuf.h diff --git a/ringbuf.c b/ringbuf.c new file mode 100644 index 0000000..980da33 --- /dev/null +++ b/ringbuf.c @@ -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; +} \ No newline at end of file diff --git a/ringbuf.h b/ringbuf.h new file mode 100644 index 0000000..7bcf053 --- /dev/null +++ b/ringbuf.h @@ -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); \ No newline at end of file