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.
28 #define ARR_D_MAGIC FOURCC('A','R','R','D')
29 #define ARR_A_MAGIC FOURCC('A','R','R','A')
30 #define ARR_F_MAGIC FOURCC('A','R','R','F')
33 * Creates a flexible array.
35 * @param type The element type of the new array.
36 * @param nelts a size_t expression evaluating to the number of elements
38 * This macro creates a flexible array of a given type at runtime.
39 * The size of the array can be changed later.
41 * @return A pointer to the flexible array (can be used as a pointer to the
42 * first element of this array).
44 #define NEW_ARR_F(type, nelts) \
45 ((type *)_new_arr_f ((nelts), sizeof(type) * (nelts)))
48 * Creates a new flxible array with the same number of elements as a
51 * @param type The element type of the new array.
52 * @param arr An array from which the number of elements will be taken
54 * This macro creates a flexible array of a given type at runtime.
55 * The size of the array can be changed later.
57 * @return A pointer to the flexible array (can be used as a pointer to the
58 * first element of this array).
61 #define CLONE_ARR_F(type, arr) \
62 NEW_ARR_F (__typeof__(arr[0]), ARR_LEN ((arr)))
64 #define CLONE_ARR_F(type, arr) \
65 NEW_ARR_F (type, ARR_LEN ((arr)))
69 * Duplicates an array and returns the new flexible one.
71 * @param type The element type of the new array.
72 * @param arr An array from which the elements will be duplicated
74 * This macro creates a flexible array of a given type at runtime.
75 * The size of the array can be changed later.
77 * @return A pointer to the flexible array (can be used as a pointer to the
78 * first element of this array).
81 #define DUP_ARR_F(type, arr) \
82 memcpy (CLONE_ARR_F (__typeof__(arr[0]), (arr)), (arr), sizeof(__typeof__(arr[0])) * ARR_LEN((arr)))
84 #define DUP_ARR_F(type, arr) \
85 memcpy (CLONE_ARR_F (type, (arr)), (arr), sizeof(type) * ARR_LEN((arr)))
89 * Delete a flexible array.
91 * @param arr The flexible array.
93 #define DEL_ARR_F(arr) (_del_arr_f ((arr)))
96 * Creates a dynamic array on an obstack.
98 * @param type The element type of the new array.
99 * @param obstack A struct obstack * were the data will be allocated
100 * @param nelts A size_t expression evaluating to the number of elements
102 * This macro creates a dynamic array of a given type at runtime.
103 * The size of the array cannot be changed later.
105 * @return A pointer to the dynamic array (can be used as a pointer to the
106 * first element of this array).
108 #define NEW_ARR_D(type, obstack, nelts) \
110 ? (type *)_new_arr_d((obstack), (nelts), sizeof(type) * (nelts)) \
111 : (type *)arr_mt_descr.v.elts)
114 * Creates a new dynamic array with the same number of elements as a
117 * @param type The element type of the new array.
118 * @param obstack An struct obstack * were the data will be allocated
119 * @param arr An array from which the number of elements will be taken
121 * This macro creates a dynamic array of a given type at runtime.
122 * The size of the array cannot be changed later.
124 * @return A pointer to the dynamic array (can be used as a pointer to the
125 * first element of this array).
128 #define CLONE_ARR_D(type, obstack, arr) \
129 NEW_ARR_D(__typeof__(arr[0]), (obstack), ARR_LEN((arr)))
131 #define CLONE_ARR_D(type, obstack, arr) \
132 NEW_ARR_D(type, (obstack), ARR_LEN((arr)))
136 * Duplicates an array and returns the new dynamic one.
138 * @param type The element type of the new array.
139 * @param obstack An struct obstack * were the data will be allocated
140 * @param arr An array from which the elements will be duplicated
142 * This macro creates a dynamic array of a given type at runtime.
143 * The size of the array cannot be changed later.
145 * @return A pointer to the dynamic array (can be used as a pointer to the
146 * first element of this array).
149 #define DUP_ARR_D(type, obstack, arr) \
150 memcpy(CLONE_ARR_D(__typeof__(arr[0]), (obstack), (arr)), (arr), sizeof(__typeof__(arr[0])) * ARR_LEN((arr)))
152 #define DUP_ARR_D(type, obstack, arr) \
153 memcpy(CLONE_ARR_D(type, (obstack), (arr)), (arr), sizeof(type) * ARR_LEN((arr)))
157 * Create an automatic array which will be deleted at return from function.
158 * Beware, the data will be allocated on the function stack!
160 * @param type The element type of the new array.
161 * @param var A lvalue of type (type *) which will hold the new array.
162 * @param n number of elements in this array.
164 * This macro creates a dynamic array on the functions stack of a given type at runtime.
165 * The size of the array cannot be changed later.
167 #define NEW_ARR_A(type, var, n) \
170 assert (_nelts >= 0); \
171 (var) = (void *)((_arr_descr *)alloca (_ARR_ELTS_OFFS + sizeof(type) * _nelts))->v.elts; \
172 _ARR_SET_DBGINF (_ARR_DESCR ((var)), ARR_A_MAGIC, sizeof (type)); \
173 (void)(_ARR_DESCR ((var))->nelts = _nelts); \
177 * Creates a new automatic array with the same number of elements as a
180 * @param type The element type of the array.
181 * @param var A lvalue of type (type *) which will hold the new array.
182 * @param arr An array from which the elements will be duplicated
184 * This macro creates a dynamic array of a given type at runtime.
185 * The size of the array cannot be changed later.
187 * @return A pointer to the dynamic array (can be used as a pointer to the
188 * first element of this array).
191 #define CLONE_ARR_A(type, var, arr) \
192 NEW_ARR_A(__typeof__(arr[0]), (var), ARR_LEN((arr)))
194 #define CLONE_ARR_A(type, var, arr) \
195 NEW_ARR_A(type, (var), ARR_LEN((arr)))
199 * Duplicates an array and returns a new automatic one.
201 * @param type The element type of the new array.
202 * @param var A lvalue of type (type *) which will hold the new array.
203 * @param arr An array from with the number of elements will be taken
205 * This macro creates a dynamic array of a given type at runtime.
206 * The size of the array cannot be changed later.
208 * @return A pointer to the dynamic array (can be used as a pointer to the
209 * first element of this array).
212 #define DUP_ARR_A(type, var, arr) \
213 do { CLONE_ARR_A(__typeof__(arr[0]), (var), (arr)); \
214 memcpy((var), (arr), sizeof(__typeof__(arr[0])) * ARR_LEN((arr))); } \
217 #define DUP_ARR_A(type, var, arr) \
218 do { CLONE_ARR_A(type, (var), (arr)); \
219 memcpy((var), (arr), sizeof(type) * ARR_LEN((arr))); } \
224 * Declare an initialized (zero'ed) array of fixed size.
225 * This macro should be used at file scope only.
227 * @param type The element type of the new array.
228 * @param var A lvalue of type (type *) which will hold the new array.
229 * @param _nelts Number of elements in this new array.
231 #define DECL_ARR_S(type, var, _nelts) \
232 ARR_STRUCT(type, (_nelts) ? (_nelts) : 1) _##var; \
233 type *var = (_ARR_SET_DBGINF (&_##var, ARR_A_MAGIC, sizeof (type)), \
234 _##var.nelts = _nelts, \
238 * Returns the length of an array
240 * @param arr a flexible, dynamic, automatic or static array.
242 #define ARR_LEN(arr) (ARR_VRFY ((arr)), _ARR_DESCR((arr))->nelts)
245 * Resize a flexible array, allocate more data if needed but do NOT
248 * @param type The element type of the array.
249 * @param arr The array, which must be an lvalue.
250 * @param n The new size of the array.
252 * @remark This macro may change arr, so update all references!
255 #define ARR_RESIZE(type, arr, n) \
256 ((arr) = _arr_resize((arr), (n), sizeof(__typeof__(arr[0]))))
258 #define ARR_RESIZE(type, arr, n) \
259 ((arr) = _arr_resize((arr), (n), sizeof(type)))
263 * Resize a flexible array, always reallocate data.
265 * @param type The element type of the array.
266 * @param arr The array, which must be an lvalue.
267 * @param n The new size of the array.
269 * @remark This macro may change arr, so update all references!
272 #define ARR_SETLEN(type, arr, n) \
273 ((arr) = _arr_setlen ((arr), (n), sizeof(__typeof__(arr[0])) * (n)))
275 #define ARR_SETLEN(type, arr, n) \
276 ((arr) = _arr_setlen ((arr), (n), sizeof(type) * (n)))
279 /** Set a length smaller than the current length of the array. Do not
280 * resize. len must be <= ARR_LEN(arr). */
281 #define ARR_SHRINKLEN(arr,len) \
282 (ARR_VRFY ((arr)), assert(_ARR_DESCR((arr))->nelts >= len), \
283 _ARR_DESCR((arr))->nelts = len)
286 * Resize a flexible array by growing it by delta elements.
288 * @param type The element type of the array.
289 * @param arr The array, which must be an lvalue.
290 * @param delta The delta number of elements.
292 * @remark This macro may change arr, so update all references!
294 #define ARR_EXTEND(type, arr, delta) \
295 ARR_RESIZE(type, (arr), ARR_LEN((arr)) + (delta))
298 * Resize a flexible array to hold n elements only if it is currently shorter
301 * @param type The element type of the array.
302 * @param arr The array, which must be an lvalue.
303 * @param n The new size of the array.
305 * @remark This macro may change arr, so update all references!
307 #define ARR_EXTO(type, arr, n) \
308 ((n) >= ARR_LEN((arr)) ? ARR_RESIZE(type, (arr), (n)+1) : (arr))
311 * Append one element to a flexible array.
313 * @param type The element type of the array.
314 * @param arr The array, which must be an lvalue.
315 * @param elt The new element, must be of type (type).
317 #define ARR_APP1(type, arr, elt) \
318 (ARR_EXTEND(type, (arr), 1), (arr)[ARR_LEN((arr))-1] = (elt))
322 # define ARR_VRFY(arr) ((void)0)
323 # define ARR_IDX_VRFY(arr, idx) ((void)0)
325 # define ARR_VRFY(arr) \
326 assert ( ( (_ARR_DESCR((arr))->magic == ARR_D_MAGIC) \
327 || (_ARR_DESCR((arr))->magic == ARR_A_MAGIC) \
328 || (_ARR_DESCR((arr))->magic == ARR_F_MAGIC)) \
329 && ( (_ARR_DESCR((arr))->magic != ARR_F_MAGIC) \
330 || (_ARR_DESCR((arr))->u.allocated >= _ARR_DESCR((arr))->nelts)) \
331 && (_ARR_DESCR((arr))->nelts >= 0))
332 # define ARR_IDX_VRFY(arr, idx) \
333 assert ((0 <= (idx)) && ((idx) < ARR_LEN ((arr))))
338 Don't try this at home, kids, we're trained professionals ;->
339 ... or at the IPD, either. */
341 # define _ARR_DBGINF_DECL
342 # define _ARR_SET_DBGINF(descr, co, es)
344 # define _ARR_DBGINF_DECL int magic; size_t eltsize;
345 # define _ARR_SET_DBGINF(descr, co, es) \
346 ( (descr)->magic = (co), (descr)->eltsize = (es) )
350 * Construct an array header.
352 #define ARR_STRUCT(type, _nelts) \
356 struct obstack *obstack; /* dynamic: allocated on this obstack */ \
357 int allocated; /* flexible: #slots allocated */ \
361 type elts[(_nelts)]; \
362 aligned_type align[1]; \
367 * The array descriptor header type.
369 typedef ARR_STRUCT (aligned_type, 1) _arr_descr;
371 extern _arr_descr arr_mt_descr;
373 void *_new_arr_f (int, size_t);
374 void _del_arr_f (void *);
375 void *_new_arr_d (struct obstack *obstack, int nelts, size_t elts_size);
376 void *_arr_resize (void *, int, size_t);
377 void *_arr_setlen (void *, int, size_t);
379 #define _ARR_ELTS_OFFS offsetof (_arr_descr, v.elts)
380 #define _ARR_DESCR(elts) ((_arr_descr *)(void *)((char *)(elts) - _ARR_ELTS_OFFS))