include/hamt/hamt_node.h

filter: Statementstmt 100% 146 / 146 Branchbran 73.6% 53 / 72 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 91.2% 199 / 218 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/hamt/hamt_xs.h ↑ Summary include/hamt/hamt_pool.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 8688312 static uint32_t hamt_node_ndata(const hamt_node *n) {
66 8688312 ▶ return n->ncoll ? n->ncoll : hamt_popcnt32(n->datamap);
Branch: 1 branch
BranchTrueFalseTotal
return n->ncoll ? n->ncoll : hamt_popcnt32(n->datamap); 1348100 7340212 8688312
67 }
68  
69 19542489 static uint32_t hamt_node_nslots(const hamt_node *n) {
70 19542489 return n->ncoll ? n->ncoll
71 19542489 ▶ : hamt_popcnt32(n->datamap) + hamt_popcnt32(n->nodemap);
Branch: 1 branch
BranchTrueFalseTotal
: hamt_popcnt32(n->datamap) + hamt_popcnt32(n->nodemap); 684859 18857630 19542489
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 35868451 static int hamt_node_is_single(const hamt_node *n) {
77 35868451 ▶ if (n->ncoll) return n->ncoll == 1;
Branch: 1 branch
BranchTrueFalseTotal
if (n->ncoll) return n->ncoll == 1; 12490 35855961 35868451
78 2887080 ▶ return n->nodemap == 0 && n->datamap != 0
Branch: 1 branch
BranchTrueFalseTotal
return n->nodemap == 0 && n->datamap != 0 2887080 0 2887080
79 38743041 ▶ && (n->datamap & (n->datamap - 1)) == 0;
Branch: 2 branches
BranchTrueFalseTotal
&& (n->datamap & (n->datamap - 1)) == 0; 2887080 32968881 35855961
&& (n->datamap & (n->datamap - 1)) == 0; 32940 2854140 2887080
80 }
81  
82 1645298 static hamt_node *hamt_node_new(void *cx, uint32_t nslots) {
83 1645298 ▶ 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)); 1645298 0 1645298
84 1645298 ▶ if (!n) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return NULL; 0 1645298 1645298
85 1645298 n->refcnt = 1;
86 1645298 n->datamap = 0;
87 1645298 n->nodemap = 0;
88 1645298 n->ncoll = 0;
89 (void)cx;
90 1645298 return n;
91 }
92  
93 2973841 static hamt_node *hamt_node_retain(hamt_node *n) {
94 2973841 ▶ 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 2973841 2973841
95 2973841 n->refcnt++;
96 2973841 return n;
97 }
98  
99 /* A node that has just been filled with borrowed pointers takes its own
100 * reference to each. `skip` is the one slot whose reference the caller is
101 * handing over rather than sharing, or HAMT_NO_SKIP. */
102 829591 static void hamt_node_adopt(hamt_node *n, uint32_t skip) {
103 829591 uint32_t nd = hamt_node_ndata(n), N = hamt_node_nslots(n), i;
104 4014724 ▶ for (i = 0; i < nd; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nd; i++) 3185133 829591 4014724
105 3185133 ▶ 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]); 3185133 0 3185133
106 3425486 ▶ for (; i < N; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (; i < N; i++) 2595895 829591 3425486
107 2595895 ▶ 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]); 2274560 321335 2595895
108 829591 }
109  
110 /* At zero the node is freed BEFORE anything it held is released, the same
111 * rule as an entry and for the same reason: a value's release can run code
112 * that comes back in here. The slots are copied out to make that possible.
113 *
114 * A collision node can be wider than any buffer worth putting on the stack,
115 * so a wide one releases in place and is freed after. That is safe too - a
116 * node whose count has reached zero is reachable from nowhere, so nothing
117 * that runs can find it - it is only not the same order. */
118 4544150 static void hamt_node_release(void *cx, hamt_node *n) {
119 uint32_t nd, N, i;
120  
121 4544150 ▶ if (--n->refcnt) return;
Branch: 1 branch
BranchTrueFalseTotal
if (--n->refcnt) return; 2973841 1570309 4544150
122 1570309 nd = hamt_node_ndata(n);
123 1570309 N = hamt_node_nslots(n);
124  
125 1570309 ▶ if (N <= 32) {
Branch: 1 branch
BranchTrueFalseTotal
if (N <= 32) { 1559858 10451 1570309
126 void *s[32];
127 1559858 memcpy(s, n->slot, (size_t)N * sizeof(void *));
128 1559858 ▶ HAMT_FREE(cx, n, HAMT_NODE_BYTES(N));
Branch: 1 branch
BranchTrueFalseTotal
HAMT_FREE(cx, n, HAMT_NODE_BYTES(N)); 1559858 0 1559858
129 4507842 ▶ 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]); 2947984 1559858 4507842
130 5082656 ▶ 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]); 3522798 1559858 5082656
131 }
132 else {
133 744502 ▶ 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]); 734051 10451 744502
134 10451 ▶ HAMT_FREE(cx, n, HAMT_NODE_BYTES(N));
Branch: 1 branch
BranchTrueFalseTotal
HAMT_FREE(cx, n, HAMT_NODE_BYTES(N)); 10451 0 10451
135 }
136 }
137  
138 /* ---- copies with one thing different ----------------------------------
139 *
140 * Each returns a new node holding one reference to everything in it, or
141 * NULL when there is no memory, in which case nothing was retained. The
142 * node copied from is untouched. */
143  
144 /* The same node with slot i pointing somewhere else. `skip` as for adopt:
145 * pass i to hand over the caller's reference to p, HAMT_NO_SKIP to share. */
146 439957 static hamt_node *hamt_node_with_slot(void *cx, const hamt_node *n, uint32_t i,
147 void *p, uint32_t skip) {
148 439957 uint32_t N = hamt_node_nslots(n);
149 439957 hamt_node *r = hamt_node_new(cx, N);
150 439957 ▶ if (!r) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!r) return NULL; 0 439957 439957
151 439957 r->datamap = n->datamap;
152 439957 r->nodemap = n->nodemap;
153 439957 r->ncoll = n->ncoll;
154 439957 memcpy(r->slot, n->slot, (size_t)N * sizeof(void *));
155 439957 r->slot[i] = p;
156 439957 hamt_node_adopt(r, skip);
157 439957 return r;
158 }
159  
160 /* An entry at a position that held nothing. */
161 17666 static hamt_node *hamt_node_add_entry(void *cx, const hamt_node *n,
162 uint32_t bit, hamt_entry *e) {
163 17666 uint32_t N = hamt_node_nslots(n);
164 17666 uint32_t di = hamt_popcnt32(n->datamap & (bit - 1));
165 17666 hamt_node *r = hamt_node_new(cx, N + 1);
166 17666 ▶ if (!r) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!r) return NULL; 0 17666 17666
167 17666 r->datamap = n->datamap | bit;
168 17666 r->nodemap = n->nodemap;
169 17666 memcpy(r->slot, n->slot, (size_t)di * sizeof(void *));
170 17666 r->slot[di] = e;
171 17666 memcpy(r->slot + di + 1, n->slot + di, (size_t)(N - di) * sizeof(void *));
172 17666 hamt_node_adopt(r, HAMT_NO_SKIP);
173 17666 return r;
174 }
175  
176 /* The entry at a position gone, and nothing in its place. */
177 13285 static hamt_node *hamt_node_drop_entry(void *cx, const hamt_node *n, uint32_t bit) {
178 13285 uint32_t N = hamt_node_nslots(n);
179 13285 uint32_t di = hamt_popcnt32(n->datamap & (bit - 1));
180 13285 hamt_node *r = hamt_node_new(cx, N - 1);
181 13285 ▶ if (!r) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!r) return NULL; 0 13285 13285
182 13285 r->datamap = n->datamap & ~bit;
183 13285 r->nodemap = n->nodemap;
184 13285 memcpy(r->slot, n->slot, (size_t)di * sizeof(void *));
185 13285 memcpy(r->slot + di, n->slot + di + 1, (size_t)(N - 1 - di) * sizeof(void *));
186 13285 hamt_node_adopt(r, HAMT_NO_SKIP);
187 13285 return r;
188 }
189  
190 /* The entry at a position replaced by a child: a second key arrived there.
191 * The caller's reference to the child is handed over.
192 *
193 * The entry leaves the front and the child joins the back, so every slot
194 * between the two moves down by one and nothing outside them moves:
195 *
196 * old: e0 .. [E] .. e(nd-1) | c(high) .. .. c(low)
197 * new: e0 .. .. e(nd-1) | c(high) .. [C] .. c(low)
198 */
199 6490 static hamt_node *hamt_node_entry_to_child(void *cx, const hamt_node *n,
200 uint32_t bit, hamt_node *child) {
201 6490 uint32_t N = hamt_node_nslots(n);
202 6490 uint32_t di = hamt_popcnt32(n->datamap & (bit - 1));
203 6490 uint32_t r = hamt_popcnt32(n->nodemap & (bit - 1));
204 6490 uint32_t ci = N - 1 - r;
205 6490 hamt_node *x = hamt_node_new(cx, N);
206 6490 ▶ if (!x) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!x) return NULL; 0 6490 6490
207 6490 x->datamap = n->datamap & ~bit;
208 6490 x->nodemap = n->nodemap | bit;
209 6490 memcpy(x->slot, n->slot, (size_t)di * sizeof(void *));
210 6490 memcpy(x->slot + di, n->slot + di + 1, (size_t)(ci - di) * sizeof(void *));
211 6490 x->slot[ci] = child;
212 6490 memcpy(x->slot + ci + 1, n->slot + ci + 1, (size_t)r * sizeof(void *));
213 6490 hamt_node_adopt(x, ci);
214 6490 return x;
215 }
216  
217 /* The reverse: a child that is down to one entry is folded away and the
218 * entry takes its position. The entry is shared, not handed over.
219 *
220 * old: e0 .. .. e(nd-1) | c(high) .. [C] .. c(low)
221 * new: e0 .. [E] .. e(nd-1) | c(high) .. .. c(low)
222 */
223 30859 static hamt_node *hamt_node_child_to_entry(void *cx, const hamt_node *n,
224 uint32_t bit, hamt_entry *e) {
225 30859 uint32_t N = hamt_node_nslots(n);
226 30859 uint32_t di = hamt_popcnt32(n->datamap & (bit - 1));
227 30859 uint32_t r = hamt_popcnt32(n->nodemap & (bit - 1));
228 30859 uint32_t ci = N - 1 - r;
229 30859 hamt_node *x = hamt_node_new(cx, N);
230 30859 ▶ if (!x) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!x) return NULL; 0 30859 30859
231 30859 x->datamap = n->datamap | bit;
232 30859 x->nodemap = n->nodemap & ~bit;
233 30859 memcpy(x->slot, n->slot, (size_t)di * sizeof(void *));
234 30859 x->slot[di] = e;
235 30859 memcpy(x->slot + di + 1, n->slot + di, (size_t)(ci - di) * sizeof(void *));
236 30859 memcpy(x->slot + ci + 1, n->slot + ci + 1, (size_t)r * sizeof(void *));
237 30859 hamt_node_adopt(x, HAMT_NO_SKIP);
238 30859 return x;
239 }
240  
241 /* A collision node with one more entry at its end. */
242 151363 static hamt_node *hamt_node_coll_add(void *cx, const hamt_node *n, hamt_entry *e) {
243 hamt_node *r;
244 151363 ▶ if (n->ncoll == 0xffffffffU) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (n->ncoll == 0xffffffffU) return NULL; 0 151363 151363
245 151363 r = hamt_node_new(cx, n->ncoll + 1);
246 151363 ▶ if (!r) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!r) return NULL; 0 151363 151363
247 151363 r->ncoll = n->ncoll + 1;
248 151363 memcpy(r->slot, n->slot, (size_t)n->ncoll * sizeof(void *));
249 151363 r->slot[n->ncoll] = e;
250 151363 hamt_node_adopt(r, HAMT_NO_SKIP);
251 151363 return r;
252 }
253  
254 /* A collision node without its i-th entry. Down to one, it is a node its
255 * parent will fold away at once. */
256 12490 static hamt_node *hamt_node_coll_drop(void *cx, const hamt_node *n, uint32_t i) {
257 12490 hamt_node *r = hamt_node_new(cx, n->ncoll - 1);
258 12490 ▶ if (!r) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!r) return NULL; 0 12490 12490
259 12490 r->ncoll = n->ncoll - 1;
260 12490 memcpy(r->slot, n->slot, (size_t)i * sizeof(void *));
261 12490 memcpy(r->slot + i, n->slot + i + 1, (size_t)(n->ncoll - 1 - i) * sizeof(void *));
262 12490 hamt_node_adopt(r, HAMT_NO_SKIP);
263 12490 return r;
264 }
265  
266 /* The smallest subtree that holds two entries with different keys, built to
267 * hang at `depth`. Where their hashes still agree it is a chain of nodes
268 * with one child each; where they part, a node with the two entries; and if
269 * they never part, a collision node. Both entries are shared. */
270 708337 static hamt_node *hamt_node_pair(void *cx, hamt_entry *a, hamt_entry *b,
271 unsigned depth) {
272 hamt_node *n, *sub;
273 unsigned ia, ib;
274  
275 708337 ▶ if (depth >= HAMT_MAX_DEPTH) {
Branch: 1 branch
BranchTrueFalseTotal
if (depth >= HAMT_MAX_DEPTH) { 52840 655497 708337
276 52840 n = hamt_node_new(cx, 2);
277 52840 ▶ if (!n) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return NULL; 0 52840 52840
278 52840 n->ncoll = 2;
279 52840 n->slot[0] = a;
280 52840 n->slot[1] = b;
281 52840 hamt_node_adopt(n, HAMT_NO_SKIP);
282 52840 return n;
283 }
284  
285 655497 ia = HAMT_IDX(a->hash, depth);
286 655497 ib = HAMT_IDX(b->hash, depth);
287 655497 ▶ if (ia != ib) {
Branch: 1 branch
BranchTrueFalseTotal
if (ia != ib) { 99731 555766 655497
288 99731 n = hamt_node_new(cx, 2);
289 99731 ▶ if (!n) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return NULL; 0 99731 99731
290 99731 n->datamap = ((uint32_t)1 << ia) | ((uint32_t)1 << ib);
291 99731 ▶ n->slot[0] = ia < ib ? a : b;
Branch: 1 branch
BranchTrueFalseTotal
n->slot[0] = ia < ib ? a : b; 50488 49243 99731
292 99731 ▶ n->slot[1] = ia < ib ? b : a;
Branch: 1 branch
BranchTrueFalseTotal
n->slot[1] = ia < ib ? b : a; 50488 49243 99731
293 99731 hamt_node_adopt(n, HAMT_NO_SKIP);
294 99731 return n;
295 }
296  
297 555766 sub = hamt_node_pair(cx, a, b, depth + 1);
298 555766 ▶ if (!sub) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!sub) return NULL; 0 555766 555766
299 555766 n = hamt_node_new(cx, 1);
300 555766 ▶ if (!n) { hamt_node_release(cx, sub); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!n) { hamt_node_release(cx, sub); return NULL; } 0 555766 555766
301 555766 n->nodemap = (uint32_t)1 << ia;
302 555766 n->slot[0] = sub;
303 555766 return n;
304 }
305  
306 #endif
« include/hamt/hamt_xs.h ↑ Summary include/hamt/hamt_pool.h »