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.
22 /* Undefine the macros to get the functions instead, cf tmalloc.c. */
29 # define MAX(a,b) ((a) > (b) ? (a) : (b))
32 # define MIN(a,b) ((a) > (b) ? (b) : (a))
36 * An empty dynamic array
38 _arr_descr arr_mt_descr
45 * Creates a dynamic array on a obstack.
47 * @param obstack An struct obstack * were the data will be allocated
48 * @param nelts The number of elements
49 * @param elts_size The size of the array elements.
51 * @return A pointer to the dynamic array (can be used as a pointer to the
52 * first element of this array).
54 * @remark Helper function, use NEW_ARR_D() instead.
57 _new_arr_d (struct obstack *obstack, int nelts, size_t elts_size)
61 assert (obstack && (nelts >= 0));
63 new = obstack_alloc (obstack, _ARR_ELTS_OFFS+elts_size);
64 _ARR_SET_DBGINF (new, ARR_D_MAGIC, elts_size/nelts);
65 new->u.obstack = obstack;
71 * Creates a flexible array.
73 * @param nelts The number of elements
74 * @param elts_size The size of the array elements.
76 * @return A pointer to the flexible array (can be used as a pointer to the
77 * first element of this array).
79 * @remark Helper function, use NEW_ARR_F() instead.
82 _new_arr_f (int nelts, size_t elts_size)
87 new = xmalloc (_ARR_ELTS_OFFS+elts_size);
88 _ARR_SET_DBGINF (new, ARR_F_MAGIC, nelts ? elts_size/nelts : 0);
89 new->u.allocated = new->nelts = nelts;
94 * Delete a flexible array.
96 * @param elts The flexible array (pointer to the first element).
98 * @remark Helper function, use DEL_ARR_F() instead.
101 _del_arr_f (void *elts)
103 _arr_descr *dp = _ARR_DESCR (elts);
106 assert (dp->cookie == ARR_F_MAGIC);
109 dp->cookie = 0xdeadbeef;
115 * Resize a flexible array, always reallocate data.
117 * @param elts The flexible array (pointer to the first element).
118 * @param nelts The new number of elements.
119 * @param elts_size The size of the array elements.
121 * @return A resized flexible array, possibly other address than
124 * @remark Helper function, use ARR_SETLEN() instead.
127 _arr_setlen (void *elts, int nelts, size_t elts_size)
129 _arr_descr *dp = _ARR_DESCR (elts);
131 assert ((dp->cookie == ARR_F_MAGIC) && (nelts >= 0));
133 assert (!dp->eltsize || !nelts || (dp->eltsize == elts_size/nelts));
135 dp = xrealloc (dp, _ARR_ELTS_OFFS+elts_size);
136 dp->u.allocated = dp->nelts = nelts;
142 * Resize a flexible array, allocate more data if needed but do NOT
145 * @param elts The flexible array (pointer to the first element).
146 * @param nelts The new number of elements.
147 * @param elts_size The size of the array elements.
149 * @return A resized flexible array, possibly other address than
152 * @remark Helper function, use ARR_RESIZE() instead.
155 _arr_resize (void *elts, int nelts, size_t eltsize)
157 _arr_descr *dp = _ARR_DESCR (elts);
160 assert ((dp->cookie == ARR_F_MAGIC) && (nelts >= 0));
162 assert (dp->eltsize ? dp->eltsize == eltsize : (dp->eltsize = eltsize, 1));
164 /* @@@ lots of resizes for small nelts */
165 n = MAX (1, dp->u.allocated);
166 while (nelts > n) n <<= 1;
167 while (3*nelts < n) n >>= 1;
170 if (n != dp->u.allocated) {
171 dp = xrealloc (dp, _ARR_ELTS_OFFS+eltsize*n);
173 #if defined(DEBUG) && defined(HAVE_GNU_MALLOC)