include/hamt/hamt_vec.h

filter: Statementstmt 100% 521 / 521 Branchbran 98.0% 400 / 414 (6 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 99.1% 921 / 935 (6 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/hamt/hamt_mut.h ↑ Summary include/hamt/hamt_algebra.h »
1 #ifndef HAMT_VEC_H
2 #define HAMT_VEC_H
3  
4 /* The vector: a sequence indexed from zero, with cheap append, cheap removal
5 * from the end, cheap replacement anywhere - and, from the second half of
6 * this file, concatenation, slicing and so insertion and removal in the
7 * middle, all without copying what does not change.
8 *
9 * It is a tree 32 wide whose leaves hold the values. The last few values -
10 * up to 32 of them - are kept out of the tree in a leaf of their own, the
11 * TAIL, so that nearly every append and every removal from the end touches
12 * one node and no path.
13 *
14 * count how many values in all
15 * tail the last 1..32 of them; NULL only when count is 0
16 * root a tree of everything before the tail; NULL when there is none
17 * shift five times the number of levels of nodes above the leaves
18 *
19 * REGULAR AND RELAXED. In a vector that has only been pushed to, every leaf
20 * in the tree is full and every node is full but the last on its level, so
21 * the child that holds an index is five bits of the index and nothing has
22 * to be looked up. Joining two such trees, or cutting one, leaves nodes
23 * along the join or the cut that are part full. Those nodes are RELAXED:
24 * each carries a table, `sizes`, of how many values lie under its first
25 * child, its first two, and so on. Finding a child in one is a guess from
26 * the index bits and then a step or two forward along the table.
27 *
28 * A node with no table is regular, and that is a promise: every child but
29 * its last holds exactly as many values as a child at that level can. A
30 * node is given a table exactly when that would not be true (hamt_vnode_make
31 * decides), so a tree that happens to come out regular is treated as one.
32 *
33 * How far a relaxed tree strays is put right where two trees are joined and
34 * where one is cut: the run of nodes along the join, or under each node
35 * along the cut, is repacked until no more than HAMT_VEC_EXTRAS of them are
36 * spare. That is what keeps the tree's height, and the steps a lookup takes
37 * along a table, close to a regular tree's. It is a repair made at each
38 * seam and not a promise about every node; see hamt_vec_check_spare.
39 *
40 * WHO MAY CHANGE WHAT. push, pop and set change a vector where it stands.
41 * The caller owns the hamt_vec struct, and the struct owns one reference to
42 * its root and one to its tail. A node whose reference count is one is the
43 * vector's alone and is changed in place; any other is shared, and is copied
44 * first, the copy taking the vector's place as holder of that reference. The
45 * walk makes each node on its way its own before it steps into it
46 * (hamt_vnode_own). A vector that never changes is made by giving every
47 * update a struct of its own that starts out sharing the root and the tail.
48 *
49 * concat and slice are the other kind: they build a new vector from old
50 * ones, which they only read.
51 *
52 * NO MEMORY. Every function either does what it was asked or leaves each
53 * vector holding exactly the values it held. A failed push, pop or set may
54 * leave some nodes as private copies of what they were, which nothing can
55 * observe.
56 *
57 * THE DISPLACED VALUE. A set or a pop takes one value out. It is handed
58 * back, with the reference the vector held, for the caller to release once
59 * it is done with the struct: releasing it can run code that uses this very
60 * vector. */
61  
62 #define HAMT_VEC_BITS 5
63 #define HAMT_VEC_WIDTH 32
64 #define HAMT_VEC_MASK 31
65 #define HAMT_VEC_EXTRAS 2
66  
67 /* For a test that wants to know how far a lookup had to step along a table. */
68 #ifndef HAMT_VEC_STEPPED
69 # define HAMT_VEC_STEPPED() ((void)0)
70 #endif
71  
72 typedef struct hamt_vnode {
73 uint32_t refcnt;
74 uint16_t len; /* slots in use, 1..32 */
75 uint16_t leaf; /* 1: the slots are values. 0: they are nodes */
76 size_t *sizes; /* NULL: regular. Else sizes[k] is the number of
77 * values under children 0..k, for k < len */
78 void *slot[HAMT_VEC_WIDTH];
79 } hamt_vnode;
80  
81 #define HAMT_VEC_SIZES_BYTES (HAMT_VEC_WIDTH * sizeof(size_t))
82  
83 #ifndef HAMT_VEC_FWD
84 #define HAMT_VEC_FWD
85 typedef struct hamt_vec hamt_vec;
86 #endif
87 struct hamt_vec {
88 size_t count;
89 uint32_t shift;
90 hamt_vnode *root;
91 hamt_vnode *tail;
92 };
93  
94 543909 static hamt_vnode *hamt_vnode_new(void *cx, int leaf) {
95 543909 hamt_vnode *n = (hamt_vnode *)HAMT_ALLOC(cx, sizeof(hamt_vnode));
96 543909 ▶ if (!n) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return NULL; 8395 535514 543909
97 535514 n->refcnt = 1;
98 535514 n->len = 0;
99 535514 n->leaf = leaf ? 1 : 0;
100 535514 n->sizes = NULL;
101 (void)cx;
102 535514 return n;
103 }
104  
105 1969433 static hamt_vnode *hamt_vnode_retain(hamt_vnode *n) {
106 /*
107 # uncoverable branch true
108 # uncoverable condition left count:1 */
109 1969433 ▶ if (n->refcnt == 0xffffffffU) HAMT_FATAL("a vector node's reference count would wrap");
Branch: 1 branch
BranchTrueFalseTotal
if (n->refcnt == 0xffffffffU) HAMT_FATAL("a vector node's reference count would wrap"); 0 1969433 1969433
110 1969433 n->refcnt++;
111 1969433 return n;
112 }
113  
114 /* The node's memory and its table: what it pointed at is the caller's
115 * business. */
116 535514 static void hamt_vnode_free_raw(void *cx, hamt_vnode *n) {
117 535514 ▶ if (n->sizes) HAMT_FREE(cx, n->sizes, HAMT_VEC_SIZES_BYTES);
Branch: 1 branch
BranchTrueFalseTotal
if (n->sizes) HAMT_FREE(cx, n->sizes, HAMT_VEC_SIZES_BYTES); 34381 501133 535514
118 535514 HAMT_FREE(cx, n, sizeof(hamt_vnode));
119 (void)cx;
120 535514 }
121  
122 /* At zero the node is freed before anything it held is released: a value's
123 * release can run code that comes back in here. */
124 2243303 static void hamt_vnode_release(void *cx, hamt_vnode *n) {
125 void *s[HAMT_VEC_WIDTH];
126 uint32_t i, len;
127 int leaf;
128  
129 2243303 ▶ if (--n->refcnt) return;
Branch: 1 branch
BranchTrueFalseTotal
if (--n->refcnt) return; 1737598 505705 2243303
130 505705 len = n->len;
131 505705 leaf = n->leaf;
132 505705 memcpy(s, n->slot, (size_t)len * sizeof(void *));
133 505705 hamt_vnode_free_raw(cx, n);
134 7103644 ▶ if (leaf) { for (i = 0; i < len; i++) HAMT_VAL_RELEASE(cx, s[i]); }
Branch: 2 branches
BranchTrueFalseTotal
if (leaf) { for (i = 0; i < len; i++) HAMT_VAL_RELEASE(cx, s[i]); } 369416 136289 505705
if (leaf) { for (i = 0; i < len; i++) HAMT_VAL_RELEASE(cx, s[i]); } 6597939 369416 6967355
135 1353534 ▶ else { for (i = 0; i < len; i++) hamt_vnode_release(cx, (hamt_vnode *)s[i]); }
Branch: 1 branch
BranchTrueFalseTotal
else { for (i = 0; i < len; i++) hamt_vnode_release(cx, (hamt_vnode *)s[i]); } 1217245 136289 1353534
136 (void)cx;
137 }
138  
139 /* Makes the node in *slot the holder's alone and returns it. If it already
140 * is, nothing happens. Otherwise *slot gets a copy that takes its own
141 * reference to everything the original held, and the original loses the
142 * reference *slot had. NULL when there is no memory, with *slot untouched. */
143 1096935 static hamt_vnode *hamt_vnode_own(void *cx, hamt_vnode **slot) {
144 1096935 hamt_vnode *n = *slot, *c;
145 uint32_t i;
146  
147 1096935 ▶ if (n->refcnt == 1) return n;
Branch: 1 branch
BranchTrueFalseTotal
if (n->refcnt == 1) return n; 861607 235328 1096935
148 235328 c = hamt_vnode_new(cx, n->leaf);
149 235328 ▶ if (!c) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!c) return NULL; 4068 231260 235328
150 231260 ▶ if (n->sizes) {
Branch: 1 branch
BranchTrueFalseTotal
if (n->sizes) { 8638 222622 231260
151 8638 c->sizes = (size_t *)HAMT_ALLOC(cx, HAMT_VEC_SIZES_BYTES);
152 8638 ▶ if (!c->sizes) { hamt_vnode_free_raw(cx, c); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!c->sizes) { hamt_vnode_free_raw(cx, c); return NULL; } 941 7697 8638
153 7697 memcpy(c->sizes, n->sizes, (size_t)n->len * sizeof(size_t));
154 }
155 230319 c->len = n->len;
156 230319 memcpy(c->slot, n->slot, (size_t)n->len * sizeof(void *));
157 3514847 ▶ if (n->leaf) { for (i = 0; i < n->len; i++) HAMT_VAL_RETAIN(cx, c->slot[i]); }
Branch: 2 branches
BranchTrueFalseTotal
if (n->leaf) { for (i = 0; i < n->len; i++) HAMT_VAL_RETAIN(cx, c->slot[i]); } 179675 50644 230319
if (n->leaf) { for (i = 0; i < n->len; i++) HAMT_VAL_RETAIN(cx, c->slot[i]); } 3284528 179675 3464203
158 914536 ▶ else { for (i = 0; i < n->len; i++) hamt_vnode_retain((hamt_vnode *)c->slot[i]); }
Branch: 1 branch
BranchTrueFalseTotal
else { for (i = 0; i < n->len; i++) hamt_vnode_retain((hamt_vnode *)c->slot[i]); } 863892 50644 914536
159 230319 n->refcnt--; /* it was above one: this frees nothing */
160 230319 *slot = c;
161 230319 return c;
162 }
163  
164 /* How many values a subtree holds. `level` is the node's own: 0 for a leaf.
165 * A relaxed node knows; a regular one is full children and whatever its
166 * last child holds, so this is one walk down the right-hand edge at most. */
167 460739 static size_t hamt_vnode_count(const hamt_vnode *n, uint32_t level) {
168 460739 size_t total = 0;
169 for (;;) {
170 517293 ▶ if (n->leaf) return total + n->len;
Branch: 1 branch
BranchTrueFalseTotal
if (n->leaf) return total + n->len; 440618 76675 517293
171 76675 ▶ if (n->sizes) return total + n->sizes[n->len - 1];
Branch: 1 branch
BranchTrueFalseTotal
if (n->sizes) return total + n->sizes[n->len - 1]; 20121 56554 76675
172 56554 total += (size_t)(n->len - 1) << level;
173 56554 n = (const hamt_vnode *)n->slot[n->len - 1];
174 56554 level -= HAMT_VEC_BITS;
175 }
176 }
177  
178 /* Which child of a node holds the value at *i, counted from the node's
179 * first value; *i is left counted from that child's first. */
180 1003578 static uint32_t hamt_vnode_child(const hamt_vnode *n, uint32_t level, size_t *i) {
181 1003578 uint32_t k = (uint32_t)(*i >> level);
182  
183 1003578 ▶ if (!n->sizes) {
Branch: 1 branch
BranchTrueFalseTotal
if (!n->sizes) { 918314 85264 1003578
184 918314 *i &= ((size_t)1 << level) - 1;
185 918314 return k;
186 }
187 /* The guess cannot be past the child: no child holds more than a full
188 * one, so there cannot be fewer values before it than that many. */
189 127138 ▶ while (n->sizes[k] <= *i) { k++; HAMT_VEC_STEPPED(); }
Branch: 1 branch
BranchTrueFalseTotal
while (n->sizes[k] <= *i) { k++; HAMT_VEC_STEPPED(); } 41874 85264 127138
190 85264 ▶ if (k) *i -= n->sizes[k - 1];
Branch: 1 branch
BranchTrueFalseTotal
if (k) *i -= n->sizes[k - 1]; 51163 34101 85264
191 85264 return k;
192 }
193  
194 /* A node at `level` over the children in kids[], which are nodes one level
195 * down. Takes over the caller's references to them. It is given a table
196 * exactly when it needs one. NULL when there is no memory, with the
197 * children still the caller's. */
198 109267 static hamt_vnode *hamt_vnode_make(void *cx, hamt_vnode **kids, uint32_t n, uint32_t level) {
199 109267 hamt_vnode *node = hamt_vnode_new(cx, 0);
200 109267 size_t full = (size_t)1 << level, total = 0;
201 uint32_t k;
202 109267 int regular = 1;
203  
204 109267 ▶ if (!node) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!node) return NULL; 1273 107994 109267
205 245757 ▶ for (k = 0; k + 1 < n && regular; k++)
Branch: 2 branches
BranchTrueFalseTotal
for (k = 0; k + 1 < n && regular; k++) 154463 91294 245757
for (k = 0; k + 1 < n && regular; k++) 137763 16700 154463
206 137763 ▶ if (hamt_vnode_count(kids[k], level - HAMT_VEC_BITS) != full) regular = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (hamt_vnode_count(kids[k], level - HAMT_VEC_BITS) != full) regular = 0; 27619 110144 137763
207  
208 107994 ▶ if (!regular) {
Branch: 1 branch
BranchTrueFalseTotal
if (!regular) { 27619 80375 107994
209 27619 node->sizes = (size_t *)HAMT_ALLOC(cx, HAMT_VEC_SIZES_BYTES);
210 27619 ▶ if (!node->sizes) { hamt_vnode_free_raw(cx, node); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!node->sizes) { hamt_vnode_free_raw(cx, node); return NULL; } 957 26662 27619
211 245167 ▶ for (k = 0; k < n; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < n; k++) { 218505 26662 245167
212 218505 total += hamt_vnode_count(kids[k], level - HAMT_VEC_BITS);
213 218505 node->sizes[k] = total;
214 }
215 }
216 107037 node->len = (uint16_t)n;
217 107037 memcpy(node->slot, kids, (size_t)n * sizeof(void *));
218 107037 return node;
219 }
220  
221 /* A leaf holding the n values at vals[], each of which it retains. */
222 156405 static hamt_vnode *hamt_vnode_leaf(void *cx, void *const *vals, uint32_t n) {
223 156405 hamt_vnode *leaf = hamt_vnode_new(cx, 1);
224 uint32_t i;
225 156405 ▶ if (!leaf) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!leaf) return NULL; 2502 153903 156405
226 153903 leaf->len = (uint16_t)n;
227 153903 memcpy(leaf->slot, vals, (size_t)n * sizeof(void *));
228 2730583 ▶ for (i = 0; i < n; i++) HAMT_VAL_RETAIN(cx, leaf->slot[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) HAMT_VAL_RETAIN(cx, leaf->slot[i]); 2576680 153903 2730583
229 (void)cx;
230 153903 return leaf;
231 }
232  
233 548964 static void hamt_vec_init(hamt_vec *v) {
234 548964 v->count = 0;
235 548964 v->shift = HAMT_VEC_BITS;
236 548964 v->root = NULL;
237 548964 v->tail = NULL;
238 548964 }
239  
240 /* A second struct onto the same vector: both now hold the root and the
241 * tail, so the first update through either copies what it touches. */
242 191090 static void hamt_vec_share(hamt_vec *dst, const hamt_vec *src) {
243 191090 *dst = *src;
244 191090 ▶ if (dst->root) hamt_vnode_retain(dst->root);
Branch: 1 branch
BranchTrueFalseTotal
if (dst->root) hamt_vnode_retain(dst->root); 159423 31667 191090
245 191090 ▶ if (dst->tail) hamt_vnode_retain(dst->tail);
Branch: 1 branch
BranchTrueFalseTotal
if (dst->tail) hamt_vnode_retain(dst->tail); 190108 982 191090
246 191090 }
247  
248 /* Lets go of everything the struct holds and leaves it empty. The struct is
249 * emptied first, so a value's release that looks at it sees an empty vector
250 * and not a half-released one. */
251 310833 static void hamt_vec_clear(void *cx, hamt_vec *v) {
252 310833 hamt_vnode *root = v->root, *tail = v->tail;
253 310833 hamt_vec_init(v);
254 310833 ▶ if (root) hamt_vnode_release(cx, root);
Branch: 1 branch
BranchTrueFalseTotal
if (root) hamt_vnode_release(cx, root); 206936 103897 310833
255 310833 ▶ if (tail) hamt_vnode_release(cx, tail);
Branch: 1 branch
BranchTrueFalseTotal
if (tail) hamt_vnode_release(cx, tail); 284939 25894 310833
256 310833 }
257  
258 /* Where the tail starts: the number of values in the tree. */
259 686275 static size_t hamt_vec_tailoff(const hamt_vec *v) {
260 686275 ▶ return v->tail ? v->count - v->tail->len : v->count; /* # uncoverable condition right count:1 */
Branch: 1 branch
BranchTrueFalseTotal
return v->tail ? v->count - v->tail->len : v->count; /* # uncoverable condition right count:1 */ 686275 0 686275
261 }
262  
263 /* The leaf holding index i, which must be below the count, and where in the
264 * vector that leaf starts. Borrowed. */
265 557686 static const hamt_vnode *hamt_vec_leaf(const hamt_vec *v, size_t i, size_t *start) {
266 const hamt_vnode *n;
267 uint32_t level;
268 557686 size_t tailoff = hamt_vec_tailoff(v), rel = i;
269  
270 557686 ▶ if (i >= tailoff) { *start = tailoff; return v->tail; }
Branch: 1 branch
BranchTrueFalseTotal
if (i >= tailoff) { *start = tailoff; return v->tail; } 102339 455347 557686
271 455347 n = v->root;
272 1293225 ▶ for (level = v->shift; level > 0; level -= HAMT_VEC_BITS)
Branch: 1 branch
BranchTrueFalseTotal
for (level = v->shift; level > 0; level -= HAMT_VEC_BITS) 837878 455347 1293225
273 837878 n = (const hamt_vnode *)n->slot[hamt_vnode_child(n, level, &rel)];
274 455347 *start = i - rel;
275 455347 return n;
276 }
277  
278 /* The value at index i, which must be below the count. Borrowed. */
279 184628 static void *hamt_vec_get(const hamt_vec *v, size_t i) {
280 size_t start;
281 184628 const hamt_vnode *leaf = hamt_vec_leaf(v, i, &start);
282 184628 return leaf->slot[i - start];
283 }
284  
285 /* A chain of nodes `level` deep with `leaf` at the bottom of it, taking over
286 * the caller's reference to the leaf. A node with one child is regular
287 * whatever the child holds. NULL when there is no memory, with the leaf
288 * still the caller's. */
289 26344 static hamt_vnode *hamt_vec_new_path(void *cx, uint32_t level, hamt_vnode *leaf) {
290 26344 hamt_vnode *top = leaf;
291 uint32_t l;
292  
293 27022 ▶ for (l = 0; l < level; l += HAMT_VEC_BITS) {
Branch: 1 branch
BranchTrueFalseTotal
for (l = 0; l < level; l += HAMT_VEC_BITS) { 687 26335 27022
294 687 hamt_vnode *n = hamt_vnode_new(cx, 0);
295 687 ▶ if (!n) {
Branch: 1 branch
BranchTrueFalseTotal
if (!n) { 9 678 687
296 10 ▶ while (top != leaf) {
Branch: 1 branch
BranchTrueFalseTotal
while (top != leaf) { 1 9 10
297 1 hamt_vnode *down = (hamt_vnode *)top->slot[0];
298 1 hamt_vnode_free_raw(cx, top);
299 1 top = down;
300 }
301 9 return NULL;
302 }
303 678 n->slot[0] = top;
304 678 n->len = 1;
305 678 top = n;
306 }
307 26335 return top;
308 }
309  
310 /* Hangs a leaf at the right-hand end of the tree, taking over the caller's
311 * reference to it. The leaf may be short. 0, or -1 for no memory with the
312 * leaf still the caller's and the vector holding what it held.
313 *
314 * The leaf goes under the lowest node on the right-hand edge that has a
315 * slot free; if none has, a new root goes over the old one. A regular node
316 * that gets a new last child stays regular only if the child that was last
317 * is full, and is given a table if it is not. */
318 30640 static int hamt_vec_push_leaf(void *cx, hamt_vec *v, hamt_vnode *leaf) {
319 30640 hamt_vnode *path, *n, *host = NULL;
320 hamt_vnode **at;
321 30640 size_t *table = NULL;
322 30640 uint32_t level, host_level = 0;
323 const hamt_vnode *scan;
324  
325 30640 ▶ if (!v->root) {
Branch: 1 branch
BranchTrueFalseTotal
if (!v->root) { 3708 26932 30640
326 3708 n = hamt_vnode_new(cx, 0);
327 3708 ▶ if (!n) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return -1; 39 3669 3708
328 3669 n->slot[0] = leaf;
329 3669 n->len = 1;
330 3669 v->root = n;
331 3669 v->shift = HAMT_VEC_BITS;
332 3669 return 0;
333 }
334  
335 /* First, only looking: the lowest node on the edge with room. */
336 26932 scan = v->root;
337 66727 ▶ for (level = v->shift; level > 0; level -= HAMT_VEC_BITS) {
Branch: 1 branch
BranchTrueFalseTotal
for (level = v->shift; level > 0; level -= HAMT_VEC_BITS) { 39795 26932 66727
338 39795 ▶ if (scan->len < HAMT_VEC_WIDTH) host_level = level;
Branch: 1 branch
BranchTrueFalseTotal
if (scan->len < HAMT_VEC_WIDTH) host_level = level; 38837 958 39795
339 39795 scan = (const hamt_vnode *)scan->slot[scan->len - 1];
340 }
341  
342 26932 ▶ if (!host_level) {
Branch: 1 branch
BranchTrueFalseTotal
if (!host_level) { 281 26651 26932
343 /* No room anywhere: a new root over the old one and a new path. */
344 hamt_vnode *kids[2];
345 281 path = hamt_vec_new_path(cx, v->shift, leaf);
346 281 ▶ if (!path) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!path) return -1; 7 274 281
347 274 kids[0] = v->root;
348 274 kids[1] = path;
349 274 n = hamt_vnode_make(cx, kids, 2, v->shift + HAMT_VEC_BITS);
350 274 ▶ if (!n) {
Branch: 1 branch
BranchTrueFalseTotal
if (!n) { 6 268 274
351 /* Take the chain down again without touching the leaf. */
352 13 ▶ while (path != leaf) {
Branch: 1 branch
BranchTrueFalseTotal
while (path != leaf) { 7 6 13
353 7 hamt_vnode *down = (hamt_vnode *)path->slot[0];
354 7 hamt_vnode_free_raw(cx, path);
355 7 path = down;
356 }
357 6 return -1;
358 }
359 268 v->root = n;
360 268 v->shift += HAMT_VEC_BITS;
361 268 return 0;
362 }
363  
364 /* Make the edge this vector's own down to the host. Then everything the
365 * change needs is allocated, and only then is anything changed. */
366 26651 at = &v->root;
367 26651 for (level = v->shift; ; level -= HAMT_VEC_BITS) {
368 39055 n = hamt_vnode_own(cx, at);
369 39055 ▶ if (!n) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return -1; 586 38469 39055
370 38469 ▶ if (level == host_level) { host = n; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (level == host_level) { host = n; break; } 26065 12404 38469
371 12404 at = (hamt_vnode **)&n->slot[n->len - 1];
372 }
373  
374 26065 ▶ if (!host->sizes) {
Branch: 1 branch
BranchTrueFalseTotal
if (!host->sizes) { 22878 3187 26065
375 22878 const hamt_vnode *was_last = (const hamt_vnode *)host->slot[host->len - 1];
376 22878 size_t under = hamt_vnode_count(was_last, host_level - HAMT_VEC_BITS);
377 22878 ▶ if (under != ((size_t)1 << host_level)) {
Branch: 1 branch
BranchTrueFalseTotal
if (under != ((size_t)1 << host_level)) { 24 22854 22878
378 uint32_t k;
379 24 table = (size_t *)HAMT_ALLOC(cx, HAMT_VEC_SIZES_BYTES);
380 24 ▶ if (!table) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!table) return -1; 2 22 24
381 /*
382 # uncoverable branch true class:ignore_covered_err
383 # uncoverable condition left count:1 */
384 22 ▶ for (k = 0; k + 1 < host->len; k++) table[k] = (size_t)(k + 1) << host_level;
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k + 1 < host->len; k++) table[k] = (size_t)(k + 1) << host_level; 0 22 22
385 22 table[host->len - 1] = ((size_t)(host->len - 1) << host_level) + under;
386 }
387 }
388 26063 path = hamt_vec_new_path(cx, host_level - HAMT_VEC_BITS, leaf);
389 26063 ▶ if (!path) {
Branch: 1 branch
BranchTrueFalseTotal
if (!path) { 2 26061 26063
390 /*
391 # uncoverable branch true
392 # uncoverable condition left count:1 */
393 2 ▶ if (table) HAMT_FREE(cx, table, HAMT_VEC_SIZES_BYTES);
Branch: 1 branch
BranchTrueFalseTotal
if (table) HAMT_FREE(cx, table, HAMT_VEC_SIZES_BYTES); 0 2 2
394 2 return -1;
395 }
396  
397 /* Nothing below can fail. Every relaxed node above the host has that
398 * many more values under its last child. */
399 26061 n = v->root;
400 38417 ▶ for (level = v->shift; level > host_level; level -= HAMT_VEC_BITS) {
Branch: 1 branch
BranchTrueFalseTotal
for (level = v->shift; level > host_level; level -= HAMT_VEC_BITS) { 12356 26061 38417
401 12356 ▶ if (n->sizes) n->sizes[n->len - 1] += leaf->len;
Branch: 1 branch
BranchTrueFalseTotal
if (n->sizes) n->sizes[n->len - 1] += leaf->len; 291 12065 12356
402 12356 n = (hamt_vnode *)n->slot[n->len - 1];
403 }
404 26061 ▶ if (table) host->sizes = table;
Branch: 1 branch
BranchTrueFalseTotal
if (table) host->sizes = table; 22 26039 26061
405 26061 ▶ if (host->sizes) host->sizes[host->len] = host->sizes[host->len - 1] + leaf->len;
Branch: 1 branch
BranchTrueFalseTotal
if (host->sizes) host->sizes[host->len] = host->sizes[host->len - 1] + leaf->len; 3207 22854 26061
406 26061 host->slot[host->len++] = path;
407 26061 return 0;
408 }
409  
410 /* Takes the tree's last leaf out to be the tail. The vector must have a
411 * tree and no tail. 0, or -1 for no memory with the vector as it was. */
412 41868 static int hamt_vec_pull_tail(void *cx, hamt_vec *v) {
413 hamt_vnode *path[14], *n, *leaf;
414 hamt_vnode **at;
415 uint32_t level;
416 41868 int depth = 0, d;
417  
418 41868 at = &v->root;
419 86661 ▶ for (level = v->shift; level > 0; level -= HAMT_VEC_BITS) {
Branch: 1 branch
BranchTrueFalseTotal
for (level = v->shift; level > 0; level -= HAMT_VEC_BITS) { 44855 41806 86661
420 44855 n = hamt_vnode_own(cx, at);
421 44855 ▶ if (!n) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return -1; 62 44793 44855
422 44793 path[depth++] = n;
423 44793 at = (hamt_vnode **)&n->slot[n->len - 1];
424 }
425 41806 leaf = *at;
426  
427 /* The leaf's reference moves from its parent to the tail. Going back up,
428 * a node left with nothing is freed and its parent loses the slot; the
429 * first node that still has something, and every relaxed node above it,
430 * has that many fewer values under its last child. */
431 41806 d = depth;
432 67432 ▶ while (d > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (d > 0) { 41978 25454 67432
433 41978 n = path[--d];
434 41978 ▶ if (--n->len > 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (--n->len > 0) break; 16352 25626 41978
435 25626 hamt_vnode_free_raw(cx, n);
436 25626 ▶ if (d == 0) v->root = NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (d == 0) v->root = NULL; 25454 172 25626
437 }
438 44604 ▶ while (d > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (d > 0) { 2798 41806 44604
439 2798 n = path[--d];
440 2798 ▶ if (n->sizes) n->sizes[n->len - 1] -= leaf->len;
Branch: 1 branch
BranchTrueFalseTotal
if (n->sizes) n->sizes[n->len - 1] -= leaf->len; 565 2233 2798
441 }
442 /* A root with one child is a level the tree no longer needs. */
443 41825 ▶ while (v->root && v->shift > HAMT_VEC_BITS && v->root->len == 1) {
Branch: 3 branches
BranchTrueFalseTotal
while (v->root && v->shift > HAMT_VEC_BITS && v->root->len == 1) { 16371 25454 41825
while (v->root && v->shift > HAMT_VEC_BITS && v->root->len == 1) { 2960 13411 16371
while (v->root && v->shift > HAMT_VEC_BITS && v->root->len == 1) { 19 2941 2960
444 19 hamt_vnode *top = v->root;
445 19 v->root = (hamt_vnode *)top->slot[0];
446 19 hamt_vnode_free_raw(cx, top);
447 19 v->shift -= HAMT_VEC_BITS;
448 }
449 41806 ▶ if (!v->root) v->shift = HAMT_VEC_BITS;
Branch: 1 branch
BranchTrueFalseTotal
if (!v->root) v->shift = HAMT_VEC_BITS; 25454 16352 41806
450 41806 v->tail = leaf;
451 41806 return 0;
452 }
453  
454 /* Appends a value. The vector takes its own reference. 0, or -1 for no
455 * memory with the vector holding what it held. */
456 841856 static int hamt_vec_push(void *cx, hamt_vec *v, void *val) {
457 hamt_vnode *fresh, *n;
458  
459 /* Room in the tail: the common case, one node and no path. */
460 841856 ▶ if (v->tail && v->tail->len < HAMT_VEC_WIDTH) {
Branch: 2 branches
BranchTrueFalseTotal
if (v->tail && v->tail->len < HAMT_VEC_WIDTH) { 826874 14982 841856
if (v->tail && v->tail->len < HAMT_VEC_WIDTH) { 803342 23532 826874
461 803342 n = hamt_vnode_own(cx, &v->tail);
462 803342 ▶ if (!n) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return -1; 1783 801559 803342
463 801559 HAMT_VAL_RETAIN(cx, val);
464 801559 n->slot[n->len++] = val;
465 801559 v->count++;
466 801559 return 0;
467 }
468  
469 38514 fresh = hamt_vnode_new(cx, 1);
470 38514 ▶ if (!fresh) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!fresh) return -1; 504 38010 38514
471 38010 ▶ if (v->tail && hamt_vec_push_leaf(cx, v, v->tail)) {
Branch: 2 branches
BranchTrueFalseTotal
if (v->tail && hamt_vec_push_leaf(cx, v, v->tail)) { 23355 14655 38010
if (v->tail && hamt_vec_push_leaf(cx, v, v->tail)) { 167 23188 23355
472 167 hamt_vnode_free_raw(cx, fresh);
473 167 return -1;
474 }
475 37843 HAMT_VAL_RETAIN(cx, val);
476 37843 fresh->slot[0] = val;
477 37843 fresh->len = 1;
478 37843 v->tail = fresh;
479 37843 v->count++;
480 37843 return 0;
481 }
482  
483 /* Replaces the value at index i, which must be below the count. The vector
484 * takes its own reference to the new value; the old one comes back through
485 * `dead` with the reference the vector held, or NULL when the new value is
486 * the one already there and nothing was done. 0, or -1 for no memory. */
487 43243 static int hamt_vec_set(void *cx, hamt_vec *v, size_t i, void *val, void **dead) {
488 hamt_vnode **at, *n;
489 uint32_t level;
490 43243 size_t tailoff = hamt_vec_tailoff(v);
491  
492 43243 *dead = NULL;
493 /*
494 # uncoverable branch true
495 # uncoverable condition left count:1 */
496 43243 ▶ if (hamt_vec_get(v, i) == val) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (hamt_vec_get(v, i) == val) return 0; 0 43243 43243
497  
498 43243 ▶ if (i >= tailoff) {
Branch: 1 branch
BranchTrueFalseTotal
if (i >= tailoff) { 5074 38169 43243
499 5074 n = hamt_vnode_own(cx, &v->tail);
500 5074 ▶ if (!n) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return -1; 30 5044 5074
501 5044 i -= tailoff;
502 }
503 else {
504 38169 at = &v->root;
505 103368 ▶ for (level = v->shift; level > 0; level -= HAMT_VEC_BITS) {
Branch: 1 branch
BranchTrueFalseTotal
for (level = v->shift; level > 0; level -= HAMT_VEC_BITS) { 66591 36777 103368
506 66591 n = hamt_vnode_own(cx, at);
507 66591 ▶ if (!n) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return -1; 1392 65199 66591
508 65199 at = (hamt_vnode **)&n->slot[hamt_vnode_child(n, level, &i)];
509 }
510 36777 n = hamt_vnode_own(cx, at);
511 36777 ▶ if (!n) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return -1; 581 36196 36777
512 }
513 41240 HAMT_VAL_RETAIN(cx, val);
514 41240 *dead = n->slot[i];
515 41240 n->slot[i] = val;
516 41240 return 0;
517 }
518  
519 /* Lets go of a leaf that holds exactly one value, handing the value back
520 * with a reference rather than releasing it. */
521 3521 static void *hamt_vec_take_last(void *cx, hamt_vnode *leaf) {
522 3521 void *val = leaf->slot[0];
523 3521 ▶ if (leaf->refcnt == 1) hamt_vnode_free_raw(cx, leaf); /* its reference is the caller's now */
Branch: 1 branch
BranchTrueFalseTotal
if (leaf->refcnt == 1) hamt_vnode_free_raw(cx, leaf); /* its reference is the caller's now */ 2005 1516 3521
524 else {
525 1516 HAMT_VAL_RETAIN(cx, val);
526 1516 leaf->refcnt--;
527 }
528 (void)cx;
529 3521 return val;
530 }
531  
532 /* Removes the last value, which comes back through `dead` with the
533 * reference the vector held; NULL for an empty vector, which stays empty.
534 * 0, or -1 for no memory with the vector holding what it held. */
535 105009 static int hamt_vec_pop(void *cx, hamt_vec *v, void **dead) {
536 hamt_vnode *n, *tail;
537  
538 105009 *dead = NULL;
539 105009 ▶ if (v->count == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (v->count == 0) return 0; 188 104821 105009
540  
541 104821 ▶ if (v->tail->len > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (v->tail->len > 1) { 101241 3580 104821
542 101241 n = hamt_vnode_own(cx, &v->tail);
543 101241 ▶ if (!n) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return -1; 575 100666 101241
544 100666 *dead = n->slot[--n->len];
545 100666 v->count--;
546 100666 return 0;
547 }
548  
549 /* The tail is down to its last value. If there is a tree, its last leaf
550 * comes out to be the new tail - which is all that can fail, so it is
551 * done first, with the old tail set aside. */
552 3580 tail = v->tail;
553 3580 v->tail = NULL;
554 3580 ▶ if (v->root && hamt_vec_pull_tail(cx, v)) {
Branch: 2 branches
BranchTrueFalseTotal
if (v->root && hamt_vec_pull_tail(cx, v)) { 3139 441 3580
if (v->root && hamt_vec_pull_tail(cx, v)) { 59 3080 3139
555 59 v->tail = tail;
556 59 return -1;
557 }
558 3521 v->count--;
559 3521 *dead = hamt_vec_take_last(cx, tail);
560 3521 return 0;
561 }
562  
563 /* ---- walking ----------------------------------------------------------- */
564  
565 /* Hands out the vector a leaf at a time: a run of up to 32 values and the
566 * index of the first. One descent for each run, not for each value. The
567 * vector is borrowed; whoever walks keeps it alive and unchanged. */
568 typedef struct hamt_vec_iter {
569 const hamt_vec *v;
570 size_t pos;
571 } hamt_vec_iter;
572  
573 65672 static void hamt_vec_iter_init(hamt_vec_iter *it, const hamt_vec *v) {
574 65672 it->v = v;
575 65672 it->pos = 0;
576 65672 }
577  
578 /* The next run, or NULL at the end. */
579 437917 static void *const *hamt_vec_iter_next(hamt_vec_iter *it, size_t *start, uint32_t *n) {
580 const hamt_vnode *leaf;
581 size_t leaf_start;
582  
583 437917 ▶ if (it->pos >= it->v->count) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (it->pos >= it->v->count) return NULL; 64859 373058 437917
584 /* A run is a whole leaf, so the position is always where one starts. */
585 373058 leaf = hamt_vec_leaf(it->v, it->pos, &leaf_start);
586 373058 *start = leaf_start;
587 373058 *n = leaf->len;
588 373058 it->pos = leaf_start + leaf->len;
589 373058 return (void *const *)leaf->slot;
590 }
591  
592 /* 1 when the two hold equal values in the same order, 0 when not, negative
593 * when the comparison stopped it. Two runs that are the same memory are the
594 * same values and are not looked into, so two versions of one vector are
595 * compared for what differs between them. The leaves of two vectors need
596 * not line up: each stretch compared is as long as both current runs allow. */
597 471 static int hamt_vec_equal(hamt_eq_ctx *c, const hamt_vec *a, const hamt_vec *b) {
598 hamt_vec_iter ia, ib;
599 471 void *const *ra = NULL, *const *rb = NULL;
600 471 size_t sa = 0, sb = 0, pos = 0;
601 471 uint32_t na = 0, nb = 0;
602  
603 /*
604 # uncoverable branch true
605 # uncoverable condition left count:1 */
606 471 ▶ if (a->count != b->count) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (a->count != b->count) return 0; 0 471 471
607 471 ▶ if (a->root == b->root && a->tail == b->tail) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (a->root == b->root && a->tail == b->tail) return 1; 150 321 471
if (a->root == b->root && a->tail == b->tail) return 1; 141 9 150
608  
609 330 hamt_vec_iter_init(&ia, a);
610 330 hamt_vec_iter_init(&ib, b);
611 2433 ▶ while (pos < a->count) {
Branch: 1 branch
BranchTrueFalseTotal
while (pos < a->count) { 2389 44 2433
612 uint32_t i, k;
613 2389 ▶ if (pos >= sa + na) ra = hamt_vec_iter_next(&ia, &sa, &na);
Branch: 1 branch
BranchTrueFalseTotal
if (pos >= sa + na) ra = hamt_vec_iter_next(&ia, &sa, &na); 2293 96 2389
614 2389 ▶ if (pos >= sb + nb) rb = hamt_vec_iter_next(&ib, &sb, &nb);
Branch: 1 branch
BranchTrueFalseTotal
if (pos >= sb + nb) rb = hamt_vec_iter_next(&ib, &sb, &nb); 2292 97 2389
615 2389 ▶ k = (uint32_t)((sa + na < sb + nb ? sa + na : sb + nb) - pos);
Branch: 1 branch
BranchTrueFalseTotal
k = (uint32_t)((sa + na < sb + nb ? sa + na : sb + nb) - pos); 97 2292 2389
616 2389 ▶ if (ra + (pos - sa) != rb + (pos - sb)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ra + (pos - sa) != rb + (pos - sb)) { 1831 558 2389
617 1831 c->visits++;
618 49352 ▶ for (i = 0; i < k; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < k; i++) { 47807 1545 49352
619 47807 int rc = hamt_val_same(c, ra[pos - sa + i], rb[pos - sb + i]);
620 47799 ▶ if (rc <= 0) return rc;
Branch: 1 branch
BranchTrueFalseTotal
if (rc <= 0) return rc; 278 47521 47799
621 }
622 }
623 2103 pos += k;
624 }
625 44 return 1;
626 }
627  
628 /* ---- joining ------------------------------------------------------------
629 *
630 * Two trees are joined along the right-hand edge of the first and the
631 * left-hand edge of the second. From the bottom up, at each level, there is
632 * a run of nodes: what the left node holds but for its last child, what
633 * came up from the level below, and what the right node holds but for its
634 * first child. The run is repacked - nodes that are nearly full are left
635 * exactly as they are and shared, a short one is poured into those after
636 * it - until it is no more than HAMT_VEC_EXTRAS nodes longer than the
637 * shortest it could be. Then it is gathered under one parent, or two, and
638 * those go up to the level above.
639 *
640 * Nothing that is read is changed: every node the result uses unchanged is
641 * shared, and only the nodes along the join are new. */
642  
643 /* A handful of nodes, each of which the list owns a reference to. */
644 typedef struct hamt_vlist {
645 hamt_vnode *n[2 * HAMT_VEC_WIDTH + 2];
646 uint32_t len;
647 } hamt_vlist;
648  
649 113152 static void hamt_vlist_release(void *cx, hamt_vlist *l) {
650 uint32_t i;
651 602685 ▶ for (i = 0; i < l->len; i++) hamt_vnode_release(cx, l->n[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < l->len; i++) hamt_vnode_release(cx, l->n[i]); 489533 113152 602685
652 113152 l->len = 0;
653 113152 }
654  
655 /* The run `all`, whose nodes are at `level` (0: leaves), repacked into
656 * `out`. 0, or -1 for no memory with `out` empty. `all` is only read. */
657 108709 static int hamt_rrb_repack(void *cx, const hamt_vlist *all, uint32_t level,
658 hamt_vlist *out) {
659 uint32_t c[2 * HAMT_VEC_WIDTH + 2];
660 108709 uint32_t n = all->len, i, j, src = 0, off = 0;
661 108709 size_t total = 0, shortest;
662  
663 108709 out->len = 0;
664 548565 ▶ for (i = 0; i < n; i++) { c[i] = all->n[i]->len; total += c[i]; }
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { c[i] = all->n[i]->len; total += c[i]; } 439856 108709 548565
665 108709 shortest = (total + HAMT_VEC_MASK) / HAMT_VEC_WIDTH;
666  
667 /* The plan: how many slots each node of the result will hold. While the
668 * run is too long, the first node with a slot or more to spare is
669 * emptied into the nodes after it, each filled in turn, and the run is
670 * one node shorter. */
671 108709 i = 0;
672 114886 ▶ while (n > shortest + HAMT_VEC_EXTRAS) {
Branch: 1 branch
BranchTrueFalseTotal
while (n > shortest + HAMT_VEC_EXTRAS) { 6177 108709 114886
673 uint32_t carry;
674 24789 ▶ while (c[i] >= HAMT_VEC_WIDTH - 1) i++;
Branch: 1 branch
BranchTrueFalseTotal
while (c[i] >= HAMT_VEC_WIDTH - 1) i++; 18612 6177 24789
675 6177 carry = c[i];
676 do {
677 39291 uint32_t take = carry + c[i + 1] < HAMT_VEC_WIDTH ? carry + c[i + 1] : HAMT_VEC_WIDTH;
678 39291 carry = carry + c[i + 1] - take;
679 39291 c[i] = take;
680 39291 i++;
681 39291 ▶ } while (carry > 0);
Branch: 1 branch
BranchTrueFalseTotal
} while (carry > 0); 33114 6177 39291
682 50432 ▶ for (j = i; j + 1 < n; j++) c[j] = c[j + 1];
Branch: 1 branch
BranchTrueFalseTotal
for (j = i; j + 1 < n; j++) c[j] = c[j + 1]; 44255 6177 50432
683 6177 n--;
684 6177 i--;
685 }
686  
687 /* Carrying it out. A node the plan leaves whole is shared; anything
688 * else is a new node filled from the old ones in order. */
689 516487 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 409676 106811 516487
690 hamt_vnode *made;
691  
692 409676 ▶ if (off == 0 && all->n[src]->len == c[i]) {
Branch: 2 branches
BranchTrueFalseTotal
if (off == 0 && all->n[src]->len == c[i]) { 386201 23475 409676
if (off == 0 && all->n[src]->len == c[i]) { 380170 6031 386201
693 380170 made = hamt_vnode_retain(all->n[src++]);
694 }
695 else {
696 void *slots[HAMT_VEC_WIDTH];
697 29506 uint32_t got = 0, k;
698  
699 88601 ▶ while (got < c[i]) {
Branch: 1 branch
BranchTrueFalseTotal
while (got < c[i]) { 59095 29506 88601
700 59095 const hamt_vnode *from = all->n[src];
701 59095 uint32_t take = from->len - off;
702 59095 ▶ if (take > c[i] - got) take = c[i] - got;
Branch: 1 branch
BranchTrueFalseTotal
if (take > c[i] - got) take = c[i] - got; 25055 34040 59095
703 59095 memcpy(slots + got, from->slot + off, (size_t)take * sizeof(void *));
704 59095 got += take;
705 59095 off += take;
706 59095 ▶ if (off == from->len) { src++; off = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (off == from->len) { src++; off = 0; } 34040 25055 59095
707 }
708 29506 ▶ if (level == 0) made = hamt_vnode_leaf(cx, slots, got);
Branch: 1 branch
BranchTrueFalseTotal
if (level == 0) made = hamt_vnode_leaf(cx, slots, got); 29045 461 29506
709 else {
710 5882 ▶ for (k = 0; k < got; k++) hamt_vnode_retain((hamt_vnode *)slots[k]);
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < got; k++) hamt_vnode_retain((hamt_vnode *)slots[k]); 5421 461 5882
711 461 made = hamt_vnode_make(cx, (hamt_vnode **)slots, got, level);
712 841 ▶ if (!made) for (k = 0; k < got; k++) hamt_vnode_release(cx, (hamt_vnode *)slots[k]);
Branch: 2 branches
BranchTrueFalseTotal
if (!made) for (k = 0; k < got; k++) hamt_vnode_release(cx, (hamt_vnode *)slots[k]); 39 422 461
if (!made) for (k = 0; k < got; k++) hamt_vnode_release(cx, (hamt_vnode *)slots[k]); 380 39 419
713 }
714 29506 ▶ if (!made) { hamt_vlist_release(cx, out); return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (!made) { hamt_vlist_release(cx, out); return -1; } 1898 27608 29506
715 }
716 407778 out->n[out->len++] = made;
717 }
718 106811 return 0;
719 }
720  
721 /* The nodes of `kids`, at the level below `level`, gathered under one node
722 * at `level` or two. Takes over the list's references, which the caller must
723 * then not release. 0, or -1 for no memory with `kids` still the caller's. */
724 7993 static int hamt_rrb_gather(void *cx, hamt_vlist *kids, uint32_t level, hamt_vlist *out) {
725 7993 uint32_t first = kids->len < HAMT_VEC_WIDTH ? kids->len : HAMT_VEC_WIDTH;
726 7993 hamt_vnode *a, *b = NULL;
727  
728 7993 out->len = 0;
729 7993 a = hamt_vnode_make(cx, kids->n, first, level);
730 7993 ▶ if (!a) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!a) return -1; 787 7206 7993
731 7206 ▶ if (kids->len > first) {
Branch: 1 branch
BranchTrueFalseTotal
if (kids->len > first) { 1441 5765 7206
732 1441 b = hamt_vnode_make(cx, kids->n + first, kids->len - first, level);
733 1441 ▶ if (!b) {
Branch: 1 branch
BranchTrueFalseTotal
if (!b) { 86 1355 1441
734 /* Undo the first without letting go of its children. */
735 86 a->len = 0;
736 86 hamt_vnode_free_raw(cx, a);
737 86 return -1;
738 }
739 }
740 7120 out->n[out->len++] = a;
741 7120 ▶ if (b) out->n[out->len++] = b;
Branch: 1 branch
BranchTrueFalseTotal
if (b) out->n[out->len++] = b; 1355 5765 7120
742 7120 return 0;
743 }
744  
745 /* Joins the subtree L, at level ll, to the subtree R, at level rl. `out`
746 * gets one node or two at the higher of the two levels, holding everything
747 * under L followed by everything under R. L and R are only read. */
748 17734 static int hamt_rrb_join(void *cx, const hamt_vnode *L, uint32_t ll,
749 const hamt_vnode *R, uint32_t rl, hamt_vlist *out) {
750 hamt_vlist mid, all, packed;
751 17734 uint32_t level = ll > rl ? ll : rl, i;
752 int rc;
753  
754 17734 out->len = 0;
755 17734 ▶ if (level == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (level == 0) { 8097 9637 17734
756 8097 out->n[0] = hamt_vnode_retain((hamt_vnode *)L);
757 8097 out->n[1] = hamt_vnode_retain((hamt_vnode *)R);
758 8097 out->len = 2;
759 8097 return 0;
760 }
761  
762 /* Down whichever edge is higher, or both, until the levels meet. */
763 9637 ▶ if (ll > rl) rc = hamt_rrb_join(cx, (const hamt_vnode *)L->slot[L->len - 1], ll - HAMT_VEC_BITS, R, rl, &mid);
Branch: 1 branch
BranchTrueFalseTotal
if (ll > rl) rc = hamt_rrb_join(cx, (const hamt_vnode *)L->slot[L->len - 1], ll - HAMT_VEC_BITS, R, rl, &mid); 203 9434 9637
764 9434 ▶ else if (ll < rl) rc = hamt_rrb_join(cx, L, ll, (const hamt_vnode *)R->slot[0], rl - HAMT_VEC_BITS, &mid);
Branch: 1 branch
BranchTrueFalseTotal
else if (ll < rl) rc = hamt_rrb_join(cx, L, ll, (const hamt_vnode *)R->slot[0], rl - HAMT_VEC_BITS, &mid); 914 8520 9434
765 8520 else rc = hamt_rrb_join(cx, (const hamt_vnode *)L->slot[L->len - 1], ll - HAMT_VEC_BITS,
766 8520 (const hamt_vnode *)R->slot[0], rl - HAMT_VEC_BITS, &mid);
767 9637 ▶ if (rc) return rc;
Branch: 1 branch
BranchTrueFalseTotal
if (rc) return rc; 396 9241 9637
768  
769 9241 all.len = 0;
770 9241 ▶ if (ll >= rl)
Branch: 1 branch
BranchTrueFalseTotal
if (ll >= rl) 8663 578 9241
771 76442 ▶ for (i = 0; i + 1 < L->len; i++) all.n[all.len++] = hamt_vnode_retain((hamt_vnode *)L->slot[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 1 < L->len; i++) all.n[all.len++] = hamt_vnode_retain((hamt_vnode *)L->slot[i]); 67779 8663 76442
772 27305 ▶ for (i = 0; i < mid.len; i++) all.n[all.len++] = mid.n[i]; /* the references move */
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < mid.len; i++) all.n[all.len++] = mid.n[i]; /* the references move */ 18064 9241 27305
773 9241 ▶ if (rl >= ll)
Branch: 1 branch
BranchTrueFalseTotal
if (rl >= ll) 9054 187 9241
774 78390 ▶ for (i = 1; i < R->len; i++) all.n[all.len++] = hamt_vnode_retain((hamt_vnode *)R->slot[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < R->len; i++) all.n[all.len++] = hamt_vnode_retain((hamt_vnode *)R->slot[i]); 69336 9054 78390
775  
776 9241 rc = hamt_rrb_repack(cx, &all, level - HAMT_VEC_BITS, &packed);
777 9241 hamt_vlist_release(cx, &all);
778 9241 ▶ if (rc) return rc;
Branch: 1 branch
BranchTrueFalseTotal
if (rc) return rc; 1248 7993 9241
779  
780 7993 rc = hamt_rrb_gather(cx, &packed, level, out);
781 7993 ▶ if (rc) hamt_vlist_release(cx, &packed);
Branch: 1 branch
BranchTrueFalseTotal
if (rc) hamt_vlist_release(cx, &packed); 873 7120 7993
782 7993 return rc;
783 }
784  
785 /* `out` becomes a vector of everything in `a` followed by everything in
786 * `b`. Neither is changed, and the result shares with both everything that
787 * is not along the join. `out` is uninitialised on the way in. 0, or -1 for
788 * no memory with `out` empty. */
789 21795 static int hamt_vec_concat(void *cx, hamt_vec *out, const hamt_vec *a, const hamt_vec *b) {
790 hamt_vec left;
791 hamt_vlist top;
792 hamt_vnode *root;
793 uint32_t shift;
794  
795 21795 ▶ if (a->count == 0) { hamt_vec_share(out, b); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (a->count == 0) { hamt_vec_share(out, b); return 0; } 1134 20661 21795
796 20661 ▶ if (b->count == 0) { hamt_vec_share(out, a); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (b->count == 0) { hamt_vec_share(out, a); return 0; } 1189 19472 20661
797  
798 19472 ▶ if (!b->root) {
Branch: 1 branch
BranchTrueFalseTotal
if (!b->root) { 12187 7285 19472
799 /* The second is a tail and nothing more: its values are pushed. */
800 uint32_t i;
801 12187 hamt_vec_share(out, a);
802 68606 ▶ for (i = 0; i < b->tail->len; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < b->tail->len; i++) 56782 11824 68606
803 56782 ▶ if (hamt_vec_push(cx, out, b->tail->slot[i])) { hamt_vec_clear(cx, out); return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (hamt_vec_push(cx, out, b->tail->slot[i])) { hamt_vec_clear(cx, out); return -1; } 363 56419 56782
804 11824 return 0;
805 }
806  
807 /* The first with its tail put into its tree, short as it may be, in a
808 * struct of its own so that `a` is not touched. */
809 7285 hamt_vec_share(&left, a);
810 7285 ▶ if (hamt_vec_push_leaf(cx, &left, left.tail)) { hamt_vec_clear(cx, &left); hamt_vec_init(out); return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (hamt_vec_push_leaf(cx, &left, left.tail)) { hamt_vec_clear(cx, &left); hamt_vec_init(out); return -1; } 475 6810 7285
811 6810 left.tail = NULL;
812  
813 6810 ▶ if (hamt_rrb_join(cx, left.root, left.shift, b->root, b->shift, &top)) {
Branch: 1 branch
BranchTrueFalseTotal
if (hamt_rrb_join(cx, left.root, left.shift, b->root, b->shift, &top)) { 1933 4877 6810
814 1933 hamt_vec_clear(cx, &left);
815 1933 hamt_vec_init(out);
816 1933 return -1;
817 }
818 4877 shift = left.shift > b->shift ? left.shift : b->shift;
819 4877 ▶ if (top.len == 1) root = top.n[0];
Branch: 1 branch
BranchTrueFalseTotal
if (top.len == 1) root = top.n[0]; 4608 269 4877
820 else {
821 269 root = hamt_vnode_make(cx, top.n, 2, shift + HAMT_VEC_BITS);
822 269 ▶ if (!root) {
Branch: 1 branch
BranchTrueFalseTotal
if (!root) { 46 223 269
823 46 hamt_vlist_release(cx, &top);
824 46 hamt_vec_clear(cx, &left);
825 46 hamt_vec_init(out);
826 46 return -1;
827 }
828 223 shift += HAMT_VEC_BITS;
829 }
830 4831 hamt_vec_clear(cx, &left);
831  
832 /* The root has two children at least: each tree's own root had, above
833 * the lowest level, and what the join put between them is one more. */
834 4831 out->count = a->count + b->count;
835 4831 out->shift = shift;
836 4831 out->root = root;
837 4831 out->tail = hamt_vnode_retain(b->tail);
838 4831 return 0;
839 }
840  
841 /* ---- cutting ------------------------------------------------------------ */
842  
843 /* A node at `level` over the n children in kids[], one of which has just
844 * been cut short. Cutting takes slots away without taking a child away, so
845 * the children are repacked exactly as a join repacks its run, and the node
846 * is no looser for having been cut. Takes over the caller's references to
847 * the children whether or not it succeeds. */
848 99468 static hamt_vnode *hamt_rrb_cut_node(void *cx, hamt_vnode **kids, uint32_t n, uint32_t level) {
849 hamt_vlist all, packed;
850 hamt_vnode *made;
851  
852 99468 memcpy(all.n, kids, (size_t)n * sizeof(void *));
853 99468 all.len = n;
854 99468 ▶ if (hamt_rrb_repack(cx, &all, level - HAMT_VEC_BITS, &packed)) {
Branch: 1 branch
BranchTrueFalseTotal
if (hamt_rrb_repack(cx, &all, level - HAMT_VEC_BITS, &packed)) { 650 98818 99468
855 650 hamt_vlist_release(cx, &all);
856 650 return NULL;
857 }
858 98818 hamt_vlist_release(cx, &all);
859 98818 made = hamt_vnode_make(cx, packed.n, packed.len, level);
860 98818 ▶ if (!made) hamt_vlist_release(cx, &packed);
Branch: 1 branch
BranchTrueFalseTotal
if (!made) hamt_vlist_release(cx, &packed); 1266 97552 98818
861 98818 return made;
862 }
863  
864 /* The first `keep` values under a node, as a node at the same level. `keep`
865 * is at least one and at most what the node holds. One reference, or NULL
866 * for no memory. Everything left of the cut is shared; the nodes along it
867 * are new. */
868 81593 static hamt_vnode *hamt_rrb_cut_right(void *cx, const hamt_vnode *n, uint32_t level, size_t keep) {
869 hamt_vnode *kids[HAMT_VEC_WIDTH], *sub;
870 81593 size_t i = keep - 1;
871 uint32_t k, j;
872  
873 81593 ▶ if (keep == hamt_vnode_count(n, level)) return hamt_vnode_retain((hamt_vnode *)n);
Branch: 1 branch
BranchTrueFalseTotal
if (keep == hamt_vnode_count(n, level)) return hamt_vnode_retain((hamt_vnode *)n); 2146 79447 81593
874 79447 ▶ if (level == 0) return hamt_vnode_leaf(cx, (void *const *)n->slot, (uint32_t)keep);
Branch: 1 branch
BranchTrueFalseTotal
if (level == 0) return hamt_vnode_leaf(cx, (void *const *)n->slot, (uint32_t)keep); 37529 41918 79447
875  
876 41918 k = hamt_vnode_child(n, level, &i);
877 41918 sub = hamt_rrb_cut_right(cx, (const hamt_vnode *)n->slot[k], level - HAMT_VEC_BITS, i + 1);
878 41918 ▶ if (!sub) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!sub) return NULL; 487 41431 41918
879 136702 ▶ for (j = 0; j < k; j++) kids[j] = hamt_vnode_retain((hamt_vnode *)n->slot[j]);
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < k; j++) kids[j] = hamt_vnode_retain((hamt_vnode *)n->slot[j]); 95271 41431 136702
880 41431 kids[k] = sub;
881 41431 return hamt_rrb_cut_node(cx, kids, k + 1, level);
882 }
883  
884 /* Everything under a node but its first `drop` values. `drop` is less than
885 * what the node holds. */
886 124841 static hamt_vnode *hamt_rrb_cut_left(void *cx, const hamt_vnode *n, uint32_t level, size_t drop) {
887 hamt_vnode *kids[HAMT_VEC_WIDTH], *sub;
888 124841 size_t i = drop;
889 124841 uint32_t k, j, m = 0;
890  
891 124841 ▶ if (drop == 0) return hamt_vnode_retain((hamt_vnode *)n);
Branch: 1 branch
BranchTrueFalseTotal
if (drop == 0) return hamt_vnode_retain((hamt_vnode *)n); 11859 112982 124841
892 112982 ▶ if (level == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (level == 0) 54399 58583 112982
893 54399 return hamt_vnode_leaf(cx, (void *const *)n->slot + drop, (uint32_t)(n->len - drop));
894  
895 58583 k = hamt_vnode_child(n, level, &i);
896 58583 sub = hamt_rrb_cut_left(cx, (const hamt_vnode *)n->slot[k], level - HAMT_VEC_BITS, i);
897 58583 ▶ if (!sub) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!sub) return NULL; 546 58037 58583
898 58037 kids[m++] = sub;
899 147975 ▶ for (j = k + 1; j < n->len; j++) kids[m++] = hamt_vnode_retain((hamt_vnode *)n->slot[j]);
Branch: 1 branch
BranchTrueFalseTotal
for (j = k + 1; j < n->len; j++) kids[m++] = hamt_vnode_retain((hamt_vnode *)n->slot[j]); 89938 58037 147975
900 58037 return hamt_rrb_cut_node(cx, kids, m, level);
901 }
902  
903 /* `out` becomes a vector of the `len` values of `v` starting at `from`,
904 * which must lie within it; `len` is at least one. `v` is not changed.
905 * `out` is uninitialised on the way in. 0, or -1 for no memory with `out`
906 * empty. */
907 85346 static int hamt_vec_slice(void *cx, hamt_vec *out, const hamt_vec *v, size_t from, size_t len) {
908 85346 size_t to = from + len, tailoff = hamt_vec_tailoff(v);
909 85346 hamt_vnode *root = NULL, *tail = NULL;
910 85346 uint32_t shift = v->shift;
911  
912 85346 hamt_vec_init(out);
913  
914 85346 ▶ if (to > tailoff) {
Branch: 1 branch
BranchTrueFalseTotal
if (to > tailoff) { 45671 39675 85346
915 /* Some of the tail is kept: that is the new tail. */
916 45671 ▶ size_t t0 = from > tailoff ? from - tailoff : 0, t1 = to - tailoff;
Branch: 1 branch
BranchTrueFalseTotal
size_t t0 = from > tailoff ? from - tailoff : 0, t1 = to - tailoff; 14257 31414 45671
917 45671 ▶ if (t0 == 0 && t1 == v->tail->len) tail = hamt_vnode_retain(v->tail);
Branch: 2 branches
BranchTrueFalseTotal
if (t0 == 0 && t1 == v->tail->len) tail = hamt_vnode_retain(v->tail); 31414 14257 45671
if (t0 == 0 && t1 == v->tail->len) tail = hamt_vnode_retain(v->tail); 10274 21140 31414
918 35397 else tail = hamt_vnode_leaf(cx, (void *const *)v->tail->slot + t0, (uint32_t)(t1 - t0));
919 45671 ▶ if (!tail) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!tail) return -1; 47 45624 45671
920 45624 ▶ if (from < tailoff) {
Branch: 1 branch
BranchTrueFalseTotal
if (from < tailoff) { 27492 18132 45624
921 27492 root = hamt_rrb_cut_left(cx, v->root, shift, from);
922 27492 ▶ if (!root) { hamt_vnode_release(cx, tail); return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (!root) { hamt_vnode_release(cx, tail); return -1; } 1582 25910 27492
923 }
924 }
925 else {
926 /* It ends inside the tree: cut the right, then the left. The last
927 * leaf of what is left will be taken out as the tail. */
928 39675 hamt_vnode *right = hamt_rrb_cut_right(cx, v->root, shift, to);
929 39675 ▶ if (!right) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!right) return -1; 909 38766 39675
930 38766 root = hamt_rrb_cut_left(cx, right, shift, from);
931 38766 hamt_vnode_release(cx, right);
932 38766 ▶ if (!root) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!root) return -1; 21 38745 38766
933 }
934  
935 /* What is left may be far smaller than the tree it was cut from, and
936 * still as tall: a root with one child is a level too many, and a root
937 * with two is the two cut edges and nothing between them, which is
938 * exactly what a join takes. So the two are joined, level by level,
939 * until the root is one that holds something. */
940 86317 ▶ while (root && shift > HAMT_VEC_BITS) {
Branch: 2 branches
BranchTrueFalseTotal
while (root && shift > HAMT_VEC_BITS) { 68185 18132 86317
while (root && shift > HAMT_VEC_BITS) { 5387 62798 68185
941 hamt_vnode *down;
942 5387 ▶ if (root->len == 1) down = hamt_vnode_retain((hamt_vnode *)root->slot[0]);
Branch: 1 branch
BranchTrueFalseTotal
if (root->len == 1) down = hamt_vnode_retain((hamt_vnode *)root->slot[0]); 2791 2596 5387
943 2596 ▶ else if (root->len == 2) {
Branch: 1 branch
BranchTrueFalseTotal
else if (root->len == 2) { 1287 1309 2596
944 hamt_vlist top;
945 1287 ▶ if (hamt_rrb_join(cx, (const hamt_vnode *)root->slot[0], shift - HAMT_VEC_BITS,
Branch: 1 branch
BranchTrueFalseTotal
if (hamt_rrb_join(cx, (const hamt_vnode *)root->slot[0], shift - HAMT_VEC_BITS, 188 1099 1287
946 1287 (const hamt_vnode *)root->slot[1], shift - HAMT_VEC_BITS, &top)) {
947 188 hamt_vnode_release(cx, root);
948 188 ▶ if (tail) hamt_vnode_release(cx, tail);
Branch: 1 branch
BranchTrueFalseTotal
if (tail) hamt_vnode_release(cx, tail); 172 16 188
949 188 return -1;
950 }
951 1099 ▶ if (top.len == 2) { hamt_vlist_release(cx, &top); break; } /* it does need both */
Branch: 1 branch
BranchTrueFalseTotal
if (top.len == 2) { hamt_vlist_release(cx, &top); break; } /* it does need both */ 360 739 1099
952 739 down = top.n[0];
953 }
954 1309 else break;
955 3530 hamt_vnode_release(cx, root);
956 3530 root = down;
957 3530 shift -= HAMT_VEC_BITS;
958 }
959 82599 out->count = len;
960 82599 ▶ out->shift = root ? shift : HAMT_VEC_BITS;
Branch: 1 branch
BranchTrueFalseTotal
out->shift = root ? shift : HAMT_VEC_BITS; 64467 18132 82599
961 82599 out->root = root;
962 82599 out->tail = tail;
963 82599 ▶ if (!tail && hamt_vec_pull_tail(cx, out)) { hamt_vec_clear(cx, out); return -1; }
Branch: 2 branches
BranchTrueFalseTotal
if (!tail && hamt_vec_pull_tail(cx, out)) { hamt_vec_clear(cx, out); return -1; } 38729 43870 82599
if (!tail && hamt_vec_pull_tail(cx, out)) { hamt_vec_clear(cx, out); return -1; } 3 38726 38729
964 82596 return 0;
965 }
966  
967 /* ---- the checker ------------------------------------------------------- */
968  
969 #define HAMT_VCHK_OK 0
970 #define HAMT_VCHK_EMPTY 1 /* count 0 with a root or a tail, or the reverse */
971 #define HAMT_VCHK_TAIL 2 /* a tail that is not a leaf of 1..32 */
972 #define HAMT_VCHK_ROOT 3 /* a root where there should be none, or none where one */
973 #define HAMT_VCHK_SHIFT 4 /* a shift that is not a positive multiple of 5 */
974 #define HAMT_VCHK_REFCNT 5 /* a node with a count of zero */
975 #define HAMT_VCHK_LEN 6 /* a node holding nothing, or more than 32 */
976 #define HAMT_VCHK_KIND 7 /* a leaf where a node belongs, or the reverse */
977 #define HAMT_VCHK_NOT_FULL 8 /* a regular node with a child that is not last and not full */
978 #define HAMT_VCHK_TALL 9 /* a root with one child above the lowest level */
979 #define HAMT_VCHK_TOTAL 10 /* the tree does not hold what the count says */
980 #define HAMT_VCHK_NULL 11 /* a slot holding nothing */
981 #define HAMT_VCHK_SIZES 12 /* a table that does not say what is under the node */
982 #define HAMT_VCHK_SPARE 13 /* a relaxed node with more children to spare than allowed */
983 #define HAMT_VCHK_LEAF_TABLE 14 /* a leaf with a table */
984  
985 /* How many values the subtree holds, recounted from its leaves; *rc gets
986 * the first rule broken. */
987 706528 static size_t hamt_vnode_check(const hamt_vnode *n, uint32_t level, uint32_t spare, int *rc) {
988 706528 size_t total = 0, full = (size_t)1 << level, slots = 0;
989 uint32_t i;
990  
991 /*
992 # uncoverable branch true
993 # uncoverable condition left count:1 */
994 706528 ▶ if (*rc) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (*rc) return 0; 0 706528 706528
995 706528 ▶ if (!n) { *rc = HAMT_VCHK_NULL; return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!n) { *rc = HAMT_VCHK_NULL; return 0; } 1 706527 706528
996 706527 ▶ if (n->refcnt == 0) { *rc = HAMT_VCHK_REFCNT; return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (n->refcnt == 0) { *rc = HAMT_VCHK_REFCNT; return 0; } 1 706526 706527
997 706526 ▶ if (n->len == 0 || n->len > HAMT_VEC_WIDTH) { *rc = HAMT_VCHK_LEN; return 0; } /* # uncoverable condition left count:2 */
Branch: 2 branches
BranchTrueFalseTotal
if (n->len == 0 || n->len > HAMT_VEC_WIDTH) { *rc = HAMT_VCHK_LEN; return 0; } /* # uncoverable condition left count:2 */ 706525 1 706526
if (n->len == 0 || n->len > HAMT_VEC_WIDTH) { *rc = HAMT_VCHK_LEN; return 0; } /* # uncoverable condition left count:2 */ 0 706525 706525
998 706525 ▶ if ((n->leaf != 0) != (level == 0)) { *rc = HAMT_VCHK_KIND; return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ((n->leaf != 0) != (level == 0)) { *rc = HAMT_VCHK_KIND; return 0; } 1 706524 706525
999  
1000 706524 ▶ if (level == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (level == 0) { 648898 57626 706524
1001 648898 ▶ if (n->sizes) { *rc = HAMT_VCHK_LEAF_TABLE; return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (n->sizes) { *rc = HAMT_VCHK_LEAF_TABLE; return 0; } 1 648897 648898
1002 20491717 ▶ for (i = 0; i < n->len; i++) if (!n->slot[i]) { *rc = HAMT_VCHK_NULL; return 0; }
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < n->len; i++) if (!n->slot[i]) { *rc = HAMT_VCHK_NULL; return 0; } 1 19842820 19842821
for (i = 0; i < n->len; i++) if (!n->slot[i]) { *rc = HAMT_VCHK_NULL; return 0; } 19842821 648896 20491717
1003 648896 return n->len;
1004 }
1005 727418 ▶ for (i = 0; i < n->len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n->len; i++) { 669797 57621 727418
1006 669797 const hamt_vnode *kid = (const hamt_vnode *)n->slot[i];
1007 669797 size_t got = hamt_vnode_check(kid, level - HAMT_VEC_BITS, spare, rc);
1008 669797 ▶ if (*rc) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (*rc) return 0; 3 669794 669797
1009 669794 total += got;
1010 669794 slots += kid->len;
1011 669794 ▶ if (n->sizes) { if (n->sizes[i] != total) { *rc = HAMT_VCHK_SIZES; return 0; } }
Branch: 2 branches
BranchTrueFalseTotal
if (n->sizes) { if (n->sizes[i] != total) { *rc = HAMT_VCHK_SIZES; return 0; } } 82532 587262 669794
if (n->sizes) { if (n->sizes[i] != total) { *rc = HAMT_VCHK_SIZES; return 0; } } 1 82531 82532
1012 587262 ▶ else if (i + 1 < n->len && got != full) { *rc = HAMT_VCHK_NOT_FULL; return 0; }
Branch: 2 branches
BranchTrueFalseTotal
else if (i + 1 < n->len && got != full) { *rc = HAMT_VCHK_NOT_FULL; return 0; } 546762 40500 587262
else if (i + 1 < n->len && got != full) { *rc = HAMT_VCHK_NOT_FULL; return 0; } 1 546761 546762
1013 }
1014 /* No more children than the slots under them need, plus the spare. */
1015 57621 ▶ if (n->len > (slots + HAMT_VEC_MASK) / HAMT_VEC_WIDTH + spare) {
Branch: 1 branch
BranchTrueFalseTotal
if (n->len > (slots + HAMT_VEC_MASK) / HAMT_VEC_WIDTH + spare) { 1 57620 57621
1016 1 *rc = HAMT_VCHK_SPARE;
1017 1 return 0;
1018 }
1019 57620 return total;
1020 }
1021  
1022 /* 0 when the vector is one this library could have built, otherwise the
1023 * number of the first rule it breaks. Visits every node.
1024 *
1025 * `spare` is how many more children a node may have than the slots under
1026 * them need, for a caller that wants to hold a vector to that as well.
1027 *
1028 * It is NOT something every vector satisfies, and hamt_vec_check does not
1029 * ask it. A join repacks the run of nodes along its seam until the RUN is
1030 * within HAMT_VEC_EXTRAS of the shortest it could be, and a cut does the
1031 * same for each node along the cut. That bounds the run, not each node
1032 * made from it: one level up, the same repacking moves whole children from
1033 * one parent to the next, and a parent can end up holding the loose end of
1034 * one neighbour and the loose start of another. So looseness is put right
1035 * where two trees meet and can gather, a little, elsewhere. What holds for
1036 * a vector made by joining and cutting vectors that were only ever pushed
1037 * to is the bound itself, and the tests hold those to it. What is held for
1038 * every vector is measured instead: its height, and how far a lookup steps
1039 * along a table. */
1040 82179 static int hamt_vec_check_spare(const hamt_vec *v, uint32_t spare) {
1041 size_t tailoff, total;
1042 uint32_t i;
1043 82179 int rc = 0;
1044  
1045 /*
1046 # uncoverable condition left count:1
1047 # uncoverable condition left count:2 */
1048 82179 ▶ if (v->count == 0) return (v->root || v->tail) ? HAMT_VCHK_EMPTY : HAMT_VCHK_OK;
Branch: 3 branches
BranchTrueFalseTotal
if (v->count == 0) return (v->root || v->tail) ? HAMT_VCHK_EMPTY : HAMT_VCHK_OK; 2849 79330 82179
if (v->count == 0) return (v->root || v->tail) ? HAMT_VCHK_EMPTY : HAMT_VCHK_OK; 2849 0 2849
if (v->count == 0) return (v->root || v->tail) ? HAMT_VCHK_EMPTY : HAMT_VCHK_OK; 0 2849 2849
1049 79330 ▶ if (!v->tail) return HAMT_VCHK_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (!v->tail) return HAMT_VCHK_EMPTY; 1 79329 79330
1050 79329 ▶ if (!v->tail->leaf || v->tail->len == 0 || v->tail->len > HAMT_VEC_WIDTH /* # uncoverable condition left count:3 */
Branch: 3 branches
BranchTrueFalseTotal
if (!v->tail->leaf || v->tail->len == 0 || v->tail->len > HAMT_VEC_WIDTH /* # uncoverable condition left count:3 */ 79328 1 79329
if (!v->tail->leaf || v->tail->len == 0 || v->tail->len > HAMT_VEC_WIDTH /* # uncoverable condition left count:3 */ 79327 1 79328
if (!v->tail->leaf || v->tail->len == 0 || v->tail->len > HAMT_VEC_WIDTH /* # uncoverable condition left count:3 */ 79327 0 79327
1051 79329 ▶ || v->tail->len > v->count) return HAMT_VCHK_TAIL;
Branch: 1 branch
BranchTrueFalseTotal
|| v->tail->len > v->count) return HAMT_VCHK_TAIL; 1 79326 79327
1052 79326 ▶ if (v->tail->sizes) return HAMT_VCHK_LEAF_TABLE;
Branch: 1 branch
BranchTrueFalseTotal
if (v->tail->sizes) return HAMT_VCHK_LEAF_TABLE; 1 79325 79326
1053 79325 ▶ if (v->tail->refcnt == 0) return HAMT_VCHK_REFCNT;
Branch: 1 branch
BranchTrueFalseTotal
if (v->tail->refcnt == 0) return HAMT_VCHK_REFCNT; 1 79324 79325
1054 977296 ▶ for (i = 0; i < v->tail->len; i++) if (!v->tail->slot[i]) return HAMT_VCHK_NULL;
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < v->tail->len; i++) if (!v->tail->slot[i]) return HAMT_VCHK_NULL; 1 897972 897973
for (i = 0; i < v->tail->len; i++) if (!v->tail->slot[i]) return HAMT_VCHK_NULL; 897973 79323 977296
1055  
1056 79323 tailoff = v->count - v->tail->len;
1057 79323 ▶ if ((tailoff == 0) != (v->root == NULL)) return HAMT_VCHK_ROOT;
Branch: 1 branch
BranchTrueFalseTotal
if ((tailoff == 0) != (v->root == NULL)) return HAMT_VCHK_ROOT; 2 79321 79323
1058 79321 ▶ if (!v->root) return HAMT_VCHK_OK;
Branch: 1 branch
BranchTrueFalseTotal
if (!v->root) return HAMT_VCHK_OK; 42588 36733 79321
1059  
1060 /*
1061 # uncoverable condition left count:1
1062 # uncoverable condition left count:3 */
1063 36733 ▶ if (v->shift == 0 || v->shift % HAMT_VEC_BITS || v->shift > 60) return HAMT_VCHK_SHIFT;
Branch: 3 branches
BranchTrueFalseTotal
if (v->shift == 0 || v->shift % HAMT_VEC_BITS || v->shift > 60) return HAMT_VCHK_SHIFT; 36733 0 36733
if (v->shift == 0 || v->shift % HAMT_VEC_BITS || v->shift > 60) return HAMT_VCHK_SHIFT; 36732 1 36733
if (v->shift == 0 || v->shift % HAMT_VEC_BITS || v->shift > 60) return HAMT_VCHK_SHIFT; 0 36732 36732
1064 36732 ▶ if (v->shift > HAMT_VEC_BITS && v->root->len == 1) return HAMT_VCHK_TALL;
Branch: 2 branches
BranchTrueFalseTotal
if (v->shift > HAMT_VEC_BITS && v->root->len == 1) return HAMT_VCHK_TALL; 2814 33918 36732
if (v->shift > HAMT_VEC_BITS && v->root->len == 1) return HAMT_VCHK_TALL; 1 2813 2814
1065 36731 total = hamt_vnode_check(v->root, v->shift, spare, &rc);
1066 36731 ▶ if (rc) return rc;
Branch: 1 branch
BranchTrueFalseTotal
if (rc) return rc; 9 36722 36731
1067 36722 ▶ return total == tailoff ? HAMT_VCHK_OK : HAMT_VCHK_TOTAL; /* # uncoverable condition right count:1 */
Branch: 1 branch
BranchTrueFalseTotal
return total == tailoff ? HAMT_VCHK_OK : HAMT_VCHK_TOTAL; /* # uncoverable condition right count:1 */ 36722 0 36722
1068 }
1069  
1070 82178 static int hamt_vec_check(const hamt_vec *v) {
1071 82178 return hamt_vec_check_spare(v, HAMT_VEC_WIDTH); /* no node has more children than that */
1072 }
1073  
1074 /* How many levels of nodes are over the leaves. */
1075 5 static uint32_t hamt_vec_height(const hamt_vec *v) {
1076 5 ▶ return v->root ? v->shift / HAMT_VEC_BITS : 0;
Branch: 1 branch
BranchTrueFalseTotal
return v->root ? v->shift / HAMT_VEC_BITS : 0; 3 2 5
1077 }
1078  
1079 #endif
« include/hamt/hamt_mut.h ↑ Summary include/hamt/hamt_algebra.h »