30#ifndef MUTT_MUTT_ARRAY_H
31#define MUTT_MUTT_ARRAY_H
40#define ARRAY_HEADROOM 25
47#define ARRAY_HEAD(name, type) \
58#define ARRAY_HEAD_INITIALIZER \
65#define ARRAY_INIT(head) \
66 memset((head), 0, sizeof(*(head)))
74#define ARRAY_EMPTY(head) \
87#define ARRAY_SIZE(head) \
95#define ARRAY_CAPACITY(head) \
109#define ARRAY_GET(head, idx) \
110 ((head)->size > (idx) ? &(head)->entries[(idx)] : NULL)
123#define ARRAY_SET(head, idx, elem) \
124 (((head)->capacity > (idx) \
126 : ARRAY_RESERVE((head), (idx) + 1)), \
127 ARRAY_SET_NORESERVE((head), (idx), (elem)))
135#define ARRAY_FIRST(head) \
144#define ARRAY_LAST(head) \
145 (ARRAY_EMPTY((head)) \
147 : ARRAY_GET((head), ARRAY_SIZE((head)) - 1))
156#define ARRAY_ADD(head, elem) \
157 (((head)->capacity > (head)->size \
159 : ARRAY_RESERVE((head), (head)->size + 1)), \
160 ARRAY_ADD_NORESERVE((head), (elem)))
172#define ARRAY_SHRINK(head, num) \
173 ((head)->size -= MIN((num), (head)->size))
180#define ARRAY_ELEM_SIZE(head) \
181 (sizeof(*(head)->entries))
189#define ARRAY_RESERVE(head, num) \
190 (((head)->capacity > (num)) ? \
192 ((mutt_mem_reallocarray( \
193 &(head)->entries, (num) + ARRAY_HEADROOM, ARRAY_ELEM_SIZE(head))), \
194 (memset((head)->entries + (head)->capacity, 0, \
195 ((num) + ARRAY_HEADROOM - (head)->capacity) * \
196 ARRAY_ELEM_SIZE(head))), \
197 ((head)->capacity = (num) + ARRAY_HEADROOM)))
204#define ARRAY_FREE(head) \
205 (FREE(&(head)->entries), (head)->size = (head)->capacity = 0)
212#define ARRAY_FOREACH(elem, head) \
213 ARRAY_FOREACH_FROM_TO((elem), (head), 0, (head)->size)
223#define ARRAY_FOREACH_FROM(elem, head, from) \
224 ARRAY_FOREACH_FROM_TO((elem), (head), (from), (head)->size)
234#define ARRAY_FOREACH_TO(elem, head, to) \
235 ARRAY_FOREACH_FROM_TO((elem), (head), 0, (to))
247#define ARRAY_FOREACH_FROM_TO(elem, head, from, to) \
248 for (size_t ARRAY_FOREACH_IDX = (from); \
249 (ARRAY_FOREACH_IDX < (to)) && \
250 ((elem) = ARRAY_GET((head), ARRAY_FOREACH_IDX)); \
259#define ARRAY_IDX(head, elem) \
260 (elem - (head)->entries)
267#define ARRAY_REMOVE(head, elem) \
268 (memmove((elem), (elem) + 1, \
269 ARRAY_ELEM_SIZE((head)) * \
270 MAX(0, (ARRAY_SIZE((head)) - ARRAY_IDX((head), (elem)) - 1))), \
271 ARRAY_SHRINK((head), 1))
279#define ARRAY_SORT(head, fn, sdata) \
280 ((head)->entries && \
281 (mutt_qsort_r((head)->entries, ARRAY_SIZE(head), ARRAY_ELEM_SIZE(head), (fn), (sdata)), true))
286#define ARRAY_SET_NORESERVE(head, idx, elem) \
287 ((head)->capacity > (idx) \
288 ? (((head)->size = MAX((head)->size, ((idx) + 1))), \
289 ((head)->entries[(idx)] = (elem)), \
294#define __coverity_escape__(x) 0
296#define ARRAY_ADD_NORESERVE(head, elem) \
297 ((head)->capacity > (head)->size \
298 ? (((head)->entries[(head)->size++] = (elem)), \
299 ((void)__coverity_escape__(elem), true)) \
300 : ((void)__coverity_escape__(elem), false))
Memory management wrappers.