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 ------------------------------------------------------*/
39 /*----- Magic numbers -----------------------------------------------------*/
41 #define DA_INITSZ 16 /* Default size for new array */
42 #define DA_SLOTS 8 /* Number of preshifted slots */
44 /*----- Main code ---------------------------------------------------------*/
46 /* --- @da_ensure@ --- *
48 * Arguments: @da_base *b@ = pointer to array base structure
49 * @void *v@ = pointer to array vector
50 * @size_t sz@ = size of individual array elements
51 * @size_t n@ = number of items required at the end
53 * Returns: Pointer to newly allocated or adjusted array vector.
55 * Use: Extends a dynamic array to accommodate a number of new items
56 * at its end. This function is a helper for the @DA_ENSURE@
57 * macro, which should be used by preference.
60 void *da_ensure(da_base *b, void *v, size_t sz, size_t n)
62 size_t rq = n + b->len;
67 /* --- Make sure there's something which needs doing --- *
69 * If there's enough space already then return immediately.
75 /* --- Compute a number of `unshift' slots --- *
77 * When returning from this function, the offset will be set to @slots@.
78 * If @unshift@ is zero, there's no point in reserving slots. Otherwise
79 * choose a power of two greater than @unshift@, with a minimum of
80 * @DA_SLOTS@. Then add the number of slots to the requirement.
87 while (slots < b->unshift)
92 /* --- Maybe just shunt data around a bit --- *
94 * If the vector is large enough, then theoretically we could cope by
95 * moving the objects about in their existing storage. It's not worth
96 * bothering if there's not actually double the amount of space I need.
99 if (rq * 2 < b->sz + b->off) {
100 q = p - (b->off - slots) * sz;
101 memmove(q, p, b->len * sz);
102 b->sz += b->off - slots;
104 b->unshift = b->push = 0;
108 /* --- Decide on a new size --- *
110 * There's a minimum possible size for the array which is used if it's
111 * currently completely empty. Otherwise I choose the smallest power of
112 * two which is big enough, starting at double the current size.
115 nsz = b->sz + b->off;
116 GROWBUF_SIZE(size_t, nsz, rq, DA_INITSZ, sz);
118 /* --- Reallocate the block --- *
120 * If I'm not changing the base offset then it's worth using @realloc@;
121 * otherwise there'll probably be two calls to @memcpy@ to shunt the data
122 * around so it's not worth bothering.
125 if (p && slots == b->off) {
126 q = x_realloc(b->a, p - b->off * sz, nsz * sz, b->sz + b->off);
129 q = x_alloc(b->a, nsz * sz);
132 memcpy(q, p, b->len * sz);
133 x_free(b->a, p - b->off * sz);
137 /* --- Fill in the other parts of the base structure --- */
141 b->unshift = b->push = 0;
145 /* --- @da_shunt@ --- *
147 * Arguments: @da_base *b@ = pointer to array base structure
148 * @void *v@ = pointer to array vector
149 * @size_t sz@ = size of the array elements
150 * @size_t n@ = number of items required at the start
152 * Returns: Pointer to appropriately bodged vector.
154 * Use: Extends an array to accommodate items inserted at its front.
155 * This function is a helper for the @DA_SHUNT@ macro, which
156 * should be used by preference.
159 void *da_shunt(da_base *b, void *v, size_t sz, size_t n)
166 /* --- Make sure there's something which needs doing --- *
168 * If there's enough space already then return immediately.
174 /* --- Compute a number of `push' slots --- *
176 * When returning from this function, there will be @slots@ free spaces at
177 * the end of the array. If @push@ is zero, there's no point in reserving
178 * slots. Otherwise choose a power of two greater than @push@, with a
179 * minimum of @DA_SLOTS@. To simplify matters, add the number of items
180 * already in the array to @slots@, and then add the number of slots to the
188 while (slots < b->push)
194 /* --- Maybe just shunt data around a bit --- *
196 * If the vector is large enough, then theoretically we could cope by
197 * moving the objects about in their existing storage. Again, if there's
198 * not actually twice the space needed, reallocate the array.
201 if (rq * 2 < b->sz + b->off) {
202 q = p + (b->sz - slots) * sz;
203 memmove(q, p, b->len * sz);
204 b->off += b->sz - slots;
206 b->unshift = b->push = 0;
210 /* --- Reallocate the array --- *
212 * The neat @realloc@ code doesn't need to be here: the offset changes
213 * almost all the time -- that's the whole point of this routine!
216 /* --- Decide on a new size --- *
218 * There's a minimum possible size for the array which is used if it's
219 * currently completely empty. Otherwise I choose the smallest power of
220 * two which is big enough, starting at double the current size.
223 nsz = b->sz + b->off;
224 GROWBUF_SIZE(size_t, nsz, rq, DA_INITSZ, sz);
226 /* --- Reallocate the block --- *
228 * The neat @realloc@ code doesn't need to be here: the offset changes
229 * almost all the time -- that's the whole point of this routine!
232 q = x_alloc(b->a, nsz * sz);
233 q += (nsz - slots) * sz;
235 memcpy(q, p, b->len * sz);
236 x_free(b->a, p - b->off * sz);
239 /* --- Fill in the other parts of the base structure --- */
241 b->off = nsz - slots;
243 b->unshift = b->push = 0;
247 /* --- @da_tidy@ --- *
249 * Arguments: @da_base *b@ = pointer to array base structure
250 * @void *v@ = pointer to vector
251 * @size_t sz@ = size of the array elements
253 * Returns: Newly allocated vector.
255 * Use: Minimizes the space occupied by an array. This function is a
256 * helper for the @DA_TIDY@ macro, which should be used by
260 void *da_tidy(da_base *b, void *v, size_t sz)
264 b->unshift = b->push = 0;
268 if (b->sz == b->len && b->off == 0)
272 xfree(p - b->off * sz);
276 q = x_alloc(b->a, b->len * sz);
277 memcpy(q, p, b->len * sz);
278 x_free(b->a, p - b->off * sz);
284 /* --- Note about testing --- *
286 * The test rig for this code is split into three parts. There's `da-gtest',
287 * which is a Perl script which generates a list of commands. The `da-ref'
288 * Perl script interprets these commands as operations on a Perl array. It's
289 * relatively conservatively written and believed to be reliable. The
290 * `da-test.c' file implements a command reader for the same syntax and
291 * performs the operations on an integer darray, producing output in the same
292 * format. To test darray, generate a command script with `da-gtest', pass
293 * it through both `da-ref' and `da-test' (the result of compiling
294 * da-test.c'), and compare the results. If they're not byte-for-byte
295 * identical, there's something wrong.
298 /*----- That's all, folks -------------------------------------------------*/