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
|
||||
rb_push_back(struct RingBuf *rb, const void *item, MEMCPY_T memcpy_fn)
|
||||
{
|
||||
if(rb->count == rb->capacity) {
|
||||
// handle error
|
||||
}
|
||||
if(rb->count == rb->capacity)
|
||||
return Full;
|
||||
|
||||
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++;
|
||||
}
|
||||
|
||||
void
|
||||
enum ReadResult
|
||||
rb_pop_front(struct RingBuf *rb, void *item)
|
||||
{
|
||||
if(rb->count == 0) {
|
||||
// handle error
|
||||
}
|
||||
if(rb->count == 0)
|
||||
return Empty;
|
||||
|
||||
memcpy(item, rb->read_head, rb->size);
|
||||
|
||||
rb->read_head = (char *)rb->read_head + rb->size;
|
||||
|
||||
if(rb->read_head == rb->buffer_end)
|
||||
rb->read_head = rb->buffer;
|
||||
|
||||
rb->count--;
|
||||
}
|
||||
|
|
|
@ -24,7 +24,10 @@ struct RingBuf {
|
|||
void *buffer_end; /* The end of the buffer */
|
||||
} __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 */
|
||||
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);
|
||||
|
||||
/** 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 */
|
||||
void rb_destroy(struct RingBuf *rb, void(free)());
|
||||
|
|
Loading…
Reference in a new issue