diff options
author | Yorhel <git@yorhel.nl> | 2012-12-04 13:18:47 +0100 |
---|---|---|
committer | Yorhel <git@yorhel.nl> | 2012-12-04 13:18:47 +0100 |
commit | 5814a4e347928749437f121895776b2160d801fe (patch) | |
tree | b316f05d9a677467461dd30164ec258ea23c7262 | |
parent | 188bde96ff21a7400c3faff8d4d35380a7c24878 (diff) |
ecbuf: Remove _pry() macro
It's rather trivial to implement if the using application needs it.
Not necessary to always keep an extra variable around for functionality
that may not be used.
-rw-r--r-- | ecbuf.h | 28 | ||||
-rw-r--r-- | test/ecbuf.c | 5 |
2 files changed, 12 insertions, 21 deletions
@@ -21,8 +21,8 @@ */ /* This is an efficient circular buffer implementation that automatically - * expands the buffer when it's full. As such, it behaves like any other FIFO - * queue. + * expands the buffer when it's full. As such, it behaves like any other + * unbounded FIFO queue. * * Usage: * @@ -35,8 +35,6 @@ * ecbuf_push(queue, 7); * ecbuf_push(queue, 11); * - * // = 11 - * printf("Most recently queued = %d\n", ecbuf_pry(queue)); * // = 3 * printf("Least recently queued = %d\n", ecbuf_peek(queue)); * // = 4 @@ -53,9 +51,8 @@ * The concept is explained at * http://blog.labix.org/2010/12/23/efficient-algorithm-for-expanding-circular-buffers * - * This implementation is slightly different, in that it provides a macro to - * read back the most recently written value, without adding more overhead in - * terms of the number of variables to keep track of. + * This implementation is slightly different, in that it is slightly smaller + * and requires one less variable to keep track of. */ #ifndef ECBUF_H @@ -66,14 +63,13 @@ /* The variables are: * l: Number of items in the queue - * i: Last index we've written to in the last push() * o: Index we're going to read from in the next pop() * b: Index of the last written item before the buffer has been expanded. * cn: Number of slots in the cirbular buffer * bn: Number of slots in the complete buffer */ #define ecbuf_t(type) struct {\ - int l, i, o, b, cn, bn;\ + int l, o, b, cn, bn;\ type *a;\ } @@ -83,7 +79,7 @@ typedef ecbuf_t(void) ecbuf__t; #define ecbuf_init(v) do {\ (v).bn = (v).cn = 32;\ - (v).o = (v).i = (v).l = 0;\ + (v).o = (v).l = 0;\ (v).b = -1;\ (v).a = malloc((v).bn*sizeof(*(v).a));\ } while(0) @@ -98,10 +94,6 @@ typedef ecbuf_t(void) ecbuf__t; /* Peek into the queue, requires !ecbuf_empty(v) */ #define ecbuf_peek(v) ((v).a[(v).o]) -/* Similar to peek, but reads back the most *recently* queued item. - * (I'm bad at naming, I know) */ -#define ecbuf_pry(v) ((v).a[(v).i]) - #define ecbuf_push(v, x) do {\ if((v).l == (v).bn) {\ if((v).cn == (v).bn)\ @@ -109,11 +101,11 @@ typedef ecbuf_t(void) ecbuf__t; (v).bn <<= 1;\ (v).a = realloc((v).a, (v).bn*sizeof(*(v).a));\ }\ - (v).i = (v).l + (v).o - (v).b - 1;\ - if((v).bn == (v).cn) (v).i &= (v).cn-1;\ - else if((v).o <= (v).b) (v).i += (v).cn;\ + int _ecbuf_i = (v).l + (v).o - (v).b - 1;\ + if((v).bn == (v).cn) _ecbuf_i &= (v).cn-1;\ + else if((v).o <= (v).b) _ecbuf_i += (v).cn;\ (v).l++;\ - (v).a[(v).i] = (x);\ + (v).a[_ecbuf_i] = (x);\ } while(0) diff --git a/test/ecbuf.c b/test/ecbuf.c index db2dc2f..1027e58 100644 --- a/test/ecbuf.c +++ b/test/ecbuf.c @@ -30,7 +30,7 @@ /* This is the original implementation provided in the article (converted to C, - * obviously). Provided here for testing, but doesn't have a _pry(). */ + * obviously). Provided here for testing. */ #else #define ecbuf_t(type) struct {\ @@ -47,7 +47,6 @@ typedef ecbuf_t(void) ecbuf__t; #define ecbuf_len(v) ((v).buflen) #define ecbuf_empty(v) ((v).buflen == 0) #define ecbuf_peek(v) ((v).a[(v).popi]) -#define ecbuf_pry(v) /* Doesn't work */ #define ecbuf_push(v, x) do {\ int expandbuf = 0, expandring = 0;\ if((v).ringcap != (v).bufcap) {\ @@ -159,7 +158,7 @@ int main(int argc, char **argv) { for(i=0; i<100; i++) { for(j=0; j<100; j++) { ecbuf_push(lst, w); - assert(ecbuf_pry(lst) == w++); + w++; assert(ecbuf_len(lst) == w-r); } for(j=0; j<99; j++) { |