2 * UCW Library -- A simple growing buffer
4 * (c) 2004, Robert Spalek <robert@ucw.cz>
5 * (c) 2005, Martin Mares <mj@ucw.cz>
7 * Define the following macros:
9 * GBUF_TYPE data type of records stored in the buffer
10 * GBUF_PREFIX(x) add a name prefix to all global symbols
11 * GBUF_TRACE(msg...) log growing of buffer [optional]
13 * This software may be freely distributed and used according to the terms
14 * of the GNU Lesser General Public License.
17 #define BUF_T GBUF_PREFIX(t)
27 GBUF_PREFIX(init)(BUF_T *b)
34 GBUF_PREFIX(done)(BUF_T *b)
43 GBUF_PREFIX(set_size)(BUF_T *b, uns len)
46 b->ptr = xrealloc(b->ptr, len * sizeof(GBUF_TYPE));
48 GBUF_TRACE(STRINGIFY_EXPANDED(BUF_T) " growing to %u items", len);
53 GBUF_PREFIX(do_grow)(BUF_T *b, uns len)
55 if (len < 2*b->len) // to ensure logarithmic cost
57 GBUF_PREFIX(set_size)(b, len);
60 static inline GBUF_TYPE *
61 GBUF_PREFIX(grow)(BUF_T *b, uns len)
63 if (unlikely(len > b->len))
64 GBUF_PREFIX(do_grow)(b, len);