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.
20 /* Undefine the macros to get the functions instead, cf tmalloc.c. */
27 _arr_descr arr_mt_descr
34 _new_arr_d (struct obstack *obstack, int nelts, size_t elts_size)
38 assert (obstack && (nelts >= 0));
40 new = obstack_alloc (obstack, _ARR_ELTS_OFFS+elts_size);
41 _ARR_SET_DBGINF (new, ARR_D_MAGIC, elts_size/nelts);
42 new->u.obstack = obstack;
49 _new_arr_f (int nelts, size_t elts_size)
54 new = xmalloc (_ARR_ELTS_OFFS+elts_size);
55 _ARR_SET_DBGINF (new, ARR_F_MAGIC, nelts ? elts_size/nelts : 0);
56 new->u.allocated = new->nelts = nelts;
62 _del_arr_f (void *elts)
64 _arr_descr *dp = _ARR_DESCR (elts);
67 assert (dp->cookie == ARR_F_MAGIC);
70 dp->cookie = 0xdeadbeef;
77 _arr_setlen (void *elts, int nelts, size_t elts_size)
79 _arr_descr *dp = _ARR_DESCR (elts);
81 assert ((dp->cookie == ARR_F_MAGIC) && (nelts >= 0));
83 assert (!dp->eltsize || !nelts || (dp->eltsize == elts_size/nelts));
85 dp = xrealloc (dp, _ARR_ELTS_OFFS+elts_size);
86 dp->u.allocated = dp->nelts = nelts;
93 _arr_resize (void *elts, int nelts, size_t eltsize)
95 _arr_descr *dp = _ARR_DESCR (elts);
98 assert ((dp->cookie == ARR_F_MAGIC) && (nelts >= 0));
100 assert (dp->eltsize ? dp->eltsize == eltsize : (dp->eltsize = eltsize, 1));
102 /* @@@ lots of resizes for small nelts */
103 n = MAX (1, dp->u.allocated);
104 while (nelts > n) n <<= 1;
105 while (3*nelts < n) n >>= 1;
108 if (n != dp->u.allocated) {
109 dp = xrealloc (dp, _ARR_ELTS_OFFS+eltsize*n);
111 #if defined(DEBUG) && defined(HAVE_GNU_MALLOC)