3 * $Id: darray.c,v 1.4 1999/11/06 12:40:45 mdw Exp $
5 * Dynamically growing dense arrays
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the mLib utilities library.
14 * mLib is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * mLib is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with mLib; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.4 1999/11/06 12:40:45 mdw
34 * Minor changes to allocation strategy.
36 * Revision 1.3 1999/10/29 22:59:22 mdw
37 * New array adjustment macros for unsigned arguments.
39 * Revision 1.2 1999/10/28 22:05:28 mdw
40 * Modify and debug allocation routines.
42 * Revision 1.1 1999/10/22 22:37:26 mdw
43 * New dynamic array implementation replaces `dynarray.h'.
47 /*----- Header files ------------------------------------------------------*/
56 /*----- Magic numbers -----------------------------------------------------*/
58 #define DA_INITSZ 16 /* Default size for new array */
59 #define DA_SLOTS 8 /* Number of preshifted slots */
61 /*----- Main code ---------------------------------------------------------*/
63 /* --- @da_ensure@ --- *
65 * Arguments: @da_base *b@ = pointer to array base structure
66 * @void *v@ = pointer to array vector
67 * @size_t sz@ = size of individual array elements
68 * @size_t n@ = number of items required at the end
70 * Returns: Pointer to newly allocated or adjusted array vector.
72 * Use: Extends a dynamic array to accommodate a number of new items
73 * at its end. This function is a helper for the @DA_ENSURE@
74 * macro, which should be used by preference.
77 void *da_ensure(da_base *b, void *v, size_t sz, size_t n)
79 size_t rq = n + b->len;
84 /* --- Make sure there's something which needs doing --- *
86 * If there's enough space already then return immediately.
92 /* --- Compute a number of `unshift' slots --- *
94 * When returning from this function, the offset will be set to @slots@.
95 * If @unshift@ is zero, there's no point in reserving slots. Otherwise
96 * choose a power of two greater than @unshift@, with a minimum of
97 * @DA_SLOTS@. Then add the number of slots to the requirement.
104 while (slots < b->unshift)
109 /* --- Maybe just shunt data around a bit --- *
111 * If the vector is large enough, then theoretically we could cope by
112 * moving the objects about in their existing storage. It's not worth
113 * bothering if there's not actually double the amount of space I need.
116 if (rq * 2 < b->sz + b->off) {
117 q = p - (b->off - slots) * sz;
118 memmove(q, p, b->len * sz);
119 b->sz += b->off - slots;
121 b->unshift = b->push = 0;
125 /* --- Decide on a new size --- *
127 * There's a minimum possible size for the array which is used if it's
128 * currently completely empty. Otherwise I choose the smallest power of
129 * two which is big enough, starting at double the current size.
132 nsz = v ? b->sz + b->off : (DA_INITSZ >> 1);
133 do nsz <<= 1; while (nsz < rq);
135 /* --- Reallocate the block --- *
137 * If I'm not changing the base offset then it's worth using @realloc@;
138 * otherwise there'll probably be two calls to @memcpy@ to shunt the data
139 * around so it's not worth bothering.
142 if (p && slots == b->off) {
143 q = xrealloc(p - b->off * sz, nsz * sz);
146 q = xmalloc(nsz * sz);
149 memcpy(q, p, b->len * sz);
150 free(p - b->off * sz);
154 /* --- Fill in the other parts of the base structure --- */
158 b->unshift = b->push = 0;
162 /* --- @da_shunt@ --- *
164 * Arguments: @da_base *b@ = pointer to array base structure
165 * @void *v@ = pointer to array vector
166 * @size_t sz@ = size of the array elements
167 * @size_t n@ = number of items required at the start
169 * Returns: Pointer to appropriately bodged vector.
171 * Use: Extends an array to accommodate items inserted at its front.
172 * This function is a helper for the @DA_SHUNT@ macro, which
173 * should be used by preference.
176 void *da_shunt(da_base *b, void *v, size_t sz, size_t n)
183 /* --- Make sure there's something which needs doing --- *
185 * If there's enough space already then return immediately.
191 /* --- Compute a number of `push' slots --- *
193 * When returning from this function, there will be @slots@ free spaces at
194 * the end of the array. If @push@ is zero, there's no point in reserving
195 * slots. Otherwise choose a power of two greater than @push@, with a
196 * minimum of @DA_SLOTS@. To simplify matters, add the number of items
197 * already in the array to @slots@, and then add the number of slots to the
205 while (slots < b->push)
211 /* --- Maybe just shunt data around a bit --- *
213 * If the vector is large enough, then theoretically we could cope by
214 * moving the objects about in their existing storage. Again, if there's
215 * not actually twice the space needed, reallocate the array.
218 if (rq * 2 < b->sz + b->off) {
219 q = p + (b->sz - slots) * sz;
220 memmove(q, p, b->len * sz);
221 b->off += b->sz - slots;
223 b->unshift = b->push = 0;
227 /* --- Reallocate the array --- *
229 * The neat @realloc@ code doesn't need to be here: the offset changes
230 * almost all the time -- that's the whole point of this routine!
233 /* --- Decide on a new size --- *
235 * There's a minimum possible size for the array which is used if it's
236 * currently completely empty. Otherwise I choose the smallest power of
237 * two which is big enough, starting at double the current size.
240 nsz = v ? b->sz + b->off : (DA_INITSZ >> 1);
241 do nsz <<= 1; while (nsz < rq);
243 /* --- Reallocate the block --- *
245 * The neat @realloc@ code doesn't need to be here: the offset changes
246 * almost all the time -- that's the whole point of this routine!
249 q = xmalloc(nsz * sz);
250 q += (nsz - slots) * sz;
252 memcpy(q, p, b->len * sz);
253 free(p - b->off * sz);
256 /* --- Fill in the other parts of the base structure --- */
258 b->off = nsz - slots;
260 b->unshift = b->push = 0;
264 /* --- @da_tidy@ --- *
266 * Arguments: @da_base *b@ = pointer to array base structure
267 * @void *v@ = pointer to vector
268 * @size_t sz@ = size of the array elements
270 * Returns: Newly allocated vector.
272 * Use: Minimizes the space occupied by an array. This function is a
273 * helper for the @DA_TIDY@ macro, which should be used by
277 void *da_tidy(da_base *b, void *v, size_t sz)
281 b->unshift = b->push = 0;
285 if (b->sz == b->len && b->off == 0)
289 free(p - b->off * sz);
293 q = xmalloc(b->len * sz);
294 memcpy(q, p, b->len * sz);
295 free(p - b->off * sz);
301 /* --- Note about testing --- *
303 * The test rig for this code is split into three parts. There's `da-gtest',
304 * which is a Perl script which generates a list of commands. The `da-ref'
305 * Perl script interprets these commands as operations on a Perl array. It's
306 * relatively conservatively written and believed to be reliable. The
307 * `da-test.c' file implements a command reader for the same syntax and
308 * performs the operations on an integer darray, producing output in the same
309 * format. To test darray, generate a command script with `da-gtest', pass
310 * it through both `da-ref' and `da-test' (the result of compiling
311 * da-test.c'), and compare the results. If they're not byte-for-byte
312 * identical, there's something wrong.
315 /*----- That's all, folks -------------------------------------------------*/