summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--.gitignore3
-rw-r--r--lf_ringbuffer.c18
-rw-r--r--lf_ringbuffer.h18
-rw-r--r--lf_ringbuffer_test.c34
4 files changed, 36 insertions, 37 deletions
diff --git a/.gitignore b/.gitignore
index 2bdb0e4..4ea41d1 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3,6 +3,5 @@
cas
container_of
lf_fifo_test
-lf_ring_buffer_test
-lf_ring_buffer_test2
+lf_ringbuffer_test
lock_free_queue_test
diff --git a/lf_ringbuffer.c b/lf_ringbuffer.c
index 99b8766..facf055 100644
--- a/lf_ringbuffer.c
+++ b/lf_ringbuffer.c
@@ -63,13 +63,13 @@
* Thus the maximum size of the buffer is 0xffff-1 = 65534 elements
*/
-/* initialize an empty lf_ring_buffer struct */
-lf_ring_buffer_t* lf_ring_buffer_create( size_t n_buf ) {
+/* initialize an empty lf_ringbuffer struct */
+lf_ringbuffer_t* lf_ringbuffer_create( size_t n_buf ) {
if(n_buf>=USHORTMAX) {
return NULL;
}
- /* alloc ring_buffer struct */
- lf_ring_buffer_t *r = malloc(sizeof(lf_ring_buffer_t));
+ /* alloc ringbuffer struct */
+ lf_ringbuffer_t *r = malloc(sizeof(lf_ringbuffer_t));
if(r==NULL) return NULL;
/* alloc buffer */
r->buffer = malloc(LFRB_BUFFER_SIZE*n_buf);
@@ -83,17 +83,17 @@ lf_ring_buffer_t* lf_ring_buffer_create( size_t n_buf ) {
return r;
}
-/* destroy an lf_ring_buffer strcture */
-void lf_ring_buffer_destroy( lf_ring_buffer_t *r ) {
+/* destroy an lf_ringbuffer strcture */
+void lf_ringbuffer_destroy( lf_ringbuffer_t *r ) {
free(r->buffer);
free(r);
}
/* return 1 if is empty */
-int lf_ring_buffer_empty( lf_ring_buffer_t *r ) { return (r->indexes>>16)==USHORTMAX; }
+int lf_ringbuffer_empty( lf_ringbuffer_t *r ) { return (r->indexes>>16)==USHORTMAX; }
/* write data into the ring buffer */
-int lf_ring_buffer_write( lf_ring_buffer_t *r, void *data, int flags ) {
+int lf_ringbuffer_write( lf_ringbuffer_t *r, void *data, int flags ) {
unsigned int current, next;
unsigned int write_to, read_from;
struct timespec backoff;
@@ -139,7 +139,7 @@ int lf_ring_buffer_write( lf_ring_buffer_t *r, void *data, int flags ) {
}
/* read data from the ring buffer */
-int lf_ring_buffer_read( lf_ring_buffer_t *r, void *data, int flags ) {
+int lf_ringbuffer_read( lf_ringbuffer_t *r, void *data, int flags ) {
unsigned int current, next;
unsigned int write_to, read_from, tmp;
struct timespec backoff;
diff --git a/lf_ringbuffer.h b/lf_ringbuffer.h
index 2266ba2..a5dc0cb 100644
--- a/lf_ringbuffer.h
+++ b/lf_ringbuffer.h
@@ -39,32 +39,32 @@ extern "C" {
#define IS_NOT_BLOCKING( flags ) ( (flags)&LFRB_NO_BLOCK )
/* the ring buffer structure */
-typedef struct ring_buffer {
+typedef struct ringbuffer {
LFRB_BUFFER_TYPE *buffer; /* buffer data */
size_t n_buf; /* number of buffers, max 65534, see implementation for details */
unsigned int indexes; /* indexes where to read_from and write_to */
-} lf_ring_buffer_t;
+} lf_ringbuffer_t;
-/* return an initialized lf_ring_buffer_t struct, size is limited to 65534, see implementation for details */
-lf_ring_buffer_t* lf_ring_buffer_create( size_t n_buf );
+/* return an initialized lf_ringbuffer_t struct, size is limited to 65534, see implementation for details */
+lf_ringbuffer_t* lf_ringbuffer_create( size_t n_buf );
-/* destroy an lf_ring_buffer_t struct */
-void lf_ring_buffer_destroy( lf_ring_buffer_t *r );
+/* destroy an lf_ringbuffer_t struct */
+void lf_ringbuffer_destroy( lf_ringbuffer_t *r );
/* return 1 if is empty */
-int lf_ring_buffer_empty( lf_ring_buffer_t *r );
+int lf_ringbuffer_empty( lf_ringbuffer_t *r );
/* write data into the ring buffer
* return 0 on success
* return -1 if IS_NOT_BLOCKING and buffer is full
*/
-int lf_ring_buffer_write( lf_ring_buffer_t *r, void *data, int flags );
+int lf_ringbuffer_write( lf_ringbuffer_t *r, void *data, int flags );
/* read data from the ring buffer
* return 0 on success
* return -1 if IS_NOT_BLOCKING and buffer is empty
*/
-int lf_ring_buffer_read( lf_ring_buffer_t *r, void *data, int flags );
+int lf_ringbuffer_read( lf_ringbuffer_t *r, void *data, int flags );
# ifdef __cplusplus
}
diff --git a/lf_ringbuffer_test.c b/lf_ringbuffer_test.c
index 8c41b97..eca1cca 100644
--- a/lf_ringbuffer_test.c
+++ b/lf_ringbuffer_test.c
@@ -27,7 +27,7 @@ static void report( char* op, int n, uint64_t dt, int redo ) {
}
struct thread_params {
- lf_ring_buffer_t *ring;
+ lf_ringbuffer_t *ring;
int n;
int flags;
uint64_t dt;
@@ -42,16 +42,16 @@ static void feed_data( int n){
}
*/
-static uint64_t sequential_writes( lf_ring_buffer_t *ring, int n, int flags ) {
+static uint64_t sequential_writes( lf_ringbuffer_t *ring, int n, int flags ) {
int i, redo=0;
rb_data_t data[RB_DATA_LEN];
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
if(flags==0) {
- for(i=0; i<n; i++) lf_ring_buffer_write( ring, data, flags );
+ for(i=0; i<n; i++) lf_ringbuffer_write( ring, data, flags );
} else {
for(i=0; i<n;) {
- if(lf_ring_buffer_write( ring, data, flags )==0) { i++; } else { redo+=1; }
+ if(lf_ringbuffer_write( ring, data, flags )==0) { i++; } else { redo+=1; }
}
}
clock_gettime(CLOCK_MONOTONIC, &end);
@@ -59,16 +59,16 @@ static uint64_t sequential_writes( lf_ring_buffer_t *ring, int n, int flags ) {
return time_diff( &start, &end );
}
-static uint64_t sequential_reads( lf_ring_buffer_t *ring, int n, int flags ) {
+static uint64_t sequential_reads( lf_ringbuffer_t *ring, int n, int flags ) {
int i,redo=0;
rb_data_t data[RB_DATA_LEN];
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
if(flags==0) {
- for(i=0; i<n; i++) lf_ring_buffer_read( ring, data, flags );
+ for(i=0; i<n; i++) lf_ringbuffer_read( ring, data, flags );
} else {
for(i=0; i<n;) {
- if(lf_ring_buffer_read( ring, data, flags )==0) { i++; } else { redo+=1; }
+ if(lf_ringbuffer_read( ring, data, flags )==0) { i++; } else { redo+=1; }
}
}
clock_gettime(CLOCK_MONOTONIC, &end);
@@ -90,7 +90,7 @@ void* reader_thread( void* param ) {
return NULL;
}
-static void parallel_op( int op, int nt, lf_ring_buffer_t *ring, int n, int flags ) {
+static void parallel_op( int op, int nt, lf_ringbuffer_t *ring, int n, int flags ) {
int i;
uint64_t dt;
@@ -139,11 +139,11 @@ int main( int argc, char** argv ) {
int i;
int b_len = BUFFER_LEN;
- lf_ring_buffer_t *ring;
+ lf_ringbuffer_t *ring;
- ring = lf_ring_buffer_create( b_len );
+ ring = lf_ringbuffer_create( b_len );
if(ring==NULL){
- fprintf(stderr,"ERROR : lf_ring_buffer_create( %d );\n",b_len);
+ fprintf(stderr,"ERROR : lf_ringbuffer_create( %d );\n",b_len);
exit( EXIT_FAILURE );
}
@@ -157,34 +157,34 @@ int main( int argc, char** argv ) {
sequential_writes( ring, b_len, 0 );
printf("sequential non blocking read operations ...\n");
sequential_reads( ring, b_len, 0 );
- if(!lf_ring_buffer_empty(ring)) { fprintf(stderr,"ring should be empty but is not\n"); exit( EXIT_FAILURE ); }
+ if(!lf_ringbuffer_empty(ring)) { fprintf(stderr,"ring should be empty but is not\n"); exit( EXIT_FAILURE ); }
printf("sequential blocking write operations ...\n");
sequential_writes( ring, b_len, LFRB_NO_BLOCK );
printf("sequential blocking read operations ...\n");
sequential_reads( ring, b_len, LFRB_NO_BLOCK );
- if(!lf_ring_buffer_empty(ring)) { fprintf(stderr,"ring should be empty but is not\n"); exit( EXIT_FAILURE ); }
+ if(!lf_ringbuffer_empty(ring)) { fprintf(stderr,"ring should be empty but is not\n"); exit( EXIT_FAILURE ); }
for(i=5; i<=50;i*=2) {
printf("%d parallel blocking with backoff inc write operations .... \n",i);
parallel_op( 0, i, ring, b_len, 0 );
printf("parallel blocking read operations ...\n");
sequential_reads( ring, b_len, 0 );
- if(!lf_ring_buffer_empty(ring)) { fprintf(stderr,"ring should be empty but is not\n"); exit( EXIT_FAILURE ); }
+ if(!lf_ringbuffer_empty(ring)) { fprintf(stderr,"ring should be empty but is not\n"); exit( EXIT_FAILURE ); }
}
for(i=5; i<=50;i*=2) {
printf("%d parallel non blocking write operations .... \n",i);
parallel_op( 0, i, ring, b_len, LFRB_NO_BLOCK );
printf("non blocking read operations ...\n");
sequential_reads( ring, b_len, LFRB_NO_BLOCK );
- if(!lf_ring_buffer_empty(ring)) { fprintf(stderr,"ring should be empty but is not\n"); exit( EXIT_FAILURE ); }
+ if(!lf_ringbuffer_empty(ring)) { fprintf(stderr,"ring should be empty but is not\n"); exit( EXIT_FAILURE ); }
}
for(i=10; i<=50;i*=2) {
printf("%d parallel blocking write and read operations .... \n",i*2);
parallel_op( 3, i, ring, b_len, 0 );
- if(!lf_ring_buffer_empty(ring)) { fprintf(stderr,"ring should be empty but is not\n"); exit( EXIT_FAILURE ); }
+ if(!lf_ringbuffer_empty(ring)) { fprintf(stderr,"ring should be empty but is not\n"); exit( EXIT_FAILURE ); }
}
- lf_ring_buffer_destroy( ring );
+ lf_ringbuffer_destroy( ring );
return EXIT_SUCCESS;
}