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 Array --- dynamic & flexible arrays.
23 * @author Markus Armbruster
33 /* Undefine the macros to get the functions instead, cf tmalloc.c. */
40 # define MAX(a,b) ((a) > (b) ? (a) : (b))
43 # define MIN(a,b) ((a) > (b) ? (b) : (a))
47 * An empty dynamic array descriptor.
49 ir_arr_descr arr_mt_descr = { ARR_D_MAGIC, 0, 0, { { 0 } } };
51 void ir_verify_arr(const void *arr)
54 ir_arr_descr *descr = ARR_DESCR(arr);
55 assert(descr->magic == ARR_D_MAGIC || descr->magic == ARR_A_MAGIC
56 || descr->magic == ARR_F_MAGIC);
57 assert(descr->magic != ARR_F_MAGIC || descr->allocated >= descr->nelts);
64 * Creates a dynamic array on a obstack.
66 * @param obstack An struct obstack * were the data will be allocated
67 * @param nelts The number of elements
68 * @param elts_size The size of the array elements.
70 * @return A pointer to the dynamic array (can be used as a pointer to the
71 * first element of this array).
73 * @remark Helper function, use NEW_ARR_D() instead.
75 void *ir_new_arr_d(struct obstack *obstack, size_t nelts, size_t elts_size)
81 dp = (ir_arr_descr*)obstack_alloc(obstack, ARR_ELTS_OFFS + elts_size);
82 ARR_SET_DBGINF(dp, ARR_D_MAGIC);
83 dp->allocated = dp->nelts = nelts;
88 * Creates a flexible array.
90 * @param nelts The number of elements
91 * @param elts_size The size of the array elements.
93 * @return A pointer to the flexible array (can be used as a pointer to the
94 * first element of this array).
96 * @remark Helper function, use NEW_ARR_F() instead.
98 void *ir_new_arr_f(size_t nelts, size_t elts_size)
102 newa = (ir_arr_descr*)xmalloc(ARR_ELTS_OFFS+elts_size);
103 ARR_SET_DBGINF(newa, ARR_F_MAGIC);
104 newa->allocated = newa->nelts = nelts;
109 * Delete a flexible array.
111 * @param elts The flexible array (pointer to the first element).
113 * @remark Helper function, use DEL_ARR_F() instead.
115 void ir_del_arr_f(void *elts)
117 ir_arr_descr *dp = ARR_DESCR (elts);
120 assert(dp->magic == ARR_F_MAGIC);
123 dp->magic = 0xdeadbeef;
129 * Resize a flexible array, always reallocate data.
131 * @param elts The flexible array (pointer to the first element).
132 * @param nelts The new number of elements.
133 * @param elts_size The size of the array elements.
135 * @return A resized flexible array, possibly other address than
138 * @remark Helper function, use ARR_SETLEN() instead.
140 void *ir_arr_setlen (void *elts, size_t nelts, size_t elts_size)
142 ir_arr_descr *dp = ARR_DESCR (elts);
144 assert(dp->magic == ARR_F_MAGIC);
147 dp = (ir_arr_descr*) xrealloc(dp, ARR_ELTS_OFFS+elts_size);
148 dp->allocated = dp->nelts = nelts;
154 * Resize a flexible array, allocate more data if needed but do NOT
157 * @param elts The flexible array (pointer to the first element).
158 * @param nelts The new number of elements.
159 * @param eltsize The size of the array elements.
161 * @return A resized flexible array, possibly other address than
164 * @remark Helper function, use ARR_RESIZE() instead.
166 void *ir_arr_resize(void *elts, size_t nelts, size_t eltsize)
168 ir_arr_descr *dp = ARR_DESCR(elts);
171 assert(dp->magic == ARR_F_MAGIC);
174 /* @@@ lots of resizes for small nelts */
175 n = MAX(1, dp->allocated);
176 while (nelts > n) n <<= 1;
177 while (3*nelts < n) n >>= 1;
180 if (n != dp->allocated) {
181 dp = (ir_arr_descr*) xrealloc(dp, ARR_ELTS_OFFS+eltsize*n);
190 /* forward declarations to avoid warnings */
191 size_t array_len(const void *arr);
192 ir_arr_descr *array_descr(const void *arr);
195 * This function returns the length of a flexible array.
196 * Do NOT use is in code, use ARR_LEN() macro!
197 * This function is intended to be called from a debugger.
199 size_t array_len(const void *arr)
205 * This function returns the array descriptor of a flexible array.
206 * Do NOT use is in code!.
207 * This function is intended to be called from a debugger.
209 ir_arr_descr *array_descr(const void *arr)
213 return ARR_DESCR(arr);
215 #endif /* DEBUG_libfirm */