reindent
[libfirm] / ir / adt / array.h
1 /*
2  * Project:     libFIRM
3  * File name:   ir/adt/array.h
4  * Purpose:     Declarations for Array.
5  * Author:      Markus Armbruster
6  * Modified by:
7  * Created:     1999 by getting from fiasco
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 1995, 1996 Markus Armbruster
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 /**
14  * @file array.h   Dynamic and flexible arrays for C.
15  */
16
17 #ifndef _ARRAY_H
18 #define _ARRAY_H
19
20 #include <assert.h>
21 #include <stddef.h>
22 #include <obstack.h>
23
24 #ifdef HAVE_ALLOCA_H
25 #include <alloca.h>
26 #endif
27 #ifdef HAVE_MALLOC_H
28 #include <malloc.h>
29 #endif
30
31 #include "fourcc.h"
32 #include "align.h"
33
34 #define ARR_D_MAGIC     FOURCC('A','R','R','D')
35 #define ARR_A_MAGIC     FOURCC('A','R','R','A')
36 #define ARR_F_MAGIC     FOURCC('A','R','R','F')
37
38 /**
39  * Creates a flexible array.
40  *
41  * @param type     The element type of the new array.
42  * @param nelts    a size_t expression evaluating to the number of elements
43  *
44  * This macro creates a flexible array of a given type at runtime.
45  * The size of the array can be changed later.
46  *
47  * @return A pointer to the flexible array (can be used as a pointer to the
48  *         first element of this array).
49  */
50 #define NEW_ARR_F(type, nelts)                                          \
51   ((type *)_new_arr_f ((nelts), sizeof(type) * (nelts)))
52
53 /**
54  * Creates a new flxible array with the same number of elements as a
55  * given one.
56  *
57  * @param type     The element type of the new array.
58  * @param arr      An array from which the number of elements will be taken
59  *
60  * This macro creates a flexible array of a given type at runtime.
61  * The size of the array can be changed later.
62  *
63  * @return A pointer to the flexible array (can be used as a pointer to the
64  *         first element of this array).
65  */
66 #define CLONE_ARR_F(type, arr)                  \
67   NEW_ARR_F (type, ARR_LEN ((arr)))
68
69 /**
70  * Duplicates an array and returns the new flexible one.
71  *
72  * @param type     The element type of the new array.
73  * @param arr      An array from which the elements will be duplicated
74  *
75  * This macro creates a flexible array of a given type at runtime.
76  * The size of the array can be changed later.
77  *
78  * @return A pointer to the flexible array (can be used as a pointer to the
79  *         first element of this array).
80  */
81 #define DUP_ARR_F(type, arr)                                                    \
82   memcpy (CLONE_ARR_F (type, (arr)), (arr), sizeof(type) * ARR_LEN((arr)))
83
84 /**
85  * Delete a flexible array.
86  *
87  * @param arr    The flexible array.
88  */
89 #define DEL_ARR_F(arr) (_del_arr_f ((arr)))
90
91 /**
92  * Creates a dynamic array on an obstack.
93  *
94  * @param type     The element type of the new array.
95  * @param obstack  A struct obstack * were the data will be allocated
96  * @param nelts    A size_t expression evaluating to the number of elements
97  *
98  * This macro creates a dynamic array of a given type at runtime.
99  * The size of the array cannot be changed later.
100  *
101  * @return A pointer to the dynamic array (can be used as a pointer to the
102  *         first element of this array).
103  */
104 #define NEW_ARR_D(type, obstack, nelts)                                 \
105   (  nelts                                                              \
106    ? (type *)_new_arr_d ((obstack), (nelts), sizeof(type) * (nelts))    \
107    : (type *)arr_mt_descr.v.elts)
108
109 /**
110  * Creates a new dynamic array with the same number of elements as a
111  * given one.
112  *
113  * @param type     The element type of the new array.
114  * @param obstack  An struct obstack * were the data will be allocated
115  * @param arr      An array from which the number of elements will be taken
116  *
117  * This macro creates a dynamic array of a given type at runtime.
118  * The size of the array cannot be changed later.
119  *
120  * @return A pointer to the dynamic array (can be used as a pointer to the
121  *         first element of this array).
122  */
123 #define CLONE_ARR_D(type, obstack, arr)         \
124   NEW_ARR_D (type, (obstack), ARR_LEN ((arr)))
125
126 /**
127  * Duplicates an array and returns the new dynamic one.
128  *
129  * @param type     The element type of the new array.
130  * @param obstack  An struct obstack * were the data will be allocated
131  * @param arr      An array from which the elements will be duplicated
132  *
133  * This macro creates a dynamic array of a given type at runtime.
134  * The size of the array cannot be changed later.
135  *
136  * @return A pointer to the dynamic array (can be used as a pointer to the
137  *         first element of this array).
138  */
139 #define DUP_ARR_D(type, obstack, arr)                                                   \
140   memcpy (CLONE_ARR_D (type, (obstack), (arr)), (arr), sizeof(type) * ARR_LEN ((arr)))
141
142 /**
143  * Create an automatic array which will be deleted at return from function.
144  * Beware, the data will be allocated on the function stack!
145  *
146  * @param type     The element type of the new array.
147  * @param var      A lvalue of type (type *) which will hold the new array.
148  * @param n        number of elements in this array.
149  *
150  * This macro creates a dynamic array on the functions stack of a given type at runtime.
151  * The size of the array cannot be changed later.
152  */
153 #define NEW_ARR_A(type, var, n)                                                                 \
154   do {                                                                                          \
155     int _nelts = (n);                                                                           \
156     assert (_nelts >= 0);                                                                       \
157     (var) = (void *)((_arr_descr *)alloca (_ARR_ELTS_OFFS + sizeof(type) * _nelts))->v.elts;    \
158     _ARR_SET_DBGINF (_ARR_DESCR ((var)), ARR_A_MAGIC, sizeof (type));                           \
159     (void)(_ARR_DESCR ((var))->nelts = _nelts);                                                 \
160   } while (0)
161
162 /**
163  * Creates a new automatic array with the same number of elements as a
164  * given one.
165  *
166  * @param type     The element type of the new array.
167  * @param var      A lvalue of type (type *) which will hold the new array.
168  * @param arr      An array from which the elements will be duplicated
169  *
170  * This macro creates a dynamic array of a given type at runtime.
171  * The size of the array cannot be changed later.
172  *
173  * @return A pointer to the dynamic array (can be used as a pointer to the
174  *         first element of this array).
175  */
176 #define CLONE_ARR_A(type, var, arr)             \
177   NEW_ARR_A (type, (var), ARR_LEN ((arr)))
178
179 /**
180  * Duplicates an array and returns a new automatic one.
181  *
182  * @param type     The element type of the new array.
183  * @param var      A lvalue of type (type *) which will hold the new array.
184  * @param arr      An array from with the number of elements will be taken
185  *
186  * This macro creates a dynamic array of a given type at runtime.
187  * The size of the array cannot be changed later.
188  *
189  * @return A pointer to the dynamic array (can be used as a pointer to the
190  *         first element of this array).
191  */
192 #define DUP_ARR_A(type, var, arr)                                       \
193   do { CLONE_ARR_A(type, (var), (arr));                                 \
194        memcpy ((var), (arr), sizeof (type) * ARR_LEN ((arr))); }        \
195   while (0)
196
197 /**
198  * Declare an initialized (zero'ed) array of fixed size.
199  * This macro should be used at file scope only.
200  *
201  * @param type     The element type of the new array.
202  * @param var      A lvalue of type (type *) which will hold the new array.
203  * @param _nelts   Number of elements in this new array.
204  */
205 #define DECL_ARR_S(type, var, _nelts)                                   \
206   ARR_STRUCT(type, (_nelts) ? (_nelts) : 1) _##var;                     \
207   type *var = (_ARR_SET_DBGINF (&_##var, ARR_A_MAGIC, sizeof (type)),   \
208                _##var.nelts = _nelts,                                   \
209                _##var.v.elts)
210
211 /**
212  * Returns the length of an array
213  *
214  * @param arr  a flexible, dynamic, automatic or static array.
215  */
216 #define ARR_LEN(arr) (ARR_VRFY ((arr)), _ARR_DESCR((arr))->nelts)
217
218 /**
219  * Resize a flexible array, allocate more data if needed but do NOT
220  * reduce.
221  *
222  * @param type     The element type of the array.
223  * @param arr      The array, which must be an lvalue.
224  * @param n        The new size of the array.
225  *
226  * @remark  This macro may change arr, so update all references!
227  */
228 #define ARR_RESIZE(type, arr, n)                                        \
229   ((arr) = _arr_resize ((arr), (n), sizeof(type)))
230
231 /**
232  * Resize a flexible array, always reallocate data.
233  *
234  * @param type     The element type of the array.
235  * @param arr      The array, which must be an lvalue.
236  * @param n        The new size of the array.
237  *
238  * @remark  This macro may change arr, so update all references!
239  */
240 #define ARR_SETLEN(type, arr, n)                                        \
241   ((arr) = _arr_setlen ((arr), (n), sizeof(type) * (n)))
242
243 /** Set a length smaller than the current length of the array.  Do not
244  *  resize. len must be <= ARR_LEN(arr). */
245 #define ARR_SHRINKLEN(arr,len)                                          \
246    (ARR_VRFY ((arr)), assert(_ARR_DESCR((arr))->nelts >= len),             \
247     _ARR_DESCR((arr))->nelts = len)
248
249 /**
250  * Resize a flexible array by growing it by delta elements.
251  *
252  * @param type     The element type of the array.
253  * @param arr      The array, which must be an lvalue.
254  * @param delta    The delta number of elements.
255  *
256  * @remark  This macro may change arr, so update all references!
257  */
258 #define ARR_EXTEND(type, arr, delta)                    \
259   ARR_RESIZE (type, (arr), ARR_LEN ((arr)) + (delta))
260
261 /**
262  * Resize a flexible array to hold n elements only if it is currently shorter
263  * than n.
264  *
265  * @param type     The element type of the array.
266  * @param arr      The array, which must be an lvalue.
267  * @param n        The new size of the array.
268  *
269  * @remark  This macro may change arr, so update all references!
270  */
271 #define ARR_EXTO(type, arr, n)                                          \
272   ((n) >= ARR_LEN ((arr)) ? ARR_RESIZE (type, (arr), (n)+1) : (arr))
273
274 /**
275  * Append one element to a flexible array.
276  *
277  * @param type     The element type of the array.
278  * @param arr      The array, which must be an lvalue.
279  * @param elt      The new element, must be of type (type).
280  */
281 #define ARR_APP1(type, arr, elt)                                        \
282   (ARR_EXTEND (type, (arr), 1), (arr)[ARR_LEN ((arr))-1] = (elt))
283
284
285 #ifdef NDEBUG
286 # define ARR_VRFY(arr) ((void)0)
287 # define ARR_IDX_VRFY(arr, idx) ((void)0)
288 #else
289 # define ARR_VRFY(arr)                                                                  \
290     assert (   (   (_ARR_DESCR((arr))->magic == ARR_D_MAGIC)                            \
291                 || (_ARR_DESCR((arr))->magic == ARR_A_MAGIC)                            \
292                 || (_ARR_DESCR((arr))->magic == ARR_F_MAGIC))                           \
293             && (   (_ARR_DESCR((arr))->magic != ARR_F_MAGIC)                            \
294                 || (_ARR_DESCR((arr))->u.allocated >= _ARR_DESCR((arr))->nelts))        \
295             && (_ARR_DESCR((arr))->nelts >= 0))
296 # define ARR_IDX_VRFY(arr, idx)                         \
297     assert ((0 <= (idx)) && ((idx) < ARR_LEN ((arr))))
298 #endif
299
300
301 /* Private !!!
302    Don't try this at home, kids, we're trained professionals ;->
303    ... or at the IPD, either. */
304 #ifdef NDEBUG
305 # define _ARR_DBGINF_DECL
306 # define _ARR_SET_DBGINF(descr, co, es)
307 #else
308 # define _ARR_DBGINF_DECL int magic; size_t eltsize;
309 # define _ARR_SET_DBGINF(descr, co, es)                                 \
310     ( (descr)->magic = (co), (descr)->eltsize = (es) )
311 #endif
312
313 /**
314  * Construct an array header.
315  */
316 #define ARR_STRUCT(type, _nelts)                                                \
317   struct {                                                                      \
318     _ARR_DBGINF_DECL                                                            \
319     union {                                                                     \
320       struct obstack *obstack;  /* dynamic: allocated on this obstack */        \
321       int allocated;                    /* flexible: #slots allocated */        \
322     } u;                                                                        \
323     int nelts;                                                                  \
324     union {                                                                     \
325       type elts[(_nelts)];                                                      \
326       aligned_type align[1];                                                    \
327     } v;                                                                        \
328   }
329
330 /**
331  * The array descriptor header type.
332  */
333 typedef ARR_STRUCT (aligned_type, 1) _arr_descr;
334
335 extern _arr_descr arr_mt_descr;
336
337 void *_new_arr_f (int, size_t);
338 void _del_arr_f (void *);
339 void *_new_arr_d (struct obstack *obstack, int nelts, size_t elts_size);
340 void *_arr_resize (void *, int, size_t);
341 void *_arr_setlen (void *, int, size_t);
342
343 #define _ARR_ELTS_OFFS offsetof (_arr_descr, v.elts)
344 #define _ARR_DESCR(elts) ((_arr_descr *)(void *)((char *)(elts) - _ARR_ELTS_OFFS))
345
346 #endif