0ec934168e4e8f49d826351e9ed328c6f30526ff
[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  * Create a dynamic array on an obstack and null its contents.
114  */
115 #define NEW_ARR_DZ(type, obstack, nelts) \
116         ((type*)memset(NEW_ARR_D(type, (obstack), (nelts)), 0, sizeof(type) * (nelts)))
117
118 /**
119  * Creates a new dynamic array with the same number of elements as a
120  * given one.
121  *
122  * @param type     The element type of the new array.
123  * @param obstack  An struct obstack * were the data will be allocated
124  * @param arr      An array from which the number of elements will be taken
125  *
126  * This macro creates a dynamic array of a given type at runtime.
127  * The size of the array cannot be changed later.
128  *
129  * @return A pointer to the dynamic array (can be used as a pointer to the
130  *         first element of this array).
131  */
132 #define CLONE_ARR_D(type, obstack, arr) \
133   NEW_ARR_D(type, (obstack), ARR_LEN((arr)))
134
135 /**
136  * Duplicates an array and returns the new dynamic one.
137  *
138  * @param type     The element type of the new array.
139  * @param obstack  An struct obstack * were the data will be allocated
140  * @param arr      An array from which the elements will be duplicated
141  *
142  * This macro creates a dynamic array of a given type at runtime.
143  * The size of the array cannot be changed later.
144  *
145  * @return A pointer to the dynamic array (can be used as a pointer to the
146  *         first element of this array).
147  */
148 #define DUP_ARR_D(type, obstack, arr) \
149   ((type*)memcpy(CLONE_ARR_D(type, (obstack), (arr)), (arr), sizeof(type) * ARR_LEN ((arr))))
150
151 /**
152  * Returns the length of an array
153  *
154  * @param arr  a flexible, dynamic, automatic or static array.
155  */
156 #define ARR_LEN(arr) (ARR_VRFY((arr)), ARR_DESCR((arr))->nelts)
157
158 /**
159  * Resize a flexible array, allocate more data if needed but do NOT
160  * reduce.
161  *
162  * @param type     The element type of the array.
163  * @param arr      The array, which must be an lvalue.
164  * @param n        The new size of the array.
165  *
166  * @remark  This macro may change arr, so update all references!
167  */
168 #define ARR_RESIZE(type, arr, n) \
169   ((arr) = (type*) ir_arr_resize((void *)(arr), (n), sizeof(type)))
170
171 /**
172  * Resize a flexible array, always reallocate data.
173  *
174  * @param type     The element type of the array.
175  * @param arr      The array, which must be an lvalue.
176  * @param n        The new size of the array.
177  *
178  * @remark  This macro may change arr, so update all references!
179  */
180 #define ARR_SETLEN(type, arr, n) \
181   ((arr) = (type*) ir_arr_setlen((void *)(arr), (n), sizeof(type) * (n)))
182
183 /**
184  * Resize a flexible array by growing it by delta elements.
185  *
186  * @param type     The element type of the array.
187  * @param arr      The array, which must be an lvalue.
188  * @param delta    The delta number of elements.
189  *
190  * @remark  This macro may change arr, so update all references!
191  */
192 #define ARR_EXTEND(type, arr, delta) \
193   ARR_RESIZE(type, (arr), ARR_LEN((arr)) + (delta))
194
195 /**
196  * Resize a flexible array to hold n elements only if it is currently shorter
197  * than n.
198  *
199  * @param type     The element type of the array.
200  * @param arr      The array, which must be an lvalue.
201  * @param n        The new size of the array.
202  *
203  * @remark  This macro may change arr, so update all references!
204  */
205 #define ARR_EXTO(type, arr, n) \
206         do { \
207                 if ((n) >= ARR_LEN(arr)) { ARR_RESIZE(type, arr, (n)+1); } \
208         } while(0)
209
210 /**
211  * Append one element to a flexible array.
212  *
213  * @param type     The element type of the array.
214  * @param arr      The array, which must be an lvalue.
215  * @param elt      The new element, must be of type (type).
216  */
217 #define ARR_APP1(type, arr, elt) \
218   (ARR_EXTEND(type, (arr), 1), (arr)[ARR_LEN((arr))-1] = (elt))
219
220 #ifdef NDEBUG
221 # define ARR_VRFY(arr)          ((void)0)
222 # define ARR_IDX_VRFY(arr, idx) ((void)0)
223 #else
224 /** Check array for consistency */
225 # define ARR_VRFY(arr)          ir_verify_arr(arr)
226 /** Check if index is within array bounds */
227 # define ARR_IDX_VRFY(arr, idx) \
228     assert((0 <= (idx)) && ((idx) < ARR_LEN((arr))))
229 #endif
230
231 /** @cond PRIVATE */
232
233 /** A type that has most constrained alignment.  */
234 typedef union {
235   long double d;
236   void *p;
237   long l;
238 } aligned_type;
239
240 /**
241  * The array descriptor header type.
242  */
243 typedef struct {
244         int magic;                    /**< array magic. */
245         size_t allocated;         /**< number of allocated elements. */
246         size_t nelts;                 /**< current length of the array. */
247         aligned_type elts[1];         /**< start of the array data. */
248 } ir_arr_descr;
249
250 extern ir_arr_descr arr_mt_descr;
251
252 FIRM_API void *ir_new_arr_f(size_t nelts, size_t elts_size);
253 FIRM_API void ir_del_arr_f(void *elts);
254 FIRM_API void *ir_new_arr_d(struct obstack *obstack, size_t nelts, size_t elts_size);
255 FIRM_API void *ir_arr_resize(void *elts, size_t nelts, size_t elts_size);
256 FIRM_API void *ir_arr_setlen(void *elts, size_t nelts, size_t elts_size);
257 FIRM_API void ir_verify_arr(const void *elts);
258
259 #define ARR_ELTS_OFFS offsetof(ir_arr_descr, elts)
260 #define ARR_DESCR(elts) ((ir_arr_descr *)(void *)((char *)(elts) - ARR_ELTS_OFFS))
261
262 /** Set a length smaller than the current length of the array.  Do not
263  *  resize. len must be <= ARR_LEN(arr). */
264 static inline void ARR_SHRINKLEN(void *arr, size_t new_len)
265 {
266         ARR_VRFY(arr);
267         assert(ARR_DESCR(arr)->nelts >= new_len);
268         ARR_DESCR(arr)->nelts = new_len;
269 }
270
271 /** @endcond */
272
273 /** @} */
274
275 #include "../end.h"
276
277 #endif