]> mj.ucw.cz Git - libucw.git/blobdiff - lib/gbuf.h
* dev-threads merged to dev-img
[libucw.git] / lib / gbuf.h
index c2853ba1198041a5d5463c569730cfaf05635bad..daf0bfbb6b74be7729c3aa34b56e31d46cae1772 100644 (file)
@@ -1,19 +1,22 @@
 /*
- *     A simple growing buffer
+ *     UCW Library -- A simple growing buffer
  *
  *     (c) 2004, Robert Spalek <robert@ucw.cz>
+ *     (c) 2005, Martin Mares <mj@ucw.cz>
  *
  *     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
+ *     GBUF_TRACE(msg...) log growing of buffer [optional]
+ *
+ *     This software may be freely distributed and used according to the terms
+ *     of the GNU Lesser General Public License.
  */
 
-#include <stdlib.h>
-
 #define        BUF_T   GBUF_PREFIX(t)
 
-typedef struct
+typedef struct BUF_T
 {
   uns len;
   GBUF_TYPE *ptr;
@@ -27,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)
@@ -36,25 +39,33 @@ 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;
-  if (b->ptr)
-    b->ptr = xrealloc(b->ptr, len * sizeof(GBUF_TYPE));
-  else
-    b->ptr = xmalloc(len * sizeof(GBUF_TYPE));
+  b->ptr = xrealloc(b->ptr, len * sizeof(GBUF_TYPE));
+#ifdef GBUF_TRACE
+  GBUF_TRACE(STRINGIFY_EXPANDED(BUF_T) " growing to %u items", 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 (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
 #undef GBUF_PREFIX
+#undef  GBUF_TRACE
+#undef BUF_T