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))
35 _arr_descr arr_mt_descr
42 _new_arr_d (struct obstack *obstack, int nelts, size_t elts_size)
46 assert (obstack && (nelts >= 0));
48 new = obstack_alloc (obstack, _ARR_ELTS_OFFS+elts_size);
49 _ARR_SET_DBGINF (new, ARR_D_MAGIC, elts_size/nelts);
50 new->u.obstack = obstack;
57 _new_arr_f (int nelts, size_t elts_size)
62 new = xmalloc (_ARR_ELTS_OFFS+elts_size);
63 _ARR_SET_DBGINF (new, ARR_F_MAGIC, nelts ? elts_size/nelts : 0);
64 new->u.allocated = new->nelts = nelts;
70 _del_arr_f (void *elts)
72 _arr_descr *dp = _ARR_DESCR (elts);
75 assert (dp->cookie == ARR_F_MAGIC);
78 dp->cookie = 0xdeadbeef;
85 _arr_setlen (void *elts, int nelts, size_t elts_size)
87 _arr_descr *dp = _ARR_DESCR (elts);
89 assert ((dp->cookie == ARR_F_MAGIC) && (nelts >= 0));
91 assert (!dp->eltsize || !nelts || (dp->eltsize == elts_size/nelts));
93 dp = xrealloc (dp, _ARR_ELTS_OFFS+elts_size);
94 dp->u.allocated = dp->nelts = nelts;
101 _arr_resize (void *elts, int nelts, size_t eltsize)
103 _arr_descr *dp = _ARR_DESCR (elts);
106 assert ((dp->cookie == ARR_F_MAGIC) && (nelts >= 0));
108 assert (dp->eltsize ? dp->eltsize == eltsize : (dp->eltsize = eltsize, 1));
110 /* @@@ lots of resizes for small nelts */
111 n = MAX (1, dp->u.allocated);
112 while (nelts > n) n <<= 1;
113 while (3*nelts < n) n >>= 1;
116 if (n != dp->u.allocated) {
117 dp = xrealloc (dp, _ARR_ELTS_OFFS+eltsize*n);
119 #if defined(DEBUG) && defined(HAVE_GNU_MALLOC)