summaryrefslogtreecommitdiffstats
path: root/lf_ringbuffer.c
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2010-01-12 13:14:19 +0100
committerJérémy Zurcher <jeremy@asynk.ch>2010-01-12 13:14:19 +0100
commite5d1c949ac4c15e353160a5006ac3a8cea14dac5 (patch)
treef71b9e585ce08a14f6f10c65d5f85bde13732a79 /lf_ringbuffer.c
parent1b393afeafa57a1a9baf72834b4f75388361931e (diff)
downloadlock_free-e5d1c949ac4c15e353160a5006ac3a8cea14dac5.zip
lock_free-e5d1c949ac4c15e353160a5006ac3a8cea14dac5.tar.gz
add _read_size and _write_size to lf_ringbuffer
Diffstat (limited to 'lf_ringbuffer.c')
-rw-r--r--lf_ringbuffer.c28
1 files changed, 27 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;