1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
|
#ifndef ARRAY_H
#define ARRAY_H
/* Array is a buffer accessible using fixed size elements. As long as the
compiler provides a typeof() operator, the array provides type safety. If
a wrong type is tried to be added to the array, or if the array's contents
are tried to be used using a wrong type, the compiler will give a warning.
Example usage:
struct foo {
ARRAY(struct bar) bars;
...
};
i_array_init(&foo->bars, 10);
struct bar *bar = array_idx(&foo->bars, 5);
struct baz *baz = array_idx(&foo->bars, 5); // compiler warning
If you want to pass an array as a parameter to a function, you'll need to
create a type for the array using ARRAY_DEFINE_TYPE() and use the type in
the parameter using ARRAY_TYPE(). Any arrays that you want to be passing
around, such as structure members as in the above example, must also be
defined using ARRAY_TYPE() too, rather than ARRAY().
Example:
ARRAY_DEFINE_TYPE(foo, struct foo);
void do_foo(ARRAY_TYPE(foo) *foos) {
struct foo *foo = array_idx(foos, 0);
}
struct foo_manager {
ARRAY_TYPE(foo) foos; // pedantically, ARRAY(struct foo) is a different type
};
// ...
do_foo(&my_foo_manager->foos); // No compiler warning about mismatched types
*/
#include "array-decl.h"
#include "buffer.h"
#define p_array_init(array, pool, init_count) \
array_create(array, pool, sizeof(**(array)->v), init_count)
#define i_array_init(array, init_count) \
p_array_init(array, default_pool, init_count)
#define t_array_init(array, init_count) \
p_array_init(array, pool_datastack_create(), init_count)
#ifdef HAVE_TYPEOF
# define ARRAY_TYPE_CAST_CONST(array) \
(typeof(*(array)->v))
# define ARRAY_TYPE_CAST_MODIFIABLE(array) \
(typeof(*(array)->v_modifiable))
# define ARRAY_TYPE_CHECK(array, data) \
COMPILE_ERROR_IF_TYPES_NOT_COMPATIBLE( \
**(array)->v_modifiable, *(data))
# define ARRAY_TYPES_CHECK(array1, array2) \
COMPILE_ERROR_IF_TYPES_NOT_COMPATIBLE( \
**(array1)->v_modifiable, **(array2)->v_modifiable)
#else
# define ARRAY_TYPE_CAST_CONST(array)
# define ARRAY_TYPE_CAST_MODIFIABLE(array)
# define ARRAY_TYPE_CHECK(array, data) 0
# define ARRAY_TYPES_CHECK(array1, array2) 0
#endif
/* Usage:
ARRAY(struct foo) foo_arr;
struct foo *foo;
array_foreach(&foo_arr, foo) {
..
}
Note that deleting an element while iterating will cause the iteration to
skip over the next element. So deleting a single element and breaking out
of the loop is fine, but continuing the loop is likely a bug. Use
array_foreach_reverse() instead when deleting multiple elements.
*/
#define array_foreach(array, elem) \
for (const void *elem ## __foreach_end = \
(const char *)(elem = *(array)->v) + (array)->arr.buffer->used; \
elem != elem ## __foreach_end; (elem)++)
#define array_foreach_modifiable(array, elem) \
for (const void *elem ## _end = \
(const char *)(elem = ARRAY_TYPE_CAST_MODIFIABLE(array) \
buffer_get_modifiable_data((array)->arr.buffer, NULL)) + \
(array)->arr.buffer->used; \
elem != elem ## _end; (elem)++)
/* Iterate the array in reverse order. */
#define array_foreach_reverse(array, elem) \
for (elem = CONST_PTR_OFFSET(*(array)->v, (array)->arr.buffer->used); \
(const char *)(elem--) > (const char *)*(array)->v; )
#define array_foreach_reverse_modifiable(array, elem) \
for (elem = ARRAY_TYPE_CAST_MODIFIABLE(array) \
((char *)buffer_get_modifiable_data((array)->arr.buffer, NULL) + \
(array)->arr.buffer->used); \
(const char *)(elem--) > (const char *)*(array)->v; )
/* Usage:
ARRAY(struct foo *) foo_ptrs_arr;
struct foo *foo;
array_foreach_elem(&foo_ptrs_arr, foo) {
..
} */
#define array_foreach_elem(array, elem) \
for (const void *_foreach_end = \
CONST_PTR_OFFSET(*(array)->v, (array)->arr.buffer->used), \
*_foreach_ptr = CONST_PTR_OFFSET(*(array)->v, ARRAY_TYPE_CHECK(array, &elem) + \
COMPILE_ERROR_IF_TRUE(sizeof(elem) > sizeof(void *))) \
; \
(_foreach_ptr != _foreach_end && \
(memcpy(&elem, _foreach_ptr, sizeof(elem)), TRUE)) \
; \
_foreach_ptr = CONST_PTR_OFFSET(_foreach_ptr, sizeof(elem)))
#define array_ptr_to_idx(array, elem) \
((elem) - (array)->v[0])
/* Return index of iterated element inside array_foreach() or
array_foreach_modifiable() loop. Note that this doesn't work inside
array_foreach_elem() loop. */
#define array_foreach_idx(array, elem) \
array_ptr_to_idx(array, elem)
static inline void
array_create_from_buffer_i(struct array *array, buffer_t *buffer,
size_t element_size)
{
array->buffer = buffer;
array->element_size = element_size;
}
#define array_create_from_buffer(array, buffer, element_size) \
array_create_from_buffer_i(&(array)->arr, buffer, element_size)
static inline void
array_create_i(struct array *array, pool_t pool,
size_t element_size, unsigned int init_count)
{
buffer_t *buffer;
buffer = buffer_create_dynamic_max(pool, init_count * element_size,
SIZE_MAX / element_size < UINT_MAX ? SIZE_MAX :
UINT_MAX * element_size);
array_create_from_buffer_i(array, buffer, element_size);
}
#define array_create(array, pool, element_size, init_count) \
array_create_i(&(array)->arr, pool, element_size, init_count)
static inline void
array_free_i(struct array *array)
{
buffer_free(&array->buffer);
}
#define array_free(array) \
array_free_i(&(array)->arr)
static inline void * ATTR_WARN_UNUSED_RESULT
array_free_without_data_i(struct array *array)
{
return buffer_free_without_data(&array->buffer);
}
#define array_free_without_data(array) \
ARRAY_TYPE_CAST_MODIFIABLE(array)array_free_without_data_i(&(array)->arr)
static inline bool
array_is_created_i(const struct array *array)
{
return array->buffer != NULL;
}
#define array_is_created(array) \
array_is_created_i(&(array)->arr)
static inline pool_t ATTR_PURE
array_get_pool_i(struct array *array)
{
return buffer_get_pool(array->buffer);
}
#define array_get_pool(array) \
array_get_pool_i(&(array)->arr)
static inline void
array_clear_i(struct array *array)
{
buffer_set_used_size(array->buffer, 0);
}
#define array_clear(array) \
array_clear_i(&(array)->arr)
static inline unsigned int ATTR_PURE
array_count_i(const struct array *array)
{
return array->buffer->used / array->element_size;
}
#define array_count(array) \
array_count_i(&(array)->arr)
/* No need for the real count if all we're doing is comparing against 0 */
#define array_is_empty(array) \
((array)->arr.buffer->used == 0)
#define array_not_empty(array) \
((array)->arr.buffer->used > 0)
static inline void
array_append_i(struct array *array, const void *data, unsigned int count)
{
buffer_append(array->buffer, data, count * array->element_size);
}
#define array_append(array, data, count) \
TYPE_CHECKS(void, ARRAY_TYPE_CHECK(array, data), \
array_append_i(&(array)->arr, data, count))
static inline void
array_append_array_i(struct array *dest_array, const struct array *src_array)
{
i_assert(dest_array->element_size == src_array->element_size);
buffer_append_buf(dest_array->buffer, src_array->buffer, 0, SIZE_MAX);
}
#define array_append_array(dest_array, src_array) \
TYPE_CHECKS(void, ARRAY_TYPES_CHECK(dest_array, src_array), \
array_append_array_i(&(dest_array)->arr, &(src_array)->arr))
static inline void
array_insert_i(struct array *array, unsigned int idx,
const void *data, unsigned int count)
{
buffer_insert(array->buffer, idx * array->element_size,
data, count * array->element_size);
}
#define array_insert(array, idx, data, count) \
TYPE_CHECKS(void, ARRAY_TYPE_CHECK(array, data), \
array_insert_i(&(array)->arr, idx, data, count))
static inline void
array_delete_i(struct array *array, unsigned int idx, unsigned int count)
{
buffer_delete(array->buffer, idx * array->element_size,
count * array->element_size);
}
#define array_delete(array, idx, count) \
array_delete_i(&(array)->arr, idx, count)
static inline const void *
array_get_i(const struct array *array, unsigned int *count_r)
{
*count_r = array_count_i(array);
return array->buffer->data;
}
#define array_get(array, count) \
ARRAY_TYPE_CAST_CONST(array)array_get_i(&(array)->arr, count)
/* Re: i_assert() vs. pure: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=51971#c1 */
static inline const void * ATTR_PURE
array_idx_i(const struct array *array, unsigned int idx)
{
i_assert(idx < array->buffer->used / array->element_size);
return CONST_PTR_OFFSET(array->buffer->data, idx * array->element_size);
}
#define array_front(array) array_idx(array, 0)
#define array_front_modifiable(array) array_idx_modifiable(array, 0)
#define array_back(array) array_idx(array, array_count(array)-1)
#define array_back_modifiable(array) array_idx_modifiable(array, array_count(array)-1)
#define array_pop_back(array) array_delete(array, array_count(array)-1, 1);
#define array_push_back(array, item) array_append(array, (item), 1)
#define array_pop_front(array) array_delete(array, 0, 1)
#define array_push_front(array, item) array_insert(array, 0, (item), 1)
#define array_idx(array, idx) \
ARRAY_TYPE_CAST_CONST(array)array_idx_i(&(array)->arr, idx)
/* Using *array_idx() will fail if the compiler doesn't support typeof().
The same can be done with array_idx_elem() for arrays that have pointers. */
#ifdef HAVE_TYPEOF
# define array_idx_elem(array, idx) \
(TRUE ? *array_idx(array, idx) : \
COMPILE_ERROR_IF_TRUE(sizeof(**(array)->v) != sizeof(void *)))
#else
# define array_idx_elem(array, idx) \
(*(void **)array_idx_i(&(array)->arr, idx))
#endif
static inline void *
array_get_modifiable_i(struct array *array, unsigned int *count_r)
{
*count_r = array_count_i(array);
return buffer_get_modifiable_data(array->buffer, NULL);
}
#define array_get_modifiable(array, count) \
ARRAY_TYPE_CAST_MODIFIABLE(array) \
array_get_modifiable_i(&(array)->arr, count)
void *
array_idx_modifiable_i(const struct array *array, unsigned int idx) ATTR_PURE;
#define array_idx_modifiable(array, idx) \
ARRAY_TYPE_CAST_MODIFIABLE(array) \
array_idx_modifiable_i(&(array)->arr, idx)
void *array_idx_get_space_i(struct array *array, unsigned int idx);
#define array_idx_get_space(array, idx) \
ARRAY_TYPE_CAST_MODIFIABLE(array) \
array_idx_get_space_i(&(array)->arr, idx)
void array_idx_set_i(struct array *array, unsigned int idx, const void *data);
#define array_idx_set(array, idx, data) \
TYPE_CHECKS(void, ARRAY_TYPE_CHECK(array, data), \
array_idx_set_i(&(array)->arr, idx, data))
void array_idx_clear_i(struct array *array, unsigned int idx);
#define array_idx_clear(array, idx) \
array_idx_clear_i(&(array)->arr, idx)
static inline void *
array_append_space_i(struct array *array)
{
void *data;
data = buffer_append_space_unsafe(array->buffer, array->element_size);
memset(data, 0, array->element_size);
return data;
}
#define array_append_space(array) \
ARRAY_TYPE_CAST_MODIFIABLE(array)array_append_space_i(&(array)->arr)
#define array_append_zero(array) \
(void)array_append_space_i(&(array)->arr)
void *array_insert_space_i(struct array *array, unsigned int idx);
#define array_insert_space(array, idx) \
ARRAY_TYPE_CAST_MODIFIABLE(array) \
array_insert_space_i(&(array)->arr, idx)
static inline void
array_copy(struct array *dest, unsigned int dest_idx,
const struct array *src, unsigned int src_idx, unsigned int count)
{
i_assert(dest->element_size == src->element_size);
buffer_copy(dest->buffer, dest_idx * dest->element_size,
src->buffer, src_idx * src->element_size,
count * dest->element_size);
}
bool array_cmp_i(const struct array *array1,
const struct array *array2) ATTR_PURE;
#define array_cmp(array1, array2) \
array_cmp_i(&(array1)->arr, &(array2)->arr)
/* Test equality via a comparator */
bool array_equal_fn_i(const struct array *array1,
const struct array *array2,
int (*cmp)(const void*, const void *)) ATTR_PURE;
#define array_equal_fn(array1, array2, cmp) \
TYPE_CHECKS(bool, \
ARRAY_TYPES_CHECK(array1, array2) || \
CALLBACK_TYPECHECK(cmp, int (*)(typeof(*(array1)->v), \
typeof(*(array2)->v))), \
array_equal_fn_i(&(array1)->arr, &(array2)->arr, \
(int (*)(const void *, const void *))cmp))
bool array_equal_fn_ctx_i(const struct array *array1,
const struct array *array2,
int (*cmp)(const void*, const void *, const void *),
const void *context) ATTR_PURE;
/* Same, but with a context pointer.
context can't be void* as ``const typeof(context)'' won't compile,
so ``const typeof(*context)*'' is required instead, and that requires a
complete type. */
#define array_equal_fn_ctx(array1, array2, cmp, ctx) \
TYPE_CHECKS(bool, \
ARRAY_TYPES_CHECK(array1, array2) || \
CALLBACK_TYPECHECK(cmp, int (*)(typeof(*(array1)->v), \
typeof(*(array2)->v), \
const typeof(*ctx)*)), \
array_equal_fn_ctx_i(&(array1)->arr, &(array2)->arr, \
(int (*)(const void *, const void *, const void *))cmp, ctx))
void array_reverse_i(struct array *array);
#define array_reverse(array) \
array_reverse_i(&(array)->arr)
void array_sort_i(struct array *array, int (*cmp)(const void *, const void *));
#define array_sort(array, cmp) \
TYPE_CHECKS(void, \
CALLBACK_TYPECHECK(cmp, int (*)(typeof(*(array)->v), \
typeof(*(array)->v))), \
array_sort_i(&(array)->arr, (int (*)(const void *, const void *))cmp))
void *array_bsearch_i(struct array *array, const void *key,
int (*cmp)(const void *, const void *));
#define array_bsearch(array, key, cmp) \
TYPE_CHECKS(void *, \
CALLBACK_TYPECHECK(cmp, int (*)(typeof(const typeof(*key) *), \
typeof(*(array)->v))), \
ARRAY_TYPE_CAST_MODIFIABLE(array)array_bsearch_i(&(array)->arr, \
(const void *)key, (int (*)(const void *, const void *))cmp))
/* Returns pointer to first element for which cmp(key,elem)==0, or NULL */
const void *array_lsearch_i(const struct array *array, const void *key,
int (*cmp)(const void *, const void *));
static inline void *array_lsearch_modifiable_i(struct array *array, const void *key,
int (*cmp)(const void *, const void *))
{
return (void *)array_lsearch_i(array, key, cmp);
}
#define ARRAY_LSEARCH_CALL(modifiable, array, key, cmp) \
TYPE_CHECKS(void *, \
CALLBACK_TYPECHECK(cmp, int (*)(typeof(const typeof(*key) *), \
typeof(*(array)->v))), \
array_lsearch##modifiable##i( \
&(array)->arr, (const void *)key, \
(int (*)(const void *, const void *))cmp))
#define array_lsearch(array, key, cmp) \
ARRAY_TYPE_CAST_CONST(array)ARRAY_LSEARCH_CALL(_, array, key, cmp)
#define array_lsearch_modifiable(array, key, cmp) \
ARRAY_TYPE_CAST_MODIFIABLE(array)ARRAY_LSEARCH_CALL(_modifiable_, array, key, cmp)
#endif
|