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).
60 #define CLONE_ARR_F(arr) \
61 NEW_ARR_F (__typeof__(arr[0]), ARR_LEN ((arr)))
64 * Duplicates an array and returns the new flexible one.
66 * @param type The element type of the new array.
67 * @param arr An array from which the elements will be duplicated
69 * This macro creates a flexible array of a given type at runtime.
70 * The size of the array can be changed later.
72 * @return A pointer to the flexible array (can be used as a pointer to the
73 * first element of this array).
75 #define DUP_ARR_F(arr) \
76 memcpy (CLONE_ARR_F (__typeof__(arr[0]), (arr)), (arr), sizeof(__typeof__(arr[0])) * ARR_LEN((arr)))
79 * Delete a flexible array.
81 * @param arr The flexible array.
83 #define DEL_ARR_F(arr) (_del_arr_f ((arr)))
86 * Creates a dynamic array on an obstack.
88 * @param type The element type of the new array.
89 * @param obstack A struct obstack * were the data will be allocated
90 * @param nelts A size_t expression evaluating to the number of elements
92 * This macro creates a dynamic array of a given type at runtime.
93 * The size of the array cannot be changed later.
95 * @return A pointer to the dynamic array (can be used as a pointer to the
96 * first element of this array).
98 #define NEW_ARR_D(type, obstack, nelts) \
100 ? (type *)_new_arr_d ((obstack), (nelts), sizeof(type) * (nelts)) \
101 : (type *)arr_mt_descr.v.elts)
104 * Creates a new dynamic array with the same number of elements as a
107 * @param type The element type of the new array.
108 * @param obstack An struct obstack * were the data will be allocated
109 * @param arr An array from which the number of elements will be taken
111 * This macro creates a dynamic array of a given type at runtime.
112 * The size of the array cannot be changed later.
114 * @return A pointer to the dynamic array (can be used as a pointer to the
115 * first element of this array).
117 #define CLONE_ARR_D(obstack, arr) \
118 NEW_ARR_D (__typeof__(arr[0]), (obstack), ARR_LEN ((arr)))
121 * Duplicates an array and returns the new dynamic one.
123 * @param type The element type of the new array.
124 * @param obstack An struct obstack * were the data will be allocated
125 * @param arr An array from which the elements will be duplicated
127 * This macro creates a dynamic array of a given type at runtime.
128 * The size of the array cannot be changed later.
130 * @return A pointer to the dynamic array (can be used as a pointer to the
131 * first element of this array).
133 #define DUP_ARR_D(obstack, arr) \
134 memcpy (CLONE_ARR_D (__typeof__(arr[0]), (obstack), (arr)), (arr), sizeof(__typeof__(arr[0])) * ARR_LEN ((arr)))
137 * Create an automatic array which will be deleted at return from function.
138 * Beware, the data will be allocated on the function stack!
140 * @param type The element type of the new array.
141 * @param var A lvalue of type (type *) which will hold the new array.
142 * @param n number of elements in this array.
144 * This macro creates a dynamic array on the functions stack of a given type at runtime.
145 * The size of the array cannot be changed later.
147 #define NEW_ARR_A(type, var, n) \
150 assert (_nelts >= 0); \
151 (var) = (void *)((_arr_descr *)alloca (_ARR_ELTS_OFFS + sizeof(type) * _nelts))->v.elts; \
152 _ARR_SET_DBGINF (_ARR_DESCR ((var)), ARR_A_MAGIC, sizeof (type)); \
153 (void)(_ARR_DESCR ((var))->nelts = _nelts); \
157 * Creates a new automatic array with the same number of elements as a
160 * @param var A lvalue of type (type *) which will hold the new array.
161 * @param arr An array from which the elements will be duplicated
163 * This macro creates a dynamic array of a given type at runtime.
164 * The size of the array cannot be changed later.
166 * @return A pointer to the dynamic array (can be used as a pointer to the
167 * first element of this array).
169 #define CLONE_ARR_A(var, arr) \
170 NEW_ARR_A (__typeof__(arr[0]), (var), ARR_LEN ((arr)))
173 * Duplicates an array and returns a new automatic one.
175 * @param type The element type of the new array.
176 * @param var A lvalue of type (type *) which will hold the new array.
177 * @param arr An array from with the number of elements will be taken
179 * This macro creates a dynamic array of a given type at runtime.
180 * The size of the array cannot be changed later.
182 * @return A pointer to the dynamic array (can be used as a pointer to the
183 * first element of this array).
185 #define DUP_ARR_A(var, arr) \
186 do { CLONE_ARR_A(__typeof__(arr[0]), (var), (arr)); \
187 memcpy ((var), (arr), sizeof (__typeof__(arr[0])) * ARR_LEN ((arr))); } \
191 * Declare an initialized (zero'ed) array of fixed size.
192 * This macro should be used at file scope only.
194 * @param type The element type of the new array.
195 * @param var A lvalue of type (type *) which will hold the new array.
196 * @param _nelts Number of elements in this new array.
198 #define DECL_ARR_S(type, var, _nelts) \
199 ARR_STRUCT(type, (_nelts) ? (_nelts) : 1) _##var; \
200 type *var = (_ARR_SET_DBGINF (&_##var, ARR_A_MAGIC, sizeof (type)), \
201 _##var.nelts = _nelts, \
205 * Returns the length of an array
207 * @param arr a flexible, dynamic, automatic or static array.
209 #define ARR_LEN(arr) (ARR_VRFY ((arr)), _ARR_DESCR((arr))->nelts)
212 * Resize a flexible array, allocate more data if needed but do NOT
215 * @param arr The array, which must be an lvalue.
216 * @param n The new size of the array.
218 * @remark This macro may change arr, so update all references!
220 #define ARR_RESIZE(arr, n) \
221 ((arr) = _arr_resize ((arr), (n), sizeof(__typeof__(arr[0]))))
224 * Resize a flexible array, always reallocate data.
226 * @param arr The array, which must be an lvalue.
227 * @param n The new size of the array.
229 * @remark This macro may change arr, so update all references!
231 #define ARR_SETLEN(arr, n) \
232 ((arr) = _arr_setlen ((arr), (n), sizeof(__typeof__(arr[0])) * (n)))
234 /** Set a length smaller than the current length of the array. Do not
235 * resize. len must be <= ARR_LEN(arr). */
236 #define ARR_SHRINKLEN(arr,len) \
237 (ARR_VRFY ((arr)), assert(_ARR_DESCR((arr))->nelts >= len), \
238 _ARR_DESCR((arr))->nelts = len)
241 * Resize a flexible array by growing it by delta elements.
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(arr, delta) \
249 ARR_RESIZE ((arr), ARR_LEN ((arr)) + (delta))
252 * Resize a flexible array to hold n elements only if it is currently shorter
255 * @param arr The array, which must be an lvalue.
256 * @param n The new size of the array.
258 * @remark This macro may change arr, so update all references!
260 #define ARR_EXTO(arr, n) \
261 ((n) >= ARR_LEN ((arr)) ? ARR_RESIZE ((arr), (n)+1) : (arr))
264 * Append one element to a flexible array.
266 * @param arr The array, which must be an lvalue.
267 * @param elt The new element, must be of type (type).
269 #define ARR_APP1(arr, elt) \
270 (ARR_EXTEND ((arr), 1), (arr)[ARR_LEN ((arr))-1] = (elt))
274 # define ARR_VRFY(arr) ((void)0)
275 # define ARR_IDX_VRFY(arr, idx) ((void)0)
277 # define ARR_VRFY(arr) \
278 assert ( ( (_ARR_DESCR((arr))->magic == ARR_D_MAGIC) \
279 || (_ARR_DESCR((arr))->magic == ARR_A_MAGIC) \
280 || (_ARR_DESCR((arr))->magic == ARR_F_MAGIC)) \
281 && ( (_ARR_DESCR((arr))->magic != ARR_F_MAGIC) \
282 || (_ARR_DESCR((arr))->u.allocated >= _ARR_DESCR((arr))->nelts)) \
283 && (_ARR_DESCR((arr))->nelts >= 0))
284 # define ARR_IDX_VRFY(arr, idx) \
285 assert ((0 <= (idx)) && ((idx) < ARR_LEN ((arr))))
290 Don't try this at home, kids, we're trained professionals ;->
291 ... or at the IPD, either. */
293 # define _ARR_DBGINF_DECL
294 # define _ARR_SET_DBGINF(descr, co, es)
296 # define _ARR_DBGINF_DECL int magic; size_t eltsize;
297 # define _ARR_SET_DBGINF(descr, co, es) \
298 ( (descr)->magic = (co), (descr)->eltsize = (es) )
302 * Construct an array header.
304 #define ARR_STRUCT(type, _nelts) \
308 struct obstack *obstack; /* dynamic: allocated on this obstack */ \
309 int allocated; /* flexible: #slots allocated */ \
313 type elts[(_nelts)]; \
314 aligned_type align[1]; \
319 * The array descriptor header type.
321 typedef ARR_STRUCT (aligned_type, 1) _arr_descr;
323 extern _arr_descr arr_mt_descr;
325 void *_new_arr_f (int, size_t);
326 void _del_arr_f (void *);
327 void *_new_arr_d (struct obstack *obstack, int nelts, size_t elts_size);
328 void *_arr_resize (void *, int, size_t);
329 void *_arr_setlen (void *, int, size_t);
331 #define _ARR_ELTS_OFFS offsetof (_arr_descr, v.elts)
332 #define _ARR_DESCR(elts) ((_arr_descr *)(void *)((char *)(elts) - _ARR_ELTS_OFFS))