/usr/include/dovecot/array.h is in dovecot-dev 1:2.2.9-1ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | #ifndef ARRAY_H
#define ARRAY_H
/* Array is a buffer accessible using fixed size elements. As long as the
compiler provides typeof() function, 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().
Example:
ARRAY_DEFINE_TYPE(foo, struct foo);
void do_foo(ARRAY_TYPE(foo) *bars) {
struct foo *foo = array_idx(bars, 0);
}
*/
#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 __GNUC__
# 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)
#else
# define ARRAY_TYPE_CAST_CONST(array)
# define ARRAY_TYPE_CAST_MODIFIABLE(array)
# define ARRAY_TYPE_CHECK(array, data) 0
#endif
/* usage: struct foo *foo; array_foreach(foo_arr, foo) { .. } */
#if (defined(__STDC__) && __STDC_VERSION__ >= 199901L)
# 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++)
#else
# define array_foreach(array, elem) \
for (elem = *(array)->v; \
elem != CONST_PTR_OFFSET(*(array)->v, (array)->arr.buffer->used); \
(elem)++)
# define array_foreach_modifiable(array, elem) \
for (elem = ARRAY_TYPE_CAST_MODIFIABLE(array) \
buffer_get_modifiable_data((array)->arr.buffer, NULL); \
elem != CONST_PTR_OFFSET(*(array)->v, (array)->arr.buffer->used); \
(elem)++)
#endif
#define array_foreach_idx(array, elem) \
((elem) - (array)->v[0])
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(pool, init_count * 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 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)
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) \
array_append_i(&(array)->arr + ARRAY_TYPE_CHECK(array, data), \
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_t)-1);
}
#define array_append_array(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) \
array_insert_i(&(array)->arr + ARRAY_TYPE_CHECK(array, data), \
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)
static inline const void * ATTR_PURE
array_idx_i(const struct array *array, unsigned int idx)
{
i_assert(idx * array->element_size < array->buffer->used);
return CONST_PTR_OFFSET(array->buffer->data, idx * array->element_size);
}
#define array_idx(array, idx) \
ARRAY_TYPE_CAST_CONST(array)array_idx_i(&(array)->arr, idx)
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(struct array *array, unsigned int idx);
#define array_idx_modifiable(array, idx) \
ARRAY_TYPE_CAST_MODIFIABLE(array) \
array_idx_modifiable_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) \
array_idx_set_i(&(array)->arr + ARRAY_TYPE_CHECK(array, data), \
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)
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) \
array_sort_i(&(array)->arr + \
CALLBACK_TYPECHECK(cmp, int (*)(typeof(*(array)->v), \
typeof(*(array)->v))), \
(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) \
ARRAY_TYPE_CAST_MODIFIABLE(array)array_bsearch_i(&(array)->arr + \
CALLBACK_TYPECHECK(cmp, int (*)(typeof(const typeof(*key) *), \
typeof(*(array)->v))), \
(const void *)key, (int (*)(const void *, const void *))cmp)
#endif
|