3 * File name: ir/adt/array.c
4 * Purpose: Array --- dynamic & flexible arrays.
5 * Author: Markus Armbruster
7 * Created: 1999 by getting from fiasco
9 * Copyright: (c) 1995, 1996 Markus Armbruster
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
24 /* Undefine the macros to get the functions instead, cf tmalloc.c. */
31 # define MAX(a,b) ((a) > (b) ? (a) : (b))
34 # define MIN(a,b) ((a) > (b) ? (b) : (a))
38 * An empty dynamic array
40 _arr_descr arr_mt_descr
47 * Creates a dynamic array on a obstack.
49 * @param obstack An struct obstack * were the data will be allocated
50 * @param nelts The number of elements
51 * @param elts_size The size of the array elements.
53 * @return A pointer to the dynamic array (can be used as a pointer to the
54 * first element of this array).
56 * @remark Helper function, use NEW_ARR_D() instead.
59 _new_arr_d (struct obstack *obstack, int nelts, size_t elts_size)
63 assert (obstack && (nelts >= 0));
65 new = obstack_alloc (obstack, _ARR_ELTS_OFFS+elts_size);
66 _ARR_SET_DBGINF (new, ARR_D_MAGIC, elts_size/nelts);
67 new->u.obstack = obstack;
73 * Creates a flexible array.
75 * @param nelts The number of elements
76 * @param elts_size The size of the array elements.
78 * @return A pointer to the flexible array (can be used as a pointer to the
79 * first element of this array).
81 * @remark Helper function, use NEW_ARR_F() instead.
84 _new_arr_f (int nelts, size_t elts_size)
89 new = xmalloc (_ARR_ELTS_OFFS+elts_size);
90 _ARR_SET_DBGINF (new, ARR_F_MAGIC, nelts ? elts_size/nelts : 0);
91 new->u.allocated = new->nelts = nelts;
96 * Delete a flexible array.
98 * @param elts The flexible array (pointer to the first element).
100 * @remark Helper function, use DEL_ARR_F() instead.
103 _del_arr_f (void *elts)
105 _arr_descr *dp = _ARR_DESCR (elts);
108 assert (dp->magic == ARR_F_MAGIC);
111 dp->magic = 0xdeadbeef;
117 * Resize a flexible array, always reallocate data.
119 * @param elts The flexible array (pointer to the first element).
120 * @param nelts The new number of elements.
121 * @param elts_size The size of the array elements.
123 * @return A resized flexible array, possibly other address than
126 * @remark Helper function, use ARR_SETLEN() instead.
129 _arr_setlen (void *elts, int nelts, size_t elts_size)
131 _arr_descr *dp = _ARR_DESCR (elts);
133 assert ((dp->magic == ARR_F_MAGIC) && (nelts >= 0));
135 assert (!dp->eltsize || !nelts || (dp->eltsize == elts_size/nelts));
137 dp = xrealloc (dp, _ARR_ELTS_OFFS+elts_size);
138 dp->u.allocated = dp->nelts = nelts;
144 * Resize a flexible array, allocate more data if needed but do NOT
147 * @param elts The flexible array (pointer to the first element).
148 * @param nelts The new number of elements.
149 * @param eltsize The size of the array elements.
151 * @return A resized flexible array, possibly other address than
154 * @remark Helper function, use ARR_RESIZE() instead.
157 _arr_resize (void *elts, int nelts, size_t eltsize)
159 _arr_descr *dp = _ARR_DESCR (elts);
162 assert ((dp->magic == ARR_F_MAGIC) && (nelts >= 0));
164 assert (dp->eltsize ? dp->eltsize == eltsize : (dp->eltsize = eltsize, 1));
166 /* @@@ lots of resizes for small nelts */
167 n = MAX (1, dp->u.allocated);
168 while (nelts > n) n <<= 1;
169 while (3*nelts < n) n >>= 1;
172 if (n != dp->u.allocated) {
173 dp = xrealloc (dp, _ARR_ELTS_OFFS+eltsize*n);
175 #if defined(DEBUG) && defined(HAVE_GNU_MALLOC)
187 * This function returns the length of a flexible array.
188 * Do NOT use is in code, use ARR_LEN() macro!
189 * This function is intended to be called from a debugger.
191 int array_len(void *arr) {
196 * This function returns the array descriptor of a flexible array.
197 * Do NOT use is in code!.
198 * This function is intended to be called from a debugger.
200 _arr_descr *array_descr(void *arr) {
203 return _ARR_DESCR(arr);
205 #endif /* DEBUG_libfirm */