implemented Confirm node
[libfirm] / ir / adt / array.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/adt/array.c
4  * Purpose:     Array --- dynamic & flexible arrays.
5  * Author:      Markus Armbruster
6  * Modified by:
7  * Created:     1999 by getting from fiasco
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 1995, 1996 Markus Armbruster
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 #ifdef HAVE_CONFIG_H
14 # include <config.h>
15 #endif
16
17 #include <stdlib.h>
18
19 #include "array.h"
20 #include "xmalloc.h"
21
22 /* Undefine the macros to get the functions instead, cf tmalloc.c.  */
23 #undef xmalloc
24 #undef xrealloc
25 #undef xstrdup
26 #undef xfree
27
28 #ifndef MAX
29 # define MAX(a,b) ((a) > (b) ? (a) : (b))
30 #endif
31 #ifndef MIN
32 # define MIN(a,b) ((a) > (b) ? (b) : (a))
33 #endif
34
35 _arr_descr arr_mt_descr
36 #ifndef NDEBUG
37   = { ARR_D_MAGIC }
38 #endif
39 ;
40
41 void *
42 _new_arr_d (struct obstack *obstack, int nelts, size_t elts_size)
43 {
44   _arr_descr *new;
45
46   assert (obstack && (nelts >= 0));
47
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;
51   new->nelts = nelts;
52   return new->v.elts;
53 }
54
55
56 void *
57 _new_arr_f (int nelts, size_t elts_size)
58 {
59   _arr_descr *new;
60
61   assert (nelts >= 0);
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;
65   return new->v.elts;
66 }
67
68
69 void
70 _del_arr_f (void *elts)
71 {
72   _arr_descr *dp = _ARR_DESCR (elts);
73
74   ARR_VRFY (elts);
75   assert (dp->cookie == ARR_F_MAGIC);
76
77 #ifndef NDEBUG
78   dp->cookie = 0xdeadbeef;
79 #endif
80   free (dp);
81 }
82
83
84 void *
85 _arr_setlen (void *elts, int nelts, size_t elts_size)
86 {
87   _arr_descr *dp = _ARR_DESCR (elts);
88
89   assert ((dp->cookie == ARR_F_MAGIC) && (nelts >= 0));
90   ARR_VRFY (elts);
91   assert (!dp->eltsize || !nelts || (dp->eltsize == elts_size/nelts));
92
93   dp = xrealloc (dp, _ARR_ELTS_OFFS+elts_size);
94   dp->u.allocated = dp->nelts = nelts;
95
96   return dp->v.elts;
97 }
98
99 \
100 void *
101 _arr_resize (void *elts, int nelts, size_t eltsize)
102 {
103   _arr_descr *dp = _ARR_DESCR (elts);
104   int n;
105
106   assert ((dp->cookie == ARR_F_MAGIC) && (nelts >= 0));
107   ARR_VRFY (elts);
108   assert (dp->eltsize ? dp->eltsize == eltsize : (dp->eltsize = eltsize, 1));
109
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;
114   assert (n >= nelts);
115
116   if (n != dp->u.allocated) {
117     dp = xrealloc (dp, _ARR_ELTS_OFFS+eltsize*n);
118     dp->u.allocated = n;
119 #if defined(DEBUG) && defined(HAVE_GNU_MALLOC)
120   } else {
121     tmalloc_tag = NULL;
122 #endif
123   }
124   dp->nelts = nelts;
125
126   return dp->v.elts;
127 }