From d6401802316b01bc5be830b6850b438ae727b163 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Sun, 23 Jun 2024 14:15:56 +0200 Subject: [PATCH] rb_push draft --- ringbuf.c | 9 +++++++-- ringbuf.h | 5 +++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/ringbuf.c b/ringbuf.c index 980da33..9c57e68 100644 --- a/ringbuf.c +++ b/ringbuf.c @@ -13,12 +13,17 @@ void rb_init(struct RingBuf *rb, int capacity, void *(*alloc)(int), void rb_destroy(struct RingBuf *rb, int(free)(void *)) { free(rb->buffer); } -enum WriteResult rb_push(struct RingBuf *rb, void *data[], int amount) { +enum WriteResult rb_push(struct RingBuf *rb, void *data[], int amount, + int (*memcpy)(void *, const void *, int)) { if (rb->write_idx == rb->read_idx) { return CollisionError; } - // Iterate over the data and copy it to the buffer + for (int i = 0; i < amount; i++) { + memcpy(rb->buffer + rb->write_idx * rb->struct_size, data[i], + rb->struct_size); + rb->write_idx = (rb->write_idx + 1) % rb->capacity; + } return Ok; } \ No newline at end of file diff --git a/ringbuf.h b/ringbuf.h index 7bcf053..94c5795 100644 --- a/ringbuf.h +++ b/ringbuf.h @@ -1,6 +1,6 @@ /* SPDX-License-Identifier: MIT */ -/** +/** * Ring buffer, also known as circular buffer. */ struct RingBuf { @@ -18,7 +18,8 @@ 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); +enum WriteResult rb_push(struct RingBuf *rb, void *data[], int amount, + int (*memcpy)(void *, const void *, int)); /** Read data from the ring buffer */ // void *rb_read(struct RingBuf *rb, int amount); \ No newline at end of file