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