2 * Copyright (C) 1995-2008 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
34 /* Undefine the macros to get the functions instead, cf tmalloc.c. */
41 # define MAX(a,b) ((a) > (b) ? (a) : (b))
44 # define MIN(a,b) ((a) > (b) ? (b) : (a))
48 * An empty dynamic array descriptor.
50 ir_arr_descr arr_mt_descr = { ARR_D_MAGIC, 0, {0}, 0, {{{0}}} };
52 void ir_verify_arr(const void *arr)
55 ir_arr_descr *descr = ARR_DESCR(arr);
56 assert(descr->magic == ARR_D_MAGIC || descr->magic == ARR_A_MAGIC
57 || descr->magic == ARR_F_MAGIC);
58 if (descr->magic == ARR_F_MAGIC) {
59 assert(descr->u.allocated >= descr->nelts);
61 assert(descr->nelts >= 0);
68 * Creates a dynamic array on a obstack.
70 * @param obstack An struct obstack * were the data will be allocated
71 * @param nelts The number of elements
72 * @param elts_size The size of the array elements.
74 * @return A pointer to the dynamic array (can be used as a pointer to the
75 * first element of this array).
77 * @remark Helper function, use NEW_ARR_D() instead.
79 void *ir_new_arr_d(struct obstack *obstack, int nelts, size_t elts_size) {
82 assert(obstack && (nelts >= 0));
84 dp = obstack_alloc(obstack, ARR_ELTS_OFFS + elts_size);
85 ARR_SET_DBGINF(dp, ARR_D_MAGIC, elts_size/nelts);
86 dp->u.obstack = obstack;
92 * Creates a flexible array.
94 * @param nelts The number of elements
95 * @param elts_size The size of the array elements.
97 * @return A pointer to the flexible array (can be used as a pointer to the
98 * first element of this array).
100 * @remark Helper function, use NEW_ARR_F() instead.
102 void *ir_new_arr_f(int nelts, size_t elts_size) {
106 new = xmalloc (ARR_ELTS_OFFS+elts_size);
107 ARR_SET_DBGINF (new, ARR_F_MAGIC, nelts ? elts_size/nelts : 0);
108 new->u.allocated = new->nelts = nelts;
113 * Delete a flexible array.
115 * @param elts The flexible array (pointer to the first element).
117 * @remark Helper function, use DEL_ARR_F() instead.
119 void ir_del_arr_f(void *elts) {
120 ir_arr_descr *dp = ARR_DESCR (elts);
123 assert (dp->magic == ARR_F_MAGIC);
127 ir_arr_descr *wdp = (ir_arr_descr *)dp;
128 wdp->magic = 0xdeadbeef;
135 * Resize a flexible array, always reallocate data.
137 * @param elts The flexible array (pointer to the first element).
138 * @param nelts The new number of elements.
139 * @param elts_size The size of the array elements.
141 * @return A resized flexible array, possibly other address than
144 * @remark Helper function, use ARR_SETLEN() instead.
146 void *ir_arr_setlen (void *elts, int nelts, size_t elts_size) {
147 ir_arr_descr *dp = ARR_DESCR (elts);
149 assert ((dp->magic == ARR_F_MAGIC) && (nelts >= 0));
151 assert (!dp->eltsize || !nelts || (dp->eltsize == elts_size/nelts));
153 dp = xrealloc (dp, ARR_ELTS_OFFS+elts_size);
154 dp->u.allocated = dp->nelts = nelts;
160 * Resize a flexible array, allocate more data if needed but do NOT
163 * @param elts The flexible array (pointer to the first element).
164 * @param nelts The new number of elements.
165 * @param eltsize The size of the array elements.
167 * @return A resized flexible array, possibly other address than
170 * @remark Helper function, use ARR_RESIZE() instead.
172 void *ir_arr_resize(void *elts, int nelts, size_t eltsize) {
173 ir_arr_descr *dp = ARR_DESCR(elts);
176 assert((dp->magic == ARR_F_MAGIC) && (nelts >= 0));
178 assert(dp->eltsize ? dp->eltsize == eltsize : (dp->eltsize = eltsize, 1));
180 /* @@@ lots of resizes for small nelts */
181 n = MAX(1, dp->u.allocated);
182 while (nelts > n) n <<= 1;
183 while (3*nelts < n) n >>= 1;
186 if (n != dp->u.allocated) {
187 dp = xrealloc(dp, ARR_ELTS_OFFS+eltsize*n);
189 #if defined(DEBUG) && defined(HAVE_GNU_MALLOC)
201 * This function returns the length of a flexible array.
202 * Do NOT use is in code, use ARR_LEN() macro!
203 * This function is intended to be called from a debugger.
205 int array_len(const void *arr) {
210 * This function returns the array descriptor of a flexible array.
211 * Do NOT use is in code!.
212 * This function is intended to be called from a debugger.
214 ir_arr_descr *array_descr(const void *arr) {
217 return ARR_DESCR(arr);
219 #endif /* DEBUG_libfirm */