libFirm
array.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1995-2011 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 
25 #ifndef FIRM_ADT_ARRAY_H
26 #define FIRM_ADT_ARRAY_H
27 
28 #include <assert.h>
29 #include <stddef.h>
30 
31 #include "obst.h"
32 
33 #include "../begin.h"
34 
53 #define NEW_ARR_F(type, nelts) \
54  ((type *)ir_new_arr_f((nelts), sizeof(type) * (nelts)))
55 
69 #define CLONE_ARR_F(type, arr) \
70  NEW_ARR_F(type, ARR_LEN((arr)))
71 
84 #define DUP_ARR_F(type, arr) \
85  ((type*) memcpy(CLONE_ARR_F(type, (arr)), (arr), sizeof(type) * ARR_LEN((arr))))
86 
92 #define DEL_ARR_F(arr) (ir_del_arr_f((void *)(arr)))
93 
107 #define NEW_ARR_D(type, obstack, nelts) \
108  ( nelts \
109  ? (type *)ir_new_arr_d((obstack), (nelts), sizeof(type) * (nelts)) \
110  : (type *)arr_mt_descr.elts)
111 
126 #define CLONE_ARR_D(type, obstack, arr) \
127  NEW_ARR_D(type, (obstack), ARR_LEN((arr)))
128 
142 #define DUP_ARR_D(type, obstack, arr) \
143  ((type*)memcpy(CLONE_ARR_D(type, (obstack), (arr)), (arr), sizeof(type) * ARR_LEN ((arr))))
144 
150 #define ARR_LEN(arr) (ARR_VRFY((arr)), ARR_DESCR((arr))->nelts)
151 
162 #define ARR_RESIZE(type, arr, n) \
163  ((arr) = (type*) ir_arr_resize((void *)(arr), (n), sizeof(type)))
164 
174 #define ARR_SETLEN(type, arr, n) \
175  ((arr) = (type*) ir_arr_setlen((void *)(arr), (n), sizeof(type) * (n)))
176 
186 #define ARR_EXTEND(type, arr, delta) \
187  ARR_RESIZE(type, (arr), ARR_LEN((arr)) + (delta))
188 
199 #define ARR_EXTO(type, arr, n) \
200  do { \
201  if ((n) >= ARR_LEN(arr)) { ARR_RESIZE(type, arr, (n)+1); } \
202  } while(0)
203 
211 #define ARR_APP1(type, arr, elt) \
212  (ARR_EXTEND(type, (arr), 1), (arr)[ARR_LEN((arr))-1] = (elt))
213 
214 #ifdef NDEBUG
215 # define ARR_VRFY(arr) ((void)0)
216 # define ARR_IDX_VRFY(arr, idx) ((void)0)
217 #else
218 
219 # define ARR_VRFY(arr) ir_verify_arr(arr)
220 
221 # define ARR_IDX_VRFY(arr, idx) \
222  assert((0 <= (idx)) && ((idx) < ARR_LEN((arr))))
223 #endif
224 
228 typedef union {
229  long double d;
230  void *p;
231  long l;
232 } aligned_type;
233 
237 typedef struct {
238  int magic;
239  size_t eltsize;
240  union {
241  struct obstack *obstack;
242  size_t allocated;
243  } u;
244  size_t nelts;
245  aligned_type elts[1];
246 } ir_arr_descr;
247 
248 extern ir_arr_descr arr_mt_descr;
249 
250 FIRM_API void *ir_new_arr_f(size_t nelts, size_t elts_size);
251 FIRM_API void ir_del_arr_f(void *elts);
252 FIRM_API void *ir_new_arr_d(struct obstack *obstack, size_t nelts, size_t elts_size);
253 FIRM_API void *ir_arr_resize(void *elts, size_t nelts, size_t elts_size);
254 FIRM_API void *ir_arr_setlen(void *elts, size_t nelts, size_t elts_size);
255 FIRM_API void ir_verify_arr(const void *elts);
256 
257 #define ARR_ELTS_OFFS offsetof(ir_arr_descr, elts)
258 #define ARR_DESCR(elts) ((ir_arr_descr *)(void *)((char *)(elts) - ARR_ELTS_OFFS))
259 
262 static inline void ARR_SHRINKLEN(void *arr, size_t new_len)
263 {
264  ARR_VRFY(arr);
265  assert(ARR_DESCR(arr)->nelts >= new_len);
266  ARR_DESCR(arr)->nelts = new_len;
267 }
268 
273 #include "../end.h"
274 
275 #endif