diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2010-01-12 13:14:19 +0100 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2010-01-12 13:14:19 +0100 |
commit | e5d1c949ac4c15e353160a5006ac3a8cea14dac5 (patch) | |
tree | f71b9e585ce08a14f6f10c65d5f85bde13732a79 | |
parent | 1b393afeafa57a1a9baf72834b4f75388361931e (diff) | |
download | lock_free-e5d1c949ac4c15e353160a5006ac3a8cea14dac5.zip lock_free-e5d1c949ac4c15e353160a5006ac3a8cea14dac5.tar.gz |
add _read_size and _write_size to lf_ringbuffer
-rw-r--r-- | lf_ringbuffer.c | 28 | ||||
-rw-r--r-- | lf_ringbuffer.h | 6 |
2 files changed, 33 insertions, 1 deletions
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 |