include/hamt/hamt_node.h

filter: Statementstmt 100% 146 / 146 Branchbran 94.4% 65 / 72 (3 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 98.1% 211 / 218 (3 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/hamt/hamt_entry.h ↑ Summary include/hamt/hamt_mut.h »
1 #ifndef HAMT_NODE_H
2 #define HAMT_NODE_H
3  
4 /* One node of the map's trie, and the handful of ways a node is copied with
5 * one thing different.
6 *
7 * A node is 32 positions wide, and each position is empty, or holds an
8 * entry, or holds a child. Two bitmaps say which: `datamap` has a bit for
9 * each position holding an entry and `nodemap` a bit for each holding a
10 * child, and no bit is in both. Only what is there is stored:
11 *
12 * slot[]: [ entries, lowest position first | children, HIGHEST first ]
13 *
14 * So with nd entries and N slots in all, the entry at position p is at
15 *
16 * slot[ popcount(datamap & (bit(p) - 1)) ]
17 *
18 * and the child at position p is at
19 *
20 * slot[ N - 1 - popcount(nodemap & (bit(p) - 1)) ]
21 *
22 * Children fill from the end so that turning an entry into a child, or a
23 * child back into an entry, moves the fewest slots.
24 *
25 * The position at each level is five bits of the key's hash: bits 0-4 at the
26 * root, 5-9 below it, and so on for twelve levels and sixty bits. A node
27 * thirteen deep has no hash left to index by, and is a COLLISION node: both
28 * maps zero, `ncoll` entries in a plain array, searched one by one.
29 *
30 * A node is never changed once more than one thing points at it. Every
31 * function here that "changes" a node allocates a new one; the functions
32 * that change one where it stands are in hamt_mut.h, and they first make
33 * sure nobody else is looking. */
34  
35 #define HAMT_BITS 5
36 #define HAMT_MAX_DEPTH 12 /* depths 0..11 index by hash; depth 12 collides */
37  
38 #define HAMT_IDX(hash, depth) \
39 ((unsigned)(((uint64_t)(hash) >> (HAMT_BITS * (depth))) & 31))
40 #define HAMT_BIT(hash, depth) ((uint32_t)1 << HAMT_IDX((hash), (depth)))
41  
42 typedef struct hamt_node {
43 uint32_t refcnt;
44 uint32_t datamap;
45 uint32_t nodemap;
46 uint32_t ncoll; /* > 0: a collision node, both maps zero */
47 void *slot[1];
48 } hamt_node;
49  
50 /* How many slots a node holding n things has room for: n rounded up to the
51 * next multiple of four. A node that is being built up in place gains a
52 * slot at a time, and with no room to spare every one of those would be a
53 * new allocation, a copy and a free; with this, three in four are a few
54 * pointers moved along. The room is a function of n and is stored nowhere,
55 * which is what keeps one rule true everywhere: A NODE HOLDING n THINGS IS
56 * ALLOCATED AT HAMT_NODE_BYTES(n). So whoever changes n in place has to
57 * check first that the new n has the same room, and reallocate if not. */
58 #define HAMT_NODE_ROOM(n) (((uint32_t)(n) + 3u) & ~3u)
59 #define HAMT_NODE_BYTES(n) \
60 (offsetof(hamt_node, slot) + (size_t)((n) ? HAMT_NODE_ROOM(n) : 4) * sizeof(void *))
61  
62 #define HAMT_NO_SKIP 0xffffffffU
63  
64 /* Entries in the node: the first this-many slots. */
65 9642444 static uint32_t hamt_node_ndata(const hamt_node *n) {
66 9642444 ▶ return n->ncoll ? n->ncoll : hamt_popcnt32(n->datamap);
Branch: 1 branch
BranchTrueFalseTotal
return n->ncoll ? n->ncoll : hamt_popcnt32(n->datamap); 1383110 8259334 9642444
67 }
68  
69 21197979 static uint32_t hamt_node_nslots(const hamt_node *n) {
70 21197979 return n->ncoll ? n->ncoll
71 21197979 ▶ : hamt_popcnt32(n->datamap) + hamt_popcnt32(n->nodemap);
Branch: 1 branch
BranchTrueFalseTotal
: hamt_popcnt32(n->datamap) + hamt_popcnt32(n->nodemap); 712628 20485351 21197979
72 }
73  
74 /* One entry and nothing else. Legal as a root. Anywhere else it is a node
75 * on its way to being folded into its parent, and never left standing. */
76 35834205 static int hamt_node_is_single(const hamt_node *n) {
77 35834205 ▶ if (n->ncoll) return n->ncoll == 1;
Branch: 1 branch
BranchTrueFalseTotal
if (n->ncoll) return n->ncoll == 1; 12720 35821485 35834205
78 3072471 ▶ return n->nodemap == 0 && n->datamap != 0
Branch: 1 branch
BranchTrueFalseTotal
return n->nodemap == 0 && n->datamap != 0 3072471 0 3072471
79 38893956 ▶ && (n->datamap & (n->datamap - 1)) == 0;
Branch: 2 branches
BranchTrueFalseTotal
&& (n->datamap & (n->datamap - 1)) == 0; 3072471 32749014 35821485
&& (n->datamap & (n->datamap - 1)) == 0; 38738 3033733 3072471
80 }
81  
82 2019906 static hamt_node *hamt_node_new(void *cx, uint32_t nslots) {
83 2019906 ▶ hamt_node *n = (hamt_node *)HAMT_ALLOC(cx, HAMT_NODE_BYTES(nslots));
Branch: 1 branch
BranchTrueFalseTotal
hamt_node *n = (hamt_node *)HAMT_ALLOC(cx, HAMT_NODE_BYTES(nslots)); 2019906 0 2019906
84 2019906 ▶ if (!n) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return NULL; 2459 2017447 2019906
85 2017447 n->refcnt = 1;
86 2017447 n->datamap = 0;
87 2017447 n->nodemap = 0;
88 2017447 n->ncoll = 0;
89 (void)cx;
90 2017447 return n;
91 }
92  
93 3112510 static hamt_node *hamt_node_retain(hamt_node *n) {
94 /*
95 # uncoverable branch true
96 # uncoverable condition left count:1 */
97 3112510 ▶ if (n->refcnt == 0xffffffffU) HAMT_FATAL("a node's reference count would wrap");
Branch: 1 branch
BranchTrueFalseTotal
if (n->refcnt == 0xffffffffU) HAMT_FATAL("a node's reference count would wrap"); 0 3112510 3112510
98 3112510 n->refcnt++;
99 3112510 return n;
100 }
101  
102 /* A node that has just been filled with borrowed pointers takes its own
103 * reference to each. `skip` is the one slot whose reference the caller is
104 * handing over rather than sharing, or HAMT_NO_SKIP. */
105 1018765 static void hamt_node_adopt(hamt_node *n, uint32_t skip) {
106 1018765 uint32_t nd = hamt_node_ndata(n), N = hamt_node_nslots(n), i;
107 4749670 ▶ for (i = 0; i < nd; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nd; i++) 3730905 1018765 4749670
108 /*
109 # uncoverable branch false class:ignore_covered_err
110 # uncoverable condition right count:1 */
111 3730905 ▶ if (i != skip) hamt_entry_retain((hamt_entry *)n->slot[i]);
Branch: 1 branch
BranchTrueFalseTotal
if (i != skip) hamt_entry_retain((hamt_entry *)n->slot[i]); 3730905 0 3730905
112 3713507 ▶ for (; i < N; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (; i < N; i++) 2694742 1018765 3713507
113 2694742 ▶ if (i != skip) hamt_node_retain((hamt_node *)n->slot[i]);
Branch: 1 branch
BranchTrueFalseTotal
if (i != skip) hamt_node_retain((hamt_node *)n->slot[i]); 2355723 339019 2694742
114 1018765 }
115  
116 /* At zero the node is freed BEFORE anything it held is released, the same
117 * rule as an entry and for the same reason: a value's release can run code
118 * that comes back in here. The slots are copied out to make that possible.
119 *
120 * A collision node can be wider than any buffer worth putting on the stack,
121 * so a wide one releases in place and is freed after. That is safe too - a
122 * node whose count has reached zero is reachable from nowhere, so nothing
123 * that runs can find it - it is only not the same order. */
124 4994339 static void hamt_node_release(void *cx, hamt_node *n) {
125 uint32_t nd, N, i;
126  
127 4994339 ▶ if (--n->refcnt) return;
Branch: 1 branch
BranchTrueFalseTotal
if (--n->refcnt) return; 3112510 1881829 4994339
128 1881829 nd = hamt_node_ndata(n);
129 1881829 N = hamt_node_nslots(n);
130  
131 1881829 ▶ if (N <= 32) {
Branch: 1 branch
BranchTrueFalseTotal
if (N <= 32) { 1871299 10530 1881829
132 void *s[32];
133 1871299 memcpy(s, n->slot, (size_t)N * sizeof(void *));
134 1871299 ▶ HAMT_FREE(cx, n, HAMT_NODE_BYTES(N));
Branch: 1 branch
BranchTrueFalseTotal
HAMT_FREE(cx, n, HAMT_NODE_BYTES(N)); 1871299 0 1871299
135 5723362 ▶ for (i = 0; i < nd; i++) hamt_entry_release(cx, (hamt_entry *)s[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nd; i++) hamt_entry_release(cx, (hamt_entry *)s[i]); 3852063 1871299 5723362
136 5736200 ▶ for (; i < N; i++) hamt_node_release(cx, (hamt_node *)s[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (; i < N; i++) hamt_node_release(cx, (hamt_node *)s[i]); 3864901 1871299 5736200
137 }
138 else {
139 749126 ▶ for (i = 0; i < N; i++) hamt_entry_release(cx, (hamt_entry *)n->slot[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < N; i++) hamt_entry_release(cx, (hamt_entry *)n->slot[i]); 738596 10530 749126
140 10530 ▶ HAMT_FREE(cx, n, HAMT_NODE_BYTES(N));
Branch: 1 branch
BranchTrueFalseTotal
HAMT_FREE(cx, n, HAMT_NODE_BYTES(N)); 10530 0 10530
141 }
142 }
143  
144 /* ---- copies with one thing different ----------------------------------
145 *
146 * Each returns a new node holding one reference to everything in it, or
147 * NULL when there is no memory, in which case nothing was retained. The
148 * node copied from is untouched. */
149  
150 /* The same node with slot i pointing somewhere else. `skip` as for adopt:
151 * pass i to hand over the caller's reference to p, HAMT_NO_SKIP to share. */
152 468637 static hamt_node *hamt_node_with_slot(void *cx, const hamt_node *n, uint32_t i,
153 void *p, uint32_t skip) {
154 468637 uint32_t N = hamt_node_nslots(n);
155 468637 hamt_node *r = hamt_node_new(cx, N);
156 468637 ▶ if (!r) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!r) return NULL; 221 468416 468637
157 468416 r->datamap = n->datamap;
158 468416 r->nodemap = n->nodemap;
159 468416 r->ncoll = n->ncoll;
160 468416 memcpy(r->slot, n->slot, (size_t)N * sizeof(void *));
161 468416 r->slot[i] = p;
162 468416 hamt_node_adopt(r, skip);
163 468416 return r;
164 }
165  
166 /* An entry at a position that held nothing. */
167 38159 static hamt_node *hamt_node_add_entry(void *cx, const hamt_node *n,
168 uint32_t bit, hamt_entry *e) {
169 38159 uint32_t N = hamt_node_nslots(n);
170 38159 uint32_t di = hamt_popcnt32(n->datamap & (bit - 1));
171 38159 hamt_node *r = hamt_node_new(cx, N + 1);
172 38159 ▶ if (!r) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!r) return NULL; 130 38029 38159
173 38029 r->datamap = n->datamap | bit;
174 38029 r->nodemap = n->nodemap;
175 38029 memcpy(r->slot, n->slot, (size_t)di * sizeof(void *));
176 38029 r->slot[di] = e;
177 38029 memcpy(r->slot + di + 1, n->slot + di, (size_t)(N - di) * sizeof(void *));
178 38029 hamt_node_adopt(r, HAMT_NO_SKIP);
179 38029 return r;
180 }
181  
182 /* The entry at a position gone, and nothing in its place. */
183 22708 static hamt_node *hamt_node_drop_entry(void *cx, const hamt_node *n, uint32_t bit) {
184 22708 uint32_t N = hamt_node_nslots(n);
185 22708 uint32_t di = hamt_popcnt32(n->datamap & (bit - 1));
186 22708 hamt_node *r = hamt_node_new(cx, N - 1);
187 22708 ▶ if (!r) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!r) return NULL; 156 22552 22708
188 22552 r->datamap = n->datamap & ~bit;
189 22552 r->nodemap = n->nodemap;
190 22552 memcpy(r->slot, n->slot, (size_t)di * sizeof(void *));
191 22552 memcpy(r->slot + di, n->slot + di + 1, (size_t)(N - 1 - di) * sizeof(void *));
192 22552 hamt_node_adopt(r, HAMT_NO_SKIP);
193 22552 return r;
194 }
195  
196 /* The entry at a position replaced by a child: a second key arrived there.
197 * The caller's reference to the child is handed over.
198 *
199 * The entry leaves the front and the child joins the back, so every slot
200 * between the two moves down by one and nothing outside them moves:
201 *
202 * old: e0 .. [E] .. e(nd-1) | c(high) .. .. c(low)
203 * new: e0 .. .. e(nd-1) | c(high) .. [C] .. c(low)
204 */
205 11016 static hamt_node *hamt_node_entry_to_child(void *cx, const hamt_node *n,
206 uint32_t bit, hamt_node *child) {
207 11016 uint32_t N = hamt_node_nslots(n);
208 11016 uint32_t di = hamt_popcnt32(n->datamap & (bit - 1));
209 11016 uint32_t r = hamt_popcnt32(n->nodemap & (bit - 1));
210 11016 uint32_t ci = N - 1 - r;
211 11016 hamt_node *x = hamt_node_new(cx, N);
212 11016 ▶ if (!x) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!x) return NULL; 29 10987 11016
213 10987 x->datamap = n->datamap & ~bit;
214 10987 x->nodemap = n->nodemap | bit;
215 10987 memcpy(x->slot, n->slot, (size_t)di * sizeof(void *));
216 10987 memcpy(x->slot + di, n->slot + di + 1, (size_t)(ci - di) * sizeof(void *));
217 10987 x->slot[ci] = child;
218 10987 memcpy(x->slot + ci + 1, n->slot + ci + 1, (size_t)r * sizeof(void *));
219 10987 hamt_node_adopt(x, ci);
220 10987 return x;
221 }
222  
223 /* The reverse: a child that is down to one entry is folded away and the
224 * entry takes its position. The entry is shared, not handed over.
225 *
226 * old: e0 .. .. e(nd-1) | c(high) .. [C] .. c(low)
227 * new: e0 .. [E] .. e(nd-1) | c(high) .. .. c(low)
228 */
229 32101 static hamt_node *hamt_node_child_to_entry(void *cx, const hamt_node *n,
230 uint32_t bit, hamt_entry *e) {
231 32101 uint32_t N = hamt_node_nslots(n);
232 32101 uint32_t di = hamt_popcnt32(n->datamap & (bit - 1));
233 32101 uint32_t r = hamt_popcnt32(n->nodemap & (bit - 1));
234 32101 uint32_t ci = N - 1 - r;
235 32101 hamt_node *x = hamt_node_new(cx, N);
236 32101 ▶ if (!x) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!x) return NULL; 19 32082 32101
237 32082 x->datamap = n->datamap | bit;
238 32082 x->nodemap = n->nodemap & ~bit;
239 32082 memcpy(x->slot, n->slot, (size_t)di * sizeof(void *));
240 32082 x->slot[di] = e;
241 32082 memcpy(x->slot + di + 1, n->slot + di, (size_t)(ci - di) * sizeof(void *));
242 32082 memcpy(x->slot + ci + 1, n->slot + ci + 1, (size_t)r * sizeof(void *));
243 32082 hamt_node_adopt(x, HAMT_NO_SKIP);
244 32082 return x;
245 }
246  
247 /* A collision node with one more entry at its end. */
248 153017 static hamt_node *hamt_node_coll_add(void *cx, const hamt_node *n, hamt_entry *e) {
249 hamt_node *r;
250 /*
251 # uncoverable branch true
252 # uncoverable condition left count:1 */
253 153017 ▶ if (n->ncoll == 0xffffffffU) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (n->ncoll == 0xffffffffU) return NULL; 0 153017 153017
254 153017 r = hamt_node_new(cx, n->ncoll + 1);
255 153017 ▶ if (!r) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!r) return NULL; 24 152993 153017
256 152993 r->ncoll = n->ncoll + 1;
257 152993 memcpy(r->slot, n->slot, (size_t)n->ncoll * sizeof(void *));
258 152993 r->slot[n->ncoll] = e;
259 152993 hamt_node_adopt(r, HAMT_NO_SKIP);
260 152993 return r;
261 }
262  
263 /* A collision node without its i-th entry. Down to one, it is a node its
264 * parent will fold away at once. */
265 12727 static hamt_node *hamt_node_coll_drop(void *cx, const hamt_node *n, uint32_t i) {
266 12727 hamt_node *r = hamt_node_new(cx, n->ncoll - 1);
267 12727 ▶ if (!r) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!r) return NULL; 7 12720 12727
268 12720 r->ncoll = n->ncoll - 1;
269 12720 memcpy(r->slot, n->slot, (size_t)i * sizeof(void *));
270 12720 memcpy(r->slot + i, n->slot + i + 1, (size_t)(n->ncoll - 1 - i) * sizeof(void *));
271 12720 hamt_node_adopt(r, HAMT_NO_SKIP);
272 12720 return r;
273 }
274  
275 /* The smallest subtree that holds two entries with different keys, built to
276 * hang at `depth`. Where their hashes still agree it is a chain of nodes
277 * with one child each; where they part, a node with the two entries; and if
278 * they never part, a collision node. Both entries are shared. */
279 925804 static hamt_node *hamt_node_pair(void *cx, hamt_entry *a, hamt_entry *b,
280 unsigned depth) {
281 hamt_node *n, *sub;
282 unsigned ia, ib;
283  
284 925804 ▶ if (depth >= HAMT_MAX_DEPTH) {
Branch: 1 branch
BranchTrueFalseTotal
if (depth >= HAMT_MAX_DEPTH) { 62026 863778 925804
285 62026 n = hamt_node_new(cx, 2);
286 62026 ▶ if (!n) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return NULL; 46 61980 62026
287 61980 n->ncoll = 2;
288 61980 n->slot[0] = a;
289 61980 n->slot[1] = b;
290 61980 hamt_node_adopt(n, HAMT_NO_SKIP);
291 61980 return n;
292 }
293  
294 863778 ia = HAMT_IDX(a->hash, depth);
295 863778 ib = HAMT_IDX(b->hash, depth);
296 863778 ▶ if (ia != ib) {
Branch: 1 branch
BranchTrueFalseTotal
if (ia != ib) { 214654 649124 863778
297 214654 n = hamt_node_new(cx, 2);
298 214654 ▶ if (!n) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return NULL; 647 214007 214654
299 214007 n->datamap = ((uint32_t)1 << ia) | ((uint32_t)1 << ib);
300 214007 ▶ n->slot[0] = ia < ib ? a : b;
Branch: 1 branch
BranchTrueFalseTotal
n->slot[0] = ia < ib ? a : b; 118399 95608 214007
301 214007 ▶ n->slot[1] = ia < ib ? b : a;
Branch: 1 branch
BranchTrueFalseTotal
n->slot[1] = ia < ib ? b : a; 118399 95608 214007
302 214007 hamt_node_adopt(n, HAMT_NO_SKIP);
303 214007 return n;
304 }
305  
306 649124 sub = hamt_node_pair(cx, a, b, depth + 1);
307 649124 ▶ if (!sub) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!sub) return NULL; 2550 646574 649124
308 646574 n = hamt_node_new(cx, 1);
309 646574 ▶ if (!n) { hamt_node_release(cx, sub); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!n) { hamt_node_release(cx, sub); return NULL; } 470 646104 646574
310 646104 n->nodemap = (uint32_t)1 << ia;
311 646104 n->slot[0] = sub;
312 646104 return n;
313 }
314  
315 #endif
« include/hamt/hamt_entry.h ↑ Summary include/hamt/hamt_mut.h »