array.h 6.99 KB
Newer Older
Götz Lindenmaier's avatar
Götz Lindenmaier committed
1
/*
2
 * This file is part of libFirm.
3
 * Copyright (C) 2012 University of Karlsruhe.
Götz Lindenmaier's avatar
Götz Lindenmaier committed
4
 */
Boris Boesler's avatar
Boris Boesler committed
5

Michael Beck's avatar
Michael Beck committed
6
/**
7
8
 * @file
 * @brief     Dynamic and flexible arrays for C.
9
 * @author    Markus Armbruster, Michael Beck, Matthias Braun, Sebastian Hack
Michael Beck's avatar
Michael Beck committed
10
 */
11
12
#ifndef FIRM_ADT_ARRAY_H
#define FIRM_ADT_ARRAY_H
Christian Schäfer's avatar
Christian Schäfer committed
13
14
15

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

17
#include "obst.h"
Christian Schäfer's avatar
Christian Schäfer committed
18

19
20
#include "../begin.h"

21
22
23
24
25
26
/**
 * @ingroup adt
 * @defgroup array Arrays
 * @{
 */

Michael Beck's avatar
Michael Beck committed
27
28
29
30
31
32
33
34
35
36
37
38
/**
 * 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).
 */
39
#define NEW_ARR_F(type, nelts) \
40
  ((type *)ir_new_arr_f((nelts), sizeof(type) * (nelts)))
Michael Beck's avatar
Michael Beck committed
41

42
43
44
45
46
47
/**
 * Create a flexible array and null its contents.
 */
#define NEW_ARR_FZ(type, nelts) \
	((type*)memset(NEW_ARR_F(type, (nelts)), 0, sizeof(type) * (nelts)))

Michael Beck's avatar
Michael Beck committed
48
49
50
51
52
53
54
55
56
57
58
59
/**
 * 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).
 */
60
#define DUP_ARR_F(type, arr) \
61
  ((type*)memcpy(NEW_ARR_F(type, ARR_LEN((arr))), (arr), sizeof(type) * ARR_LEN((arr))))
Michael Beck's avatar
Michael Beck committed
62
63
64
65
66
67

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

Michael Beck's avatar
Michael Beck committed
70
/**
Götz Lindenmaier's avatar
Götz Lindenmaier committed
71
 * Creates a dynamic array on an obstack.
Michael Beck's avatar
Michael Beck committed
72
73
 *
 * @param type     The element type of the new array.
Götz Lindenmaier's avatar
Götz Lindenmaier committed
74
75
 * @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
76
77
78
79
80
81
82
 *
 * 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).
 */
83
84
85
#define NEW_ARR_D(type, obstack, nelts)                                 \
  (  nelts                                                              \
   ? (type *)ir_new_arr_d((obstack), (nelts), sizeof(type) * (nelts))   \
86
   : (type *)arr_mt_descr.elts)
Michael Beck's avatar
Michael Beck committed
87

88
89
90
91
92
93
/**
 * Create a dynamic array on an obstack and null its contents.
 */
#define NEW_ARR_DZ(type, obstack, nelts) \
	((type*)memset(NEW_ARR_D(type, (obstack), (nelts)), 0, sizeof(type) * (nelts)))

Michael Beck's avatar
Michael Beck committed
94
95
96
97
98
99
100
101
102
103
104
105
106
/**
 * 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).
 */
107
#define DUP_ARR_D(type, obstack, arr) \
108
  ((type*)memcpy(NEW_ARR_D(type, (obstack), ARR_LEN((arr))), (arr), sizeof(type) * ARR_LEN ((arr))))
Christian Schäfer's avatar
Christian Schäfer committed
109

Michael Beck's avatar
Michael Beck committed
110
111
112
113
114
/**
 * Returns the length of an array
 *
 * @param arr  a flexible, dynamic, automatic or static array.
 */
115
#define ARR_LEN(arr) (ARR_VRFY((arr)), ARR_DESCR((arr))->nelts)
Christian Schäfer's avatar
Christian Schäfer committed
116

Michael Beck's avatar
Michael Beck committed
117
118
119
120
121
122
123
124
125
126
/**
 * 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!
 */
127
#define ARR_RESIZE(type, arr, n) \
128
  ((arr) = (type*) ir_arr_resize((void *)(arr), (n), sizeof(type)))
Michael Beck's avatar
Michael Beck committed
129
130
131
132
133
134
135
136
137
138

/**
 * 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!
 */
139
#define ARR_SETLEN(type, arr, n) \
140
  ((arr) = (type*) ir_arr_setlen((void *)(arr), (n), sizeof(type) * (n)))
Michael Beck's avatar
Michael Beck committed
141
142
143
144
145
146
147
148
149
150

/**
 * 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!
 */
151
#define ARR_EXTEND(type, arr, delta) \
152
  ARR_RESIZE(type, (arr), ARR_LEN((arr)) + (delta))
Michael Beck's avatar
Michael Beck committed
153
154
155
156
157
158
159
160
161
162
163

/**
 * 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!
 */
164
#define ARR_EXTO(type, arr, n) \
165
166
167
	do { \
		if ((n) >= ARR_LEN(arr)) { ARR_RESIZE(type, arr, (n)+1); } \
	} while(0)
Michael Beck's avatar
Michael Beck committed
168
169
170
171
172
173
174
175

/**
 * 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).
 */
176
#define ARR_APP1(type, arr, elt) \
177
  (ARR_EXTEND(type, (arr), 1), (arr)[ARR_LEN((arr))-1] = (elt))
Christian Schäfer's avatar
Christian Schäfer committed
178
179

#ifdef NDEBUG
180
# define ARR_VRFY(arr)          ((void)0)
Christian Schäfer's avatar
Christian Schäfer committed
181
182
# define ARR_IDX_VRFY(arr, idx) ((void)0)
#else
183
/** Check array for consistency */
184
# define ARR_VRFY(arr)          ir_verify_arr(arr)
185
/** Check if index is within array bounds */
186
# define ARR_IDX_VRFY(arr, idx) \
187
    assert((0 <= (idx)) && ((idx) < ARR_LEN((arr))))
Christian Schäfer's avatar
Christian Schäfer committed
188
189
#endif

190
191
/** @cond PRIVATE */

192
193
194
195
196
197
/** 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
198

Michael Beck's avatar
Michael Beck committed
199
200
201
/**
 * The array descriptor header type.
 */
202
203
typedef struct {
	int magic;                    /**< array magic. */
204
	size_t allocated;         /**< number of allocated elements. */
205
206
207
	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
208

209
extern ir_arr_descr arr_mt_descr;
Christian Schäfer's avatar
Christian Schäfer committed
210

211
FIRM_API void *ir_new_arr_f(size_t nelts, size_t elts_size);
Michael Beck's avatar
Michael Beck committed
212
FIRM_API void ir_del_arr_f(void *elts);
213
214
215
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
216
FIRM_API void ir_verify_arr(const void *elts);
Christian Schäfer's avatar
Christian Schäfer committed
217

218
#define ARR_ELTS_OFFS offsetof(ir_arr_descr, elts)
219
#define ARR_DESCR(elts) ((ir_arr_descr *)(void *)((char *)(elts) - ARR_ELTS_OFFS))
Christian Schäfer's avatar
Christian Schäfer committed
220

Matthias Braun's avatar
Matthias Braun committed
221
222
/** Set a length smaller than the current length of the array.  Do not
 *  resize. len must be <= ARR_LEN(arr). */
223
224
225
226
227
228
229
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;
}

230
231
232
233
/** @endcond */

/** @} */

234
235
236
#include "../end.h"

#endif