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
38 * Creates a flexible array.
40 * @param type The element type of the new array.
41 * @param nelts a size_t expression evaluating to the number of elements
43 * This macro creates a flexible array of a given type at runtime.
44 * The size of the array can be changed later.
46 * @return A pointer to the flexible array (can be used as a pointer to the
47 * first element of this array).
49 #define NEW_ARR_F(type, nelts) \
50 ((type *)ir_new_arr_f((nelts), sizeof(type) * (nelts)))
53 * Creates a new flexible array with the same number of elements as a
56 * @param type The element type of the new array.
57 * @param arr An array from which the number of elements will be taken
59 * This macro creates a flexible array of a given type at runtime.
60 * The size of the array can be changed later.
62 * @return A pointer to the flexible array (can be used as a pointer to the
63 * first element of this array).
65 #define CLONE_ARR_F(type, arr) \
66 NEW_ARR_F(type, ARR_LEN((arr)))
69 * Duplicates an array and returns the new flexible one.
71 * @param type The element type of the new array.
72 * @param arr An array from which the elements will be duplicated
74 * This macro creates a flexible array of a given type at runtime.
75 * The size of the array can be changed later.
77 * @return A pointer to the flexible array (can be used as a pointer to the
78 * first element of this array).
80 #define DUP_ARR_F(type, arr) \
81 ((type*) memcpy(CLONE_ARR_F(type, (arr)), (arr), sizeof(type) * ARR_LEN((arr))))
84 * Delete a flexible array.
86 * @param arr The flexible array.
88 #define DEL_ARR_F(arr) (ir_del_arr_f((void *)(arr)))
91 * Creates a dynamic array on an obstack.
93 * @param type The element type of the new array.
94 * @param obstack A struct obstack * were the data will be allocated
95 * @param nelts A size_t expression evaluating to the number of elements
97 * This macro creates a dynamic array of a given type at runtime.
98 * The size of the array cannot be changed later.
100 * @return A pointer to the dynamic array (can be used as a pointer to the
101 * first element of this array).
103 #define NEW_ARR_D(type, obstack, nelts) \
105 ? (type *)ir_new_arr_d((obstack), (nelts), sizeof(type) * (nelts)) \
106 : (type *)arr_mt_descr.elts)
109 * Creates a new dynamic array with the same number of elements as a
112 * @param type The element type of the new array.
113 * @param obstack An struct obstack * were the data will be allocated
114 * @param arr An array from which the number of elements will be taken
116 * This macro creates a dynamic array of a given type at runtime.
117 * The size of the array cannot be changed later.
119 * @return A pointer to the dynamic array (can be used as a pointer to the
120 * first element of this array).
122 #define CLONE_ARR_D(type, obstack, arr) \
123 NEW_ARR_D(type, (obstack), ARR_LEN((arr)))
126 * Duplicates an array and returns the new dynamic one.
128 * @param type The element type of the new array.
129 * @param obstack An struct obstack * were the data will be allocated
130 * @param arr An array from which the elements will be duplicated
132 * This macro creates a dynamic array of a given type at runtime.
133 * The size of the array cannot be changed later.
135 * @return A pointer to the dynamic array (can be used as a pointer to the
136 * first element of this array).
138 #define DUP_ARR_D(type, obstack, arr) \
139 ((type*)memcpy(CLONE_ARR_D(type, (obstack), (arr)), (arr), sizeof(type) * ARR_LEN ((arr))))
142 * Returns the length of an array
144 * @param arr a flexible, dynamic, automatic or static array.
146 #define ARR_LEN(arr) (ARR_VRFY((arr)), ARR_DESCR((arr))->nelts)
149 * Resize a flexible array, allocate more data if needed but do NOT
152 * @param type The element type of the array.
153 * @param arr The array, which must be an lvalue.
154 * @param n The new size of the array.
156 * @remark This macro may change arr, so update all references!
158 #define ARR_RESIZE(type, arr, n) \
159 ((arr) = (type*) ir_arr_resize((void *)(arr), (n), sizeof(type)))
162 * Resize a flexible array, always reallocate data.
164 * @param type The element type of the array.
165 * @param arr The array, which must be an lvalue.
166 * @param n The new size of the array.
168 * @remark This macro may change arr, so update all references!
170 #define ARR_SETLEN(type, arr, n) \
171 ((arr) = (type*) ir_arr_setlen((void *)(arr), (n), sizeof(type) * (n)))
174 * Resize a flexible array by growing it by delta elements.
176 * @param type The element type of the array.
177 * @param arr The array, which must be an lvalue.
178 * @param delta The delta number of elements.
180 * @remark This macro may change arr, so update all references!
182 #define ARR_EXTEND(type, arr, delta) \
183 ARR_RESIZE(type, (arr), ARR_LEN((arr)) + (delta))
186 * Resize a flexible array to hold n elements only if it is currently shorter
189 * @param type The element type of the array.
190 * @param arr The array, which must be an lvalue.
191 * @param n The new size of the array.
193 * @remark This macro may change arr, so update all references!
195 #define ARR_EXTO(type, arr, n) \
197 if ((n) >= ARR_LEN(arr)) { ARR_RESIZE(type, arr, (n)+1); } \
201 * Append one element to a flexible array.
203 * @param type The element type of the array.
204 * @param arr The array, which must be an lvalue.
205 * @param elt The new element, must be of type (type).
207 #define ARR_APP1(type, arr, elt) \
208 (ARR_EXTEND(type, (arr), 1), (arr)[ARR_LEN((arr))-1] = (elt))
211 # define ARR_VRFY(arr) ((void)0)
212 # define ARR_IDX_VRFY(arr, idx) ((void)0)
214 # define ARR_VRFY(arr) ir_verify_arr(arr)
215 # define ARR_IDX_VRFY(arr, idx) \
216 assert((0 <= (idx)) && ((idx) < ARR_LEN((arr))))
219 /** A type that has most constrained alignment. */
228 * The array descriptor header type.
231 int magic; /**< array magic. */
232 size_t eltsize; /**< size of array elements. */
234 struct obstack *obstack; /**< for obstack array: the obstack. */
235 size_t allocated; /**< number of allocated elements. */
237 size_t nelts; /**< current length of the array. */
238 aligned_type elts[1]; /**< start of the array data. */
241 extern ir_arr_descr arr_mt_descr;
243 FIRM_API void *ir_new_arr_f(size_t nelts, size_t elts_size);
244 FIRM_API void ir_del_arr_f(void *elts);
245 FIRM_API void *ir_new_arr_d(struct obstack *obstack, size_t nelts, size_t elts_size);
246 FIRM_API void *ir_arr_resize(void *elts, size_t nelts, size_t elts_size);
247 FIRM_API void *ir_arr_setlen(void *elts, size_t nelts, size_t elts_size);
248 FIRM_API void ir_verify_arr(const void *elts);
250 #define ARR_ELTS_OFFS offsetof(ir_arr_descr, elts)
251 #define ARR_DESCR(elts) ((ir_arr_descr *)(void *)((char *)(elts) - ARR_ELTS_OFFS))
253 /** Set a length smaller than the current length of the array. Do not
254 * resize. len must be <= ARR_LEN(arr). */
255 static inline void ARR_SHRINKLEN(void *arr, size_t new_len)
258 assert(ARR_DESCR(arr)->nelts >= new_len);
259 ARR_DESCR(arr)->nelts = new_len;