From e5d1c949ac4c15e353160a5006ac3a8cea14dac5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Tue, 12 Jan 2010 13:14:19 +0100 Subject: add _read_size and _write_size to lf_ringbuffer --- lf_ringbuffer.c | 28 +++++++++++++++++++++++++++- lf_ringbuffer.h | 6 ++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/lf_ringbuffer.c b/lf_ringbuffer.c index facf055..48b848e 100644 --- a/lf_ringbuffer.c +++ b/lf_ringbuffer.c @@ -83,7 +83,7 @@ lf_ringbuffer_t* lf_ringbuffer_create( size_t n_buf ) { return r; } -/* destroy an lf_ringbuffer strcture */ +/* destroy an lf_ringbuffer struct */ void lf_ringbuffer_destroy( lf_ringbuffer_t *r ) { free(r->buffer); free(r); @@ -92,6 +92,32 @@ void lf_ringbuffer_destroy( lf_ringbuffer_t *r ) { /* return 1 if is empty */ int lf_ringbuffer_empty( lf_ringbuffer_t *r ) { return (r->indexes>>16)==USHORTMAX; } +/* return count of filled buffers */ +size_t lf_ringbuffer_read_size( lf_ringbuffer_t *r ) { + unsigned int write_to, read_from; + read_from = (r->indexes)>>16; + if(read_from==USHORTMAX) return 0; + write_to = (r->indexes)&0xffff; + if(write_to>read_from) { + return write_to-read_from; + } else { + return r->n_buf-read_from+write_to; + } +} + +/* return count of available buffers */ +size_t lf_ringbuffer_write_size( lf_ringbuffer_t *r ) { + unsigned int write_to, read_from; + read_from = (r->indexes)>>16; + if(read_from==USHORTMAX) return r->n_buf; + write_to = (r->indexes)&0xffff; + if(write_to>read_from) { + return r->n_buf-write_to+read_from; + } else { + return read_from-write_to; + } +} + /* write data into the ring buffer */ int lf_ringbuffer_write( lf_ringbuffer_t *r, void *data, int flags ) { unsigned int current, next; diff --git a/lf_ringbuffer.h b/lf_ringbuffer.h index a5dc0cb..feb5f17 100644 --- a/lf_ringbuffer.h +++ b/lf_ringbuffer.h @@ -54,6 +54,12 @@ void lf_ringbuffer_destroy( lf_ringbuffer_t *r ); /* return 1 if is empty */ int lf_ringbuffer_empty( lf_ringbuffer_t *r ); +/* return the count of filled buffers */ +size_t lf_ringbuffer_read_size( lf_ringbuffer_t *r ); + +/* return the count of available buffers */ +size_t lf_ringbuffer_write_size( lf_ringbuffer_t *r ); + /* write data into the ring buffer * return 0 on success * return -1 if IS_NOT_BLOCKING and buffer is full -- cgit v1.1-2-g2b99