22#define DEFINE_LIST(name, type) DEFINE_LIST_WITH_DTOR(name, type, name##_noop_)
30#define DEFINE_LIST_WITH_DTOR(name, type, dtor) \
48 static inline UNUSED void name##_noop_(type item) { (void)item; } \
51 static inline size_t name##_size(const name##_t *list) { \
52 assert(list != NULL); \
57 static inline UNUSED bool name##_is_empty(const name##_t *list) { \
58 assert(list != NULL); \
59 return name##_size(list) == 0; \
62 static inline int name##_try_append(name##_t *list, type item) { \
63 assert(list != NULL); \
66 if (list->size == list->capacity) { \
67 const size_t c = list->capacity == 0 ? 1 : (list->capacity * 2); \
70 if (SIZE_MAX / c < sizeof(type)) { \
74 type *base = (type *)realloc(list->base, c * sizeof(type)); \
80 memset(&base[list->capacity], 0, (c - list->capacity) * sizeof(type)); \
83 ASAN_POISON(&base[list->capacity], (c - list->capacity) * sizeof(type)); \
97 if (list->head + list->size > list->capacity) { \
98 const size_t prefix = list->capacity - list->head; \
99 const size_t new_head = c - prefix; \
101 ASAN_UNPOISON(&base[new_head], prefix * sizeof(type)); \
102 memmove(&base[new_head], &base[list->head], prefix * sizeof(type)); \
104 ASAN_POISON(&base[list->size - prefix], \
105 (list->capacity - list->size) * sizeof(type)); \
106 list->head = new_head; \
110 list->capacity = c; \
113 const size_t new_slot = (list->head + list->size) % list->capacity; \
114 ASAN_UNPOISON(&list->base[new_slot], sizeof(type)); \
115 list->base[new_slot] = item; \
121 static inline void name##_append(name##_t *list, type item) { \
122 int rc = name##_try_append(list, item); \
124 fprintf(stderr, "realloc failed: %s\n", strerror(rc)); \
125 graphviz_exit(EXIT_FAILURE); \
130 static inline UNUSED void name##_prepend(name##_t *list, type item) { \
131 assert(list != NULL); \
134 if (list->size == list->capacity) { \
135 const size_t c = list->capacity == 0 ? 1 : (list->capacity * 2); \
138 if (SIZE_MAX / c < sizeof(type)) { \
139 fprintf(stderr, "integer overflow during list prepend\n"); \
140 graphviz_exit(EXIT_FAILURE); \
144 (type *)gv_recalloc(list->base, list->capacity, c, sizeof(type)); \
147 memset(&base[list->capacity], 0, (c - list->capacity) * sizeof(type)); \
150 ASAN_POISON(&base[list->capacity], (c - list->capacity) * sizeof(type)); \
164 if (list->head + list->size > list->capacity) { \
165 const size_t prefix = list->capacity - list->head; \
166 const size_t new_head = c - prefix; \
168 ASAN_UNPOISON(&base[new_head], prefix * sizeof(type)); \
169 memmove(&base[new_head], &base[list->head], prefix * sizeof(type)); \
171 ASAN_POISON(&base[list->size - prefix], \
172 (list->capacity - list->size) * sizeof(type)); \
173 list->head = new_head; \
177 list->capacity = c; \
180 assert(list->size < list->capacity); \
181 const size_t new_slot = \
182 (list->head + (list->capacity - 1)) % list->capacity; \
183 ASAN_UNPOISON(&list->base[new_slot], sizeof(type)); \
184 list->base[new_slot] = item; \
185 list->head = (list->head + (list->capacity - 1)) % list->capacity; \
195 static inline type name##_get(const name##_t *list, size_t index) { \
196 assert(list != NULL); \
197 assert(index < list->size && "index out of bounds"); \
198 return list->base[(list->head + index) % list->capacity]; \
213 static inline type *name##_at(name##_t *list, size_t index) { \
214 assert(list != NULL); \
215 assert(index < list->size && "index out of bounds"); \
216 return &list->base[(list->head + index) % list->capacity]; \
220 static inline UNUSED type *name##_front(name##_t *list) { \
221 assert(list != NULL); \
222 assert(!name##_is_empty(list)); \
223 return name##_at(list, 0); \
227 static inline UNUSED type *name##_back(name##_t *list) { \
228 assert(list != NULL); \
229 assert(!name##_is_empty(list)); \
230 return name##_at(list, name##_size(list) - 1); \
239 static inline void name##_set(name##_t *list, size_t index, type item) { \
240 assert(list != NULL); \
241 assert(index < name##_size(list) && "index out of bounds"); \
242 type *target = name##_at(list, index); \
252 static inline UNUSED void name##_remove(name##_t *list, const type item) { \
253 assert(list != NULL); \
255 for (size_t i = 0; i < list->size; ++i) { \
257 type *candidate = name##_at(list, i); \
258 if (memcmp(candidate, &item, sizeof(type)) == 0) { \
264 for (size_t j = i + 1; j < list->size; ++j) { \
265 type *replacement = name##_at(list, j); \
266 *candidate = *replacement; \
267 candidate = replacement; \
269 ASAN_POISON(name##_at(list, list->size - 1), sizeof(type)); \
277 static inline void name##_clear(name##_t *list) { \
278 assert(list != NULL); \
280 for (size_t i = 0; i < list->size; ++i) { \
281 dtor(name##_get(list, i)); \
282 ASAN_POISON(name##_at(list, i), sizeof(type)); \
296 static inline UNUSED void name##_reserve(name##_t *list, size_t capacity) { \
297 assert(list != NULL); \
300 if (list->capacity >= capacity) { \
304 list->base = (type *)gv_recalloc(list->base, list->capacity, capacity, \
319 if (list->head + list->size > list->capacity) { \
320 const size_t prefix = list->capacity - list->head; \
321 const size_t new_head = capacity - prefix; \
323 ASAN_UNPOISON(&list->base[new_head], prefix * sizeof(type)); \
324 memmove(&list->base[new_head], &list->base[list->head], \
325 prefix * sizeof(type)); \
327 ASAN_POISON(&list->base[list->size - prefix], \
328 (list->capacity - list->size) * sizeof(type)); \
329 list->head = new_head; \
332 list->capacity = capacity; \
336 static inline UNUSED bool name##_contains( \
337 const name##_t *haystack, const type needle, \
338 bool (*eq)(const type a, const type b)) { \
339 assert(haystack != NULL); \
340 assert(eq != NULL); \
342 for (size_t i = 0; i < name##_size(haystack); ++i) { \
343 if (eq(name##_get(haystack, i), needle)) { \
351 static inline UNUSED name##_t name##_copy(const name##_t *source) { \
352 assert(source != NULL); \
354 name##_t destination = {(type *)gv_calloc(source->capacity, sizeof(type)), \
355 0, 0, source->capacity}; \
356 for (size_t i = 0; i < source->size; ++i) { \
357 name##_append(&destination, name##_get(source, i)); \
359 return destination; \
376 static inline UNUSED bool name##_is_contiguous(const name##_t *list) { \
377 assert(list != NULL); \
378 return list->head + list->size <= list->capacity; \
382 static inline void name##_sync(name##_t *list) { \
383 assert(list != NULL); \
388 ASAN_UNPOISON(list->base, list->capacity * sizeof(type)); \
392 while (list->head != 0) { \
394 assert(list->capacity > 0); \
395 type replacement = list->base[0]; \
396 for (size_t i = list->capacity - 1; i != SIZE_MAX; --i) { \
397 type temp = list->base[i]; \
398 list->base[i] = replacement; \
399 replacement = temp; \
405 assert(name##_is_contiguous(list)); \
408 ASAN_POISON(&list->base[list->size], \
409 (list->capacity - list->size) * sizeof(type)); \
413 static inline UNUSED void name##_sort( \
414 name##_t *list, int (*cmp)(const type *a, const type *b)) { \
415 assert(list != NULL); \
416 assert(cmp != NULL); \
420 int (*compar)(const void *, const void *) = \
421 (int (*)(const void *, const void *))cmp; \
422 if (list->size > 0) { \
423 qsort(list->base, list->size, sizeof(type), compar); \
428 static inline UNUSED void name##_reverse(name##_t *list) { \
429 assert(list != NULL); \
431 for (size_t i = 0; i < name##_size(list) / 2; ++i) { \
432 type const temp1 = name##_get(list, i); \
433 type const temp2 = name##_get(list, name##_size(list) - i - 1); \
434 name##_set(list, i, temp2); \
435 name##_set(list, name##_size(list) - i - 1, temp1); \
440 static inline UNUSED void name##_shrink_to_fit(name##_t *list) { \
441 assert(list != NULL); \
445 if (list->capacity > list->size) { \
446 list->base = (type *)gv_recalloc(list->base, list->capacity, list->size, \
448 list->capacity = list->size; \
453 static inline UNUSED void name##_free(name##_t *list) { \
454 assert(list != NULL); \
455 name##_clear(list); \
457 memset(list, 0, sizeof(*list)); \
461 static inline UNUSED void name##_push_back(name##_t *list, type value) { \
462 name##_append(list, value); \
466 static inline UNUSED type name##_pop_front(name##_t *list) { \
467 assert(list != NULL); \
468 assert(list->size > 0); \
470 type value = name##_get(list, 0); \
475 ASAN_POISON(name##_at(list, 0), sizeof(type)); \
476 list->head = (list->head + 1) % list->capacity; \
483 static inline UNUSED type name##_pop_back(name##_t *list) { \
484 assert(list != NULL); \
485 assert(list->size > 0); \
487 type value = name##_get(list, list->size - 1); \
492 ASAN_POISON(name##_at(list, list->size - 1), sizeof(type)); \
507 static inline UNUSED type *name##_detach(name##_t *list) { \
508 assert(list != NULL); \
510 type *data = list->base; \
511 memset(list, 0, sizeof(*list)); \
Memory allocation wrappers that exit on failure.
macros for interacting with Address Sanitizer
abstraction for squashing compiler warnings for unused symbols