3 * File name: ir/adt/array.h
4 * Purpose: Declarations for Array.
5 * Author: Markus Armbruster
7 * Created: 1999 by getting from fiasco
9 * Copyright: (c) 1995, 1996 Markus Armbruster
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
14 * @file array.h Dynamic and flexible arrays for C.
29 * Creates a flexible array.
31 * @param type The element type of the new array.
32 * @param nelts a size_t expression evaluating to the number of elements
34 * This macro creates a flexible array of a given type at runtime.
35 * The size of the array can be changed later.
37 * @return A pointer to the flexible array (can be used as a pointer to the
38 * first element of this array).
40 #define NEW_ARR_F(type, nelts) \
41 (XMALLOC_TRACE (type *)_new_arr_f ((nelts), sizeof(type) * (nelts)))
44 * Creates a new flxible array with the same number of elements as a
47 * @param type The element type of the new array.
48 * @param arr An array from which the number of elements will be taken
50 * This macro creates a flexible array of a given type at runtime.
51 * The size of the array can be changed later.
53 * @return A pointer to the flexible array (can be used as a pointer to the
54 * first element of this array).
56 #define CLONE_ARR_F(type, arr) \
57 NEW_ARR_F (type, ARR_LEN ((arr)))
60 * Duplicates an array and returns the new flexible one.
62 * @param type The element type of the new array.
63 * @param arr An array from which the elements will be duplicated
65 * This macro creates a flexible array of a given type at runtime.
66 * The size of the array can be changed later.
68 * @return A pointer to the flexible array (can be used as a pointer to the
69 * first element of this array).
71 #define DUP_ARR_F(type, arr) \
72 memcpy (CLONE_ARR_F (type, (arr)), (arr), sizeof(type) * ARR_LEN((arr)))
75 * Delete a flexible array.
77 * @param arr The flexible array.
79 #define DEL_ARR_F(arr) (XMALLOC_TRACE _del_arr_f ((arr)))
82 * Creates a dynamic array on an obstack.
84 * @param type The element type of the new array.
85 * @param obstack A struct obstack * were the data will be allocated
86 * @param nelts A size_t expression evaluating to the number of elements
88 * This macro creates a dynamic array of a given type at runtime.
89 * The size of the array cannot be changed later.
91 * @return A pointer to the dynamic array (can be used as a pointer to the
92 * first element of this array).
94 #define NEW_ARR_D(type, obstack, nelts) \
96 ? (type *)_new_arr_d ((obstack), (nelts), sizeof(type) * (nelts)) \
97 : (type *)arr_mt_descr.v.elts)
100 * Creates a new dynamic array with the same number of elements as a
103 * @param type The element type of the new array.
104 * @param obstack An struct obstack * were the data will be allocated
105 * @param arr An array from which the number of elements will be taken
107 * This macro creates a dynamic array of a given type at runtime.
108 * The size of the array cannot be changed later.
110 * @return A pointer to the dynamic array (can be used as a pointer to the
111 * first element of this array).
113 #define CLONE_ARR_D(type, obstack, arr) \
114 NEW_ARR_D (type, (obstack), ARR_LEN ((arr)))
117 * Duplicates an array and returns the new dynamic one.
119 * @param type The element type of the new array.
120 * @param obstack An struct obstack * were the data will be allocated
121 * @param arr An array from which the elements will be duplicated
123 * This macro creates a dynamic array of a given type at runtime.
124 * The size of the array cannot be changed later.
126 * @return A pointer to the dynamic array (can be used as a pointer to the
127 * first element of this array).
129 #define DUP_ARR_D(type, obstack, arr) \
130 memcpy (CLONE_ARR_D (type, (obstack), (arr)), (arr), sizeof(type) * ARR_LEN ((arr)))
133 * Create an automatic array which will be deleted at return from function.
134 * Beware, the data will be allocated un the functions stack!
136 * @param type The element type of the new array.
137 * @param var A lvalue of type (type *) which will hold the new array.
138 * @param n number of elements in this array.
140 * This macro creates a dynamic array on the functions stack of a given type at runtime.
141 * The size of the array cannot be changed later.
143 #define NEW_ARR_A(type, var, n) \
146 assert (_nelts >= 0); \
147 (var) = (void *)((_arr_descr *)alloca (_ARR_ELTS_OFFS + sizeof(type) * _nelts))->v.elts; \
148 _ARR_SET_DBGINF (_ARR_DESCR ((var)), ARR_A_MAGIC, sizeof (type)); \
149 (void)(_ARR_DESCR ((var))->nelts = _nelts); \
153 * Creates a new automatic array with the same number of elements as a
156 * @param type The element type of the new array.
157 * @param var A lvalue of type (type *) which will hold the new array.
158 * @param arr An array from which the elements will be duplicated
160 * This macro creates a dynamic array of a given type at runtime.
161 * The size of the array cannot be changed later.
163 * @return A pointer to the dynamic array (can be used as a pointer to the
164 * first element of this array).
166 #define CLONE_ARR_A(type, var, arr) \
167 NEW_ARR_A (type, (var), ARR_LEN ((arr)))
170 * Duplicates an array and returns a new automatic one.
172 * @param type The element type of the new array.
173 * @param var A lvalue of type (type *) which will hold the new array.
174 * @param arr An array from with the number of elements will be taken
176 * This macro creates a dynamic array of a given type at runtime.
177 * The size of the array cannot be changed later.
179 * @return A pointer to the dynamic array (can be used as a pointer to the
180 * first element of this array).
182 #define DUP_ARR_A(type, var, arr) \
183 do { CLONE_ARR_A(type, (var), (arr)); \
184 memcpy ((var), (arr), sizeof (type) * ARR_LEN ((arr))); } \
188 * Declare an initialized (zero'ed) array of fixed size.
189 * This macro should be used at file scope only.
191 * @param type The element type of the new array.
192 * @param var A lvalue of type (type *) which will hold the new array.
193 * @param _nelts Number of elements in this new array.
195 #define DECL_ARR_S(type, var, _nelts) \
196 ARR_STRUCT(type, (_nelts) ? (_nelts) : 1) _##var; \
197 type *var = (_ARR_SET_DBGINF (&_##var, ARR_A_MAGIC, sizeof (type)), \
198 _##var.nelts = _nelts, \
202 * Returns the length of an array
204 * @param arr a flexible, dynamic, automatic or static array.
206 #define ARR_LEN(arr) (ARR_VRFY ((arr)), _ARR_DESCR((arr))->nelts)
209 * Resize a flexible array, allocate more data if needed but do NOT
212 * @param type The element type of the array.
213 * @param arr The array, which must be an lvalue.
214 * @param n The new size of the array.
216 * @remark This macro may change arr, so update all references!
218 #define ARR_RESIZE(type, arr, n) \
219 (XMALLOC_TRACE (arr) = _arr_resize ((arr), (n), sizeof(type)))
222 * Resize a flexible array, always reallocate data.
224 * @param type The element type of the array.
225 * @param arr The array, which must be an lvalue.
226 * @param n The new size of the array.
228 * @remark This macro may change arr, so update all references!
230 #define ARR_SETLEN(type, arr, n) \
231 (XMALLOC_TRACE (arr) = _arr_setlen ((arr), (n), sizeof(type) * (n)))
233 /** Set a length smaller than the current length of the array. Do not
234 * resize. len must be <= ARR_LEN(arr). */
235 #define ARR_SHRINKLEN(arr,len) \
236 (ARR_VRFY ((arr)), assert(_ARR_DESCR((arr))->nelts >= len), \
237 _ARR_DESCR((arr))->nelts = len)
240 * Resize a flexible array by growing it by delta elements.
242 * @param type The element type of the array.
243 * @param arr The array, which must be an lvalue.
244 * @param delta The delta number of elements.
246 * @remark This macro may change arr, so update all references!
248 #define ARR_EXTEND(type, arr, delta) \
249 ARR_RESIZE (type, (arr), ARR_LEN ((arr)) + (delta))
252 * Resize a flexible array to hold n elements only if it is currently shorter
255 * @param type The element type of the array.
256 * @param arr The array, which must be an lvalue.
257 * @param n The new size of the array.
259 * @remark This macro may change arr, so update all references!
261 #define ARR_EXTO(type, arr, n) \
262 ((n) >= ARR_LEN ((arr)) ? ARR_RESIZE (type, (arr), (n)+1) : (arr))
265 * Append one element to a flexible array.
267 * @param type The element type of the array.
268 * @param arr The array, which must be an lvalue.
269 * @param elt The new element, must be of type (type).
271 #define ARR_APP1(type, arr, elt) \
272 (ARR_EXTEND (type, (arr), 1), (arr)[ARR_LEN ((arr))-1] = (elt))
276 # define ARR_VRFY(arr) ((void)0)
277 # define ARR_IDX_VRFY(arr, idx) ((void)0)
279 # define ARR_VRFY(arr) \
280 assert ( ( (_ARR_DESCR((arr))->cookie == ARR_D_MAGIC) \
281 || (_ARR_DESCR((arr))->cookie == ARR_A_MAGIC) \
282 || (_ARR_DESCR((arr))->cookie == ARR_F_MAGIC)) \
283 && ( (_ARR_DESCR((arr))->cookie != ARR_F_MAGIC) \
284 || (_ARR_DESCR((arr))->u.allocated >= _ARR_DESCR((arr))->nelts)) \
285 && (_ARR_DESCR((arr))->nelts >= 0))
286 # define ARR_IDX_VRFY(arr, idx) \
287 assert ((0 <= (idx)) && ((idx) < ARR_LEN ((arr))))
292 Don't try this at home, kids, we're trained professionals ;->
293 ... or at the IPD, either. */
295 # define _ARR_DBGINF_DECL
296 # define _ARR_SET_DBGINF(descr, co, es) ((co), (es))
298 # define _ARR_DBGINF_DECL int cookie; size_t eltsize;
299 # define _ARR_SET_DBGINF(descr, co, es) \
300 ((descr)->cookie = (co), (descr)->eltsize = (es))
304 * Construct an array header.
306 #define ARR_STRUCT(type, _nelts) \
310 struct obstack *obstack; /* dynamic: allocated on this obstack */ \
311 int allocated; /* flexible: #slots allocated */ \
315 type elts[(_nelts)]; \
316 aligned_type align[1]; \
321 * The array descriptor header type.
323 typedef ARR_STRUCT (aligned_type, 1) _arr_descr;
325 extern _arr_descr arr_mt_descr;
327 void *_new_arr_f (int, size_t);
328 void _del_arr_f (void *);
329 void *_new_arr_d (struct obstack *obstack, int nelts, size_t elts_size);
330 void *_arr_resize (void *, int, size_t);
331 void *_arr_setlen (void *, int, size_t);
333 #define _ARR_ELTS_OFFS offsetof (_arr_descr, v.elts)
334 #define _ARR_DESCR(elts) ((_arr_descr *)(void *)((char *)(elts) - _ARR_ELTS_OFFS))