properly mark symbols in the public API to be exported. This allows us to use -fvisib...
[libfirm] / include / libfirm / adt / array.h
1 /*
2  * Copyright (C) 1995-2008 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  * @version   $Id$
25  */
26 #ifndef FIRM_ADT_ARRAY_H
27 #define FIRM_ADT_ARRAY_H
28
29 #include <assert.h>
30 #include <stddef.h>
31
32 #include "obst.h"
33 #include "fourcc.h"
34 #include "xmalloc.h"
35
36 #include "../begin.h"
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 *)ir_new_arr_f((nelts), sizeof(type) * (nelts)))
52
53 /**
54  * Creates a new flexible 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) (ir_del_arr_f((void *)(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 *)ir_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  * Returns the length of an array
144  *
145  * @param arr  a flexible, dynamic, automatic or static array.
146  */
147 #define ARR_LEN(arr) (ARR_VRFY((arr)), ARR_DESCR((arr))->nelts)
148
149 /**
150  * Resize a flexible array, allocate more data if needed but do NOT
151  * reduce.
152  *
153  * @param type     The element type of the array.
154  * @param arr      The array, which must be an lvalue.
155  * @param n        The new size of the array.
156  *
157  * @remark  This macro may change arr, so update all references!
158  */
159 #define ARR_RESIZE(type, arr, n)                                        \
160   ((arr) = ir_arr_resize((void *)(arr), (n), sizeof(type)))
161
162 /**
163  * Resize a flexible array, always reallocate data.
164  *
165  * @param type     The element type of the array.
166  * @param arr      The array, which must be an lvalue.
167  * @param n        The new size of the array.
168  *
169  * @remark  This macro may change arr, so update all references!
170  */
171 #define ARR_SETLEN(type, arr, n)                                        \
172   ((arr) = ir_arr_setlen((void *)(arr), (n), sizeof(type) * (n)))
173
174 /** Set a length smaller than the current length of the array.  Do not
175  *  resize. len must be <= ARR_LEN(arr). */
176 #define ARR_SHRINKLEN(arr,len)                                          \
177    (ARR_VRFY((arr)), assert(ARR_DESCR((arr))->nelts >= len),             \
178     ARR_DESCR((arr))->nelts = len)
179
180 /**
181  * Resize a flexible array by growing it by delta elements.
182  *
183  * @param type     The element type of the array.
184  * @param arr      The array, which must be an lvalue.
185  * @param delta    The delta number of elements.
186  *
187  * @remark  This macro may change arr, so update all references!
188  */
189 #define ARR_EXTEND(type, arr, delta)                    \
190   ARR_RESIZE(type, (arr), ARR_LEN((arr)) + (delta))
191
192 /**
193  * Resize a flexible array to hold n elements only if it is currently shorter
194  * than n.
195  *
196  * @param type     The element type of the array.
197  * @param arr      The array, which must be an lvalue.
198  * @param n        The new size of the array.
199  *
200  * @remark  This macro may change arr, so update all references!
201  */
202 #define ARR_EXTO(type, arr, n)                                          \
203   ((n) >= ARR_LEN((arr)) ? ARR_RESIZE(type, (arr), (n)+1) : (arr))
204
205 /**
206  * Append one element to a flexible array.
207  *
208  * @param type     The element type of the array.
209  * @param arr      The array, which must be an lvalue.
210  * @param elt      The new element, must be of type (type).
211  */
212 #define ARR_APP1(type, arr, elt)                                        \
213   (ARR_EXTEND(type, (arr), 1), (arr)[ARR_LEN((arr))-1] = (elt))
214
215 #ifdef NDEBUG
216 # define ARR_VRFY(arr)          ((void)0)
217 # define ARR_IDX_VRFY(arr, idx) ((void)0)
218 #else
219 # define ARR_VRFY(arr)          ir_verify_arr(arr)
220 # define ARR_IDX_VRFY(arr, idx)                         \
221     assert((0 <= (idx)) && ((idx) < ARR_LEN((arr))))
222 #endif
223
224 /** A type that has most constrained alignment.  */
225 typedef union {
226   long double d;
227   void *p;
228   long l;
229 } aligned_type;
230
231 /**
232  * Construct an array header.
233  */
234 #define ARR_STRUCT(type, rnelts)                    \
235   struct {                                          \
236         int magic;                                      \
237         size_t eltsize;                                 \
238     union {                                         \
239       struct obstack *obstack;  /* dynamic: allocated on this obstack */  \
240       int allocated;                    /* flexible: #slots allocated */          \
241     } u;                                            \
242     int nelts;                                      \
243     union {                                         \
244       type elts[(rnelts)];                          \
245       aligned_type align[1];                        \
246     } v;                                            \
247   }
248
249 /**
250  * The array descriptor header type.
251  */
252 typedef ARR_STRUCT(aligned_type, 1) ir_arr_descr;
253
254 extern ir_arr_descr arr_mt_descr;
255
256 void *ir_new_arr_f(int nelts, size_t elts_size);
257 void ir_del_arr_f(void *elts);
258 void *ir_new_arr_d(struct obstack *obstack, int nelts, size_t elts_size);
259 void *ir_arr_resize(void *elts, int nelts, size_t elts_size);
260 void *ir_arr_setlen(void *elts, int nelts, size_t elts_size);
261 void ir_verify_arr(const void *elts);
262
263 #define ARR_ELTS_OFFS offsetof(ir_arr_descr, v.elts)
264 #define ARR_DESCR(elts) ((ir_arr_descr *)(void *)((char *)(elts) - ARR_ELTS_OFFS))
265
266 /*
267  ____             _           _      _
268 / ___|  ___  _ __| |_ ___  __| |    / \   _ __ _ __ __ _ _   _ ___
269 \___ \ / _ \| '__| __/ _ \/ _` |   / _ \ | '__| '__/ _` | | | / __|
270  ___) | (_) | |  | ||  __/ (_| |  / ___ \| |  | | | (_| | |_| \__ \
271 |____/ \___/|_|   \__\___|\__,_| /_/   \_\_|  |_|  \__,_|\__, |___/
272                                                          |___/
273 */
274
275 typedef int (ir_arr_cmp_func_t)(const void *a, const void *b);
276
277 /**
278  * Do a binary search in an array.
279  * @param arr      The array.
280  * @param elm_size The size of an array element.
281  * @param cmp      A comparison function for two array elements (see qsort(3) for example).
282  * @param elm      A pointer to the element we are looking for.
283  * @return         This is somewhat tricky. Let <code>res</code> be the return value.
284  *                 If the return value is negative, then <code>elm</code> was not in the array
285  *                 but <code>-res - 1</code> gives the proper location where it should be inserted.
286  *                 If <code>res >= 0</code> then the element is in the array and <code>res</code>
287  *                 represents its index.
288  *                 That allows for testing membership and finding proper insertion indices.
289  * @note           The differences to bsearch(3) which does not give proper insert locations
290  *                 in the case that the element is not conatined in the array.
291  */
292 static inline int ir_arr_bsearch(const void *arr, size_t elm_size, ir_arr_cmp_func_t *cmp, const void *elm)
293 {
294         int hi = ARR_LEN(arr);
295         int lo = 0;
296
297         while(lo < hi) {
298                 int md     = lo + ((hi - lo) >> 1);
299                 int res    = cmp((char *) arr + md * elm_size, elm);
300                 if(res < 0)
301                         lo = md + 1;
302                 else if(res > 0)
303                         hi = md;
304                 else
305                         return md;
306         }
307
308         return -(lo + 1);
309 }
310
311 #define ARR_SET_INSERT(arr, cmp, elm) \
312 do { \
313         int idx = ir_arr_bsearch((arr), sizeof((arr)[0]), (cmp), (elm)); \
314         if (idx < 0) { \
315                 idx = -idx - 1; \
316                 memmove(&(arr)[idx+1], &(arr)[idx], sizeof((arr)[0]) * (ARR_DESCR((arr))->nelts - idx)); \
317                 (arr)[idx] = *(elm); \
318                 ++ARR_DESCR((arr))->nelts; \
319         } \
320 } while(0)
321
322 #define ARR_SET_INSERT_EXT(type, arr, cmp, elm) \
323 do { \
324         int idx = ir_arr_bsearch((arr), sizeof((arr)[0]), (cmp), (elm)); \
325         if (idx < 0) { \
326                 int len = ARR_LEN(arr); \
327                 idx = -idx - 1; \
328                 ARR_EXTO(type, arr, len + 1); \
329                 memmove(&(arr)[idx+1], &(arr)[idx], sizeof((arr)[0]) * (len - idx)); \
330                 (arr)[idx] = *(elm); \
331         } \
332 } while(0)
333
334 #define ARR_SET_REMOVE(arr, cmp, elm) \
335 do { \
336         int idx = ir_arr_bsearch((arr), sizeof((arr)[0]), (cmp), (elm)); \
337         if (idx >= 0) { \
338                 --ARR_DESCR((arr))->nelts; \
339                 memmove(&(arr)[idx], &(arr)[idx+1], sizeof((arr)[0]) * (ARR_DESCR((arr))->nelts - idx)); \
340         } \
341 } while(0)
342
343 /**
344  * Return the index of an element in an array set.
345  * To check for containment, use the expression:
346  *     (ARR_SET_GET_IDX(arr, cmp, elm) >= 0)
347  *
348  * @return The index or some value < 0 if the element was not in the set.
349  */
350 #define ARR_SET_GET_IDX(arr, cmp, elm) \
351         (ARR_VRFY((arr)), ir_arr_bsearch((arr), sizeof((arr)[0]), cmp, (elm)))
352
353 #ifdef __GNUC__
354 #define ARR_SET_GET(arr, cmp, elm) \
355         ({ int idx = ARR_SET_GET_IDX(arr, cmp, elm); idx >= 0 ? &(arr)[idx] : NULL; })
356 #else
357 #define ARR_SET_GET(arr, cmp, elm) \
358         (ARR_SET_GET_IDX(arr, cmp, elm) >= 0 ? &(arr)[ARR_SET_GET_IDX(arr, cmp, elm)] : NULL)
359 #endif
360
361
362 #define ARR_SET_CONTAINS(arr, cmp, elm) \
363         (ARR_SET_GET_IDX((arr), (cmp), (elm)) >= 0)
364
365 /**
366  * Reset the array set.
367  * This just initializes the size to zero but does not wipe out any element.
368  */
369 #define ARR_SET_CLEAR(arr) ARR_SHRINKLEN(arr, 0)
370
371 #include "../end.h"
372
373 #endif