Skip to content

Commit

Permalink
ringbuffer: add "has_data" helper function
Browse files Browse the repository at this point in the history
The ringbuffers are opaque, so the only way to check if a buffer has
data is to add this simple helper function.

Signed-off-by: Chris Guikema <[email protected]>
  • Loading branch information
chrisguikema committed Oct 26, 2022
1 parent 7c0f71e commit 96170d3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
6 changes: 6 additions & 0 deletions libringbuffer/include/ringbuffer/ringbuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ typedef struct ringbuffer ringbuffer_t;
*/
ringbuffer_t *rb_new(void *base, size_t size);

/* Check if ring buffer has data
* r - Buffer to check
* Returns Boolean representing if ringbuffer has data.
*/
int rb_has_data(ringbuffer_t *r);

/* Send a byte.
* r - Buffer to send via.
* c - Byte to send.
Expand Down
5 changes: 5 additions & 0 deletions libringbuffer/src/ringbuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ ringbuffer_t *rb_new(void *base, size_t size)
return r;
}

int rb_has_data(ringbuffer_t *r)
{
return (r->base[r->offset] == 0);
}

void rb_transmit_byte(ringbuffer_t *r, unsigned char c)
{
/* We can't send 0s. */
Expand Down

0 comments on commit 96170d3

Please sign in to comment.