removed wrong INLINE
[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 /**
36  * An empty dynamic array
37  */
38 _arr_descr arr_mt_descr
39 #ifndef NDEBUG
40   = { ARR_D_MAGIC }
41 #endif
42 ;
43
44 /**
45  * Creates a dynamic array on a obstack.
46  *
47  * @param obstack    An struct obstack * were the data will be allocated
48  * @param nelts      The number of elements
49  * @param elts_size  The size of the array elements.
50  *
51  * @return A pointer to the dynamic array (can be used as a pointer to the
52  *         first element of this array).
53  *
54  * @remark Helper function, use NEW_ARR_D() instead.
55  */
56 void *
57 _new_arr_d (struct obstack *obstack, int nelts, size_t elts_size)
58 {
59   _arr_descr *new;
60
61   assert (obstack && (nelts >= 0));
62
63   new = obstack_alloc (obstack, _ARR_ELTS_OFFS+elts_size);
64   _ARR_SET_DBGINF (new, ARR_D_MAGIC, elts_size/nelts);
65   new->u.obstack = obstack;
66   new->nelts = nelts;
67   return new->v.elts;
68 }
69
70 /**
71  * Creates a flexible array.
72  *
73  * @param nelts      The number of elements
74  * @param elts_size  The size of the array elements.
75  *
76  * @return A pointer to the flexible array (can be used as a pointer to the
77  *         first element of this array).
78  *
79  * @remark Helper function, use NEW_ARR_F() instead.
80  */
81 void *
82 _new_arr_f (int nelts, size_t elts_size)
83 {
84   _arr_descr *new;
85
86   assert (nelts >= 0);
87   new = xmalloc (_ARR_ELTS_OFFS+elts_size);
88   _ARR_SET_DBGINF (new, ARR_F_MAGIC, nelts ? elts_size/nelts : 0);
89   new->u.allocated = new->nelts = nelts;
90   return new->v.elts;
91 }
92
93 /**
94  * Delete a flexible array.
95  *
96  * @param elts    The flexible array (pointer to the first element).
97  *
98  * @remark Helper function, use DEL_ARR_F() instead.
99  */
100 void
101 _del_arr_f (void *elts)
102 {
103   _arr_descr *dp = _ARR_DESCR (elts);
104
105   ARR_VRFY (elts);
106   assert (dp->magic == ARR_F_MAGIC);
107
108 #ifndef NDEBUG
109   dp->magic = 0xdeadbeef;
110 #endif
111   free (dp);
112 }
113
114 /**
115  * Resize a flexible array, always reallocate data.
116  *
117  * @param elts       The flexible array (pointer to the first element).
118  * @param nelts      The new number of elements.
119  * @param elts_size  The size of the array elements.
120  *
121  * @return A resized flexible array, possibly other address than
122  *         elts.
123  *
124  * @remark Helper function, use ARR_SETLEN() instead.
125  */
126 void *
127 _arr_setlen (void *elts, int nelts, size_t elts_size)
128 {
129   _arr_descr *dp = _ARR_DESCR (elts);
130
131   assert ((dp->magic == ARR_F_MAGIC) && (nelts >= 0));
132   ARR_VRFY (elts);
133   assert (!dp->eltsize || !nelts || (dp->eltsize == elts_size/nelts));
134
135   dp = xrealloc (dp, _ARR_ELTS_OFFS+elts_size);
136   dp->u.allocated = dp->nelts = nelts;
137
138   return dp->v.elts;
139 }
140
141 /**
142  * Resize a flexible array, allocate more data if needed but do NOT
143  * reduce.
144  *
145  * @param elts       The flexible array (pointer to the first element).
146  * @param nelts      The new number of elements.
147  * @param elts_size  The size of the array elements.
148  *
149  * @return A resized flexible array, possibly other address than
150  *         elts.
151  *
152  * @remark Helper function, use ARR_RESIZE() instead.
153  */
154 void *
155 _arr_resize (void *elts, int nelts, size_t eltsize)
156 {
157   _arr_descr *dp = _ARR_DESCR (elts);
158   int n;
159
160   assert ((dp->magic == ARR_F_MAGIC) && (nelts >= 0));
161   ARR_VRFY (elts);
162   assert (dp->eltsize ? dp->eltsize == eltsize : (dp->eltsize = eltsize, 1));
163
164   /* @@@ lots of resizes for small nelts */
165   n = MAX (1, dp->u.allocated);
166   while (nelts > n) n <<= 1;
167   while (3*nelts < n) n >>= 1;
168   assert (n >= nelts);
169
170   if (n != dp->u.allocated) {
171     dp = xrealloc (dp, _ARR_ELTS_OFFS+eltsize*n);
172     dp->u.allocated = n;
173 #if defined(DEBUG) && defined(HAVE_GNU_MALLOC)
174   } else {
175     tmalloc_tag = NULL;
176 #endif
177   }
178   dp->nelts = nelts;
179
180   return dp->v.elts;
181 }