array.h 8.37 KB
Newer Older
Götz Lindenmaier's avatar
Götz Lindenmaier committed
1
/*
2
 * Copyright (C) 1995-2011 University of Karlsruhe.  All right reserved.
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 *
 * This file is part of libFirm.
 *
 * This file may be distributed and/or modified under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation and appearing in the file LICENSE.GPL included in the
 * packaging of this file.
 *
 * Licensees holding valid libFirm Professional Edition licenses may use
 * this file in accordance with the libFirm Commercial License.
 * Agreement provided with the Software.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.
Götz Lindenmaier's avatar
Götz Lindenmaier committed
18
 */
Boris Boesler's avatar
Boris Boesler committed
19

Michael Beck's avatar
Michael Beck committed
20
/**
21
22
 * @file
 * @brief     Dynamic and flexible arrays for C.
23
 * @author    Markus Armbruster, Michael Beck, Matthias Braun, Sebastian Hack
24
 * @version   $Id$
Michael Beck's avatar
Michael Beck committed
25
 */
26
27
#ifndef FIRM_ADT_ARRAY_H
#define FIRM_ADT_ARRAY_H
Christian Schäfer's avatar
Christian Schäfer committed
28
29
30

#include <assert.h>
#include <stddef.h>
31

32
#include "obst.h"
Michael Beck's avatar
Michael Beck committed
33
#include "fourcc.h"
34
#include "xmalloc.h"
Christian Schäfer's avatar
Christian Schäfer committed
35

36
37
#include "../begin.h"

Michael Beck's avatar
Michael Beck committed
38
39
40
41
42
43
44
45
46
47
48
49
/**
 * Creates a flexible array.
 *
 * @param type     The element type of the new array.
 * @param nelts    a size_t expression evaluating to the number of elements
 *
 * This macro creates a flexible array of a given type at runtime.
 * The size of the array can be changed later.
 *
 * @return A pointer to the flexible array (can be used as a pointer to the
 *         first element of this array).
 */
Christian Schäfer's avatar
Christian Schäfer committed
50
#define NEW_ARR_F(type, nelts)						\
51
  ((type *)ir_new_arr_f((nelts), sizeof(type) * (nelts)))
Michael Beck's avatar
Michael Beck committed
52
53

/**
54
 * Creates a new flexible array with the same number of elements as a
Michael Beck's avatar
Michael Beck committed
55
56
57
58
59
60
61
62
63
64
65
 * given one.
 *
 * @param type     The element type of the new array.
 * @param arr      An array from which the number of elements will be taken
 *
 * This macro creates a flexible array of a given type at runtime.
 * The size of the array can be changed later.
 *
 * @return A pointer to the flexible array (can be used as a pointer to the
 *         first element of this array).
 */
Christian Schäfer's avatar
Christian Schäfer committed
66
#define CLONE_ARR_F(type, arr)			\
67
  NEW_ARR_F(type, ARR_LEN((arr)))
Michael Beck's avatar
Michael Beck committed
68
69
70
71
72
73
74
75
76
77
78
79
80

/**
 * Duplicates an array and returns the new flexible one.
 *
 * @param type     The element type of the new array.
 * @param arr      An array from which the elements will be duplicated
 *
 * This macro creates a flexible array of a given type at runtime.
 * The size of the array can be changed later.
 *
 * @return A pointer to the flexible array (can be used as a pointer to the
 *         first element of this array).
 */
Christian Schäfer's avatar
Christian Schäfer committed
81
#define DUP_ARR_F(type, arr)							\
82
  ((type*) memcpy(CLONE_ARR_F(type, (arr)), (arr), sizeof(type) * ARR_LEN((arr))))
Michael Beck's avatar
Michael Beck committed
83
84
85
86
87
88

/**
 * Delete a flexible array.
 *
 * @param arr    The flexible array.
 */
89
#define DEL_ARR_F(arr) (ir_del_arr_f((void *)(arr)))
Christian Schäfer's avatar
Christian Schäfer committed
90

Michael Beck's avatar
Michael Beck committed
91
/**
Götz Lindenmaier's avatar
Götz Lindenmaier committed
92
 * Creates a dynamic array on an obstack.
Michael Beck's avatar
Michael Beck committed
93
94
 *
 * @param type     The element type of the new array.
Götz Lindenmaier's avatar
Götz Lindenmaier committed
95
96
 * @param obstack  A struct obstack * were the data will be allocated
 * @param nelts    A size_t expression evaluating to the number of elements
Michael Beck's avatar
Michael Beck committed
97
98
99
100
101
102
103
 *
 * This macro creates a dynamic array of a given type at runtime.
 * The size of the array cannot be changed later.
 *
 * @return A pointer to the dynamic array (can be used as a pointer to the
 *         first element of this array).
 */
Christian Schäfer's avatar
Christian Schäfer committed
104
105
#define NEW_ARR_D(type, obstack, nelts)					\
  (  nelts								\
106
   ? (type *)ir_new_arr_d((obstack), (nelts), sizeof(type) * (nelts))	\
107
   : (type *)arr_mt_descr.elts)
Michael Beck's avatar
Michael Beck committed
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122

/**
 * Creates a new dynamic array with the same number of elements as a
 * given one.
 *
 * @param type     The element type of the new array.
 * @param obstack  An struct obstack * were the data will be allocated
 * @param arr      An array from which the number of elements will be taken
 *
 * This macro creates a dynamic array of a given type at runtime.
 * The size of the array cannot be changed later.
 *
 * @return A pointer to the dynamic array (can be used as a pointer to the
 *         first element of this array).
 */
Christian Schäfer's avatar
Christian Schäfer committed
123
#define CLONE_ARR_D(type, obstack, arr)		\
124
  NEW_ARR_D(type, (obstack), ARR_LEN((arr)))
Michael Beck's avatar
Michael Beck committed
125
126
127
128
129
130
131
132
133
134
135
136
137
138

/**
 * Duplicates an array and returns the new dynamic one.
 *
 * @param type     The element type of the new array.
 * @param obstack  An struct obstack * were the data will be allocated
 * @param arr      An array from which the elements will be duplicated
 *
 * This macro creates a dynamic array of a given type at runtime.
 * The size of the array cannot be changed later.
 *
 * @return A pointer to the dynamic array (can be used as a pointer to the
 *         first element of this array).
 */
Christian Schäfer's avatar
Christian Schäfer committed
139
#define DUP_ARR_D(type, obstack, arr)							\
140
  ((type*)memcpy(CLONE_ARR_D(type, (obstack), (arr)), (arr), sizeof(type) * ARR_LEN ((arr))))
Christian Schäfer's avatar
Christian Schäfer committed
141

Michael Beck's avatar
Michael Beck committed
142
143
144
145
146
/**
 * Returns the length of an array
 *
 * @param arr  a flexible, dynamic, automatic or static array.
 */
147
#define ARR_LEN(arr) (ARR_VRFY((arr)), ARR_DESCR((arr))->nelts)
Christian Schäfer's avatar
Christian Schäfer committed
148

Michael Beck's avatar
Michael Beck committed
149
150
151
152
153
154
155
156
157
158
/**
 * Resize a flexible array, allocate more data if needed but do NOT
 * reduce.
 *
 * @param type     The element type of the array.
 * @param arr      The array, which must be an lvalue.
 * @param n        The new size of the array.
 *
 * @remark  This macro may change arr, so update all references!
 */
Christian Schäfer's avatar
Christian Schäfer committed
159
#define ARR_RESIZE(type, arr, n)					\
160
  ((arr) = (type*) ir_arr_resize((void *)(arr), (n), sizeof(type)))
Michael Beck's avatar
Michael Beck committed
161
162
163
164
165
166
167
168
169
170

/**
 * Resize a flexible array, always reallocate data.
 *
 * @param type     The element type of the array.
 * @param arr      The array, which must be an lvalue.
 * @param n        The new size of the array.
 *
 * @remark  This macro may change arr, so update all references!
 */
Christian Schäfer's avatar
Christian Schäfer committed
171
#define ARR_SETLEN(type, arr, n)					\
172
  ((arr) = (type*) ir_arr_setlen((void *)(arr), (n), sizeof(type) * (n)))
Michael Beck's avatar
Michael Beck committed
173

174
175
/** Set a length smaller than the current length of the array.  Do not
 *  resize. len must be <= ARR_LEN(arr). */
176
static inline void ARR_SHRINKLEN(void *arr, size_t new_len);
177

Michael Beck's avatar
Michael Beck committed
178
179
180
181
182
183
184
185
186
/**
 * Resize a flexible array by growing it by delta elements.
 *
 * @param type     The element type of the array.
 * @param arr      The array, which must be an lvalue.
 * @param delta    The delta number of elements.
 *
 * @remark  This macro may change arr, so update all references!
 */
Christian Schäfer's avatar
Christian Schäfer committed
187
#define ARR_EXTEND(type, arr, delta)			\
188
  ARR_RESIZE(type, (arr), ARR_LEN((arr)) + (delta))
Michael Beck's avatar
Michael Beck committed
189
190
191
192
193
194
195
196
197
198
199

/**
 * Resize a flexible array to hold n elements only if it is currently shorter
 * than n.
 *
 * @param type     The element type of the array.
 * @param arr      The array, which must be an lvalue.
 * @param n        The new size of the array.
 *
 * @remark  This macro may change arr, so update all references!
 */
Christian Schäfer's avatar
Christian Schäfer committed
200
#define ARR_EXTO(type, arr, n)						\
201
  ((n) >= ARR_LEN((arr)) ? ARR_RESIZE(type, (arr), (n)+1) : (arr))
Michael Beck's avatar
Michael Beck committed
202
203
204
205
206
207
208
209

/**
 * Append one element to a flexible array.
 *
 * @param type     The element type of the array.
 * @param arr      The array, which must be an lvalue.
 * @param elt      The new element, must be of type (type).
 */
Christian Schäfer's avatar
Christian Schäfer committed
210
#define ARR_APP1(type, arr, elt)					\
211
  (ARR_EXTEND(type, (arr), 1), (arr)[ARR_LEN((arr))-1] = (elt))
Christian Schäfer's avatar
Christian Schäfer committed
212
213

#ifdef NDEBUG
214
# define ARR_VRFY(arr)          ((void)0)
Christian Schäfer's avatar
Christian Schäfer committed
215
216
# define ARR_IDX_VRFY(arr, idx) ((void)0)
#else
217
# define ARR_VRFY(arr)          ir_verify_arr(arr)
Christian Schäfer's avatar
Christian Schäfer committed
218
# define ARR_IDX_VRFY(arr, idx)				\
219
    assert((0 <= (idx)) && ((idx) < ARR_LEN((arr))))
Christian Schäfer's avatar
Christian Schäfer committed
220
221
#endif

222
223
224
225
226
227
/** A type that has most constrained alignment.  */
typedef union {
  long double d;
  void *p;
  long l;
} aligned_type;
Christian Schäfer's avatar
Christian Schäfer committed
228
229


Michael Beck's avatar
Michael Beck committed
230
231
232
/**
 * The array descriptor header type.
 */
233
234
235
236
237
238
239
240
241
242
typedef struct {
	int magic;                    /**< array magic. */
	size_t eltsize;               /**< size of array elements. */
	union {
		struct obstack *obstack;  /**< for obstack array: the obstack. */
		size_t allocated;         /**< number of allocated elements. */
	} u;
	size_t nelts;                 /**< current length of the array. */
	aligned_type elts[1];         /**< start of the array data. */
} ir_arr_descr;
Christian Schäfer's avatar
Christian Schäfer committed
243

244
extern ir_arr_descr arr_mt_descr;
Christian Schäfer's avatar
Christian Schäfer committed
245

246
FIRM_API void *ir_new_arr_f(size_t nelts, size_t elts_size);
Michael Beck's avatar
Michael Beck committed
247
FIRM_API void ir_del_arr_f(void *elts);
248
249
250
FIRM_API void *ir_new_arr_d(struct obstack *obstack, size_t nelts, size_t elts_size);
FIRM_API void *ir_arr_resize(void *elts, size_t nelts, size_t elts_size);
FIRM_API void *ir_arr_setlen(void *elts, size_t nelts, size_t elts_size);
Michael Beck's avatar
Michael Beck committed
251
FIRM_API void ir_verify_arr(const void *elts);
Christian Schäfer's avatar
Christian Schäfer committed
252

253
#define ARR_ELTS_OFFS offsetof(ir_arr_descr, elts)
254
#define ARR_DESCR(elts) ((ir_arr_descr *)(void *)((char *)(elts) - ARR_ELTS_OFFS))
Christian Schäfer's avatar
Christian Schäfer committed
255

256
257
258
259
260
261
262
static inline void ARR_SHRINKLEN(void *arr, size_t new_len)
{
	ARR_VRFY(arr);
	assert(ARR_DESCR(arr)->nelts >= new_len);
	ARR_DESCR(arr)->nelts = new_len;
}

263
264
265
#include "../end.h"

#endif