/* SPDX-License-Identifier: MIT */ #include "ringbuf.h" void rb_init(struct RingBuf *rb, rb_size_t capacity, void *(*alloc)(rb_size_t), rb_size_t 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, rb_size_t(free)(void *)) { free(rb->buffer); } enum WriteResult rb_push(struct RingBuf *rb, void *data[], rb_size_t amount, void *(*memcpy)(void *, const void *, rb_size_t)) { if (rb->write_idx + amount >= rb->capacity) { return CollisionError; } for (rb_size_t 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; }