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