3 * Dynamically growing dense arrays
5 * (c) 1999 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
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * mLib is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public 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
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
37 /*----- Magic numbers -----------------------------------------------------*/
39 #define DA_INITSZ 16 /* Default size for new array */
40 #define DA_SLOTS 8 /* Number of preshifted slots */
42 /*----- Main code ---------------------------------------------------------*/
44 /* --- @da_ensure@ --- *
46 * Arguments: @da_base *b@ = pointer to array base structure
47 * @void *v@ = pointer to array vector
48 * @size_t sz@ = size of individual array elements
49 * @size_t n@ = number of items required at the end
51 * Returns: Pointer to newly allocated or adjusted array vector.
53 * Use: Extends a dynamic array to accommodate a number of new items
54 * at its end. This function is a helper for the @DA_ENSURE@
55 * macro, which should be used by preference.
58 void *da_ensure(da_base *b, void *v, size_t sz, size_t n)
60 size_t rq = n + b->len;
65 /* --- Make sure there's something which needs doing --- *
67 * If there's enough space already then return immediately.
73 /* --- Compute a number of `unshift' slots --- *
75 * When returning from this function, the offset will be set to @slots@.
76 * If @unshift@ is zero, there's no point in reserving slots. Otherwise
77 * choose a power of two greater than @unshift@, with a minimum of
78 * @DA_SLOTS@. Then add the number of slots to the requirement.
85 while (slots < b->unshift)
90 /* --- Maybe just shunt data around a bit --- *
92 * If the vector is large enough, then theoretically we could cope by
93 * moving the objects about in their existing storage. It's not worth
94 * bothering if there's not actually double the amount of space I need.
97 if (rq * 2 < b->sz + b->off) {
98 q = p - (b->off - slots) * sz;
99 memmove(q, p, b->len * sz);
100 b->sz += b->off - slots;
102 b->unshift = b->push = 0;
106 /* --- Decide on a new size --- *
108 * There's a minimum possible size for the array which is used if it's
109 * currently completely empty. Otherwise I choose the smallest power of
110 * two which is big enough, starting at double the current size.
113 nsz = b->sz + b->off;
114 GROWBUF_SIZE(size_t, nsz, rq, DA_INITSZ, sz);
116 /* --- Reallocate the block --- *
118 * If I'm not changing the base offset then it's worth using @realloc@;
119 * otherwise there'll probably be two calls to @memcpy@ to shunt the data
120 * around so it's not worth bothering.
123 if (p && slots == b->off) {
124 q = x_realloc(b->a, p - b->off * sz, nsz * sz, b->sz + b->off);
127 q = x_alloc(b->a, nsz * sz);
130 memcpy(q, p, b->len * sz);
131 x_free(b->a, p - b->off * sz);
135 /* --- Fill in the other parts of the base structure --- */
139 b->unshift = b->push = 0;
143 /* --- @da_shunt@ --- *
145 * Arguments: @da_base *b@ = pointer to array base structure
146 * @void *v@ = pointer to array vector
147 * @size_t sz@ = size of the array elements
148 * @size_t n@ = number of items required at the start
150 * Returns: Pointer to appropriately bodged vector.
152 * Use: Extends an array to accommodate items inserted at its front.
153 * This function is a helper for the @DA_SHUNT@ macro, which
154 * should be used by preference.
157 void *da_shunt(da_base *b, void *v, size_t sz, size_t n)
164 /* --- Make sure there's something which needs doing --- *
166 * If there's enough space already then return immediately.
172 /* --- Compute a number of `push' slots --- *
174 * When returning from this function, there will be @slots@ free spaces at
175 * the end of the array. If @push@ is zero, there's no point in reserving
176 * slots. Otherwise choose a power of two greater than @push@, with a
177 * minimum of @DA_SLOTS@. To simplify matters, add the number of items
178 * already in the array to @slots@, and then add the number of slots to the
186 while (slots < b->push)
192 /* --- Maybe just shunt data around a bit --- *
194 * If the vector is large enough, then theoretically we could cope by
195 * moving the objects about in their existing storage. Again, if there's
196 * not actually twice the space needed, reallocate the array.
199 if (rq * 2 < b->sz + b->off) {
200 q = p + (b->sz - slots) * sz;
201 memmove(q, p, b->len * sz);
202 b->off += b->sz - slots;
204 b->unshift = b->push = 0;
208 /* --- Reallocate the array --- *
210 * The neat @realloc@ code doesn't need to be here: the offset changes
211 * almost all the time -- that's the whole point of this routine!
214 /* --- Decide on a new size --- *
216 * There's a minimum possible size for the array which is used if it's
217 * currently completely empty. Otherwise I choose the smallest power of
218 * two which is big enough, starting at double the current size.
221 nsz = b->sz + b->off;
222 GROWBUF_SIZE(size_t, nsz, rq, DA_INITSZ, sz);
224 /* --- Reallocate the block --- *
226 * The neat @realloc@ code doesn't need to be here: the offset changes
227 * almost all the time -- that's the whole point of this routine!
230 q = x_alloc(b->a, nsz * sz);
231 q += (nsz - slots) * sz;
233 memcpy(q, p, b->len * sz);
234 x_free(b->a, p - b->off * sz);
237 /* --- Fill in the other parts of the base structure --- */
239 b->off = nsz - slots;
241 b->unshift = b->push = 0;
245 /* --- @da_tidy@ --- *
247 * Arguments: @da_base *b@ = pointer to array base structure
248 * @void *v@ = pointer to vector
249 * @size_t sz@ = size of the array elements
251 * Returns: Newly allocated vector.
253 * Use: Minimizes the space occupied by an array. This function is a
254 * helper for the @DA_TIDY@ macro, which should be used by
258 void *da_tidy(da_base *b, void *v, size_t sz)
262 b->unshift = b->push = 0;
266 if (b->sz == b->len && b->off == 0)
270 xfree(p - b->off * sz);
274 q = x_alloc(b->a, b->len * sz);
275 memcpy(q, p, b->len * sz);
276 x_free(b->a, p - b->off * sz);
282 /* --- Note about testing --- *
284 * The test rig for this code is split into three parts. There's `da-gtest',
285 * which is a Perl script which generates a list of commands. The `da-ref'
286 * Perl script interprets these commands as operations on a Perl array. It's
287 * relatively conservatively written and believed to be reliable. The
288 * `da-test.c' file implements a command reader for the same syntax and
289 * performs the operations on an integer darray, producing output in the same
290 * format. To test darray, generate a command script with `da-gtest', pass
291 * it through both `da-ref' and `da-test' (the result of compiling
292 * da-test.c'), and compare the results. If they're not byte-for-byte
293 * identical, there's something wrong.
296 /*----- That's all, folks -------------------------------------------------*/