1 /* Array --- dynamic & flexible arrays.
2 Copyright (C) 1995, 1996 Markus Armbruster
3 All rights reserved. */
14 /* Undefine the macros to get the functions instead, cf tmalloc.c. */
21 _arr_descr arr_mt_descr
28 _new_arr_d (struct obstack *obstack, int nelts, size_t elts_size)
32 assert (obstack && (nelts >= 0));
34 new = obstack_alloc (obstack, _ARR_ELTS_OFFS+elts_size);
35 _ARR_SET_DBGINF (new, ARR_D_MAGIC, elts_size/nelts);
36 new->u.obstack = obstack;
43 _new_arr_f (int nelts, size_t elts_size)
48 new = xmalloc (_ARR_ELTS_OFFS+elts_size);
49 _ARR_SET_DBGINF (new, ARR_F_MAGIC, nelts ? elts_size/nelts : 0);
50 new->u.allocated = new->nelts = nelts;
56 _del_arr_f (void *elts)
58 _arr_descr *dp = _ARR_DESCR (elts);
61 assert (dp->cookie == ARR_F_MAGIC);
64 dp->cookie = 0xdeadbeef;
71 _arr_setlen (void *elts, int nelts, size_t elts_size)
73 _arr_descr *dp = _ARR_DESCR (elts);
75 assert ((dp->cookie == ARR_F_MAGIC) && (nelts >= 0));
77 assert (!dp->eltsize || !nelts || (dp->eltsize == elts_size/nelts));
79 dp = xrealloc (dp, _ARR_ELTS_OFFS+elts_size);
80 dp->u.allocated = dp->nelts = nelts;
87 _arr_resize (void *elts, int nelts, size_t eltsize)
89 _arr_descr *dp = _ARR_DESCR (elts);
92 assert ((dp->cookie == ARR_F_MAGIC) && (nelts >= 0));
94 assert (dp->eltsize ? dp->eltsize == eltsize : (dp->eltsize = eltsize, 1));
96 /* @@@ lots of resizes for small nelts */
97 n = MAX (1, dp->u.allocated);
98 while (nelts > n) n <<= 1;
99 while (3*nelts < n) n >>= 1;
102 if (n != dp->u.allocated) {
103 dp = xrealloc (dp, _ARR_ELTS_OFFS+eltsize*n);
105 #if defined(DEBUG) && defined(HAVE_GNU_MALLOC)