3 * Grow a buffer if it's too small
5 * (c) 2024 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the mLib utilities library.
12 * mLib is free software: you can redistribute it and/or modify it under
13 * the terms of the GNU Library General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or (at
15 * your option) any later version.
17 * mLib is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
20 * License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with mLib. If not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
28 #ifndef MLIB_GROWBUF_H
29 #define MLIB_GROWBUF_H
35 /*----- Header files ------------------------------------------------------*/
44 /*----- Macros provided ---------------------------------------------------*/
46 /* --- @GROWBUF_LIMIT@ --- *
48 * Arguments: @size_t granule@ = allocation granule
50 * Returns: The largest number %$n$% such that the total size of %$n$%
51 * objects, each of size @granule@, can be represented in a
55 #define GROWBUF_LIMIT(granule) (~(size_t)0/(granule))
57 /* --- @GROWBUF_SIZE@ --- *
59 * Arguments: @size_t sz@ = the current size (updated)
60 * @size_t want@ = the desired minimum size
61 * @size_t init@ = a suitable initial size
62 * @size_t granule@ = the allocation granule size
66 * Use: On entry, @sz@ should be the current capacity of some buffer,
67 * in terms of objects of size @granule@, and @want@ a needed
68 * capacity, in the same terms, with @want > sz@; @init@ should
69 * be some suitable positive initial size, in case the current
70 * size is zero. The macro updates @sz@ to be some suitable new
71 * positive size at least as large as @want@.
74 #define GROWBUF_SIZE(sz, want, init, granule) do { \
75 size_t _sz_ = (sz), _want_ = (want); \
77 assert(_want_ < GROWBUF_LIMIT(granule)/2); \
78 if (!_sz_) _sz_ = (init); \
79 while (_sz_ < _want_) _sz_ *= 2; \
83 /* --- @GROWBUF_EXTEND@, @GROWBUF_REPLACE@ --- *
85 * Arguments: @arena *a@ = pointer to an arena
86 * @type *buf@ = pointer to some buffer, possibly null (updated)
87 * @size_t sz@ = current buffer size (updated)
88 * @size_t want@ = desired minimum size
89 * @size_t init@ = a suitable initial size
90 * @size_t granule@ = the allocation granule size
94 * Use: On entry, @buf@ should be a pointer to a buffer, allocated
95 * from the arena @a@, with space for @sz@ objects of size
96 * @granule@; @buf@ may be null if @sz@ is zero. On exit, @buf@
97 * and @sz@ will be updated to refer to a possibly different
98 * buffer, with space for at least @want@ objects (but certainly
99 * not smaller than before).
101 * @GROWBUF_EXTEND@ preserves the contents of the buffer;
102 * @GROWBUF_REPLACE@ discards the existing contents.
105 #define GROWBUF_EXTEND(a, buf, sz, want, init, granule) do { \
106 size_t _sz0 = (sz), _sz = _sz0, _want = (want), _gr = (granule); \
111 GROWBUF_SIZE(_sz, _want, init, _gr); \
112 if (!_p) _p = x_alloc(_a, _sz*_gr); \
113 else _p = x_realloc(_a, _p, _sz*_gr, _sz0*_gr); \
114 (buf) = _p; (sz) = _sz; \
118 #define GROWBUF_REPLACE(a, buf, sz, want, init, granule) do { \
119 size_t _sz0 = (sz), _sz = _sz0, _want = (want), _gr = (granule); \
124 GROWBUF_SIZE(_sz, want, init, _gr); \
125 if (_p) x_free(_a, _p); \
126 _p = x_alloc(_a, _sz*_gr); \
127 (buf) = _p; (sz) = _sz; \
131 /*----- That's all, folks -------------------------------------------------*/