dte test coverage


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 50.0% high: ≥ 85.0%
Coverage Exec / Excl / Total
Lines: 100.0% 34 / 0 / 34
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 #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 void ptr_array_pop(PointerArray *array, FreeFunction free_ptr, size_t count) NONNULL_ARGS;
31 size_t ptr_array_remove(PointerArray *array, void *ptr) NONNULL_ARG(1);
32 void *ptr_array_remove_index(PointerArray *array, size_t idx) NONNULL_ARGS WARN_UNUSED_RESULT;
33 size_t ptr_array_index(const PointerArray *array, const void *ptr) NONNULL_ARG(1) WARN_UNUSED_RESULT;
34 void ptr_array_trim_nulls(PointerArray *array) NONNULL_ARGS;
35
36 NONNULL_ARGS
37 9 static inline void ptr_array_init(PointerArray *array, size_t capacity)
38 {
39 9 capacity = next_multiple(capacity, 8);
40 9 array->count = 0;
41
2/2
✓ Branch 2 → 3 taken 7 times.
✓ Branch 2 → 4 taken 2 times.
9 array->ptrs = capacity ? xmallocarray(capacity, sizeof(array->ptrs[0])) : NULL;
42 9 array->alloc = capacity;
43 9 }
44
45 NONNULL_ARG(1)
46 49679 static inline void ptr_array_append(PointerArray *array, void *ptr)
47 {
48
2/2
✓ Branch 2 → 3 taken 13882 times.
✓ Branch 2 → 5 taken 35797 times.
49679 if (unlikely(array->alloc <= array->count)) {
49 13882 ptr_array_grow_and_append(array, ptr);
50 13882 return;
51 }
52 35797 array->ptrs[array->count++] = ptr;
53 }
54
55 // Like ptr_array_index(), but asserting that `ptr` should always be found
56 // in the array (i.e. is known to be present), instead of the caller doing so
57 2185 static inline size_t ptr_array_xindex(const PointerArray *array, const void *ptr)
58 {
59 2185 size_t idx = ptr_array_index(array, ptr);
60 2185 BUG_ON(idx >= array->count);
61 2185 return idx;
62 }
63
64 // Swap the pointers at two indices
65 NONNULL_ARGS
66 1 static inline void ptr_array_swap(PointerArray *array, size_t a, size_t b)
67 {
68 1 BUG_ON(a >= array->count);
69 1 BUG_ON(b >= array->count);
70 1 void **ptrs = array->ptrs;
71 1 void *tmp = ptrs[a];
72 1 ptrs[a] = ptrs[b];
73 1 ptrs[b] = tmp;
74 1 }
75
76 // Free each pointer and then free the array
77 NONNULL_ARGS
78 10461 static inline void ptr_array_free(PointerArray *array)
79 {
80 10461 ptr_array_free_cb(array, free);
81 10461 }
82
83 // Free the array itself but not the pointers (useful when the
84 // pointers are "borrowed" references)
85 NONNULL_ARGS
86 15586 static inline void ptr_array_free_array(PointerArray *array)
87 {
88 15586 free(array->ptrs);
89 15586 *array = (PointerArray) PTR_ARRAY_INIT;
90 15586 }
91
92 89 static inline void ptr_array_sort (
93 const PointerArray *array,
94 CompareFunction compare
95 ) {
96
2/2
✓ Branch 2 → 3 taken 29 times.
✓ Branch 2 → 4 taken 60 times.
89 if (array->count >= 2) {
97 29 qsort(array->ptrs, array->count, sizeof(*array->ptrs), compare);
98 }
99 89 }
100
101 static inline void *ptr_array_bsearch (
102 const PointerArray array,
103 const void *ptr,
104 CompareFunction compare
105 ) {
106 return bsearch(&ptr, array.ptrs, array.count, sizeof(*array.ptrs), compare);
107 }
108
109 #endif
110