3 * $Id: dynarray.h,v 1.2 1999/05/05 18:50:31 mdw Exp $
5 * Dynamic arrays implementation
7 * (c) 1998 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 Software
26 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Revision history --------------------------------------------------*
31 * $Log: dynarray.h,v $
32 * Revision 1.2 1999/05/05 18:50:31 mdw
33 * Change licensing conditions to LGPL.
35 * Revision 1.1.1.1 1998/06/17 23:44:42 mdw
36 * Initial version of mLib
47 /*----- Required header files ---------------------------------------------*/
60 /*----- Horrific hacking -------------------------------------------------*
62 * Several bits of this code need extending arrays (in particular, the array
63 * handler and the label allocator), although the sizes of the objects being
66 * This file contains (horrors!) a sort of C++ template-a-like which
67 * implements such growing arrays. Read on, if your constitution can stand
71 /* --- Macro: @DYNDECLS@ --- *
73 * Arguments: @prefix@ = prefix string which begins all external
75 * @obtype@ = the element type of the array
76 * @chunksize@ = the number of items in a chunk
78 * Use: Provides declarations suitable for use in a header file which
79 * define functions for manipulating the specific dynamic array
80 * described in the arguments.
83 #define DYNDECLS(prefix, obtype, chunksize) /* ... */ \
85 /* --- Define some constants --- */ \
88 prefix ## __mask = chunksize - 1, \
89 prefix ## __size = chunksize \
92 /* --- Type definitions --- */ \
94 typedef obtype prefix ## __object; \
96 typedef struct prefix ## _chunk { \
97 struct prefix ## _chunk *next; \
99 prefix ## __object o[prefix ## __size]; \
100 } prefix ## _chunk; \
102 /* --- External routines --- */ \
104 /* --- @PREFIX_find@ --- * \
106 * Arguments: @PREFIX_chunk **base@ = anchor address of chunk list \
107 * @size_t index@ = index into array which we want \
109 * Returns: Pointer to the object at appropriate index, or null \
111 * Use: Indexes an item without creating it if it's not there \
115 extern prefix ## __object *prefix ## _find(prefix ## _chunk **base, \
118 /* --- @PREFIX_new@ --- * \
120 * Arguments: @PREFIX_chunk **base@ = anchor address of chunk list \
121 * @size_t index@ = index into array which we want \
123 * Returns: Pointer to the object at appropriate index \
125 * Use: Indexes an item, creating it if necessary. \
128 extern prefix ## __object *prefix ## _new(prefix ## _chunk **base, \
131 /* --- @PREFIX_free@ --- * \
133 * Arguments: @PREFIX_chunk **base@ = anchor address of chunk list \
137 * Use: Releases all the memory directly attached to an array. \
140 extern void prefix ## _free(prefix ## _chunk **base);
142 /* --- Macro: @DYNARRAY@ --- *
144 * Arguments: @prefix@ = prefix string for uniquification
145 * @init@ = how to initialise a chunk
146 * @kill@ = how to free a chunk
148 * Use: Builds template routines for dynamically growing arrays.
149 * The two arguments @init@ and @kill@ must use the macros
153 #define DYNARRAY(prefix, init, kill) /* ... */ \
155 /* --- @PREFIX_find@ --- * \
157 * Arguments: @PREFIX_chunk **base@ = anchor address of chunk list \
158 * @size_t index@ = index into array which we want \
160 * Returns: Pointer to the object at appropriate index, or null \
162 * Use: Indexes an item without creating it if it's not there \
166 prefix ## __object *prefix ## _find(prefix ## _chunk **base, \
169 size_t chunkid = index & ~prefix ## __mask; \
170 prefix ## _chunk *p = *base; \
171 index &= prefix ## __mask; \
174 if (!p || p->base > chunkid) \
176 if (p->base == chunkid) \
181 return (p->o + index); \
184 /* --- @PREFIX_new@ --- * \
186 * Arguments: @PREFIX_chunk **base@ = anchor address of chunk list \
187 * @size_t index@ = index into array which we want \
189 * Returns: Pointer to the object at appropriate index \
191 * Use: Indexes an item, creating it if necessary. \
194 prefix ## __object *prefix ## _new(prefix ## _chunk **base, \
197 size_t chunkid = index & ~prefix ## __mask; \
198 prefix ## _chunk *p = (prefix ## _chunk *)base; \
199 index &= prefix ## __mask; \
201 while (p->next && p->next->base < chunkid) \
204 if (!p->next || p->next->base != chunkid) { \
205 prefix ## _chunk *q = CREATE(prefix ## _chunk); \
210 DYN__GRABARGS(init, (q, prefix)); \
213 return (p->next->o + index); \
216 /* --- @PREFIX_free@ --- * \
218 * Arguments: @PREFIX_chunk **base@ = anchor address of chunk list \
222 * Use: Releases all the memory directly attached to an array. \
225 void prefix ## _free(prefix ## _chunk **base) \
227 prefix ## _chunk *p = *base, *q; \
230 DYN__GRABARGS(kill, (p, prefix)); \
239 /* --- A vile bit of hacking --- *
241 * All of this yukkiness is a perfectly legitimate consequence of the (daft)
242 * rules about when macro arguments get expanded.
246 #define DYN__ID2(x, y) x, y
247 #define DYN__CONTORT(mac, args) mac args
248 #define DYN__GRABARGS(mac, args, more) \
249 DYN__CONTORT(mac, (DYN__ID args, DYN__ID2 more))
251 /* --- Macro: @DYNITER@ --- *
253 * Arguments: @what@ = what to do for each item -- a macro or function
254 * which is passed the address of an item
256 * Use: Does something for each item.
259 #define DYNITER DYN__ITER ,
260 #define DYN__ITER(what, chunk, prefix) do { \
262 for (i = 0; i < prefix ## __size; i++) \
263 what(chunk->o + i); \
266 /* --- Macro: @DYNNOP@ --- *
273 #define DYNNOP DYN__NOP, (dummy)
274 #define DYN__NOP(dummy, chunk, prefix) /* nop */
276 /*----- That's all, folks -------------------------------------------------*/