1 /* Array --- dynamic & flexible arrays.
2 Copyright (C) 1995, 1996 Markus Armbruster
3 All rights reserved. */
12 /* Undefine the macros to get the functions instead, cf tmalloc.c. */
19 _arr_descr arr_mt_descr
26 _new_arr_d (struct obstack *obstack, int nelts, size_t elts_size)
30 assert (obstack && (nelts >= 0));
32 new = obstack_alloc (obstack, _ARR_ELTS_OFFS+elts_size);
33 _ARR_SET_DBGINF (new, ARR_D_MAGIC, elts_size/nelts);
34 new->u.obstack = obstack;
41 _new_arr_f (int nelts, size_t elts_size)
46 new = xmalloc (_ARR_ELTS_OFFS+elts_size);
47 _ARR_SET_DBGINF (new, ARR_F_MAGIC, nelts ? elts_size/nelts : 0);
48 new->u.allocated = new->nelts = nelts;
54 _del_arr_f (void *elts)
56 _arr_descr *dp = _ARR_DESCR (elts);
59 assert (dp->cookie == ARR_F_MAGIC);
62 dp->cookie = 0xdeadbeef;
69 _arr_setlen (void *elts, int nelts, size_t elts_size)
71 _arr_descr *dp = _ARR_DESCR (elts);
73 assert ((dp->cookie == ARR_F_MAGIC) && (nelts >= 0));
75 assert (!dp->eltsize || !nelts || (dp->eltsize == elts_size/nelts));
77 dp = xrealloc (dp, _ARR_ELTS_OFFS+elts_size);
78 dp->u.allocated = dp->nelts = nelts;
85 _arr_resize (void *elts, int nelts, size_t eltsize)
87 _arr_descr *dp = _ARR_DESCR (elts);
90 assert ((dp->cookie == ARR_F_MAGIC) && (nelts >= 0));
92 assert (dp->eltsize ? dp->eltsize == eltsize : (dp->eltsize = eltsize, 1));
94 /* @@@ lots of resizes for small nelts */
95 n = MAX (1, dp->u.allocated);
96 while (nelts > n) n <<= 1;
97 while (3*nelts < n) n >>= 1;
100 if (n != dp->u.allocated) {
101 dp = xrealloc (dp, _ARR_ELTS_OFFS+eltsize*n);
103 #if defined(DEBUG) && defined(HAVE_GNU_MALLOC)