libFirm
array.h
1 /*
2  * This file is part of libFirm.
3  * Copyright (C) 2012 University of Karlsruhe.
4  */
5 
11 #ifndef FIRM_ADT_ARRAY_H
12 #define FIRM_ADT_ARRAY_H
13 
14 #include <assert.h>
15 #include <stddef.h>
16 
17 #include "obst.h"
18 
19 #include "../begin.h"
20 
30 typedef union {
31  long double d;
32  void *p;
33  long l;
34 } aligned_type;
35 
39 typedef struct {
40  int magic;
41  size_t allocated;
42  size_t nelts;
43  aligned_type elts[];
44 } ir_arr_descr;
45 
46 extern ir_arr_descr arr_mt_descr;
47 
59 FIRM_API void *ir_new_arr_f(size_t nelts, size_t elts_size);
60 
73 FIRM_API void *ir_new_arr_d(struct obstack *obstack, size_t nelts, size_t elts_size);
74 
88 FIRM_API void *ir_arr_resize(void *elts, size_t nelts, size_t elts_size);
89 
102 FIRM_API void *ir_arr_setlen(void *elts, size_t nelts, size_t elts_size);
103 
104 FIRM_API void ir_verify_arr(const void *elts);
105 
106 #define ARR_ELTS_OFFS offsetof(ir_arr_descr, elts)
107 #define ARR_DESCR(elts) ((ir_arr_descr *)(void *)((char *)(elts) - ARR_ELTS_OFFS))
108 
111 static inline void ARR_SHRINKLEN(void *arr, size_t new_len)
112 {
113 #ifndef NDEBUG
114  ir_verify_arr(arr);
115 #endif
116  assert(ARR_DESCR(arr)->nelts >= new_len);
117  ARR_DESCR(arr)->nelts = new_len;
118 }
119 
134 #define NEW_ARR_F(type, nelts) \
135  ((type *)ir_new_arr_f((nelts), sizeof(type) * (nelts)))
136 
140 #define NEW_ARR_FZ(type, nelts) \
141  ((type*)memset(NEW_ARR_F(type, (nelts)), 0, sizeof(type) * (nelts)))
142 
155 #define DUP_ARR_F(type, arr) \
156  ((type*)memcpy(NEW_ARR_F(type, ARR_LEN((arr))), (arr), sizeof(type) * ARR_LEN((arr))))
157 
162 FIRM_API void DEL_ARR_F(void *arr);
163 
177 #define NEW_ARR_D(type, obstack, nelts) \
178  ( nelts \
179  ? (type *)ir_new_arr_d((obstack), (nelts), sizeof(type) * (nelts)) \
180  : (type *)arr_mt_descr.elts)
181 
185 #define NEW_ARR_DZ(type, obstack, nelts) \
186  ((type*)memset(NEW_ARR_D(type, (obstack), (nelts)), 0, sizeof(type) * (nelts)))
187 
201 #define DUP_ARR_D(type, obstack, arr) \
202  ((type*)memcpy(NEW_ARR_D(type, (obstack), ARR_LEN((arr))), (arr), sizeof(type) * ARR_LEN ((arr))))
203 
209 static inline size_t ARR_LEN(void const *const arr)
210 {
211 #ifndef NDEBUG
212  ir_verify_arr(arr);
213 #endif
214  return ARR_DESCR(arr)->nelts;
215 }
216 
227 #define ARR_RESIZE(type, arr, n) \
228  ((arr) = (type*) ir_arr_resize((void *)(arr), (n), sizeof(type)))
229 
239 #define ARR_SETLEN(type, arr, n) \
240  ((arr) = (type*) ir_arr_setlen((void *)(arr), (n), sizeof(type) * (n)))
241 
251 #define ARR_EXTEND(type, arr, delta) \
252  ARR_RESIZE(type, (arr), ARR_LEN((arr)) + (delta))
253 
264 #define ARR_EXTO(type, arr, n) \
265  do { \
266  if ((n) >= ARR_LEN(arr)) { ARR_RESIZE(type, arr, (n)+1); } \
267  } while(0)
268 
276 #define ARR_APP1(type, arr, elt) \
277  (ARR_EXTEND(type, (arr), 1), (arr)[ARR_LEN((arr))-1] = (elt))
278 
281 #include "../end.h"
282 
283 #endif
static size_t ARR_LEN(void const *const arr)
Returns the length of an array.
Definition: array.h:209
void DEL_ARR_F(void *arr)
Delete a flexible array.