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.
19 #define BUF_T GBUF_PREFIX(t)
29 GBUF_PREFIX(init)(BUF_T *b)
36 GBUF_PREFIX(done)(BUF_T *b)
45 GBUF_PREFIX(set_size)(BUF_T *b, uns len)
48 b->ptr = xrealloc(b->ptr, len * sizeof(GBUF_TYPE));
50 GBUF_TRACE(STRINGIFY_EXPANDED(BUF_T) " growing to %u items", len);
55 GBUF_PREFIX(do_grow)(BUF_T *b, uns len)
57 if (len < 2*b->len) // to ensure logarithmic cost
59 GBUF_PREFIX(set_size)(b, len);
62 static inline GBUF_TYPE *
63 GBUF_PREFIX(grow)(BUF_T *b, uns len)
65 if (unlikely(len > b->len))
66 GBUF_PREFIX(do_grow)(b, len);