bestabs: move stabs but not backend specific text0: code into stabs
[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  * @version     $Id$
25  */
26
27 #include "config.h"
28
29 #include <stdlib.h>
30
31 #include "array_t.h"
32 #include "xmalloc.h"
33
34 /* Undefine the macros to get the functions instead, cf tmalloc.c.  */
35 #undef xmalloc
36 #undef xrealloc
37 #undef xstrdup
38 #undef xfree
39
40 #ifndef MAX
41 # define MAX(a,b) ((a) > (b) ? (a) : (b))
42 #endif
43 #ifndef MIN
44 # define MIN(a,b) ((a) > (b) ? (b) : (a))
45 #endif
46
47 /**
48  * An empty dynamic array descriptor.
49  */
50 ir_arr_descr arr_mt_descr = { ARR_D_MAGIC, 0, { 0 }, 0,  { { 0 } } };
51
52 void ir_verify_arr(const void *arr)
53 {
54 #ifndef NDEBUG
55         ir_arr_descr *descr = ARR_DESCR(arr);
56         assert(descr->magic == ARR_D_MAGIC || descr->magic == ARR_A_MAGIC
57                          || descr->magic == ARR_F_MAGIC);
58         if (descr->magic == ARR_F_MAGIC) {
59                 assert(descr->u.allocated >= descr->nelts);
60         }
61 #else
62         (void) arr;
63 #endif
64 }
65
66 /**
67  * Creates a dynamic array on a obstack.
68  *
69  * @param obstack    An struct obstack * were the data will be allocated
70  * @param nelts      The number of elements
71  * @param elts_size  The size of the array elements.
72  *
73  * @return A pointer to the dynamic array (can be used as a pointer to the
74  *         first element of this array).
75  *
76  * @remark Helper function, use NEW_ARR_D() instead.
77  */
78 void *ir_new_arr_d(struct obstack *obstack, size_t nelts, size_t elts_size)
79 {
80         ir_arr_descr *dp;
81
82         assert(obstack);
83
84         dp = (ir_arr_descr*)obstack_alloc(obstack, ARR_ELTS_OFFS + elts_size);
85         ARR_SET_DBGINF(dp, ARR_D_MAGIC, elts_size/nelts);
86         dp->u.obstack = obstack;
87         dp->nelts = nelts;
88         return dp->elts;
89 }
90
91 /**
92  * Creates a flexible array.
93  *
94  * @param nelts      The number of elements
95  * @param elts_size  The size of the array elements.
96  *
97  * @return A pointer to the flexible array (can be used as a pointer to the
98  *         first element of this array).
99  *
100  * @remark Helper function, use NEW_ARR_F() instead.
101  */
102 void *ir_new_arr_f(size_t nelts, size_t elts_size)
103 {
104         ir_arr_descr *newa;
105
106         newa = (ir_arr_descr*)xmalloc(ARR_ELTS_OFFS+elts_size);
107         ARR_SET_DBGINF(newa, ARR_F_MAGIC, nelts ? elts_size/nelts : 0);
108         newa->u.allocated = newa->nelts = nelts;
109         return newa->elts;
110 }
111
112 /**
113  * Delete a flexible array.
114  *
115  * @param elts    The flexible array (pointer to the first element).
116  *
117  * @remark Helper function, use DEL_ARR_F() instead.
118  */
119 void ir_del_arr_f(void *elts)
120 {
121         ir_arr_descr *dp = ARR_DESCR (elts);
122
123         ARR_VRFY(elts);
124         assert(dp->magic == ARR_F_MAGIC);
125
126 #ifndef NDEBUG
127         dp->magic = 0xdeadbeef;
128 #endif
129         free(dp);
130 }
131
132 /**
133  * Resize a flexible array, always reallocate data.
134  *
135  * @param elts       The flexible array (pointer to the first element).
136  * @param nelts      The new number of elements.
137  * @param elts_size  The size of the array elements.
138  *
139  * @return A resized flexible array, possibly other address than
140  *         elts.
141  *
142  * @remark Helper function, use ARR_SETLEN() instead.
143  */
144 void *ir_arr_setlen (void *elts, size_t nelts, size_t elts_size)
145 {
146         ir_arr_descr *dp = ARR_DESCR (elts);
147
148         assert(dp->magic == ARR_F_MAGIC);
149         ARR_VRFY(elts);
150         assert(!dp->eltsize || !nelts || (dp->eltsize == elts_size/nelts));
151
152         dp = (ir_arr_descr*) xrealloc(dp, ARR_ELTS_OFFS+elts_size);
153         dp->u.allocated = dp->nelts = nelts;
154
155         return dp->elts;
156 }
157
158 /**
159  * Resize a flexible array, allocate more data if needed but do NOT
160  * reduce.
161  *
162  * @param elts     The flexible array (pointer to the first element).
163  * @param nelts    The new number of elements.
164  * @param eltsize  The size of the array elements.
165  *
166  * @return A resized flexible array, possibly other address than
167  *         elts.
168  *
169  * @remark Helper function, use ARR_RESIZE() instead.
170  */
171 void *ir_arr_resize(void *elts, size_t nelts, size_t eltsize)
172 {
173         ir_arr_descr *dp = ARR_DESCR(elts);
174         size_t n;
175
176         assert(dp->magic == ARR_F_MAGIC);
177         ARR_VRFY(elts);
178         assert(dp->eltsize ? dp->eltsize == eltsize : (dp->eltsize = eltsize, 1));
179
180         /* @@@ lots of resizes for small nelts */
181         n = MAX(1, dp->u.allocated);
182         while (nelts > n) n <<= 1;
183         while (3*nelts < n) n >>= 1;
184         assert(n >= nelts);
185
186         if (n != dp->u.allocated) {
187                 dp = (ir_arr_descr*) xrealloc(dp, ARR_ELTS_OFFS+eltsize*n);
188                 dp->u.allocated = n;
189         }
190         dp->nelts = nelts;
191
192         return dp->elts;
193 }
194
195 #ifdef DEBUG_libfirm
196 /* forward declarations to avoid warnings */
197 size_t array_len(const void *arr);
198 ir_arr_descr *array_descr(const void *arr);
199
200 /**
201  * This function returns the length of a flexible array.
202  * Do NOT use is in code, use ARR_LEN() macro!
203  * This function is intended to be called from a debugger.
204  */
205 size_t array_len(const void *arr)
206 {
207         return ARR_LEN(arr);
208 }
209
210 /**
211  * This function returns the array descriptor of a flexible array.
212  * Do NOT use is in code!.
213  * This function is intended to be called from a debugger.
214  */
215 ir_arr_descr *array_descr(const void *arr)
216 {
217         if (! arr)
218                 return NULL;
219         return ARR_DESCR(arr);
220 }
221 #endif /* DEBUG_libfirm */