| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #ifndef UTIL_PTR_ARRAY_H | ||
| 2 | #define UTIL_PTR_ARRAY_H | ||
| 3 | |||
| 4 | #include <stdlib.h> | ||
| 5 | #include "bit.h" | ||
| 6 | #include "container.h" | ||
| 7 | #include "debug.h" | ||
| 8 | #include "macros.h" | ||
| 9 | #include "xmalloc.h" | ||
| 10 | |||
| 11 | typedef struct { | ||
| 12 | void **ptrs; | ||
| 13 | size_t alloc; | ||
| 14 | size_t count; | ||
| 15 | } PointerArray; | ||
| 16 | |||
| 17 | #define PTR_ARRAY_INIT { \ | ||
| 18 | .ptrs = NULL, \ | ||
| 19 | .alloc = 0, \ | ||
| 20 | .count = 0 \ | ||
| 21 | } | ||
| 22 | |||
| 23 | typedef int (*CompareFunction)(const void *, const void *); | ||
| 24 | |||
| 25 | void ptr_array_grow_and_append(PointerArray *array, void *ptr) NONNULL_ARG(1) NOINLINE; | ||
| 26 | void ptr_array_insert(PointerArray *array, void *ptr, size_t idx) NONNULL_ARG(1); | ||
| 27 | void ptr_array_move(PointerArray *array, size_t from, size_t to) NONNULL_ARGS; | ||
| 28 | void ptr_array_clear(PointerArray *array, FreeFunction free_ptr) NONNULL_ARGS; | ||
| 29 | void ptr_array_free_cb(PointerArray *array, FreeFunction free_ptr) NONNULL_ARGS; | ||
| 30 | size_t ptr_array_remove(PointerArray *array, void *ptr) NONNULL_ARG(1); | ||
| 31 | void *ptr_array_remove_index(PointerArray *array, size_t idx) NONNULL_ARGS WARN_UNUSED_RESULT; | ||
| 32 | size_t ptr_array_index(const PointerArray *array, const void *ptr) NONNULL_ARG(1) WARN_UNUSED_RESULT; | ||
| 33 | void ptr_array_trim_nulls(PointerArray *array) NONNULL_ARGS; | ||
| 34 | |||
| 35 | NONNULL_ARGS | ||
| 36 | 9 | static inline void ptr_array_init(PointerArray *array, size_t capacity) | |
| 37 | { | ||
| 38 | 9 | capacity = next_multiple(capacity, 8); | |
| 39 | 9 | array->count = 0; | |
| 40 |
2/2✓ Branch 0 (2→3) taken 7 times.
✓ Branch 1 (2→4) taken 2 times.
|
9 | array->ptrs = capacity ? xmallocarray(capacity, sizeof(array->ptrs[0])) : NULL; |
| 41 | 9 | array->alloc = capacity; | |
| 42 | 9 | } | |
| 43 | |||
| 44 | NONNULL_ARG(1) | ||
| 45 | 48909 | static inline void ptr_array_append(PointerArray *array, void *ptr) | |
| 46 | { | ||
| 47 |
2/2✓ Branch 0 (2→3) taken 13656 times.
✓ Branch 1 (2→5) taken 35253 times.
|
48909 | if (unlikely(array->alloc <= array->count)) { |
| 48 | 13656 | ptr_array_grow_and_append(array, ptr); | |
| 49 | 13656 | return; | |
| 50 | } | ||
| 51 | 35253 | array->ptrs[array->count++] = ptr; | |
| 52 | } | ||
| 53 | |||
| 54 | // Like ptr_array_index(), but asserting that `ptr` should always be found | ||
| 55 | // in the array (i.e. is known to be present), instead of the caller doing so | ||
| 56 | 2179 | static inline size_t ptr_array_xindex(const PointerArray *array, const void *ptr) | |
| 57 | { | ||
| 58 | 2179 | size_t idx = ptr_array_index(array, ptr); | |
| 59 | 2179 | BUG_ON(idx >= array->count); | |
| 60 | 2179 | return idx; | |
| 61 | } | ||
| 62 | |||
| 63 | // Swap the pointers at two indices | ||
| 64 | NONNULL_ARGS | ||
| 65 | 1 | static inline void ptr_array_swap(PointerArray *array, size_t a, size_t b) | |
| 66 | { | ||
| 67 | 1 | BUG_ON(a >= array->count); | |
| 68 | 1 | BUG_ON(b >= array->count); | |
| 69 | 1 | void **ptrs = array->ptrs; | |
| 70 | 1 | void *tmp = ptrs[a]; | |
| 71 | 1 | ptrs[a] = ptrs[b]; | |
| 72 | 1 | ptrs[b] = tmp; | |
| 73 | 1 | } | |
| 74 | |||
| 75 | // Free each pointer and then free the array | ||
| 76 | NONNULL_ARGS | ||
| 77 | 10266 | static inline void ptr_array_free(PointerArray *array) | |
| 78 | { | ||
| 79 | 10266 | ptr_array_free_cb(array, free); | |
| 80 | 10266 | } | |
| 81 | |||
| 82 | // Free the array itself but not the pointers (useful when the | ||
| 83 | // pointers are "borrowed" references) | ||
| 84 | NONNULL_ARGS | ||
| 85 | 15269 | static inline void ptr_array_free_array(PointerArray *array) | |
| 86 | { | ||
| 87 | 15269 | free(array->ptrs); | |
| 88 | 15269 | *array = (PointerArray) PTR_ARRAY_INIT; | |
| 89 | 15269 | } | |
| 90 | |||
| 91 | 89 | static inline void ptr_array_sort ( | |
| 92 | const PointerArray *array, | ||
| 93 | CompareFunction compare | ||
| 94 | ) { | ||
| 95 |
2/2✓ Branch 0 (2→3) taken 29 times.
✓ Branch 1 (2→4) taken 60 times.
|
89 | if (array->count >= 2) { |
| 96 | 29 | qsort(array->ptrs, array->count, sizeof(*array->ptrs), compare); | |
| 97 | } | ||
| 98 | 89 | } | |
| 99 | |||
| 100 | static inline void *ptr_array_bsearch ( | ||
| 101 | const PointerArray array, | ||
| 102 | const void *ptr, | ||
| 103 | CompareFunction compare | ||
| 104 | ) { | ||
| 105 | return bsearch(&ptr, array.ptrs, array.count, sizeof(*array.ptrs), compare); | ||
| 106 | } | ||
| 107 | |||
| 108 | #endif | ||
| 109 |