29 lines
No EOL
818 B
C
29 lines
No EOL
818 B
C
/* 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,
|
|
int (*memcpy)(void *, const void *, int)) {
|
|
if (rb->write_idx == rb->read_idx) {
|
|
return CollisionError;
|
|
}
|
|
|
|
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;
|
|
} |