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 9649777 static uint32_t hamt_node_ndata(const hamt_node *n) {
66 9649777 ▶ return n->ncoll ? n->ncoll : hamt_popcnt32(n->datamap);
Branch: 1 branch
BranchTrueFalseTotal
return n->ncoll ? n->ncoll : hamt_popcnt32(n->datamap); 1377887 8271890 9649777
67 }
68  
69 21247055 static uint32_t hamt_node_nslots(const hamt_node *n) {
70 21247055 return n->ncoll ? n->ncoll
71 21247055 ▶ : hamt_popcnt32(n->datamap) + hamt_popcnt32(n->nodemap);
Branch: 1 branch
BranchTrueFalseTotal
: hamt_popcnt32(n->datamap) + hamt_popcnt32(n->nodemap); 711678 20535377 21247055
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 37048125 static int hamt_node_is_single(const hamt_node *n) {
77 37048125 ▶ if (n->ncoll) return n->ncoll == 1;
Branch: 1 branch
BranchTrueFalseTotal
if (n->ncoll) return n->ncoll == 1; 12693 37035432 37048125
78 3185953 ▶ return n->nodemap == 0 && n->datamap != 0
Branch: 1 branch
BranchTrueFalseTotal
return n->nodemap == 0 && n->datamap != 0 3185953 0 3185953
79 40221385 ▶ && (n->datamap & (n->datamap - 1)) == 0;
Branch: 2 branches
BranchTrueFalseTotal
&& (n->datamap & (n->datamap - 1)) == 0; 3185953 33849479 37035432
&& (n->datamap & (n->datamap - 1)) == 0; 38192 3147761 3185953
80 }
81  
82 2014045 static hamt_node *hamt_node_new(void *cx, uint32_t nslots) {
83 2014045 ▶ 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)); 2014045 0 2014045
84 2014045 ▶ if (!n) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return NULL; 2459 2011586 2014045
85 2011586 n->refcnt = 1;
86 2011586 n->datamap = 0;
87 2011586 n->nodemap = 0;
88 2011586 n->ncoll = 0;
89 (void)cx;
90 2011586 return n;
91 }
92  
93 3120062 static hamt_node *hamt_node_retain(hamt_node *n) {
94 /*
95 # uncoverable branch true
96 # uncoverable condition left count:1 */
97 3120062 ▶ 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 3120062 3120062
98 3120062 n->refcnt++;
99 3120062 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 1015758 static void hamt_node_adopt(hamt_node *n, uint32_t skip) {
106 1015758 uint32_t nd = hamt_node_ndata(n), N = hamt_node_nslots(n), i;
107 4720369 ▶ for (i = 0; i < nd; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nd; i++) 3704611 1015758 4720369
108 /*
109 # uncoverable branch false class:ignore_covered_err
110 # uncoverable condition right count:1 */
111 3704611 ▶ 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]); 3704611 0 3704611
112 3718638 ▶ for (; i < N; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (; i < N; i++) 2702880 1015758 3718638
113 2702880 ▶ 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]); 2364223 338657 2702880
114 1015758 }
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 4996503 static void hamt_node_release(void *cx, hamt_node *n) {
125 uint32_t nd, N, i;
126  
127 4996503 ▶ if (--n->refcnt) return;
Branch: 1 branch
BranchTrueFalseTotal
if (--n->refcnt) return; 3120062 1876441 4996503
128 1876441 nd = hamt_node_ndata(n);
129 1876441 N = hamt_node_nslots(n);
130  
131 1876441 ▶ if (N <= 32) {
Branch: 1 branch
BranchTrueFalseTotal
if (N <= 32) { 1866101 10340 1876441
132 void *s[32];
133 1866101 memcpy(s, n->slot, (size_t)N * sizeof(void *));
134 1866101 ▶ HAMT_FREE(cx, n, HAMT_NODE_BYTES(N));
Branch: 1 branch
BranchTrueFalseTotal
HAMT_FREE(cx, n, HAMT_NODE_BYTES(N)); 1866101 0 1866101
135 5704951 ▶ 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]); 3838850 1866101 5704951
136 5734429 ▶ 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]); 3868328 1866101 5734429
137 }
138 else {
139 740733 ▶ 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]); 730393 10340 740733
140 10340 ▶ HAMT_FREE(cx, n, HAMT_NODE_BYTES(N));
Branch: 1 branch
BranchTrueFalseTotal
HAMT_FREE(cx, n, HAMT_NODE_BYTES(N)); 10340 0 10340
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 467676 static hamt_node *hamt_node_with_slot(void *cx, const hamt_node *n, uint32_t i,
153 void *p, uint32_t skip) {
154 467676 uint32_t N = hamt_node_nslots(n);
155 467676 hamt_node *r = hamt_node_new(cx, N);
156 467676 ▶ if (!r) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!r) return NULL; 221 467455 467676
157 467455 r->datamap = n->datamap;
158 467455 r->nodemap = n->nodemap;
159 467455 r->ncoll = n->ncoll;
160 467455 memcpy(r->slot, n->slot, (size_t)N * sizeof(void *));
161 467455 r->slot[i] = p;
162 467455 hamt_node_adopt(r, skip);
163 467455 return r;
164 }
165  
166 /* An entry at a position that held nothing. */
167 38209 static hamt_node *hamt_node_add_entry(void *cx, const hamt_node *n,
168 uint32_t bit, hamt_entry *e) {
169 38209 uint32_t N = hamt_node_nslots(n);
170 38209 uint32_t di = hamt_popcnt32(n->datamap & (bit - 1));
171 38209 hamt_node *r = hamt_node_new(cx, N + 1);
172 38209 ▶ if (!r) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!r) return NULL; 130 38079 38209
173 38079 r->datamap = n->datamap | bit;
174 38079 r->nodemap = n->nodemap;
175 38079 memcpy(r->slot, n->slot, (size_t)di * sizeof(void *));
176 38079 r->slot[di] = e;
177 38079 memcpy(r->slot + di + 1, n->slot + di, (size_t)(N - di) * sizeof(void *));
178 38079 hamt_node_adopt(r, HAMT_NO_SKIP);
179 38079 return r;
180 }
181  
182 /* The entry at a position gone, and nothing in its place. */
183 22479 static hamt_node *hamt_node_drop_entry(void *cx, const hamt_node *n, uint32_t bit) {
184 22479 uint32_t N = hamt_node_nslots(n);
185 22479 uint32_t di = hamt_popcnt32(n->datamap & (bit - 1));
186 22479 hamt_node *r = hamt_node_new(cx, N - 1);
187 22479 ▶ if (!r) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!r) return NULL; 156 22323 22479
188 22323 r->datamap = n->datamap & ~bit;
189 22323 r->nodemap = n->nodemap;
190 22323 memcpy(r->slot, n->slot, (size_t)di * sizeof(void *));
191 22323 memcpy(r->slot + di, n->slot + di + 1, (size_t)(N - 1 - di) * sizeof(void *));
192 22323 hamt_node_adopt(r, HAMT_NO_SKIP);
193 22323 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 11075 static hamt_node *hamt_node_entry_to_child(void *cx, const hamt_node *n,
206 uint32_t bit, hamt_node *child) {
207 11075 uint32_t N = hamt_node_nslots(n);
208 11075 uint32_t di = hamt_popcnt32(n->datamap & (bit - 1));
209 11075 uint32_t r = hamt_popcnt32(n->nodemap & (bit - 1));
210 11075 uint32_t ci = N - 1 - r;
211 11075 hamt_node *x = hamt_node_new(cx, N);
212 11075 ▶ if (!x) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!x) return NULL; 29 11046 11075
213 11046 x->datamap = n->datamap & ~bit;
214 11046 x->nodemap = n->nodemap | bit;
215 11046 memcpy(x->slot, n->slot, (size_t)di * sizeof(void *));
216 11046 memcpy(x->slot + di, n->slot + di + 1, (size_t)(ci - di) * sizeof(void *));
217 11046 x->slot[ci] = child;
218 11046 memcpy(x->slot + ci + 1, n->slot + ci + 1, (size_t)r * sizeof(void *));
219 11046 hamt_node_adopt(x, ci);
220 11046 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 31924 static hamt_node *hamt_node_child_to_entry(void *cx, const hamt_node *n,
230 uint32_t bit, hamt_entry *e) {
231 31924 uint32_t N = hamt_node_nslots(n);
232 31924 uint32_t di = hamt_popcnt32(n->datamap & (bit - 1));
233 31924 uint32_t r = hamt_popcnt32(n->nodemap & (bit - 1));
234 31924 uint32_t ci = N - 1 - r;
235 31924 hamt_node *x = hamt_node_new(cx, N);
236 31924 ▶ if (!x) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!x) return NULL; 19 31905 31924
237 31905 x->datamap = n->datamap | bit;
238 31905 x->nodemap = n->nodemap & ~bit;
239 31905 memcpy(x->slot, n->slot, (size_t)di * sizeof(void *));
240 31905 x->slot[di] = e;
241 31905 memcpy(x->slot + di + 1, n->slot + di, (size_t)(ci - di) * sizeof(void *));
242 31905 memcpy(x->slot + ci + 1, n->slot + ci + 1, (size_t)r * sizeof(void *));
243 31905 hamt_node_adopt(x, HAMT_NO_SKIP);
244 31905 return x;
245 }
246  
247 /* A collision node with one more entry at its end. */
248 152439 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 152439 ▶ if (n->ncoll == 0xffffffffU) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (n->ncoll == 0xffffffffU) return NULL; 0 152439 152439
254 152439 r = hamt_node_new(cx, n->ncoll + 1);
255 152439 ▶ if (!r) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!r) return NULL; 24 152415 152439
256 152415 r->ncoll = n->ncoll + 1;
257 152415 memcpy(r->slot, n->slot, (size_t)n->ncoll * sizeof(void *));
258 152415 r->slot[n->ncoll] = e;
259 152415 hamt_node_adopt(r, HAMT_NO_SKIP);
260 152415 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 12700 static hamt_node *hamt_node_coll_drop(void *cx, const hamt_node *n, uint32_t i) {
266 12700 hamt_node *r = hamt_node_new(cx, n->ncoll - 1);
267 12700 ▶ if (!r) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!r) return NULL; 7 12693 12700
268 12693 r->ncoll = n->ncoll - 1;
269 12693 memcpy(r->slot, n->slot, (size_t)i * sizeof(void *));
270 12693 memcpy(r->slot + i, n->slot + i + 1, (size_t)(n->ncoll - 1 - i) * sizeof(void *));
271 12693 hamt_node_adopt(r, HAMT_NO_SKIP);
272 12693 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 920547 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 920547 ▶ if (depth >= HAMT_MAX_DEPTH) {
Branch: 1 branch
BranchTrueFalseTotal
if (depth >= HAMT_MAX_DEPTH) { 61549 858998 920547
285 61549 n = hamt_node_new(cx, 2);
286 61549 ▶ if (!n) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return NULL; 46 61503 61549
287 61503 n->ncoll = 2;
288 61503 n->slot[0] = a;
289 61503 n->slot[1] = b;
290 61503 hamt_node_adopt(n, HAMT_NO_SKIP);
291 61503 return n;
292 }
293  
294 858998 ia = HAMT_IDX(a->hash, depth);
295 858998 ib = HAMT_IDX(b->hash, depth);
296 858998 ▶ if (ia != ib) {
Branch: 1 branch
BranchTrueFalseTotal
if (ia != ib) { 213950 645048 858998
297 213950 n = hamt_node_new(cx, 2);
298 213950 ▶ if (!n) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return NULL; 645 213305 213950
299 213305 n->datamap = ((uint32_t)1 << ia) | ((uint32_t)1 << ib);
300 213305 ▶ n->slot[0] = ia < ib ? a : b;
Branch: 1 branch
BranchTrueFalseTotal
n->slot[0] = ia < ib ? a : b; 116718 96587 213305
301 213305 ▶ n->slot[1] = ia < ib ? b : a;
Branch: 1 branch
BranchTrueFalseTotal
n->slot[1] = ia < ib ? b : a; 116718 96587 213305
302 213305 hamt_node_adopt(n, HAMT_NO_SKIP);
303 213305 return n;
304 }
305  
306 645048 sub = hamt_node_pair(cx, a, b, depth + 1);
307 645048 ▶ if (!sub) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!sub) return NULL; 2552 642496 645048
308 642496 n = hamt_node_new(cx, 1);
309 642496 ▶ if (!n) { hamt_node_release(cx, sub); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!n) { hamt_node_release(cx, sub); return NULL; } 472 642024 642496
310 642024 n->nodemap = (uint32_t)1 << ia;
311 642024 n->slot[0] = sub;
312 642024 return n;
313 }
314  
315 #endif
« include/hamt/hamt_entry.h ↑ Summary include/hamt/hamt_mut.h »