dte test coverage


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 50.0% high: ≥ 85.0%
Coverage Exec / Excl / Total
Lines: 100.0% 32 / 0 / 32
Functions: 100.0% 7 / 0 / 7
Branches: 100.0% 6 / 8 / 14

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