2 * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Dynamic and flexible arrays for C.
23 * @author Markus Armbruster, Michael Beck, Matthias Braun, Sebastian Hack
26 #ifndef FIRM_ADT_ARRAY_H
27 #define FIRM_ADT_ARRAY_H
32 #include "firm_config.h"
38 #define ARR_D_MAGIC FOURCC('A','R','R','D')
39 #define ARR_A_MAGIC FOURCC('A','R','R','A')
40 #define ARR_F_MAGIC FOURCC('A','R','R','F')
43 * Creates a flexible array.
45 * @param type The element type of the new array.
46 * @param nelts a size_t expression evaluating to the number of elements
48 * This macro creates a flexible array of a given type at runtime.
49 * The size of the array can be changed later.
51 * @return A pointer to the flexible array (can be used as a pointer to the
52 * first element of this array).
54 #define NEW_ARR_F(type, nelts) \
55 ((type *)_new_arr_f ((nelts), sizeof(type) * (nelts)))
58 * Creates a new flexible array with the same number of elements as a
61 * @param type The element type of the new array.
62 * @param arr An array from which the number of elements will be taken
64 * This macro creates a flexible array of a given type at runtime.
65 * The size of the array can be changed later.
67 * @return A pointer to the flexible array (can be used as a pointer to the
68 * first element of this array).
70 #define CLONE_ARR_F(type, arr) \
71 NEW_ARR_F (type, ARR_LEN ((arr)))
74 * Duplicates an array and returns the new flexible one.
76 * @param type The element type of the new array.
77 * @param arr An array from which the elements will be duplicated
79 * This macro creates a flexible array of a given type at runtime.
80 * The size of the array can be changed later.
82 * @return A pointer to the flexible array (can be used as a pointer to the
83 * first element of this array).
85 #define DUP_ARR_F(type, arr) \
86 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).
127 #define CLONE_ARR_D(type, obstack, arr) \
128 NEW_ARR_D (type, (obstack), ARR_LEN ((arr)))
131 * Duplicates an array and returns the new dynamic one.
133 * @param type The element type of the new array.
134 * @param obstack An struct obstack * were the data will be allocated
135 * @param arr An array from which the elements will be duplicated
137 * This macro creates a dynamic array of a given type at runtime.
138 * The size of the array cannot be changed later.
140 * @return A pointer to the dynamic array (can be used as a pointer to the
141 * first element of this array).
143 #define DUP_ARR_D(type, obstack, arr) \
144 memcpy (CLONE_ARR_D (type, (obstack), (arr)), (arr), sizeof(type) * ARR_LEN ((arr)))
147 * Create an automatic array which will be deleted at return from function.
148 * Beware, the data will be allocated on the function stack!
150 * @param type The element type of the new array.
151 * @param var A lvalue of type (type *) which will hold the new array.
152 * @param n number of elements in this array.
154 * This macro creates a dynamic array on the functions stack of a given type at runtime.
155 * The size of the array cannot be changed later.
157 #define NEW_ARR_A(type, var, n) \
160 assert (_nelts >= 0); \
161 (var) = (void *)((_arr_descr *)alloca (_ARR_ELTS_OFFS + sizeof(type) * _nelts))->v.elts; \
162 _ARR_SET_DBGINF (_ARR_DESCR ((var)), ARR_A_MAGIC, sizeof (type)); \
163 (void)(_ARR_DESCR ((var))->nelts = _nelts); \
167 * Creates a new automatic array with the same number of elements as a
170 * @param type The element type of the new array.
171 * @param var A lvalue of type (type *) which will hold the new array.
172 * @param arr An array from which the elements will be duplicated
174 * This macro creates a dynamic array of a given type at runtime.
175 * The size of the array cannot be changed later.
177 * @return A pointer to the dynamic array (can be used as a pointer to the
178 * first element of this array).
180 #define CLONE_ARR_A(type, var, arr) \
181 NEW_ARR_A (type, (var), ARR_LEN ((arr)))
184 * Duplicates an array and returns a new automatic one.
186 * @param type The element type of the new array.
187 * @param var A lvalue of type (type *) which will hold the new array.
188 * @param arr An array from with the number of elements will be taken
190 * This macro creates a dynamic array of a given type at runtime.
191 * The size of the array cannot be changed later.
193 * @return A pointer to the dynamic array (can be used as a pointer to the
194 * first element of this array).
196 #define DUP_ARR_A(type, var, arr) \
197 do { CLONE_ARR_A(type, (var), (arr)); \
198 memcpy ((var), (arr), sizeof (type) * ARR_LEN ((arr))); } \
202 * Declare an initialized (zero'ed) array of fixed size.
203 * This macro should be used at file scope only.
205 * @param type The element type of the new array.
206 * @param var A lvalue of type (type *) which will hold the new array.
207 * @param _nelts Number of elements in this new array.
209 #define DECL_ARR_S(type, var, _nelts) \
210 ARR_STRUCT(type, (_nelts) ? (_nelts) : 1) _##var; \
211 type *var = (_ARR_SET_DBGINF (&_##var, ARR_A_MAGIC, sizeof (type)), \
212 _##var.nelts = _nelts, \
216 * Returns the length of an array
218 * @param arr a flexible, dynamic, automatic or static array.
220 #define ARR_LEN(arr) (ARR_VRFY ((arr)), _ARR_DESCR((arr))->nelts)
223 * Resize a flexible array, allocate more data if needed but do NOT
226 * @param type The element type of the array.
227 * @param arr The array, which must be an lvalue.
228 * @param n The new size of the array.
230 * @remark This macro may change arr, so update all references!
232 #define ARR_RESIZE(type, arr, n) \
233 ((arr) = _arr_resize ((arr), (n), sizeof(type)))
236 * Resize a flexible array, always reallocate data.
238 * @param type The element type of the array.
239 * @param arr The array, which must be an lvalue.
240 * @param n The new size of the array.
242 * @remark This macro may change arr, so update all references!
244 #define ARR_SETLEN(type, arr, n) \
245 ((arr) = _arr_setlen ((arr), (n), sizeof(type) * (n)))
247 /** Set a length smaller than the current length of the array. Do not
248 * resize. len must be <= ARR_LEN(arr). */
249 #define ARR_SHRINKLEN(arr,len) \
250 (ARR_VRFY ((arr)), assert(_ARR_DESCR((arr))->nelts >= len), \
251 _ARR_DESCR((arr))->nelts = len)
254 * Resize a flexible array by growing it by delta elements.
256 * @param type The element type of the array.
257 * @param arr The array, which must be an lvalue.
258 * @param delta The delta number of elements.
260 * @remark This macro may change arr, so update all references!
262 #define ARR_EXTEND(type, arr, delta) \
263 ARR_RESIZE (type, (arr), ARR_LEN ((arr)) + (delta))
266 * Resize a flexible array to hold n elements only if it is currently shorter
269 * @param type The element type of the array.
270 * @param arr The array, which must be an lvalue.
271 * @param n The new size of the array.
273 * @remark This macro may change arr, so update all references!
275 #define ARR_EXTO(type, arr, n) \
276 ((n) >= ARR_LEN ((arr)) ? ARR_RESIZE (type, (arr), (n)+1) : (arr))
279 * Append one element to a flexible array.
281 * @param type The element type of the array.
282 * @param arr The array, which must be an lvalue.
283 * @param elt The new element, must be of type (type).
285 #define ARR_APP1(type, arr, elt) \
286 (ARR_EXTEND (type, (arr), 1), (arr)[ARR_LEN ((arr))-1] = (elt))
289 # define ARR_VRFY(arr) ((void)0)
290 # define ARR_IDX_VRFY(arr, idx) ((void)0)
292 # define ARR_VRFY(arr) \
293 assert ( ( (_ARR_DESCR((arr))->magic == ARR_D_MAGIC) \
294 || (_ARR_DESCR((arr))->magic == ARR_A_MAGIC) \
295 || (_ARR_DESCR((arr))->magic == ARR_F_MAGIC)) \
296 && ( (_ARR_DESCR((arr))->magic != ARR_F_MAGIC) \
297 || (_ARR_DESCR((arr))->u.allocated >= _ARR_DESCR((arr))->nelts)) \
298 && (_ARR_DESCR((arr))->nelts >= 0))
299 # define ARR_IDX_VRFY(arr, idx) \
300 assert ((0 <= (idx)) && ((idx) < ARR_LEN ((arr))))
305 Don't try this at home, kids, we're trained professionals ;->
306 ... or at the IPD, either. */
308 # define _ARR_DBGINF_DECL
309 # define _ARR_SET_DBGINF(descr, co, es)
311 # define _ARR_DBGINF_DECL int magic; size_t eltsize;
312 # define _ARR_SET_DBGINF(descr, co, es) \
313 ( (descr)->magic = (co), (descr)->eltsize = (es) )
317 * Construct an array header.
319 #define ARR_STRUCT(type, _nelts) \
323 struct obstack *obstack; /* dynamic: allocated on this obstack */ \
324 int allocated; /* flexible: #slots allocated */ \
328 type elts[(_nelts)]; \
329 aligned_type align[1]; \
334 * The array descriptor header type.
336 typedef ARR_STRUCT (aligned_type, 1) _arr_descr;
338 extern _arr_descr arr_mt_descr;
340 void *_new_arr_f (int, size_t);
341 void _del_arr_f (void *);
342 void *_new_arr_d (struct obstack *obstack, int nelts, size_t elts_size);
343 void *_arr_resize (void *, int, size_t);
344 void *_arr_setlen (void *, int, size_t);
346 #define _ARR_ELTS_OFFS offsetof (_arr_descr, v.elts)
347 #define _ARR_DESCR(elts) ((_arr_descr *)(void *)((char *)(elts) - _ARR_ELTS_OFFS))
351 / ___| ___ _ __| |_ ___ __| | / \ _ __ _ __ __ _ _ _ ___
352 \___ \ / _ \| '__| __/ _ \/ _` | / _ \ | '__| '__/ _` | | | / __|
353 ___) | (_) | | | || __/ (_| | / ___ \| | | | | (_| | |_| \__ \
354 |____/ \___/|_| \__\___|\__,_| /_/ \_\_| |_| \__,_|\__, |___/
358 typedef int (_arr_cmp_func_t)(const void *a, const void *b);
361 * Do a binary search in an array.
362 * @param arr The array.
363 * @param elm_size The size of an array element.
364 * @param cmp A comparison function for two array elements (see qsort(3) for example).
365 * @param elm A pointer to the element we are looking for.
366 * @return This is somewhat tricky. Let <code>res</code> be the return value.
367 * If the return value is negative, then <code>elm</code> was not in the array
368 * but <code>-res - 1</code> gives the proper location where it should be inserted.
369 * If <code>res >= 0</code> then the element is in the array and <code>res</code>
370 * represents its index.
371 * That allows for testing membership and finding proper insertion indices.
372 * @note The differences to bsearch(3) which does not give proper insert locations
373 * in the case that the element is not conatined in the array.
375 static INLINE __attribute__((const, unused)) int
376 _arr_bsearch(const void *arr, size_t elm_size, _arr_cmp_func_t *cmp, const void *elm)
378 int hi = ARR_LEN(arr);
382 int md = lo + ((hi - lo) >> 1);
383 int res = cmp((char *) arr + md * elm_size, elm);
395 #define ARR_SET_INSERT(arr, cmp, elm) \
397 int idx = _arr_bsearch((arr), sizeof((arr)[0]), (cmp), (elm)); \
400 memmove(&(arr)[idx+1], &(arr)[idx], sizeof((arr)[0]) * (_ARR_DESCR((arr))->nelts - idx)); \
401 (arr)[idx] = (elm); \
402 ++_ARR_DESCR((arr))->nelts; \
406 #define ARR_SET_REMOVE(arr, cmp, elm) \
408 int idx = _arr_bsearch((arr), sizeof((arr)[0]), (cmp), (elm)); \
410 --_ARR_DESCR((arr))->nelts; \
411 memmove(&(arr)[idx], &(arr)[idx+1], sizeof((arr)[0]) * (_ARR_DESCR((arr))->nelts - idx)); \
416 * Return the index of an element in an array set.
417 * To check for containment, use the expression:
418 * (ARR_SET_GET_IDX(arr, cmp, elm) >= 0)
420 * @return The index or some value < 0 if the element was not in the set.
422 #define ARR_SET_GET_IDX(arr, cmp, elm) \
423 (ARR_VRFY((arr)), _arr_bsearch((arr), sizeof((arr)[0]), cmp, elm))
425 #define ARR_SET_CONTAINS(arr, cmp, elm) \
426 (ARR_SET_GET_IDX((arr), (cmp), (elm)) >= 0)
429 * Reset the array set.
430 * This just initializes the size to zero but does not wipe out any element.
432 #define ARR_SET_CLEAR(arr) ARR_SHRINKLEN(arr, 0)
435 #endif /* FIRM_ADT_ARRAY_H */