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
36 * Creates a flexible array.
38 * @param type The element type of the new array.
39 * @param nelts a size_t expression evaluating to the number of elements
41 * This macro creates a flexible array of a given type at runtime.
42 * The size of the array can be changed later.
44 * @return A pointer to the flexible array (can be used as a pointer to the
45 * first element of this array).
47 #define NEW_ARR_F(type, nelts) \
48 ((type *)ir_new_arr_f((nelts), sizeof(type) * (nelts)))
51 * Creates a new flexible array with the same number of elements as a
54 * @param type The element type of the new array.
55 * @param arr An array from which the number of elements will be taken
57 * This macro creates a flexible array of a given type at runtime.
58 * The size of the array can be changed later.
60 * @return A pointer to the flexible array (can be used as a pointer to the
61 * first element of this array).
63 #define CLONE_ARR_F(type, arr) \
64 NEW_ARR_F(type, ARR_LEN((arr)))
67 * Duplicates an array and returns the new flexible one.
69 * @param type The element type of the new array.
70 * @param arr An array from which the elements will be duplicated
72 * This macro creates a flexible array of a given type at runtime.
73 * The size of the array can be changed later.
75 * @return A pointer to the flexible array (can be used as a pointer to the
76 * first element of this array).
78 #define DUP_ARR_F(type, arr) \
79 ((type*) memcpy(CLONE_ARR_F(type, (arr)), (arr), sizeof(type) * ARR_LEN((arr))))
82 * Delete a flexible array.
84 * @param arr The flexible array.
86 #define DEL_ARR_F(arr) (ir_del_arr_f((void *)(arr)))
89 * Creates a dynamic array on an obstack.
91 * @param type The element type of the new array.
92 * @param obstack A struct obstack * were the data will be allocated
93 * @param nelts A size_t expression evaluating to the number of elements
95 * This macro creates a dynamic array of a given type at runtime.
96 * The size of the array cannot be changed later.
98 * @return A pointer to the dynamic array (can be used as a pointer to the
99 * first element of this array).
101 #define NEW_ARR_D(type, obstack, nelts) \
103 ? (type *)ir_new_arr_d((obstack), (nelts), sizeof(type) * (nelts)) \
104 : (type *)arr_mt_descr.elts)
107 * Creates a new dynamic array with the same number of elements as a
110 * @param type The element type of the new array.
111 * @param obstack An struct obstack * were the data will be allocated
112 * @param arr An array from which the number of elements will be taken
114 * This macro creates a dynamic array of a given type at runtime.
115 * The size of the array cannot be changed later.
117 * @return A pointer to the dynamic array (can be used as a pointer to the
118 * first element of this array).
120 #define CLONE_ARR_D(type, obstack, arr) \
121 NEW_ARR_D(type, (obstack), ARR_LEN((arr)))
124 * Duplicates an array and returns the new dynamic one.
126 * @param type The element type of the new array.
127 * @param obstack An struct obstack * were the data will be allocated
128 * @param arr An array from which the elements will be duplicated
130 * This macro creates a dynamic array of a given type at runtime.
131 * The size of the array cannot be changed later.
133 * @return A pointer to the dynamic array (can be used as a pointer to the
134 * first element of this array).
136 #define DUP_ARR_D(type, obstack, arr) \
137 ((type*)memcpy(CLONE_ARR_D(type, (obstack), (arr)), (arr), sizeof(type) * ARR_LEN ((arr))))
140 * Returns the length of an array
142 * @param arr a flexible, dynamic, automatic or static array.
144 #define ARR_LEN(arr) (ARR_VRFY((arr)), ARR_DESCR((arr))->nelts)
147 * Resize a flexible array, allocate more data if needed but do NOT
150 * @param type The element type of the array.
151 * @param arr The array, which must be an lvalue.
152 * @param n The new size of the array.
154 * @remark This macro may change arr, so update all references!
156 #define ARR_RESIZE(type, arr, n) \
157 ((arr) = (type*) ir_arr_resize((void *)(arr), (n), sizeof(type)))
160 * Resize a flexible array, always reallocate data.
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_SETLEN(type, arr, n) \
169 ((arr) = (type*) ir_arr_setlen((void *)(arr), (n), sizeof(type) * (n)))
172 * Resize a flexible array by growing it by delta elements.
174 * @param type The element type of the array.
175 * @param arr The array, which must be an lvalue.
176 * @param delta The delta number of elements.
178 * @remark This macro may change arr, so update all references!
180 #define ARR_EXTEND(type, arr, delta) \
181 ARR_RESIZE(type, (arr), ARR_LEN((arr)) + (delta))
184 * Resize a flexible array to hold n elements only if it is currently shorter
187 * @param type The element type of the array.
188 * @param arr The array, which must be an lvalue.
189 * @param n The new size of the array.
191 * @remark This macro may change arr, so update all references!
193 #define ARR_EXTO(type, arr, n) \
195 if ((n) >= ARR_LEN(arr)) { ARR_RESIZE(type, arr, (n)+1); } \
199 * Append one element to a flexible array.
201 * @param type The element type of the array.
202 * @param arr The array, which must be an lvalue.
203 * @param elt The new element, must be of type (type).
205 #define ARR_APP1(type, arr, elt) \
206 (ARR_EXTEND(type, (arr), 1), (arr)[ARR_LEN((arr))-1] = (elt))
209 # define ARR_VRFY(arr) ((void)0)
210 # define ARR_IDX_VRFY(arr, idx) ((void)0)
212 # define ARR_VRFY(arr) ir_verify_arr(arr)
213 # define ARR_IDX_VRFY(arr, idx) \
214 assert((0 <= (idx)) && ((idx) < ARR_LEN((arr))))
217 /** A type that has most constrained alignment. */
226 * The array descriptor header type.
229 int magic; /**< array magic. */
230 size_t eltsize; /**< size of array elements. */
232 struct obstack *obstack; /**< for obstack array: the obstack. */
233 size_t allocated; /**< number of allocated elements. */
235 size_t nelts; /**< current length of the array. */
236 aligned_type elts[1]; /**< start of the array data. */
239 extern ir_arr_descr arr_mt_descr;
241 FIRM_API void *ir_new_arr_f(size_t nelts, size_t elts_size);
242 FIRM_API void ir_del_arr_f(void *elts);
243 FIRM_API void *ir_new_arr_d(struct obstack *obstack, size_t nelts, size_t elts_size);
244 FIRM_API void *ir_arr_resize(void *elts, size_t nelts, size_t elts_size);
245 FIRM_API void *ir_arr_setlen(void *elts, size_t nelts, size_t elts_size);
246 FIRM_API void ir_verify_arr(const void *elts);
248 #define ARR_ELTS_OFFS offsetof(ir_arr_descr, elts)
249 #define ARR_DESCR(elts) ((ir_arr_descr *)(void *)((char *)(elts) - ARR_ELTS_OFFS))
251 /** Set a length smaller than the current length of the array. Do not
252 * resize. len must be <= ARR_LEN(arr). */
253 static inline void ARR_SHRINKLEN(void *arr, size_t new_len)
256 assert(ARR_DESCR(arr)->nelts >= new_len);
257 ARR_DESCR(arr)->nelts = new_len;