rb_push draft

This commit is contained in:
Imbus 2024-06-23 14:15:56 +02:00
parent 6a9b169b7d
commit d640180231
2 changed files with 10 additions and 4 deletions

View file

@ -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); } 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) { if (rb->write_idx == rb->read_idx) {
return CollisionError; 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; return Ok;
} }

View file

@ -18,7 +18,8 @@ void rb_init(struct RingBuf *rb, int capacity, void *(*alloc)(int),
int struct_size); int struct_size);
/** Insert data to the ring buffer */ /** 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 */ /** Read data from the ring buffer */
// void *rb_read(struct RingBuf *rb, int amount); // void *rb_read(struct RingBuf *rb, int amount);