]> mj.ucw.cz Git - libucw.git/blobdiff - lib/gbuf.h
Moved endianity settings etc. to the per-CPU section.
[libucw.git] / lib / gbuf.h
index c56cb795a8cb834a487d54ed260c94b5c1996e97..0775f20cb3738bb976176a72af94918322261f9d 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *     A simple growing buffer
+ *     UCW Library -- A simple growing buffer
  *
  *     (c) 2004, Robert Spalek <robert@ucw.cz>
  *
@@ -7,8 +7,13 @@
  *
  *     GBUF_TYPE       data type of records stored in the buffer
  *     GBUF_PREFIX(x)  add a name prefix to all global symbols
+ *
+ *     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
@@ -38,16 +43,13 @@ 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));
+  b->ptr = xrealloc(b->ptr, len * sizeof(GBUF_TYPE));
 }
 
 static inline void
 GBUF_PREFIX(grow)(BUF_T *b, uns len)
 {
-  if (len <= b->len)
+  if (likely(len <= b->len))
     return;
   if (len < 2*b->len)                  // to ensure logarithmic cost
     len = 2*b->len;
@@ -56,3 +58,4 @@ GBUF_PREFIX(grow)(BUF_T *b, uns len)
 
 #undef GBUF_TYPE
 #undef GBUF_PREFIX
+#undef BUF_T