rb_push draft
This commit is contained in:
parent
6a9b169b7d
commit
d640180231
2 changed files with 10 additions and 4 deletions
|
@ -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;
|
||||||
}
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
/* SPDX-License-Identifier: MIT */
|
/* SPDX-License-Identifier: MIT */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Ring buffer, also known as circular buffer.
|
* Ring buffer, also known as circular buffer.
|
||||||
*/
|
*/
|
||||||
struct RingBuf {
|
struct RingBuf {
|
||||||
|
@ -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);
|
Loading…
Reference in a new issue