2 * Copyright (C) 1995-2007 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
38 /* Undefine the macros to get the functions instead, cf tmalloc.c. */
45 # define MAX(a,b) ((a) > (b) ? (a) : (b))
48 # define MIN(a,b) ((a) > (b) ? (b) : (a))
52 * An empty dynamic array
54 _arr_descr arr_mt_descr;
57 * Creates a dynamic array on a obstack.
59 * @param obstack An struct obstack * were the data will be allocated
60 * @param nelts The number of elements
61 * @param elts_size The size of the array elements.
63 * @return A pointer to the dynamic array (can be used as a pointer to the
64 * first element of this array).
66 * @remark Helper function, use NEW_ARR_D() instead.
69 _new_arr_d (struct obstack *obstack, int nelts, size_t elts_size)
73 assert (obstack && (nelts >= 0));
75 new = obstack_alloc (obstack, _ARR_ELTS_OFFS+elts_size);
76 _ARR_SET_DBGINF (new, ARR_D_MAGIC, elts_size/nelts);
77 new->u.obstack = obstack;
83 * Creates a flexible array.
85 * @param nelts The number of elements
86 * @param elts_size The size of the array elements.
88 * @return A pointer to the flexible array (can be used as a pointer to the
89 * first element of this array).
91 * @remark Helper function, use NEW_ARR_F() instead.
94 _new_arr_f (int nelts, size_t elts_size)
99 new = xmalloc (_ARR_ELTS_OFFS+elts_size);
100 _ARR_SET_DBGINF (new, ARR_F_MAGIC, nelts ? elts_size/nelts : 0);
101 new->u.allocated = new->nelts = nelts;
106 * Delete a flexible array.
108 * @param elts The flexible array (pointer to the first element).
110 * @remark Helper function, use DEL_ARR_F() instead.
113 _del_arr_f (void *elts)
115 _arr_descr *dp = _ARR_DESCR (elts);
118 assert (dp->magic == ARR_F_MAGIC);
121 dp->magic = 0xdeadbeef;
127 * Resize a flexible array, always reallocate data.
129 * @param elts The flexible array (pointer to the first element).
130 * @param nelts The new number of elements.
131 * @param elts_size The size of the array elements.
133 * @return A resized flexible array, possibly other address than
136 * @remark Helper function, use ARR_SETLEN() instead.
139 _arr_setlen (void *elts, int nelts, size_t elts_size)
141 _arr_descr *dp = _ARR_DESCR (elts);
143 assert ((dp->magic == ARR_F_MAGIC) && (nelts >= 0));
145 assert (!dp->eltsize || !nelts || (dp->eltsize == elts_size/nelts));
147 dp = xrealloc (dp, _ARR_ELTS_OFFS+elts_size);
148 dp->u.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.
167 _arr_resize (void *elts, int nelts, size_t eltsize)
169 _arr_descr *dp = _ARR_DESCR (elts);
172 assert ((dp->magic == ARR_F_MAGIC) && (nelts >= 0));
174 assert (dp->eltsize ? dp->eltsize == eltsize : (dp->eltsize = eltsize, 1));
176 /* @@@ lots of resizes for small nelts */
177 n = MAX (1, dp->u.allocated);
178 while (nelts > n) n <<= 1;
179 while (3*nelts < n) n >>= 1;
182 if (n != dp->u.allocated) {
183 dp = xrealloc (dp, _ARR_ELTS_OFFS+eltsize*n);
185 #if defined(DEBUG) && defined(HAVE_GNU_MALLOC)
197 * This function returns the length of a flexible array.
198 * Do NOT use is in code, use ARR_LEN() macro!
199 * This function is intended to be called from a debugger.
201 int array_len(void *arr) {
206 * This function returns the array descriptor of a flexible array.
207 * Do NOT use is in code!.
208 * This function is intended to be called from a debugger.
210 _arr_descr *array_descr(void *arr) {
213 return _ARR_DESCR(arr);
215 #endif /* DEBUG_libfirm */