Switch pqueue to size_t.
[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   ((type*) 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   ((type*)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) = (type*) 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) = (type*) 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 FIRM_API void *ir_new_arr_f(int nelts, size_t elts_size);
257 FIRM_API void ir_del_arr_f(void *elts);
258 FIRM_API void *ir_new_arr_d(struct obstack *obstack, int nelts, size_t elts_size);
259 FIRM_API void *ir_arr_resize(void *elts, int nelts, size_t elts_size);
260 FIRM_API void *ir_arr_setlen(void *elts, int nelts, size_t elts_size);
261 FIRM_API 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 #include "../end.h"
267
268 #endif