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