3745e24b |
1 | /* -*-c-*- |
2 | * |
3745e24b |
3 | * Dynamically growing dense arrays |
4 | * |
5 | * (c) 1999 Straylight/Edgeware |
6 | */ |
7 | |
d4efbcd9 |
8 | /*----- Licensing notice --------------------------------------------------* |
3745e24b |
9 | * |
10 | * This file is part of the mLib utilities library. |
11 | * |
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. |
d4efbcd9 |
16 | * |
3745e24b |
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. |
d4efbcd9 |
21 | * |
3745e24b |
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, |
25 | * MA 02111-1307, USA. |
26 | */ |
27 | |
3745e24b |
28 | /*----- Header files ------------------------------------------------------*/ |
29 | |
30 | #include <stdio.h> |
31 | #include <string.h> |
32 | #include <stdlib.h> |
33 | |
34 | #include "alloc.h" |
20eb516f |
35 | #include "arena.h" |
3745e24b |
36 | #include "darray.h" |
37 | |
38 | /*----- Magic numbers -----------------------------------------------------*/ |
39 | |
f8509853 |
40 | #define DA_INITSZ 16 /* Default size for new array */ |
3745e24b |
41 | #define DA_SLOTS 8 /* Number of preshifted slots */ |
42 | |
43 | /*----- Main code ---------------------------------------------------------*/ |
44 | |
45 | /* --- @da_ensure@ --- * |
46 | * |
47 | * Arguments: @da_base *b@ = pointer to array base structure |
48 | * @void *v@ = pointer to array vector |
49 | * @size_t sz@ = size of individual array elements |
50 | * @size_t n@ = number of items required at the end |
51 | * |
52 | * Returns: Pointer to newly allocated or adjusted array vector. |
53 | * |
54 | * Use: Extends a dynamic array to accommodate a number of new items |
55 | * at its end. This function is a helper for the @DA_ENSURE@ |
56 | * macro, which should be used by preference. |
57 | */ |
58 | |
59 | void *da_ensure(da_base *b, void *v, size_t sz, size_t n) |
60 | { |
61 | size_t rq = n + b->len; |
62 | char *p = v, *q; |
63 | size_t nsz; |
64 | size_t slots; |
65 | |
66 | /* --- Make sure there's something which needs doing --- * |
67 | * |
68 | * If there's enough space already then return immediately. |
69 | */ |
70 | |
71 | if (rq < b->sz) |
72 | return (p); |
73 | |
74 | /* --- Compute a number of `unshift' slots --- * |
75 | * |
76 | * When returning from this function, the offset will be set to @slots@. |
77 | * If @unshift@ is zero, there's no point in reserving slots. Otherwise |
78 | * choose a power of two greater than @unshift@, with a minimum of |
79 | * @DA_SLOTS@. Then add the number of slots to the requirement. |
80 | */ |
81 | |
82 | if (!b->unshift) |
83 | slots = 0; |
84 | else { |
85 | slots = DA_SLOTS; |
86 | while (slots < b->unshift) |
87 | slots <<= 1; |
88 | } |
89 | rq += slots; |
90 | |
91 | /* --- Maybe just shunt data around a bit --- * |
92 | * |
93 | * If the vector is large enough, then theoretically we could cope by |
c6e5bbae |
94 | * moving the objects about in their existing storage. It's not worth |
95 | * bothering if there's not actually double the amount of space I need. |
3745e24b |
96 | */ |
97 | |
c6e5bbae |
98 | if (rq * 2 < b->sz + b->off) { |
3745e24b |
99 | q = p - (b->off - slots) * sz; |
100 | memmove(q, p, b->len * sz); |
101 | b->sz += b->off - slots; |
102 | b->off = slots; |
103 | b->unshift = b->push = 0; |
104 | return (q); |
105 | } |
106 | |
c6e5bbae |
107 | /* --- Decide on a new size --- * |
85bb21f7 |
108 | * |
c6e5bbae |
109 | * There's a minimum possible size for the array which is used if it's |
110 | * currently completely empty. Otherwise I choose the smallest power of |
111 | * two which is big enough, starting at double the current size. |
85bb21f7 |
112 | */ |
3745e24b |
113 | |
f8509853 |
114 | nsz = v ? b->sz + b->off : (DA_INITSZ >> 1); |
3745e24b |
115 | do nsz <<= 1; while (nsz < rq); |
c6e5bbae |
116 | |
117 | /* --- Reallocate the block --- * |
118 | * |
119 | * If I'm not changing the base offset then it's worth using @realloc@; |
120 | * otherwise there'll probably be two calls to @memcpy@ to shunt the data |
121 | * around so it's not worth bothering. |
122 | */ |
123 | |
85bb21f7 |
124 | if (p && slots == b->off) { |
b5ea4de3 |
125 | q = x_realloc(b->a, p - b->off * sz, nsz * sz, b->sz + b->off); |
85bb21f7 |
126 | q += slots * sz; |
127 | } else { |
20eb516f |
128 | q = x_alloc(b->a, nsz * sz); |
85bb21f7 |
129 | q += slots * sz; |
130 | if (p) { |
131 | memcpy(q, p, b->len * sz); |
20eb516f |
132 | x_free(b->a, p - b->off * sz); |
85bb21f7 |
133 | } |
134 | } |
c6e5bbae |
135 | |
136 | /* --- Fill in the other parts of the base structure --- */ |
137 | |
3745e24b |
138 | b->off = slots; |
139 | b->sz = nsz - slots; |
140 | b->unshift = b->push = 0; |
141 | return (q); |
142 | } |
143 | |
144 | /* --- @da_shunt@ --- * |
145 | * |
146 | * Arguments: @da_base *b@ = pointer to array base structure |
147 | * @void *v@ = pointer to array vector |
148 | * @size_t sz@ = size of the array elements |
149 | * @size_t n@ = number of items required at the start |
150 | * |
151 | * Returns: Pointer to appropriately bodged vector. |
152 | * |
153 | * Use: Extends an array to accommodate items inserted at its front. |
154 | * This function is a helper for the @DA_SHUNT@ macro, which |
155 | * should be used by preference. |
156 | */ |
157 | |
158 | void *da_shunt(da_base *b, void *v, size_t sz, size_t n) |
159 | { |
160 | size_t rq; |
161 | char *p = v, *q; |
162 | size_t nsz; |
163 | size_t slots; |
164 | |
165 | /* --- Make sure there's something which needs doing --- * |
166 | * |
167 | * If there's enough space already then return immediately. |
168 | */ |
169 | |
170 | if (n < b->off) |
171 | return (p); |
172 | |
173 | /* --- Compute a number of `push' slots --- * |
174 | * |
175 | * When returning from this function, there will be @slots@ free spaces at |
176 | * the end of the array. If @push@ is zero, there's no point in reserving |
177 | * slots. Otherwise choose a power of two greater than @push@, with a |
178 | * minimum of @DA_SLOTS@. To simplify matters, add the number of items |
179 | * already in the array to @slots@, and then add the number of slots to the |
180 | * requirement. |
181 | */ |
182 | |
183 | if (!b->push) |
184 | slots = 0; |
185 | else { |
186 | slots = DA_SLOTS; |
187 | while (slots < b->push) |
188 | slots <<= 1; |
189 | } |
190 | slots += b->len; |
191 | rq = n + slots; |
192 | |
193 | /* --- Maybe just shunt data around a bit --- * |
194 | * |
195 | * If the vector is large enough, then theoretically we could cope by |
c6e5bbae |
196 | * moving the objects about in their existing storage. Again, if there's |
197 | * not actually twice the space needed, reallocate the array. |
3745e24b |
198 | */ |
199 | |
c6e5bbae |
200 | if (rq * 2 < b->sz + b->off) { |
3745e24b |
201 | q = p + (b->sz - slots) * sz; |
202 | memmove(q, p, b->len * sz); |
203 | b->off += b->sz - slots; |
204 | b->sz = slots; |
205 | b->unshift = b->push = 0; |
206 | return (q); |
207 | } |
208 | |
85bb21f7 |
209 | /* --- Reallocate the array --- * |
210 | * |
211 | * The neat @realloc@ code doesn't need to be here: the offset changes |
212 | * almost all the time -- that's the whole point of this routine! |
213 | */ |
3745e24b |
214 | |
c6e5bbae |
215 | /* --- Decide on a new size --- * |
216 | * |
217 | * There's a minimum possible size for the array which is used if it's |
218 | * currently completely empty. Otherwise I choose the smallest power of |
219 | * two which is big enough, starting at double the current size. |
220 | */ |
221 | |
f8509853 |
222 | nsz = v ? b->sz + b->off : (DA_INITSZ >> 1); |
3745e24b |
223 | do nsz <<= 1; while (nsz < rq); |
c6e5bbae |
224 | |
225 | /* --- Reallocate the block --- * |
226 | * |
227 | * The neat @realloc@ code doesn't need to be here: the offset changes |
228 | * almost all the time -- that's the whole point of this routine! |
229 | */ |
230 | |
20eb516f |
231 | q = x_alloc(b->a, nsz * sz); |
3745e24b |
232 | q += (nsz - slots) * sz; |
85bb21f7 |
233 | if (p) { |
234 | memcpy(q, p, b->len * sz); |
20eb516f |
235 | x_free(b->a, p - b->off * sz); |
85bb21f7 |
236 | } |
c6e5bbae |
237 | |
238 | /* --- Fill in the other parts of the base structure --- */ |
d4efbcd9 |
239 | |
3745e24b |
240 | b->off = nsz - slots; |
241 | b->sz = slots; |
242 | b->unshift = b->push = 0; |
243 | return (q); |
244 | } |
245 | |
246 | /* --- @da_tidy@ --- * |
247 | * |
248 | * Arguments: @da_base *b@ = pointer to array base structure |
249 | * @void *v@ = pointer to vector |
250 | * @size_t sz@ = size of the array elements |
251 | * |
252 | * Returns: Newly allocated vector. |
253 | * |
254 | * Use: Minimizes the space occupied by an array. This function is a |
255 | * helper for the @DA_TIDY@ macro, which should be used by |
256 | * preference. |
257 | */ |
258 | |
259 | void *da_tidy(da_base *b, void *v, size_t sz) |
260 | { |
261 | char *p = v, *q; |
262 | |
263 | b->unshift = b->push = 0; |
264 | |
265 | if (!p) |
266 | return (0); |
267 | if (b->sz == b->len && b->off == 0) |
268 | return (p); |
269 | |
270 | if (!b->len) { |
20eb516f |
271 | xfree(p - b->off * sz); |
3745e24b |
272 | return (0); |
273 | } |
274 | |
20eb516f |
275 | q = x_alloc(b->a, b->len * sz); |
3745e24b |
276 | memcpy(q, p, b->len * sz); |
20eb516f |
277 | x_free(b->a, p - b->off * sz); |
3745e24b |
278 | b->sz = b->len; |
279 | b->off = 0; |
280 | return (q); |
281 | } |
282 | |
283 | /* --- Note about testing --- * |
284 | * |
285 | * The test rig for this code is split into three parts. There's `da-gtest', |
286 | * which is a Perl script which generates a list of commands. The `da-ref' |
287 | * Perl script interprets these commands as operations on a Perl array. It's |
288 | * relatively conservatively written and believed to be reliable. The |
289 | * `da-test.c' file implements a command reader for the same syntax and |
290 | * performs the operations on an integer darray, producing output in the same |
291 | * format. To test darray, generate a command script with `da-gtest', pass |
292 | * it through both `da-ref' and `da-test' (the result of compiling |
293 | * da-test.c'), and compare the results. If they're not byte-for-byte |
294 | * identical, there's something wrong. |
295 | */ |
296 | |
297 | /*----- That's all, folks -------------------------------------------------*/ |