dte test coverage


Directory: ./
File: src/util/ptr-array.c
Date: 2025-09-07 23:01:39
Exec Total Coverage
Lines: 70 70 100.0%
Functions: 10 10 100.0%
Branches: 30 34 88.2%

Line Branch Exec Source
1 #include <errno.h>
2 #include <string.h>
3 #include "ptr-array.h"
4
5 13655 static void ptr_array_grow(PointerArray *array)
6 {
7 13655 BUG_ON(array->alloc < array->count);
8 13655 const size_t alloc_min = 8;
9
2/2
✓ Branch 0 (4→5) taken 477 times.
✓ Branch 1 (4→6) taken 13178 times.
13655 size_t alloc = MAX((array->alloc * 3) / 2, alloc_min);
10
1/2
✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 13655 times.
13655 FATAL_ERROR_ON(alloc <= array->count, EOVERFLOW);
11 13655 array->ptrs = xrenew(array->ptrs, alloc);
12 13655 array->alloc = alloc;
13 13655 }
14
15 // This is separate from ptr_array_append(), to allow the hot path to be
16 // inlined. It also duplicates the append operation so as to allow tail
17 // calling, which appears to improve code generation.
18 13655 void ptr_array_grow_and_append(PointerArray *array, void *ptr)
19 {
20 13655 ptr_array_grow(array);
21 13655 array->ptrs[array->count++] = ptr;
22 13655 }
23
24 // Move a pointer from one index to another
25 34 void ptr_array_move(PointerArray *array, size_t from, size_t to)
26 {
27 34 BUG_ON(from >= array->count);
28 34 BUG_ON(to >= array->count);
29
2/2
✓ Branch 0 (6→7) taken 24 times.
✓ Branch 1 (6→17) taken 10 times.
34 if (unlikely(from == to)) {
30 return;
31 }
32
33 24 void **p = array->ptrs;
34 24 bool up = (from < to);
35
2/2
✓ Branch 0 (7→8) taken 8 times.
✓ Branch 1 (7→9) taken 16 times.
24 void *dest = up ? p + from : p + to + 1;
36
2/2
✓ Branch 0 (10→11) taken 8 times.
✓ Branch 1 (10→12) taken 16 times.
24 void *src = up ? p + from + 1 : p + to;
37
2/2
✓ Branch 0 (13→14) taken 8 times.
✓ Branch 1 (13→15) taken 16 times.
24 size_t difference = up ? to - from : from - to;
38
39 24 void *tmp = p[from];
40 24 memmove(dest, src, difference * sizeof(void*));
41 24 p[to] = tmp;
42 }
43
44 12 void ptr_array_insert(PointerArray *array, void *ptr, size_t idx)
45 {
46 12 size_t last = array->count;
47 12 BUG_ON(idx > last);
48 12 ptr_array_append(array, ptr); // last is now array->count - 1
49 12 ptr_array_move(array, last, idx);
50 12 }
51
52 // Free and remove and all pointers, without freeing the array itself
53 15037 void ptr_array_clear(PointerArray *array, FreeFunction free_ptr)
54 {
55
2/2
✓ Branch 0 (5→3) taken 46079 times.
✓ Branch 1 (5→6) taken 15037 times.
61116 for (size_t i = 0, n = array->count; i < n; i++) {
56 46079 do_free_value(free_ptr, array->ptrs[i]);
57 46079 array->ptrs[i] = NULL;
58 }
59 15037 array->count = 0;
60 15037 }
61
62 15037 void ptr_array_free_cb(PointerArray *array, FreeFunction free_ptr)
63 {
64 15037 ptr_array_clear(array, free_ptr);
65 15037 ptr_array_free_array(array);
66 15037 }
67
68 170 size_t ptr_array_remove(PointerArray *array, void *ptr)
69 {
70 170 size_t idx = ptr_array_xindex(array, ptr);
71 170 void *removed = ptr_array_remove_index(array, idx);
72 170 BUG_ON(removed != ptr);
73 170 return idx;
74 }
75
76 2248 void *ptr_array_remove_index(PointerArray *array, size_t idx)
77 {
78 2248 BUG_ON(idx >= array->count);
79 2248 void **ptrs = array->ptrs + idx;
80 2248 void *removed = *ptrs;
81 2248 memmove(ptrs, ptrs + 1, (--array->count - idx) * sizeof(void*));
82 2248 return removed;
83 }
84
85 // Return the first array index found to contain `ptr`, or SIZE_MAX
86 // if not found. See also: ptr_array_xindex(), ptr_array_bsearch().
87 2179 size_t ptr_array_index(const PointerArray *array, const void *ptr)
88 {
89
1/2
✓ Branch 0 (5→3) taken 7284 times.
✗ Branch 1 (5→6) not taken.
7284 for (size_t i = 0, n = array->count; i < n; i++) {
90
2/2
✓ Branch 0 (3→4) taken 5105 times.
✓ Branch 1 (3→6) taken 2179 times.
7284 if (array->ptrs[i] == ptr) {
91 return i;
92 }
93 }
94
95 // If `ptr` isn't found in the array, return -1 (SIZE_MAX). Callers
96 // should check that the returned index is less than `array->count`
97 // before indexing `array->ptrs` with it or use ptr_array_xindex()
98 // instead, if applicable.
99 return -1;
100 }
101
102 // Trim all leading NULLs and all but one trailing NULL (if any)
103 1967 void ptr_array_trim_nulls(PointerArray *array)
104 {
105 1967 size_t n = array->count;
106
2/2
✓ Branch 0 (2→3) taken 1966 times.
✓ Branch 1 (2→17) taken 1 times.
1967 if (n == 0) {
107 return;
108 }
109
110 1966 void **ptrs = array->ptrs;
111
4/4
✓ Branch 0 (4→5) taken 3933 times.
✓ Branch 1 (4→6) taken 2 times.
✓ Branch 2 (5→4) taken 1969 times.
✓ Branch 3 (5→6) taken 1964 times.
3935 while (n > 0 && ptrs[n - 1] == NULL) {
112 n--;
113 }
114
115
1/2
✓ Branch 0 (6→7) taken 1966 times.
✗ Branch 1 (6→8) not taken.
1966 if (n != array->count) {
116 // Leave 1 trailing NULL
117 1966 n++;
118 }
119
120
3/4
✓ Branch 0 (8→9) taken 1966 times.
✗ Branch 1 (8→10) not taken.
✓ Branch 2 (9→10) taken 1963 times.
✓ Branch 3 (9→12) taken 3 times.
1966 if (n == 0 || ptrs[0] != NULL) {
121 // No leading NULLs; simply adjust count and return early
122 1963 array->count = n;
123 1963 return;
124 }
125
126 size_t i = 0;
127
4/4
✓ Branch 0 (12→13) taken 5 times.
✓ Branch 1 (12→14) taken 2 times.
✓ Branch 2 (13→11) taken 4 times.
✓ Branch 3 (13→14) taken 1 times.
7 while (i < n && ptrs[i] == NULL) {
128 4 i++;
129 }
130
131 3 BUG_ON(i == 0);
132 3 n -= i;
133 3 array->count = n;
134 3 memmove(ptrs, ptrs + i, n * sizeof(void*));
135 }
136