More cleaing
This commit is contained in:
parent
46a341e537
commit
0c89aa8bfc
2 changed files with 14 additions and 9 deletions
16
ringbuf.c
16
ringbuf.c
|
@ -34,9 +34,8 @@ rb_destroy(struct RingBuf *rb, void(free)())
|
||||||
enum WriteResult
|
enum WriteResult
|
||||||
rb_push_back(struct RingBuf *rb, const void *item, MEMCPY_T memcpy_fn)
|
rb_push_back(struct RingBuf *rb, const void *item, MEMCPY_T memcpy_fn)
|
||||||
{
|
{
|
||||||
if(rb->count == rb->capacity) {
|
if(rb->count == rb->capacity)
|
||||||
// handle error
|
return Full;
|
||||||
}
|
|
||||||
|
|
||||||
memcpy_fn(rb->write_head, item, rb->size);
|
memcpy_fn(rb->write_head, item, rb->size);
|
||||||
|
|
||||||
|
@ -48,15 +47,18 @@ rb_push_back(struct RingBuf *rb, const void *item, MEMCPY_T memcpy_fn)
|
||||||
rb->count++;
|
rb->count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
enum ReadResult
|
||||||
rb_pop_front(struct RingBuf *rb, void *item)
|
rb_pop_front(struct RingBuf *rb, void *item)
|
||||||
{
|
{
|
||||||
if(rb->count == 0) {
|
if(rb->count == 0)
|
||||||
// handle error
|
return Empty;
|
||||||
}
|
|
||||||
memcpy(item, rb->read_head, rb->size);
|
memcpy(item, rb->read_head, rb->size);
|
||||||
|
|
||||||
rb->read_head = (char *)rb->read_head + rb->size;
|
rb->read_head = (char *)rb->read_head + rb->size;
|
||||||
|
|
||||||
if(rb->read_head == rb->buffer_end)
|
if(rb->read_head == rb->buffer_end)
|
||||||
rb->read_head = rb->buffer;
|
rb->read_head = rb->buffer;
|
||||||
|
|
||||||
rb->count--;
|
rb->count--;
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,7 +24,10 @@ struct RingBuf {
|
||||||
void *buffer_end; /* The end of the buffer */
|
void *buffer_end; /* The end of the buffer */
|
||||||
} __attribute__((packed));
|
} __attribute__((packed));
|
||||||
|
|
||||||
enum WriteResult { CollisionError, Ok };
|
// TODO: Perhaps unify these to RBResult?
|
||||||
|
|
||||||
|
enum WriteResult { Full, WriteOk }; /** Result of a write */
|
||||||
|
enum ReadResult { Empty, ReadOk }; /** Result of a read */
|
||||||
|
|
||||||
/** Initialize the ring buffer */
|
/** Initialize the ring buffer */
|
||||||
void rb_init(struct RingBuf *rb, rb_size_t capacity, void *(*alloc)(rb_size_t),
|
void rb_init(struct RingBuf *rb, rb_size_t capacity, void *(*alloc)(rb_size_t),
|
||||||
|
@ -35,7 +38,7 @@ enum WriteResult rb_push_back(struct RingBuf *rb, const void *item,
|
||||||
MEMCPY_T memcpy_fn);
|
MEMCPY_T memcpy_fn);
|
||||||
|
|
||||||
/** Read data from the ring buffer */
|
/** Read data from the ring buffer */
|
||||||
void rb_pop_front(struct RingBuf *rb, void *item);
|
enum ReadResult rb_pop_front(struct RingBuf *rb, void *item);
|
||||||
|
|
||||||
/** Free the ring buffer */
|
/** Free the ring buffer */
|
||||||
void rb_destroy(struct RingBuf *rb, void(free)());
|
void rb_destroy(struct RingBuf *rb, void(free)());
|
||||||
|
|
Loading…
Reference in a new issue