array: Remove the write-only attribute u.obstack from struct ir_arr_descr.
[libfirm] / include / libfirm / adt / array.h
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     Dynamic and flexible arrays for C.
23  * @author    Markus Armbruster, Michael Beck, Matthias Braun, Sebastian Hack
24  */
25 #ifndef FIRM_ADT_ARRAY_H
26 #define FIRM_ADT_ARRAY_H
27
28 #include <assert.h>
29 #include <stddef.h>
30
31 #include "obst.h"
32
33 #include "../begin.h"
34
35 /**
36  * @ingroup adt
37  * @defgroup array Arrays
38  * @{
39  */
40
41 /**
42  * Creates a flexible array.
43  *
44  * @param type     The element type of the new array.
45  * @param nelts    a size_t expression evaluating to the number of elements
46  *
47  * This macro creates a flexible array of a given type at runtime.
48  * The size of the array can be changed later.
49  *
50  * @return A pointer to the flexible array (can be used as a pointer to the
51  *         first element of this array).
52  */
53 #define NEW_ARR_F(type, nelts) \
54   ((type *)ir_new_arr_f((nelts), sizeof(type) * (nelts)))
55
56 /**
57  * Creates a new flexible array with the same number of elements as a
58  * given one.
59  *
60  * @param type     The element type of the new array.
61  * @param arr      An array from which the number of elements will be taken
62  *
63  * This macro creates a flexible array of a given type at runtime.
64  * The size of the array can be changed later.
65  *
66  * @return A pointer to the flexible array (can be used as a pointer to the
67  *         first element of this array).
68  */
69 #define CLONE_ARR_F(type, arr) \
70   NEW_ARR_F(type, ARR_LEN((arr)))
71
72 /**
73  * Duplicates an array and returns the new flexible one.
74  *
75  * @param type     The element type of the new array.
76  * @param arr      An array from which the elements will be duplicated
77  *
78  * This macro creates a flexible array of a given type at runtime.
79  * The size of the array can be changed later.
80  *
81  * @return A pointer to the flexible array (can be used as a pointer to the
82  *         first element of this array).
83  */
84 #define DUP_ARR_F(type, arr) \
85   ((type*) memcpy(CLONE_ARR_F(type, (arr)), (arr), sizeof(type) * ARR_LEN((arr))))
86
87 /**
88  * Delete a flexible array.
89  *
90  * @param arr    The flexible array.
91  */
92 #define DEL_ARR_F(arr) (ir_del_arr_f((void *)(arr)))
93
94 /**
95  * Creates a dynamic array on an obstack.
96  *
97  * @param type     The element type of the new array.
98  * @param obstack  A struct obstack * were the data will be allocated
99  * @param nelts    A size_t expression evaluating to the number of elements
100  *
101  * This macro creates a dynamic array of a given type at runtime.
102  * The size of the array cannot be changed later.
103  *
104  * @return A pointer to the dynamic array (can be used as a pointer to the
105  *         first element of this array).
106  */
107 #define NEW_ARR_D(type, obstack, nelts)                                 \
108   (  nelts                                                              \
109    ? (type *)ir_new_arr_d((obstack), (nelts), sizeof(type) * (nelts))   \
110    : (type *)arr_mt_descr.elts)
111
112 /**
113  * Creates a new dynamic array with the same number of elements as a
114  * given one.
115  *
116  * @param type     The element type of the new array.
117  * @param obstack  An struct obstack * were the data will be allocated
118  * @param arr      An array from which the number of elements will be taken
119  *
120  * This macro creates a dynamic array of a given type at runtime.
121  * The size of the array cannot be changed later.
122  *
123  * @return A pointer to the dynamic array (can be used as a pointer to the
124  *         first element of this array).
125  */
126 #define CLONE_ARR_D(type, obstack, arr) \
127   NEW_ARR_D(type, (obstack), ARR_LEN((arr)))
128
129 /**
130  * Duplicates an array and returns the new dynamic one.
131  *
132  * @param type     The element type of the new array.
133  * @param obstack  An struct obstack * were the data will be allocated
134  * @param arr      An array from which the elements will be duplicated
135  *
136  * This macro creates a dynamic array of a given type at runtime.
137  * The size of the array cannot be changed later.
138  *
139  * @return A pointer to the dynamic array (can be used as a pointer to the
140  *         first element of this array).
141  */
142 #define DUP_ARR_D(type, obstack, arr) \
143   ((type*)memcpy(CLONE_ARR_D(type, (obstack), (arr)), (arr), sizeof(type) * ARR_LEN ((arr))))
144
145 /**
146  * Returns the length of an array
147  *
148  * @param arr  a flexible, dynamic, automatic or static array.
149  */
150 #define ARR_LEN(arr) (ARR_VRFY((arr)), ARR_DESCR((arr))->nelts)
151
152 /**
153  * Resize a flexible array, allocate more data if needed but do NOT
154  * reduce.
155  *
156  * @param type     The element type of the array.
157  * @param arr      The array, which must be an lvalue.
158  * @param n        The new size of the array.
159  *
160  * @remark  This macro may change arr, so update all references!
161  */
162 #define ARR_RESIZE(type, arr, n) \
163   ((arr) = (type*) ir_arr_resize((void *)(arr), (n), sizeof(type)))
164
165 /**
166  * Resize a flexible array, always reallocate data.
167  *
168  * @param type     The element type of the array.
169  * @param arr      The array, which must be an lvalue.
170  * @param n        The new size of the array.
171  *
172  * @remark  This macro may change arr, so update all references!
173  */
174 #define ARR_SETLEN(type, arr, n) \
175   ((arr) = (type*) ir_arr_setlen((void *)(arr), (n), sizeof(type) * (n)))
176
177 /**
178  * Resize a flexible array by growing it by delta elements.
179  *
180  * @param type     The element type of the array.
181  * @param arr      The array, which must be an lvalue.
182  * @param delta    The delta number of elements.
183  *
184  * @remark  This macro may change arr, so update all references!
185  */
186 #define ARR_EXTEND(type, arr, delta) \
187   ARR_RESIZE(type, (arr), ARR_LEN((arr)) + (delta))
188
189 /**
190  * Resize a flexible array to hold n elements only if it is currently shorter
191  * than n.
192  *
193  * @param type     The element type of the array.
194  * @param arr      The array, which must be an lvalue.
195  * @param n        The new size of the array.
196  *
197  * @remark  This macro may change arr, so update all references!
198  */
199 #define ARR_EXTO(type, arr, n) \
200         do { \
201                 if ((n) >= ARR_LEN(arr)) { ARR_RESIZE(type, arr, (n)+1); } \
202         } while(0)
203
204 /**
205  * Append one element to a flexible array.
206  *
207  * @param type     The element type of the array.
208  * @param arr      The array, which must be an lvalue.
209  * @param elt      The new element, must be of type (type).
210  */
211 #define ARR_APP1(type, arr, elt) \
212   (ARR_EXTEND(type, (arr), 1), (arr)[ARR_LEN((arr))-1] = (elt))
213
214 #ifdef NDEBUG
215 # define ARR_VRFY(arr)          ((void)0)
216 # define ARR_IDX_VRFY(arr, idx) ((void)0)
217 #else
218 /** Check array for consistency */
219 # define ARR_VRFY(arr)          ir_verify_arr(arr)
220 /** Check if index is within array bounds */
221 # define ARR_IDX_VRFY(arr, idx) \
222     assert((0 <= (idx)) && ((idx) < ARR_LEN((arr))))
223 #endif
224
225 /** @cond PRIVATE */
226
227 /** A type that has most constrained alignment.  */
228 typedef union {
229   long double d;
230   void *p;
231   long l;
232 } aligned_type;
233
234 /**
235  * The array descriptor header type.
236  */
237 typedef struct {
238         int magic;                    /**< array magic. */
239         size_t eltsize;               /**< size of array elements. */
240         size_t allocated;         /**< number of allocated elements. */
241         size_t nelts;                 /**< current length of the array. */
242         aligned_type elts[1];         /**< start of the array data. */
243 } ir_arr_descr;
244
245 extern ir_arr_descr arr_mt_descr;
246
247 FIRM_API void *ir_new_arr_f(size_t nelts, size_t elts_size);
248 FIRM_API void ir_del_arr_f(void *elts);
249 FIRM_API void *ir_new_arr_d(struct obstack *obstack, size_t nelts, size_t elts_size);
250 FIRM_API void *ir_arr_resize(void *elts, size_t nelts, size_t elts_size);
251 FIRM_API void *ir_arr_setlen(void *elts, size_t nelts, size_t elts_size);
252 FIRM_API void ir_verify_arr(const void *elts);
253
254 #define ARR_ELTS_OFFS offsetof(ir_arr_descr, elts)
255 #define ARR_DESCR(elts) ((ir_arr_descr *)(void *)((char *)(elts) - ARR_ELTS_OFFS))
256
257 /** Set a length smaller than the current length of the array.  Do not
258  *  resize. len must be <= ARR_LEN(arr). */
259 static inline void ARR_SHRINKLEN(void *arr, size_t new_len)
260 {
261         ARR_VRFY(arr);
262         assert(ARR_DESCR(arr)->nelts >= new_len);
263         ARR_DESCR(arr)->nelts = new_len;
264 }
265
266 /** @endcond */
267
268 /** @} */
269
270 #include "../end.h"
271
272 #endif