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 * Creates a new dynamic array with the same number of elements as a
116 * @param type The element type of the new array.
117 * @param obstack An struct obstack * were the data will be allocated
118 * @param arr An array from which the number of elements will be taken
120 * This macro creates a dynamic array of a given type at runtime.
121 * The size of the array cannot be changed later.
123 * @return A pointer to the dynamic array (can be used as a pointer to the
124 * first element of this array).
126 #define CLONE_ARR_D(type, obstack, arr) \
127 NEW_ARR_D(type, (obstack), ARR_LEN((arr)))
130 * Duplicates an array and returns the new dynamic one.
132 * @param type The element type of the new array.
133 * @param obstack An struct obstack * were the data will be allocated
134 * @param arr An array from which the elements will be duplicated
136 * This macro creates a dynamic array of a given type at runtime.
137 * The size of the array cannot be changed later.
139 * @return A pointer to the dynamic array (can be used as a pointer to the
140 * first element of this array).
142 #define DUP_ARR_D(type, obstack, arr) \
143 ((type*)memcpy(CLONE_ARR_D(type, (obstack), (arr)), (arr), sizeof(type) * ARR_LEN ((arr))))
146 * Returns the length of an array
148 * @param arr a flexible, dynamic, automatic or static array.
150 #define ARR_LEN(arr) (ARR_VRFY((arr)), ARR_DESCR((arr))->nelts)
153 * Resize a flexible array, allocate more data if needed but do NOT
156 * @param type The element type of the array.
157 * @param arr The array, which must be an lvalue.
158 * @param n The new size of the array.
160 * @remark This macro may change arr, so update all references!
162 #define ARR_RESIZE(type, arr, n) \
163 ((arr) = (type*) ir_arr_resize((void *)(arr), (n), sizeof(type)))
166 * Resize a flexible array, always reallocate data.
168 * @param type The element type of the array.
169 * @param arr The array, which must be an lvalue.
170 * @param n The new size of the array.
172 * @remark This macro may change arr, so update all references!
174 #define ARR_SETLEN(type, arr, n) \
175 ((arr) = (type*) ir_arr_setlen((void *)(arr), (n), sizeof(type) * (n)))
178 * Resize a flexible array by growing it by delta elements.
180 * @param type The element type of the array.
181 * @param arr The array, which must be an lvalue.
182 * @param delta The delta number of elements.
184 * @remark This macro may change arr, so update all references!
186 #define ARR_EXTEND(type, arr, delta) \
187 ARR_RESIZE(type, (arr), ARR_LEN((arr)) + (delta))
190 * Resize a flexible array to hold n elements only if it is currently shorter
193 * @param type The element type of the array.
194 * @param arr The array, which must be an lvalue.
195 * @param n The new size of the array.
197 * @remark This macro may change arr, so update all references!
199 #define ARR_EXTO(type, arr, n) \
201 if ((n) >= ARR_LEN(arr)) { ARR_RESIZE(type, arr, (n)+1); } \
205 * Append one element to a flexible array.
207 * @param type The element type of the array.
208 * @param arr The array, which must be an lvalue.
209 * @param elt The new element, must be of type (type).
211 #define ARR_APP1(type, arr, elt) \
212 (ARR_EXTEND(type, (arr), 1), (arr)[ARR_LEN((arr))-1] = (elt))
215 # define ARR_VRFY(arr) ((void)0)
216 # define ARR_IDX_VRFY(arr, idx) ((void)0)
218 /** Check array for consistency */
219 # define ARR_VRFY(arr) ir_verify_arr(arr)
220 /** Check if index is within array bounds */
221 # define ARR_IDX_VRFY(arr, idx) \
222 assert((0 <= (idx)) && ((idx) < ARR_LEN((arr))))
227 /** A type that has most constrained alignment. */
235 * The array descriptor header type.
238 int magic; /**< array magic. */
239 size_t eltsize; /**< size of array elements. */
241 struct obstack *obstack; /**< for obstack array: the obstack. */
242 size_t allocated; /**< number of allocated elements. */
244 size_t nelts; /**< current length of the array. */
245 aligned_type elts[1]; /**< start of the array data. */
248 extern ir_arr_descr arr_mt_descr;
250 FIRM_API void *ir_new_arr_f(size_t nelts, size_t elts_size);
251 FIRM_API void ir_del_arr_f(void *elts);
252 FIRM_API void *ir_new_arr_d(struct obstack *obstack, size_t nelts, size_t elts_size);
253 FIRM_API void *ir_arr_resize(void *elts, size_t nelts, size_t elts_size);
254 FIRM_API void *ir_arr_setlen(void *elts, size_t nelts, size_t elts_size);
255 FIRM_API void ir_verify_arr(const void *elts);
257 #define ARR_ELTS_OFFS offsetof(ir_arr_descr, elts)
258 #define ARR_DESCR(elts) ((ir_arr_descr *)(void *)((char *)(elts) - ARR_ELTS_OFFS))
260 /** Set a length smaller than the current length of the array. Do not
261 * resize. len must be <= ARR_LEN(arr). */
262 static inline void ARR_SHRINKLEN(void *arr, size_t new_len)
265 assert(ARR_DESCR(arr)->nelts >= new_len);
266 ARR_DESCR(arr)->nelts = new_len;