X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;ds=sidebyside;f=lib%2Fgbuf.h;h=daf0bfbb6b74be7729c3aa34b56e31d46cae1772;hb=4142115bf20293bef998d1f683ac7ce3008178fb;hp=a188f5d0aa0a7bf3e8d185ea52b001f2be5ebaa2;hpb=e7deac4e1c25fba13d8c3c3929c4e18e8f8a0d21;p=libucw.git diff --git a/lib/gbuf.h b/lib/gbuf.h index a188f5d0..daf0bfbb 100644 --- a/lib/gbuf.h +++ b/lib/gbuf.h @@ -14,11 +14,9 @@ * of the GNU Lesser General Public License. */ -#include - #define BUF_T GBUF_PREFIX(t) -typedef struct +typedef struct BUF_T { uns len; GBUF_TYPE *ptr; @@ -32,7 +30,7 @@ GBUF_PREFIX(init)(BUF_T *b) b->len = 0; } -static inline void +static void UNUSED GBUF_PREFIX(done)(BUF_T *b) { if (b->ptr) @@ -41,8 +39,8 @@ GBUF_PREFIX(done)(BUF_T *b) b->len = 0; } -static inline void -GBUF_PREFIX(realloc)(BUF_T *b, uns len) +static void UNUSED +GBUF_PREFIX(set_size)(BUF_T *b, uns len) { b->len = len; b->ptr = xrealloc(b->ptr, len * sizeof(GBUF_TYPE)); @@ -51,14 +49,20 @@ GBUF_PREFIX(realloc)(BUF_T *b, uns len) #endif } -static inline void -GBUF_PREFIX(grow)(BUF_T *b, uns len) +static void UNUSED +GBUF_PREFIX(do_grow)(BUF_T *b, uns len) { - if (likely(len <= b->len)) - return; if (len < 2*b->len) // to ensure logarithmic cost len = 2*b->len; - GBUF_PREFIX(realloc)(b, len); + GBUF_PREFIX(set_size)(b, len); +} + +static inline GBUF_TYPE * +GBUF_PREFIX(grow)(BUF_T *b, uns len) +{ + if (unlikely(len > b->len)) + GBUF_PREFIX(do_grow)(b, len); + return b->ptr; } #undef GBUF_TYPE