3 * $Id: darray.c,v 1.3 1999/10/29 22:59:22 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.3 1999/10/29 22:59:22 mdw
34 * New array adjustment macros for unsigned arguments.
36 * Revision 1.2 1999/10/28 22:05:28 mdw
37 * Modify and debug allocation routines.
39 * Revision 1.1 1999/10/22 22:37:26 mdw
40 * New dynamic array implementation replaces `dynarray.h'.
44 /*----- Header files ------------------------------------------------------*/
53 /*----- Magic numbers -----------------------------------------------------*/
55 #define DA_INITSZ 16 /* Default size for new array */
56 #define DA_SLOTS 8 /* Number of preshifted slots */
58 /*----- Main code ---------------------------------------------------------*/
60 /* --- @da_ensure@ --- *
62 * Arguments: @da_base *b@ = pointer to array base structure
63 * @void *v@ = pointer to array vector
64 * @size_t sz@ = size of individual array elements
65 * @size_t n@ = number of items required at the end
67 * Returns: Pointer to newly allocated or adjusted array vector.
69 * Use: Extends a dynamic array to accommodate a number of new items
70 * at its end. This function is a helper for the @DA_ENSURE@
71 * macro, which should be used by preference.
74 void *da_ensure(da_base *b, void *v, size_t sz, size_t n)
76 size_t rq = n + b->len;
81 /* --- Make sure there's something which needs doing --- *
83 * If there's enough space already then return immediately.
89 /* --- Compute a number of `unshift' slots --- *
91 * When returning from this function, the offset will be set to @slots@.
92 * If @unshift@ is zero, there's no point in reserving slots. Otherwise
93 * choose a power of two greater than @unshift@, with a minimum of
94 * @DA_SLOTS@. Then add the number of slots to the requirement.
101 while (slots < b->unshift)
106 /* --- Maybe just shunt data around a bit --- *
108 * If the vector is large enough, then theoretically we could cope by
109 * moving the objects about in their existing storage.
112 if (rq < b->sz + b->off) {
113 q = p - (b->off - slots) * sz;
114 memmove(q, p, b->len * sz);
115 b->sz += b->off - slots;
117 b->unshift = b->push = 0;
121 /* --- Reallocate the array --- *
123 * If the offset isn't changing, it's sensible to use @realloc@ if
124 * available. Otherwise the overhead of copying all the data twice
125 * probably isn't worth it.
128 nsz = v ? b->sz + b->off : (DA_INITSZ >> 1);
129 do nsz <<= 1; while (nsz < rq);
130 if (p && slots == b->off) {
131 q = xrealloc(p - b->off * sz, nsz * sz);
134 q = xmalloc(nsz * sz);
137 memcpy(q, p, b->len * sz);
138 free(p - b->off * sz);
143 b->unshift = b->push = 0;
147 /* --- @da_shunt@ --- *
149 * Arguments: @da_base *b@ = pointer to array base structure
150 * @void *v@ = pointer to array vector
151 * @size_t sz@ = size of the array elements
152 * @size_t n@ = number of items required at the start
154 * Returns: Pointer to appropriately bodged vector.
156 * Use: Extends an array to accommodate items inserted at its front.
157 * This function is a helper for the @DA_SHUNT@ macro, which
158 * should be used by preference.
161 void *da_shunt(da_base *b, void *v, size_t sz, size_t n)
168 /* --- Make sure there's something which needs doing --- *
170 * If there's enough space already then return immediately.
176 /* --- Compute a number of `push' slots --- *
178 * When returning from this function, there will be @slots@ free spaces at
179 * the end of the array. If @push@ is zero, there's no point in reserving
180 * slots. Otherwise choose a power of two greater than @push@, with a
181 * minimum of @DA_SLOTS@. To simplify matters, add the number of items
182 * already in the array to @slots@, and then add the number of slots to the
190 while (slots < b->push)
196 /* --- Maybe just shunt data around a bit --- *
198 * If the vector is large enough, then theoretically we could cope by
199 * moving the objects about in their existing storage.
202 if (rq < b->sz + b->off) {
203 q = p + (b->sz - slots) * sz;
204 memmove(q, p, b->len * sz);
205 b->off += b->sz - slots;
207 b->unshift = b->push = 0;
211 /* --- Reallocate the array --- *
213 * The neat @realloc@ code doesn't need to be here: the offset changes
214 * almost all the time -- that's the whole point of this routine!
217 nsz = v ? b->sz + b->off : (DA_INITSZ >> 1);
218 do nsz <<= 1; while (nsz < rq);
219 q = xmalloc(nsz * sz);
220 q += (nsz - slots) * sz;
222 memcpy(q, p, b->len * sz);
223 free(p - b->off * sz);
225 b->off = nsz - slots;
227 b->unshift = b->push = 0;
231 /* --- @da_tidy@ --- *
233 * Arguments: @da_base *b@ = pointer to array base structure
234 * @void *v@ = pointer to vector
235 * @size_t sz@ = size of the array elements
237 * Returns: Newly allocated vector.
239 * Use: Minimizes the space occupied by an array. This function is a
240 * helper for the @DA_TIDY@ macro, which should be used by
244 void *da_tidy(da_base *b, void *v, size_t sz)
248 b->unshift = b->push = 0;
252 if (b->sz == b->len && b->off == 0)
256 free(p - b->off * sz);
260 q = xmalloc(b->len * sz);
261 memcpy(q, p, b->len * sz);
262 free(p - b->off * sz);
268 /* --- Note about testing --- *
270 * The test rig for this code is split into three parts. There's `da-gtest',
271 * which is a Perl script which generates a list of commands. The `da-ref'
272 * Perl script interprets these commands as operations on a Perl array. It's
273 * relatively conservatively written and believed to be reliable. The
274 * `da-test.c' file implements a command reader for the same syntax and
275 * performs the operations on an integer darray, producing output in the same
276 * format. To test darray, generate a command script with `da-gtest', pass
277 * it through both `da-ref' and `da-test' (the result of compiling
278 * da-test.c'), and compare the results. If they're not byte-for-byte
279 * identical, there's something wrong.
282 /*----- That's all, folks -------------------------------------------------*/