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
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 ((type*) 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.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 ((type*)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) = (type*) 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) = (type*) 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 static inline void ARR_SHRINKLEN(void *arr, size_t new_len);
179 * Resize a flexible array by growing it by delta elements.
181 * @param type The element type of the array.
182 * @param arr The array, which must be an lvalue.
183 * @param delta The delta number of elements.
185 * @remark This macro may change arr, so update all references!
187 #define ARR_EXTEND(type, arr, delta) \
188 ARR_RESIZE(type, (arr), ARR_LEN((arr)) + (delta))
191 * Resize a flexible array to hold n elements only if it is currently shorter
194 * @param type The element type of the array.
195 * @param arr The array, which must be an lvalue.
196 * @param n The new size of the array.
198 * @remark This macro may change arr, so update all references!
200 #define ARR_EXTO(type, arr, n) \
201 ((n) >= ARR_LEN((arr)) ? ARR_RESIZE(type, (arr), (n)+1) : (arr))
204 * Append one element to a flexible array.
206 * @param type The element type of the array.
207 * @param arr The array, which must be an lvalue.
208 * @param elt The new element, must be of type (type).
210 #define ARR_APP1(type, arr, elt) \
211 (ARR_EXTEND(type, (arr), 1), (arr)[ARR_LEN((arr))-1] = (elt))
214 # define ARR_VRFY(arr) ((void)0)
215 # define ARR_IDX_VRFY(arr, idx) ((void)0)
217 # define ARR_VRFY(arr) ir_verify_arr(arr)
218 # define ARR_IDX_VRFY(arr, idx) \
219 assert((0 <= (idx)) && ((idx) < ARR_LEN((arr))))
222 /** A type that has most constrained alignment. */
231 * The array descriptor header type.
234 int magic; /**< array magic. */
235 size_t eltsize; /**< size of array elements. */
237 struct obstack *obstack; /**< for obstack array: the obstack. */
238 size_t allocated; /**< number of allocated elements. */
240 size_t nelts; /**< current length of the array. */
241 aligned_type elts[1]; /**< start of the array data. */
244 extern ir_arr_descr arr_mt_descr;
246 FIRM_API void *ir_new_arr_f(size_t nelts, size_t elts_size);
247 FIRM_API void ir_del_arr_f(void *elts);
248 FIRM_API void *ir_new_arr_d(struct obstack *obstack, size_t nelts, size_t elts_size);
249 FIRM_API void *ir_arr_resize(void *elts, size_t nelts, size_t elts_size);
250 FIRM_API void *ir_arr_setlen(void *elts, size_t nelts, size_t elts_size);
251 FIRM_API void ir_verify_arr(const void *elts);
253 #define ARR_ELTS_OFFS offsetof(ir_arr_descr, elts)
254 #define ARR_DESCR(elts) ((ir_arr_descr *)(void *)((char *)(elts) - ARR_ELTS_OFFS))
256 static inline void ARR_SHRINKLEN(void *arr, size_t new_len)
259 assert(ARR_DESCR(arr)->nelts >= new_len);
260 ARR_DESCR(arr)->nelts = new_len;