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.
34 #define ARR_D_MAGIC FOURCC('A','R','R','D')
35 #define ARR_A_MAGIC FOURCC('A','R','R','A')
36 #define ARR_F_MAGIC FOURCC('A','R','R','F')
39 * Creates a flexible array.
41 * @param type The element type of the new array.
42 * @param nelts a size_t expression evaluating to the number of elements
44 * This macro creates a flexible array of a given type at runtime.
45 * The size of the array can be changed later.
47 * @return A pointer to the flexible array (can be used as a pointer to the
48 * first element of this array).
50 #define NEW_ARR_F(type, nelts) \
51 ((type *)_new_arr_f ((nelts), sizeof(type) * (nelts)))
54 * Creates a new flxible array with the same number of elements as a
57 * @param type The element type of the new array.
58 * @param arr An array from which the number of elements will be taken
60 * This macro creates a flexible array of a given type at runtime.
61 * The size of the array can be changed later.
63 * @return A pointer to the flexible array (can be used as a pointer to the
64 * first element of this array).
66 #define CLONE_ARR_F(type, arr) \
67 NEW_ARR_F (type, ARR_LEN ((arr)))
70 * Duplicates an array and returns the new flexible one.
72 * @param type The element type of the new array.
73 * @param arr An array from which the elements will be duplicated
75 * This macro creates a flexible array of a given type at runtime.
76 * The size of the array can be changed later.
78 * @return A pointer to the flexible array (can be used as a pointer to the
79 * first element of this array).
81 #define DUP_ARR_F(type, arr) \
82 memcpy (CLONE_ARR_F (type, (arr)), (arr), sizeof(type) * ARR_LEN((arr)))
85 * Delete a flexible array.
87 * @param arr The flexible array.
89 #define DEL_ARR_F(arr) (_del_arr_f ((arr)))
92 * Creates a dynamic array on an obstack.
94 * @param type The element type of the new array.
95 * @param obstack A struct obstack * were the data will be allocated
96 * @param nelts A size_t expression evaluating to the number of elements
98 * This macro creates a dynamic array of a given type at runtime.
99 * The size of the array cannot be changed later.
101 * @return A pointer to the dynamic array (can be used as a pointer to the
102 * first element of this array).
104 #define NEW_ARR_D(type, obstack, nelts) \
106 ? (type *)_new_arr_d ((obstack), (nelts), sizeof(type) * (nelts)) \
107 : (type *)arr_mt_descr.v.elts)
110 * Creates a new dynamic array with the same number of elements as a
113 * @param type The element type of the new array.
114 * @param obstack An struct obstack * were the data will be allocated
115 * @param arr An array from which the number of elements will be taken
117 * This macro creates a dynamic array of a given type at runtime.
118 * The size of the array cannot be changed later.
120 * @return A pointer to the dynamic array (can be used as a pointer to the
121 * first element of this array).
123 #define CLONE_ARR_D(type, obstack, arr) \
124 NEW_ARR_D (type, (obstack), ARR_LEN ((arr)))
127 * Duplicates an array and returns the new dynamic one.
129 * @param type The element type of the new array.
130 * @param obstack An struct obstack * were the data will be allocated
131 * @param arr An array from which the elements will be duplicated
133 * This macro creates a dynamic array of a given type at runtime.
134 * The size of the array cannot be changed later.
136 * @return A pointer to the dynamic array (can be used as a pointer to the
137 * first element of this array).
139 #define DUP_ARR_D(type, obstack, arr) \
140 memcpy (CLONE_ARR_D (type, (obstack), (arr)), (arr), sizeof(type) * ARR_LEN ((arr)))
143 * Create an automatic array which will be deleted at return from function.
144 * Beware, the data will be allocated on the function stack!
146 * @param type The element type of the new array.
147 * @param var A lvalue of type (type *) which will hold the new array.
148 * @param n number of elements in this array.
150 * This macro creates a dynamic array on the functions stack of a given type at runtime.
151 * The size of the array cannot be changed later.
153 #define NEW_ARR_A(type, var, n) \
156 assert (_nelts >= 0); \
157 (var) = (void *)((_arr_descr *)alloca (_ARR_ELTS_OFFS + sizeof(type) * _nelts))->v.elts; \
158 _ARR_SET_DBGINF (_ARR_DESCR ((var)), ARR_A_MAGIC, sizeof (type)); \
159 (void)(_ARR_DESCR ((var))->nelts = _nelts); \
163 * Creates a new automatic array with the same number of elements as a
166 * @param type The element type of the new array.
167 * @param var A lvalue of type (type *) which will hold the new array.
168 * @param arr An array from which the elements will be duplicated
170 * This macro creates a dynamic array of a given type at runtime.
171 * The size of the array cannot be changed later.
173 * @return A pointer to the dynamic array (can be used as a pointer to the
174 * first element of this array).
176 #define CLONE_ARR_A(type, var, arr) \
177 NEW_ARR_A (type, (var), ARR_LEN ((arr)))
180 * Duplicates an array and returns a new automatic one.
182 * @param type The element type of the new array.
183 * @param var A lvalue of type (type *) which will hold the new array.
184 * @param arr An array from with the number of elements will be taken
186 * This macro creates a dynamic array of a given type at runtime.
187 * The size of the array cannot be changed later.
189 * @return A pointer to the dynamic array (can be used as a pointer to the
190 * first element of this array).
192 #define DUP_ARR_A(type, var, arr) \
193 do { CLONE_ARR_A(type, (var), (arr)); \
194 memcpy ((var), (arr), sizeof (type) * ARR_LEN ((arr))); } \
198 * Declare an initialized (zero'ed) array of fixed size.
199 * This macro should be used at file scope only.
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 _nelts Number of elements in this new array.
205 #define DECL_ARR_S(type, var, _nelts) \
206 ARR_STRUCT(type, (_nelts) ? (_nelts) : 1) _##var; \
207 type *var = (_ARR_SET_DBGINF (&_##var, ARR_A_MAGIC, sizeof (type)), \
208 _##var.nelts = _nelts, \
212 * Returns the length of an array
214 * @param arr a flexible, dynamic, automatic or static array.
216 #define ARR_LEN(arr) (ARR_VRFY ((arr)), _ARR_DESCR((arr))->nelts)
219 * Resize a flexible array, allocate more data if needed but do NOT
222 * @param type The element type of the array.
223 * @param arr The array, which must be an lvalue.
224 * @param n The new size of the array.
226 * @remark This macro may change arr, so update all references!
228 #define ARR_RESIZE(type, arr, n) \
229 ((arr) = _arr_resize ((arr), (n), sizeof(type)))
232 * Resize a flexible array, always reallocate data.
234 * @param type The element type of the array.
235 * @param arr The array, which must be an lvalue.
236 * @param n The new size of the array.
238 * @remark This macro may change arr, so update all references!
240 #define ARR_SETLEN(type, arr, n) \
241 ((arr) = _arr_setlen ((arr), (n), sizeof(type) * (n)))
243 /** Set a length smaller than the current length of the array. Do not
244 * resize. len must be <= ARR_LEN(arr). */
245 #define ARR_SHRINKLEN(arr,len) \
246 (ARR_VRFY ((arr)), assert(_ARR_DESCR((arr))->nelts >= len), \
247 _ARR_DESCR((arr))->nelts = len)
250 * Resize a flexible array by growing it by delta elements.
252 * @param type The element type of the array.
253 * @param arr The array, which must be an lvalue.
254 * @param delta The delta number of elements.
256 * @remark This macro may change arr, so update all references!
258 #define ARR_EXTEND(type, arr, delta) \
259 ARR_RESIZE (type, (arr), ARR_LEN ((arr)) + (delta))
262 * Resize a flexible array to hold n elements only if it is currently shorter
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!
271 #define ARR_EXTO(type, arr, n) \
272 ((n) >= ARR_LEN ((arr)) ? ARR_RESIZE (type, (arr), (n)+1) : (arr))
275 * Append one element to a flexible array.
277 * @param type The element type of the array.
278 * @param arr The array, which must be an lvalue.
279 * @param elt The new element, must be of type (type).
281 #define ARR_APP1(type, arr, elt) \
282 (ARR_EXTEND (type, (arr), 1), (arr)[ARR_LEN ((arr))-1] = (elt))
286 # define ARR_VRFY(arr) ((void)0)
287 # define ARR_IDX_VRFY(arr, idx) ((void)0)
289 # define ARR_VRFY(arr) \
290 assert ( ( (_ARR_DESCR((arr))->magic == ARR_D_MAGIC) \
291 || (_ARR_DESCR((arr))->magic == ARR_A_MAGIC) \
292 || (_ARR_DESCR((arr))->magic == ARR_F_MAGIC)) \
293 && ( (_ARR_DESCR((arr))->magic != ARR_F_MAGIC) \
294 || (_ARR_DESCR((arr))->u.allocated >= _ARR_DESCR((arr))->nelts)) \
295 && (_ARR_DESCR((arr))->nelts >= 0))
296 # define ARR_IDX_VRFY(arr, idx) \
297 assert ((0 <= (idx)) && ((idx) < ARR_LEN ((arr))))
302 Don't try this at home, kids, we're trained professionals ;->
303 ... or at the IPD, either. */
305 # define _ARR_DBGINF_DECL
306 # define _ARR_SET_DBGINF(descr, co, es)
308 # define _ARR_DBGINF_DECL int magic; size_t eltsize;
309 # define _ARR_SET_DBGINF(descr, co, es) \
310 ( (descr)->magic = (co), (descr)->eltsize = (es) )
314 * Construct an array header.
316 #define ARR_STRUCT(type, _nelts) \
320 struct obstack *obstack; /* dynamic: allocated on this obstack */ \
321 int allocated; /* flexible: #slots allocated */ \
325 type elts[(_nelts)]; \
326 aligned_type align[1]; \
331 * The array descriptor header type.
333 typedef ARR_STRUCT (aligned_type, 1) _arr_descr;
335 extern _arr_descr arr_mt_descr;
337 void *_new_arr_f (int, size_t);
338 void _del_arr_f (void *);
339 void *_new_arr_d (struct obstack *obstack, int nelts, size_t elts_size);
340 void *_arr_resize (void *, int, size_t);
341 void *_arr_setlen (void *, int, size_t);
343 #define _ARR_ELTS_OFFS offsetof (_arr_descr, v.elts)
344 #define _ARR_DESCR(elts) ((_arr_descr *)(void *)((char *)(elts) - _ARR_ELTS_OFFS))