remove symconst_type_tag
[libfirm] / ir / adt / array.c
1 /*
2  * Copyright (C) 1995-2011 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief       Array --- dynamic & flexible arrays.
23  * @author      Markus Armbruster
24  */
25
26 #include "config.h"
27
28 #include <stdlib.h>
29
30 #include "array_t.h"
31 #include "xmalloc.h"
32
33 /* Undefine the macros to get the functions instead, cf tmalloc.c.  */
34 #undef xmalloc
35 #undef xrealloc
36 #undef xstrdup
37 #undef xfree
38
39 #ifndef MAX
40 # define MAX(a,b) ((a) > (b) ? (a) : (b))
41 #endif
42 #ifndef MIN
43 # define MIN(a,b) ((a) > (b) ? (b) : (a))
44 #endif
45
46 /**
47  * An empty dynamic array descriptor.
48  */
49 ir_arr_descr arr_mt_descr = { ARR_D_MAGIC, 0, { 0 }, 0,  { { 0 } } };
50
51 void ir_verify_arr(const void *arr)
52 {
53 #ifndef NDEBUG
54         ir_arr_descr *descr = ARR_DESCR(arr);
55         assert(descr->magic == ARR_D_MAGIC || descr->magic == ARR_A_MAGIC
56                          || descr->magic == ARR_F_MAGIC);
57         if (descr->magic == ARR_F_MAGIC) {
58                 assert(descr->u.allocated >= descr->nelts);
59         }
60 #else
61         (void) arr;
62 #endif
63 }
64
65 /**
66  * Creates a dynamic array on a obstack.
67  *
68  * @param obstack    An struct obstack * were the data will be allocated
69  * @param nelts      The number of elements
70  * @param elts_size  The size of the array elements.
71  *
72  * @return A pointer to the dynamic array (can be used as a pointer to the
73  *         first element of this array).
74  *
75  * @remark Helper function, use NEW_ARR_D() instead.
76  */
77 void *ir_new_arr_d(struct obstack *obstack, size_t nelts, size_t elts_size)
78 {
79         ir_arr_descr *dp;
80
81         assert(obstack);
82
83         dp = (ir_arr_descr*)obstack_alloc(obstack, ARR_ELTS_OFFS + elts_size);
84         ARR_SET_DBGINF(dp, ARR_D_MAGIC, elts_size/nelts);
85         dp->u.obstack = obstack;
86         dp->nelts = nelts;
87         return dp->elts;
88 }
89
90 /**
91  * Creates a flexible array.
92  *
93  * @param nelts      The number of elements
94  * @param elts_size  The size of the array elements.
95  *
96  * @return A pointer to the flexible array (can be used as a pointer to the
97  *         first element of this array).
98  *
99  * @remark Helper function, use NEW_ARR_F() instead.
100  */
101 void *ir_new_arr_f(size_t nelts, size_t elts_size)
102 {
103         ir_arr_descr *newa;
104
105         newa = (ir_arr_descr*)xmalloc(ARR_ELTS_OFFS+elts_size);
106         ARR_SET_DBGINF(newa, ARR_F_MAGIC, nelts ? elts_size/nelts : 0);
107         newa->u.allocated = newa->nelts = nelts;
108         return newa->elts;
109 }
110
111 /**
112  * Delete a flexible array.
113  *
114  * @param elts    The flexible array (pointer to the first element).
115  *
116  * @remark Helper function, use DEL_ARR_F() instead.
117  */
118 void ir_del_arr_f(void *elts)
119 {
120         ir_arr_descr *dp = ARR_DESCR (elts);
121
122         ARR_VRFY(elts);
123         assert(dp->magic == ARR_F_MAGIC);
124
125 #ifndef NDEBUG
126         dp->magic = 0xdeadbeef;
127 #endif
128         free(dp);
129 }
130
131 /**
132  * Resize a flexible array, always reallocate data.
133  *
134  * @param elts       The flexible array (pointer to the first element).
135  * @param nelts      The new number of elements.
136  * @param elts_size  The size of the array elements.
137  *
138  * @return A resized flexible array, possibly other address than
139  *         elts.
140  *
141  * @remark Helper function, use ARR_SETLEN() instead.
142  */
143 void *ir_arr_setlen (void *elts, size_t nelts, size_t elts_size)
144 {
145         ir_arr_descr *dp = ARR_DESCR (elts);
146
147         assert(dp->magic == ARR_F_MAGIC);
148         ARR_VRFY(elts);
149         assert(!dp->eltsize || !nelts || (dp->eltsize == elts_size/nelts));
150
151         dp = (ir_arr_descr*) xrealloc(dp, ARR_ELTS_OFFS+elts_size);
152         dp->u.allocated = dp->nelts = nelts;
153
154         return dp->elts;
155 }
156
157 /**
158  * Resize a flexible array, allocate more data if needed but do NOT
159  * reduce.
160  *
161  * @param elts     The flexible array (pointer to the first element).
162  * @param nelts    The new number of elements.
163  * @param eltsize  The size of the array elements.
164  *
165  * @return A resized flexible array, possibly other address than
166  *         elts.
167  *
168  * @remark Helper function, use ARR_RESIZE() instead.
169  */
170 void *ir_arr_resize(void *elts, size_t nelts, size_t eltsize)
171 {
172         ir_arr_descr *dp = ARR_DESCR(elts);
173         size_t n;
174
175         assert(dp->magic == ARR_F_MAGIC);
176         ARR_VRFY(elts);
177         assert(dp->eltsize ? dp->eltsize == eltsize : (dp->eltsize = eltsize, 1));
178
179         /* @@@ lots of resizes for small nelts */
180         n = MAX(1, dp->u.allocated);
181         while (nelts > n) n <<= 1;
182         while (3*nelts < n) n >>= 1;
183         assert(n >= nelts);
184
185         if (n != dp->u.allocated) {
186                 dp = (ir_arr_descr*) xrealloc(dp, ARR_ELTS_OFFS+eltsize*n);
187                 dp->u.allocated = n;
188         }
189         dp->nelts = nelts;
190
191         return dp->elts;
192 }
193
194 #ifdef DEBUG_libfirm
195 /* forward declarations to avoid warnings */
196 size_t array_len(const void *arr);
197 ir_arr_descr *array_descr(const void *arr);
198
199 /**
200  * This function returns the length of a flexible array.
201  * Do NOT use is in code, use ARR_LEN() macro!
202  * This function is intended to be called from a debugger.
203  */
204 size_t array_len(const void *arr)
205 {
206         return ARR_LEN(arr);
207 }
208
209 /**
210  * This function returns the array descriptor of a flexible array.
211  * Do NOT use is in code!.
212  * This function is intended to be called from a debugger.
213  */
214 ir_arr_descr *array_descr(const void *arr)
215 {
216         if (! arr)
217                 return NULL;
218         return ARR_DESCR(arr);
219 }
220 #endif /* DEBUG_libfirm */