2 * Copyright (C) 1995-2008 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
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 *)ir_new_arr_f((nelts), sizeof(type) * (nelts)))
54 * Creates a new flexible 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) (ir_del_arr_f((void *)(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 *)ir_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 * Returns the length of an array
145 * @param arr a flexible, dynamic, automatic or static array.
147 #define ARR_LEN(arr) (ARR_VRFY((arr)), ARR_DESCR((arr))->nelts)
150 * Resize a flexible array, allocate more data if needed but do NOT
153 * @param type The element type of the array.
154 * @param arr The array, which must be an lvalue.
155 * @param n The new size of the array.
157 * @remark This macro may change arr, so update all references!
159 #define ARR_RESIZE(type, arr, n) \
160 ((arr) = ir_arr_resize((void *)(arr), (n), sizeof(type)))
163 * Resize a flexible array, always reallocate data.
165 * @param type The element type of the array.
166 * @param arr The array, which must be an lvalue.
167 * @param n The new size of the array.
169 * @remark This macro may change arr, so update all references!
171 #define ARR_SETLEN(type, arr, n) \
172 ((arr) = ir_arr_setlen((void *)(arr), (n), sizeof(type) * (n)))
174 /** Set a length smaller than the current length of the array. Do not
175 * resize. len must be <= ARR_LEN(arr). */
176 #define ARR_SHRINKLEN(arr,len) \
177 (ARR_VRFY((arr)), assert(ARR_DESCR((arr))->nelts >= len), \
178 ARR_DESCR((arr))->nelts = len)
181 * Resize a flexible array by growing it by delta elements.
183 * @param type The element type of the array.
184 * @param arr The array, which must be an lvalue.
185 * @param delta The delta number of elements.
187 * @remark This macro may change arr, so update all references!
189 #define ARR_EXTEND(type, arr, delta) \
190 ARR_RESIZE(type, (arr), ARR_LEN((arr)) + (delta))
193 * Resize a flexible array to hold n elements only if it is currently shorter
196 * @param type The element type of the array.
197 * @param arr The array, which must be an lvalue.
198 * @param n The new size of the array.
200 * @remark This macro may change arr, so update all references!
202 #define ARR_EXTO(type, arr, n) \
203 ((n) >= ARR_LEN((arr)) ? ARR_RESIZE(type, (arr), (n)+1) : (arr))
206 * Append one element to a flexible array.
208 * @param type The element type of the array.
209 * @param arr The array, which must be an lvalue.
210 * @param elt The new element, must be of type (type).
212 #define ARR_APP1(type, arr, elt) \
213 (ARR_EXTEND(type, (arr), 1), (arr)[ARR_LEN((arr))-1] = (elt))
216 # define ARR_VRFY(arr) ((void)0)
217 # define ARR_IDX_VRFY(arr, idx) ((void)0)
219 # define ARR_VRFY(arr) ir_verify_arr(arr)
220 # define ARR_IDX_VRFY(arr, idx) \
221 assert((0 <= (idx)) && ((idx) < ARR_LEN((arr))))
224 /** A type that has most constrained alignment. */
232 * Construct an array header.
234 #define ARR_STRUCT(type, rnelts) \
239 struct obstack *obstack; /* dynamic: allocated on this obstack */ \
240 int allocated; /* flexible: #slots allocated */ \
244 type elts[(rnelts)]; \
245 aligned_type align[1]; \
250 * The array descriptor header type.
252 typedef ARR_STRUCT(aligned_type, 1) ir_arr_descr;
254 extern ir_arr_descr arr_mt_descr;
256 FIRM_API void *ir_new_arr_f(int nelts, size_t elts_size);
257 FIRM_API void ir_del_arr_f(void *elts);
258 FIRM_API void *ir_new_arr_d(struct obstack *obstack, int nelts, size_t elts_size);
259 FIRM_API void *ir_arr_resize(void *elts, int nelts, size_t elts_size);
260 FIRM_API void *ir_arr_setlen(void *elts, int nelts, size_t elts_size);
261 FIRM_API void ir_verify_arr(const void *elts);
263 #define ARR_ELTS_OFFS offsetof(ir_arr_descr, v.elts)
264 #define ARR_DESCR(elts) ((ir_arr_descr *)(void *)((char *)(elts) - ARR_ELTS_OFFS))
268 / ___| ___ _ __| |_ ___ __| | / \ _ __ _ __ __ _ _ _ ___
269 \___ \ / _ \| '__| __/ _ \/ _` | / _ \ | '__| '__/ _` | | | / __|
270 ___) | (_) | | | || __/ (_| | / ___ \| | | | | (_| | |_| \__ \
271 |____/ \___/|_| \__\___|\__,_| /_/ \_\_| |_| \__,_|\__, |___/
275 typedef int (ir_arr_cmp_func_t)(const void *a, const void *b);
278 * Do a binary search in an array.
279 * @param arr The array.
280 * @param elm_size The size of an array element.
281 * @param cmp A comparison function for two array elements (see qsort(3) for example).
282 * @param elm A pointer to the element we are looking for.
283 * @return This is somewhat tricky. Let <code>res</code> be the return value.
284 * If the return value is negative, then <code>elm</code> was not in the array
285 * but <code>-res - 1</code> gives the proper location where it should be inserted.
286 * If <code>res >= 0</code> then the element is in the array and <code>res</code>
287 * represents its index.
288 * That allows for testing membership and finding proper insertion indices.
289 * @note The differences to bsearch(3) which does not give proper insert locations
290 * in the case that the element is not conatined in the array.
292 static inline int ir_arr_bsearch(const void *arr, size_t elm_size, ir_arr_cmp_func_t *cmp, const void *elm)
294 int hi = ARR_LEN(arr);
298 int md = lo + ((hi - lo) >> 1);
299 int res = cmp((char *) arr + md * elm_size, elm);
311 #define ARR_SET_INSERT(arr, cmp, elm) \
313 int idx = ir_arr_bsearch((arr), sizeof((arr)[0]), (cmp), (elm)); \
316 memmove(&(arr)[idx+1], &(arr)[idx], sizeof((arr)[0]) * (ARR_DESCR((arr))->nelts - idx)); \
317 (arr)[idx] = *(elm); \
318 ++ARR_DESCR((arr))->nelts; \
322 #define ARR_SET_INSERT_EXT(type, arr, cmp, elm) \
324 int idx = ir_arr_bsearch((arr), sizeof((arr)[0]), (cmp), (elm)); \
326 int len = ARR_LEN(arr); \
328 ARR_EXTO(type, arr, len + 1); \
329 memmove(&(arr)[idx+1], &(arr)[idx], sizeof((arr)[0]) * (len - idx)); \
330 (arr)[idx] = *(elm); \
334 #define ARR_SET_REMOVE(arr, cmp, elm) \
336 int idx = ir_arr_bsearch((arr), sizeof((arr)[0]), (cmp), (elm)); \
338 --ARR_DESCR((arr))->nelts; \
339 memmove(&(arr)[idx], &(arr)[idx+1], sizeof((arr)[0]) * (ARR_DESCR((arr))->nelts - idx)); \
344 * Return the index of an element in an array set.
345 * To check for containment, use the expression:
346 * (ARR_SET_GET_IDX(arr, cmp, elm) >= 0)
348 * @return The index or some value < 0 if the element was not in the set.
350 #define ARR_SET_GET_IDX(arr, cmp, elm) \
351 (ARR_VRFY((arr)), ir_arr_bsearch((arr), sizeof((arr)[0]), cmp, (elm)))
354 #define ARR_SET_GET(arr, cmp, elm) \
355 ({ int idx = ARR_SET_GET_IDX(arr, cmp, elm); idx >= 0 ? &(arr)[idx] : NULL; })
357 #define ARR_SET_GET(arr, cmp, elm) \
358 (ARR_SET_GET_IDX(arr, cmp, elm) >= 0 ? &(arr)[ARR_SET_GET_IDX(arr, cmp, elm)] : NULL)
362 #define ARR_SET_CONTAINS(arr, cmp, elm) \
363 (ARR_SET_GET_IDX((arr), (cmp), (elm)) >= 0)
366 * Reset the array set.
367 * This just initializes the size to zero but does not wipe out any element.
369 #define ARR_SET_CLEAR(arr) ARR_SHRINKLEN(arr, 0)