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(type, arr) \
61 NEW_ARR_F (type, 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(type, arr) \
76 memcpy (CLONE_ARR_F (type, (arr)), (arr), sizeof(type) * 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(type, obstack, arr) \
118 NEW_ARR_D (type, (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(type, obstack, arr) \
134 memcpy (CLONE_ARR_D (type, (obstack), (arr)), (arr), sizeof(type) * 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 type The element type of the new array.
161 * @param var A lvalue of type (type *) which will hold the new array.
162 * @param arr An array from which the elements will be duplicated
164 * This macro creates a dynamic array of a given type at runtime.
165 * The size of the array cannot be changed later.
167 * @return A pointer to the dynamic array (can be used as a pointer to the
168 * first element of this array).
170 #define CLONE_ARR_A(type, var, arr) \
171 NEW_ARR_A (type, (var), ARR_LEN ((arr)))
174 * Duplicates an array and returns a new automatic one.
176 * @param type The element type of the new array.
177 * @param var A lvalue of type (type *) which will hold the new array.
178 * @param arr An array from with the number of elements will be taken
180 * This macro creates a dynamic array of a given type at runtime.
181 * The size of the array cannot be changed later.
183 * @return A pointer to the dynamic array (can be used as a pointer to the
184 * first element of this array).
186 #define DUP_ARR_A(type, var, arr) \
187 do { CLONE_ARR_A(type, (var), (arr)); \
188 memcpy ((var), (arr), sizeof (type) * ARR_LEN ((arr))); } \
192 * Declare an initialized (zero'ed) array of fixed size.
193 * This macro should be used at file scope only.
195 * @param type The element type of the new array.
196 * @param var A lvalue of type (type *) which will hold the new array.
197 * @param _nelts Number of elements in this new array.
199 #define DECL_ARR_S(type, var, _nelts) \
200 ARR_STRUCT(type, (_nelts) ? (_nelts) : 1) _##var; \
201 type *var = (_ARR_SET_DBGINF (&_##var, ARR_A_MAGIC, sizeof (type)), \
202 _##var.nelts = _nelts, \
206 * Returns the length of an array
208 * @param arr a flexible, dynamic, automatic or static array.
210 #define ARR_LEN(arr) (ARR_VRFY ((arr)), _ARR_DESCR((arr))->nelts)
213 * Resize a flexible array, allocate more data if needed but do NOT
216 * @param type The element type of the array.
217 * @param arr The array, which must be an lvalue.
218 * @param n The new size of the array.
220 * @remark This macro may change arr, so update all references!
222 #define ARR_RESIZE(type, arr, n) \
223 ((arr) = _arr_resize ((arr), (n), sizeof(type)))
226 * Resize a flexible array, always reallocate data.
228 * @param type The element type of the array.
229 * @param arr The array, which must be an lvalue.
230 * @param n The new size of the array.
232 * @remark This macro may change arr, so update all references!
234 #define ARR_SETLEN(type, arr, n) \
235 ((arr) = _arr_setlen ((arr), (n), sizeof(type) * (n)))
237 /** Set a length smaller than the current length of the array. Do not
238 * resize. len must be <= ARR_LEN(arr). */
239 #define ARR_SHRINKLEN(arr,len) \
240 (ARR_VRFY ((arr)), assert(_ARR_DESCR((arr))->nelts >= len), \
241 _ARR_DESCR((arr))->nelts = len)
244 * Resize a flexible array by growing it by delta elements.
246 * @param type The element type of the array.
247 * @param arr The array, which must be an lvalue.
248 * @param delta The delta number of elements.
250 * @remark This macro may change arr, so update all references!
252 #define ARR_EXTEND(type, arr, delta) \
253 ARR_RESIZE (type, (arr), ARR_LEN ((arr)) + (delta))
256 * Resize a flexible array to hold n elements only if it is currently shorter
259 * @param type The element type of the array.
260 * @param arr The array, which must be an lvalue.
261 * @param n The new size of the array.
263 * @remark This macro may change arr, so update all references!
265 #define ARR_EXTO(type, arr, n) \
266 ((n) >= ARR_LEN ((arr)) ? ARR_RESIZE (type, (arr), (n)+1) : (arr))
269 * Append one element to a flexible array.
271 * @param type The element type of the array.
272 * @param arr The array, which must be an lvalue.
273 * @param elt The new element, must be of type (type).
275 #define ARR_APP1(type, arr, elt) \
276 (ARR_EXTEND (type, (arr), 1), (arr)[ARR_LEN ((arr))-1] = (elt))
280 # define ARR_VRFY(arr) ((void)0)
281 # define ARR_IDX_VRFY(arr, idx) ((void)0)
283 # define ARR_VRFY(arr) \
284 assert ( ( (_ARR_DESCR((arr))->magic == ARR_D_MAGIC) \
285 || (_ARR_DESCR((arr))->magic == ARR_A_MAGIC) \
286 || (_ARR_DESCR((arr))->magic == ARR_F_MAGIC)) \
287 && ( (_ARR_DESCR((arr))->magic != ARR_F_MAGIC) \
288 || (_ARR_DESCR((arr))->u.allocated >= _ARR_DESCR((arr))->nelts)) \
289 && (_ARR_DESCR((arr))->nelts >= 0))
290 # define ARR_IDX_VRFY(arr, idx) \
291 assert ((0 <= (idx)) && ((idx) < ARR_LEN ((arr))))
296 Don't try this at home, kids, we're trained professionals ;->
297 ... or at the IPD, either. */
299 # define _ARR_DBGINF_DECL
300 # define _ARR_SET_DBGINF(descr, co, es)
302 # define _ARR_DBGINF_DECL int magic; size_t eltsize;
303 # define _ARR_SET_DBGINF(descr, co, es) \
304 ( (descr)->magic = (co), (descr)->eltsize = (es) )
308 * Construct an array header.
310 #define ARR_STRUCT(type, _nelts) \
314 struct obstack *obstack; /* dynamic: allocated on this obstack */ \
315 int allocated; /* flexible: #slots allocated */ \
319 type elts[(_nelts)]; \
320 aligned_type align[1]; \
325 * The array descriptor header type.
327 typedef ARR_STRUCT (aligned_type, 1) _arr_descr;
329 extern _arr_descr arr_mt_descr;
331 void *_new_arr_f (int, size_t);
332 void _del_arr_f (void *);
333 void *_new_arr_d (struct obstack *obstack, int nelts, size_t elts_size);
334 void *_arr_resize (void *, int, size_t);
335 void *_arr_setlen (void *, int, size_t);
337 #define _ARR_ELTS_OFFS offsetof (_arr_descr, v.elts)
338 #define _ARR_DESCR(elts) ((_arr_descr *)(void *)((char *)(elts) - _ARR_ELTS_OFFS))