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