2 * Copyright (C) 1995-2011 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
25 #ifndef FIRM_ADT_ARRAY_H
26 #define FIRM_ADT_ARRAY_H
37 * @defgroup array Arrays
42 * Creates a flexible array.
44 * @param type The element type of the new array.
45 * @param nelts a size_t expression evaluating to the number of elements
47 * This macro creates a flexible array of a given type at runtime.
48 * The size of the array can be changed later.
50 * @return A pointer to the flexible array (can be used as a pointer to the
51 * first element of this array).
53 #define NEW_ARR_F(type, nelts) \
54 ((type *)ir_new_arr_f((nelts), sizeof(type) * (nelts)))
57 * Creates a new flexible array with the same number of elements as a
60 * @param type The element type of the new array.
61 * @param arr An array from which the number of elements will be taken
63 * This macro creates a flexible array of a given type at runtime.
64 * The size of the array can be changed later.
66 * @return A pointer to the flexible array (can be used as a pointer to the
67 * first element of this array).
69 #define CLONE_ARR_F(type, arr) \
70 NEW_ARR_F(type, ARR_LEN((arr)))
73 * Duplicates an array and returns the new flexible one.
75 * @param type The element type of the new array.
76 * @param arr An array from which the elements will be duplicated
78 * This macro creates a flexible array of a given type at runtime.
79 * The size of the array can be changed later.
81 * @return A pointer to the flexible array (can be used as a pointer to the
82 * first element of this array).
84 #define DUP_ARR_F(type, arr) \
85 ((type*) memcpy(CLONE_ARR_F(type, (arr)), (arr), sizeof(type) * ARR_LEN((arr))))
88 * Delete a flexible array.
90 * @param arr The flexible array.
92 #define DEL_ARR_F(arr) (ir_del_arr_f((void *)(arr)))
95 * Creates a dynamic array on an obstack.
97 * @param type The element type of the new array.
98 * @param obstack A struct obstack * were the data will be allocated
99 * @param nelts A size_t expression evaluating to the number of elements
101 * This macro creates a dynamic array of a given type at runtime.
102 * The size of the array cannot be changed later.
104 * @return A pointer to the dynamic array (can be used as a pointer to the
105 * first element of this array).
107 #define NEW_ARR_D(type, obstack, nelts) \
109 ? (type *)ir_new_arr_d((obstack), (nelts), sizeof(type) * (nelts)) \
110 : (type *)arr_mt_descr.elts)
113 * Create a dynamic array on an obstack and null its contents.
115 #define NEW_ARR_DZ(type, obstack, nelts) \
116 ((type*)memset(NEW_ARR_D(type, (obstack), (nelts)), 0, sizeof(type) * (nelts)))
119 * Creates a new dynamic array with the same number of elements as a
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 number of elements will be taken
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 CLONE_ARR_D(type, obstack, arr) \
133 NEW_ARR_D(type, (obstack), ARR_LEN((arr)))
136 * Duplicates an array and returns the new dynamic one.
138 * @param type The element type of the new array.
139 * @param obstack An struct obstack * were the data will be allocated
140 * @param arr An array from which the elements will be duplicated
142 * This macro creates a dynamic array of a given type at runtime.
143 * The size of the array cannot be changed later.
145 * @return A pointer to the dynamic array (can be used as a pointer to the
146 * first element of this array).
148 #define DUP_ARR_D(type, obstack, arr) \
149 ((type*)memcpy(CLONE_ARR_D(type, (obstack), (arr)), (arr), sizeof(type) * ARR_LEN ((arr))))
152 * Returns the length of an array
154 * @param arr a flexible, dynamic, automatic or static array.
156 #define ARR_LEN(arr) (ARR_VRFY((arr)), ARR_DESCR((arr))->nelts)
159 * Resize a flexible array, allocate more data if needed but do NOT
162 * @param type The element type of the array.
163 * @param arr The array, which must be an lvalue.
164 * @param n The new size of the array.
166 * @remark This macro may change arr, so update all references!
168 #define ARR_RESIZE(type, arr, n) \
169 ((arr) = (type*) ir_arr_resize((void *)(arr), (n), sizeof(type)))
172 * Resize a flexible array, always reallocate data.
174 * @param type The element type of the array.
175 * @param arr The array, which must be an lvalue.
176 * @param n The new size of the array.
178 * @remark This macro may change arr, so update all references!
180 #define ARR_SETLEN(type, arr, n) \
181 ((arr) = (type*) ir_arr_setlen((void *)(arr), (n), sizeof(type) * (n)))
184 * Resize a flexible array by growing it by delta elements.
186 * @param type The element type of the array.
187 * @param arr The array, which must be an lvalue.
188 * @param delta The delta number of elements.
190 * @remark This macro may change arr, so update all references!
192 #define ARR_EXTEND(type, arr, delta) \
193 ARR_RESIZE(type, (arr), ARR_LEN((arr)) + (delta))
196 * Resize a flexible array to hold n elements only if it is currently shorter
199 * @param type The element type of the array.
200 * @param arr The array, which must be an lvalue.
201 * @param n The new size of the array.
203 * @remark This macro may change arr, so update all references!
205 #define ARR_EXTO(type, arr, n) \
207 if ((n) >= ARR_LEN(arr)) { ARR_RESIZE(type, arr, (n)+1); } \
211 * Append one element to a flexible array.
213 * @param type The element type of the array.
214 * @param arr The array, which must be an lvalue.
215 * @param elt The new element, must be of type (type).
217 #define ARR_APP1(type, arr, elt) \
218 (ARR_EXTEND(type, (arr), 1), (arr)[ARR_LEN((arr))-1] = (elt))
221 # define ARR_VRFY(arr) ((void)0)
222 # define ARR_IDX_VRFY(arr, idx) ((void)0)
224 /** Check array for consistency */
225 # define ARR_VRFY(arr) ir_verify_arr(arr)
226 /** Check if index is within array bounds */
227 # define ARR_IDX_VRFY(arr, idx) \
228 assert((0 <= (idx)) && ((idx) < ARR_LEN((arr))))
233 /** A type that has most constrained alignment. */
241 * The array descriptor header type.
244 int magic; /**< array magic. */
245 size_t allocated; /**< number of allocated elements. */
246 size_t nelts; /**< current length of the array. */
247 aligned_type elts[1]; /**< start of the array data. */
250 extern ir_arr_descr arr_mt_descr;
252 FIRM_API void *ir_new_arr_f(size_t nelts, size_t elts_size);
253 FIRM_API void ir_del_arr_f(void *elts);
254 FIRM_API void *ir_new_arr_d(struct obstack *obstack, size_t nelts, size_t elts_size);
255 FIRM_API void *ir_arr_resize(void *elts, size_t nelts, size_t elts_size);
256 FIRM_API void *ir_arr_setlen(void *elts, size_t nelts, size_t elts_size);
257 FIRM_API void ir_verify_arr(const void *elts);
259 #define ARR_ELTS_OFFS offsetof(ir_arr_descr, elts)
260 #define ARR_DESCR(elts) ((ir_arr_descr *)(void *)((char *)(elts) - ARR_ELTS_OFFS))
262 /** Set a length smaller than the current length of the array. Do not
263 * resize. len must be <= ARR_LEN(arr). */
264 static inline void ARR_SHRINKLEN(void *arr, size_t new_len)
267 assert(ARR_DESCR(arr)->nelts >= new_len);
268 ARR_DESCR(arr)->nelts = new_len;