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.
27 #define ARR_D_MAGIC FOURCC('A','R','R','D')
28 #define ARR_A_MAGIC FOURCC('A','R','R','A')
29 #define ARR_F_MAGIC FOURCC('A','R','R','F')
32 * Creates a flexible array.
34 * @param type The element type of the new array.
35 * @param nelts a size_t expression evaluating to the number of elements
37 * This macro creates a flexible array of a given type at runtime.
38 * The size of the array can be changed later.
40 * @return A pointer to the flexible array (can be used as a pointer to the
41 * first element of this array).
43 #define NEW_ARR_F(type, nelts) \
44 ((type *)_new_arr_f ((nelts), sizeof(type) * (nelts)))
47 * Creates a new flxible array with the same number of elements as a
50 * @param type The element type of the new array.
51 * @param arr An array from which the number of elements will be taken
53 * This macro creates a flexible array of a given type at runtime.
54 * The size of the array can be changed later.
56 * @return A pointer to the flexible array (can be used as a pointer to the
57 * first element of this array).
59 #define CLONE_ARR_F(type, arr) \
60 NEW_ARR_F (type, ARR_LEN ((arr)))
63 * Duplicates an array and returns the new flexible one.
65 * @param type The element type of the new array.
66 * @param arr An array from which the elements will be duplicated
68 * This macro creates a flexible array of a given type at runtime.
69 * The size of the array can be changed later.
71 * @return A pointer to the flexible array (can be used as a pointer to the
72 * first element of this array).
74 #define DUP_ARR_F(type, arr) \
75 memcpy (CLONE_ARR_F (type, (arr)), (arr), sizeof(type) * ARR_LEN((arr)))
78 * Delete a flexible array.
80 * @param arr The flexible array.
82 #define DEL_ARR_F(arr) (_del_arr_f ((arr)))
85 * Creates a dynamic array on an obstack.
87 * @param type The element type of the new array.
88 * @param obstack A struct obstack * were the data will be allocated
89 * @param nelts A size_t expression evaluating to the number of elements
91 * This macro creates a dynamic array of a given type at runtime.
92 * The size of the array cannot be changed later.
94 * @return A pointer to the dynamic array (can be used as a pointer to the
95 * first element of this array).
97 #define NEW_ARR_D(type, obstack, nelts) \
99 ? (type *)_new_arr_d ((obstack), (nelts), sizeof(type) * (nelts)) \
100 : (type *)arr_mt_descr.v.elts)
103 * Creates a new dynamic array with the same number of elements as a
106 * @param type The element type of the new array.
107 * @param obstack An struct obstack * were the data will be allocated
108 * @param arr An array from which the number of elements will be taken
110 * This macro creates a dynamic array of a given type at runtime.
111 * The size of the array cannot be changed later.
113 * @return A pointer to the dynamic array (can be used as a pointer to the
114 * first element of this array).
116 #define CLONE_ARR_D(type, obstack, arr) \
117 NEW_ARR_D (type, (obstack), ARR_LEN ((arr)))
120 * Duplicates an array and returns the new dynamic one.
122 * @param type The element type of the new array.
123 * @param obstack An struct obstack * were the data will be allocated
124 * @param arr An array from which the elements will be duplicated
126 * This macro creates a dynamic array of a given type at runtime.
127 * The size of the array cannot be changed later.
129 * @return A pointer to the dynamic array (can be used as a pointer to the
130 * first element of this array).
132 #define DUP_ARR_D(type, obstack, arr) \
133 memcpy (CLONE_ARR_D (type, (obstack), (arr)), (arr), sizeof(type) * ARR_LEN ((arr)))
136 * Create an automatic array which will be deleted at return from function.
137 * Beware, the data will be allocated on the function stack!
139 * @param type The element type of the new array.
140 * @param var A lvalue of type (type *) which will hold the new array.
141 * @param n number of elements in this array.
143 * This macro creates a dynamic array on the functions stack of a given type at runtime.
144 * The size of the array cannot be changed later.
146 #define NEW_ARR_A(type, var, n) \
149 assert (_nelts >= 0); \
150 (var) = (void *)((_arr_descr *)alloca (_ARR_ELTS_OFFS + sizeof(type) * _nelts))->v.elts; \
151 _ARR_SET_DBGINF (_ARR_DESCR ((var)), ARR_A_MAGIC, sizeof (type)); \
152 (void)(_ARR_DESCR ((var))->nelts = _nelts); \
156 * Creates a new automatic array with the same number of elements as a
159 * @param type The element type of the new array.
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(type, var, arr) \
170 NEW_ARR_A (type, (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(type, var, arr) \
186 do { CLONE_ARR_A(type, (var), (arr)); \
187 memcpy ((var), (arr), sizeof (type) * 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 type The element type of the array.
216 * @param arr The array, which must be an lvalue.
217 * @param n The new size of the array.
219 * @remark This macro may change arr, so update all references!
221 #define ARR_RESIZE(type, arr, n) \
222 ((arr) = _arr_resize ((arr), (n), sizeof(type)))
225 * Resize a flexible array, always reallocate data.
227 * @param type The element type of the array.
228 * @param arr The array, which must be an lvalue.
229 * @param n The new size of the array.
231 * @remark This macro may change arr, so update all references!
233 #define ARR_SETLEN(type, arr, n) \
234 ((arr) = _arr_setlen ((arr), (n), sizeof(type) * (n)))
236 /** Set a length smaller than the current length of the array. Do not
237 * resize. len must be <= ARR_LEN(arr). */
238 #define ARR_SHRINKLEN(arr,len) \
239 (ARR_VRFY ((arr)), assert(_ARR_DESCR((arr))->nelts >= len), \
240 _ARR_DESCR((arr))->nelts = len)
243 * Resize a flexible array by growing it by delta elements.
245 * @param type The element type of the array.
246 * @param arr The array, which must be an lvalue.
247 * @param delta The delta number of elements.
249 * @remark This macro may change arr, so update all references!
251 #define ARR_EXTEND(type, arr, delta) \
252 ARR_RESIZE (type, (arr), ARR_LEN ((arr)) + (delta))
255 * Resize a flexible array to hold n elements only if it is currently shorter
258 * @param type The element type of the array.
259 * @param arr The array, which must be an lvalue.
260 * @param n The new size of the array.
262 * @remark This macro may change arr, so update all references!
264 #define ARR_EXTO(type, arr, n) \
265 ((n) >= ARR_LEN ((arr)) ? ARR_RESIZE (type, (arr), (n)+1) : (arr))
268 * Append one element to a flexible array.
270 * @param type The element type of the array.
271 * @param arr The array, which must be an lvalue.
272 * @param elt The new element, must be of type (type).
274 #define ARR_APP1(type, arr, elt) \
275 (ARR_EXTEND (type, (arr), 1), (arr)[ARR_LEN ((arr))-1] = (elt))
279 # define ARR_VRFY(arr) ((void)0)
280 # define ARR_IDX_VRFY(arr, idx) ((void)0)
282 # define ARR_VRFY(arr) \
283 assert ( ( (_ARR_DESCR((arr))->magic == ARR_D_MAGIC) \
284 || (_ARR_DESCR((arr))->magic == ARR_A_MAGIC) \
285 || (_ARR_DESCR((arr))->magic == ARR_F_MAGIC)) \
286 && ( (_ARR_DESCR((arr))->magic != ARR_F_MAGIC) \
287 || (_ARR_DESCR((arr))->u.allocated >= _ARR_DESCR((arr))->nelts)) \
288 && (_ARR_DESCR((arr))->nelts >= 0))
289 # define ARR_IDX_VRFY(arr, idx) \
290 assert ((0 <= (idx)) && ((idx) < ARR_LEN ((arr))))
295 Don't try this at home, kids, we're trained professionals ;->
296 ... or at the IPD, either. */
298 # define _ARR_DBGINF_DECL
299 # define _ARR_SET_DBGINF(descr, co, es)
301 # define _ARR_DBGINF_DECL int magic; size_t eltsize;
302 # define _ARR_SET_DBGINF(descr, co, es) \
303 ( (descr)->magic = (co), (descr)->eltsize = (es) )
307 * Construct an array header.
309 #define ARR_STRUCT(type, _nelts) \
313 struct obstack *obstack; /* dynamic: allocated on this obstack */ \
314 int allocated; /* flexible: #slots allocated */ \
318 type elts[(_nelts)]; \
319 aligned_type align[1]; \
324 * The array descriptor header type.
326 typedef ARR_STRUCT (aligned_type, 1) _arr_descr;
328 extern _arr_descr arr_mt_descr;
330 void *_new_arr_f (int, size_t);
331 void _del_arr_f (void *);
332 void *_new_arr_d (struct obstack *obstack, int nelts, size_t elts_size);
333 void *_arr_resize (void *, int, size_t);
334 void *_arr_setlen (void *, int, size_t);
336 #define _ARR_ELTS_OFFS offsetof (_arr_descr, v.elts)
337 #define _ARR_DESCR(elts) ((_arr_descr *)(void *)((char *)(elts) - _ARR_ELTS_OFFS))