From: Robert Spalek Date: Mon, 28 Jun 2004 09:30:52 +0000 (+0000) Subject: added a simple source-code for resizable buffers, I am going to need it at X-Git-Tag: holmes-import~997 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=a489a95c5c0852fc68a0b091c42acc56c73e576b;p=libucw.git added a simple source-code for resizable buffers, I am going to need it at 3 places, so let us separate it --- diff --git a/lib/gbuf.h b/lib/gbuf.h new file mode 100644 index 00000000..c56cb795 --- /dev/null +++ b/lib/gbuf.h @@ -0,0 +1,58 @@ +/* + * A simple growing buffer + * + * (c) 2004, Robert Spalek + * + * Define the following macros: + * + * GBUF_TYPE data type of records stored in the buffer + * GBUF_PREFIX(x) add a name prefix to all global symbols + */ + +#define BUF_T GBUF_PREFIX(t) + +typedef struct +{ + uns len; + GBUF_TYPE *ptr; +} +BUF_T; + +static inline void +GBUF_PREFIX(init)(BUF_T *b) +{ + b->ptr = NULL; + b->len = 0; +} + +static inline void +GBUF_PREFIX(done)(BUF_T *b) +{ + if (b->ptr) + xfree(b->ptr); + b->ptr = NULL; + b->len = 0; +} + +static inline void +GBUF_PREFIX(realloc)(BUF_T *b, uns len) +{ + b->len = len; + if (b->ptr) + b->ptr = xrealloc(b->ptr, len * sizeof(GBUF_TYPE)); + else + b->ptr = xmalloc(len * sizeof(GBUF_TYPE)); +} + +static inline void +GBUF_PREFIX(grow)(BUF_T *b, uns len) +{ + if (len <= b->len) + return; + if (len < 2*b->len) // to ensure logarithmic cost + len = 2*b->len; + GBUF_PREFIX(realloc)(b, len); +} + +#undef GBUF_TYPE +#undef GBUF_PREFIX