| 1 |
|
| #ifndef HAMT_MUT_H |
| 2 |
|
| #define HAMT_MUT_H |
| 3 |
|
| |
| 4 |
|
| /* Insert and delete that change a trie where it stands, wherever that is |
| 5 |
|
| * safe, and copy only where it is not. |
| 6 |
|
| * |
| 7 |
|
| * WHEN IT IS SAFE. A node may be changed in place exactly when its |
| 8 |
|
| * reference count is one. The caller of these functions owns one reference |
| 9 |
|
| * to the root and hands it over; if that is the only reference there is, |
| 10 |
|
| * nobody else can reach the root, so nobody else can see it change. The |
| 11 |
|
| * same holds one level down: a child with a count of one is pointed at by |
| 12 |
|
| * that parent alone, and the walk only reaches it through a parent it has |
| 13 |
|
| * just found to be its own. A node with any other count is shared with |
| 14 |
|
| * something - an older version, a snapshot, a walk in progress - and is |
| 15 |
|
| * copied instead; the copy takes its own reference to each child, which |
| 16 |
|
| * makes every child shared too, so everything under a shared node is copied |
| 17 |
|
| * in turn without anything having to say so. |
| 18 |
|
| * |
| 19 |
|
| * This is why a batch of updates on a fresh handle costs one path of copies |
| 20 |
|
| * for the first update and almost nothing for the rest: after the first, |
| 21 |
|
| * the path is the handle's alone. |
| 22 |
|
| * |
| 23 |
|
| * It rests on one rule that every caller keeps: NOBODY HOLDS A NODE WITHOUT |
| 24 |
|
| * COUNTING IT. A pointer kept across a call here without a reference of its |
| 25 |
|
| * own is a pointer to something that may have been changed or freed. |
| 26 |
|
| * |
| 27 |
|
| * THE DISPLACED ENTRY. An update displaces at most one entry: the one whose |
| 28 |
|
| * value is replaced, or the one deleted. Releasing it can run a value's |
| 29 |
|
| * destructor, which can call back into the very handle being updated, so it |
| 30 |
|
| * is not released here. It is handed back through `dead`, with a reference, |
| 31 |
|
| * for the caller to release once the new root is in place. |
| 32 |
|
| * |
| 33 |
|
| * NO MEMORY. Every function either does all of its work or none: on failure |
| 34 |
|
| * the trie is exactly as it was and the caller still owns the reference it |
| 35 |
|
| * passed in. Nothing is changed in place until every allocation the change |
| 36 |
|
| * needs has succeeded. */ |
| 37 |
|
| |
| 38 |
|
| #define HAMT_EDITABLE(n) ((n)->refcnt == 1) |
| 39 |
|
| |
| 40 |
|
| /* The node with an entry added at a position that held nothing. If it has |
| 41 |
|
| * room, it is changed where it stands and comes back itself. If not, it is |
| 42 |
|
| * a new allocation that takes over the old node's references rather than |
| 43 |
|
| * adding to them, and the old node's memory is freed. NULL when there is no |
| 44 |
|
| * memory, with the node untouched. */ |
| 45 |
626106 |
| static hamt_node *hamt_node_add_entry_move(void *cx, hamt_node *n, uint32_t bit, |
| 46 |
|
| hamt_entry *e) { |
| 47 |
626106 |
| uint32_t N = hamt_node_nslots(n); |
| 48 |
626106 |
| uint32_t di = hamt_popcnt32(n->datamap & (bit - 1)); |
| 49 |
|
| hamt_node *r; |
| 50 |
|
| |
| 51 |
|
| /* Room for one more where it stands: nothing is allocated. */ |
| 52 |
626106 |
▶ | if (HAMT_NODE_ROOM(N + 1) == HAMT_NODE_ROOM(N) && N) { /* # uncoverable condition right count:2 */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (HAMT_NODE_ROOM(N + 1) == HAMT_NODE_ROOM(N) && N) { /* # uncoverable condition right count:2 */ |
493471 |
132635 |
626106 |
| if (HAMT_NODE_ROOM(N + 1) == HAMT_NODE_ROOM(N) && N) { /* # uncoverable condition right count:2 */ |
493471 |
0 |
493471 |
|
| 53 |
493471 |
| memmove(n->slot + di + 1, n->slot + di, (size_t)(N - di) * sizeof(void *)); |
| 54 |
493471 |
| n->slot[di] = hamt_entry_retain(e); |
| 55 |
493471 |
| n->datamap |= bit; |
| 56 |
493471 |
| return n; |
| 57 |
|
| } |
| 58 |
132635 |
| r = hamt_node_new(cx, N + 1); |
| 59 |
132635 |
▶ | if (!r) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!r) return NULL; |
309 |
132326 |
132635 |
|
| 60 |
132326 |
| r->datamap = n->datamap | bit; |
| 61 |
132326 |
| r->nodemap = n->nodemap; |
| 62 |
132326 |
| memcpy(r->slot, n->slot, (size_t)di * sizeof(void *)); |
| 63 |
132326 |
| r->slot[di] = hamt_entry_retain(e); |
| 64 |
132326 |
| memcpy(r->slot + di + 1, n->slot + di, (size_t)(N - di) * sizeof(void *)); |
| 65 |
132326 |
▶ | HAMT_FREE(cx, n, HAMT_NODE_BYTES(N)); |
Branch: 1 branch
| Branch | True | False | Total |
| HAMT_FREE(cx, n, HAMT_NODE_BYTES(N)); |
132326 |
0 |
132326 |
|
| 66 |
132326 |
| return r; |
| 67 |
|
| } |
| 68 |
|
| |
| 69 |
|
| /* The node without the entry at a position, the same way. The entry is |
| 70 |
|
| * handed back with the reference the node held. */ |
| 71 |
13182 |
| static hamt_node *hamt_node_drop_entry_move(void *cx, hamt_node *n, uint32_t bit, |
| 72 |
|
| hamt_entry **dropped) { |
| 73 |
13182 |
| uint32_t N = hamt_node_nslots(n); |
| 74 |
13182 |
| uint32_t di = hamt_popcnt32(n->datamap & (bit - 1)); |
| 75 |
|
| hamt_node *r; |
| 76 |
|
| |
| 77 |
|
| /* One fewer still needs the room it has: it stays where it is. */ |
| 78 |
13182 |
▶ | if (HAMT_NODE_ROOM(N - 1) == HAMT_NODE_ROOM(N)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (HAMT_NODE_ROOM(N - 1) == HAMT_NODE_ROOM(N)) { |
10284 |
2898 |
13182 |
|
| 79 |
10284 |
| *dropped = (hamt_entry *)n->slot[di]; |
| 80 |
10284 |
| memmove(n->slot + di, n->slot + di + 1, (size_t)(N - 1 - di) * sizeof(void *)); |
| 81 |
10284 |
| n->datamap &= ~bit; |
| 82 |
10284 |
| return n; |
| 83 |
|
| } |
| 84 |
2898 |
| r = hamt_node_new(cx, N - 1); |
| 85 |
2898 |
▶ | if (!r) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!r) return NULL; |
79 |
2819 |
2898 |
|
| 86 |
2819 |
| r->datamap = n->datamap & ~bit; |
| 87 |
2819 |
| r->nodemap = n->nodemap; |
| 88 |
2819 |
| *dropped = (hamt_entry *)n->slot[di]; |
| 89 |
2819 |
| memcpy(r->slot, n->slot, (size_t)di * sizeof(void *)); |
| 90 |
2819 |
| memcpy(r->slot + di, n->slot + di + 1, (size_t)(N - 1 - di) * sizeof(void *)); |
| 91 |
2819 |
▶ | HAMT_FREE(cx, n, HAMT_NODE_BYTES(N)); |
Branch: 1 branch
| Branch | True | False | Total |
| HAMT_FREE(cx, n, HAMT_NODE_BYTES(N)); |
2819 |
0 |
2819 |
|
| 92 |
2819 |
| return r; |
| 93 |
|
| } |
| 94 |
|
| |
| 95 |
|
| /* Same size, so no allocation: the entry at a position gives way to a |
| 96 |
|
| * child, which takes over the caller's reference. The entry is handed back |
| 97 |
|
| * with the reference the node held. */ |
| 98 |
238754 |
| static hamt_entry *hamt_node_entry_to_child_here(hamt_node *n, uint32_t bit, |
| 99 |
|
| hamt_node *child) { |
| 100 |
238754 |
| uint32_t N = hamt_node_nslots(n); |
| 101 |
238754 |
| uint32_t di = hamt_popcnt32(n->datamap & (bit - 1)); |
| 102 |
238754 |
| uint32_t ci = N - 1 - hamt_popcnt32(n->nodemap & (bit - 1)); |
| 103 |
238754 |
| hamt_entry *old = (hamt_entry *)n->slot[di]; |
| 104 |
238754 |
| memmove(n->slot + di, n->slot + di + 1, (size_t)(ci - di) * sizeof(void *)); |
| 105 |
238754 |
| n->slot[ci] = child; |
| 106 |
238754 |
| n->datamap &= ~bit; |
| 107 |
238754 |
| n->nodemap |= bit; |
| 108 |
238754 |
| return old; |
| 109 |
|
| } |
| 110 |
|
| |
| 111 |
|
| /* And back: the child at a position gives way to an entry, which takes over |
| 112 |
|
| * the caller's reference. The child is handed back with the reference the |
| 113 |
|
| * node held. */ |
| 114 |
6110 |
| static hamt_node *hamt_node_child_to_entry_here(hamt_node *n, uint32_t bit, |
| 115 |
|
| hamt_entry *e) { |
| 116 |
6110 |
| uint32_t N = hamt_node_nslots(n); |
| 117 |
6110 |
| uint32_t di = hamt_popcnt32(n->datamap & (bit - 1)); |
| 118 |
6110 |
| uint32_t ci = N - 1 - hamt_popcnt32(n->nodemap & (bit - 1)); |
| 119 |
6110 |
| hamt_node *old = (hamt_node *)n->slot[ci]; |
| 120 |
6110 |
| memmove(n->slot + di + 1, n->slot + di, (size_t)(ci - di) * sizeof(void *)); |
| 121 |
6110 |
| n->slot[di] = e; |
| 122 |
6110 |
| n->datamap |= bit; |
| 123 |
6110 |
| n->nodemap &= ~bit; |
| 124 |
6110 |
| return old; |
| 125 |
|
| } |
| 126 |
|
| |
| 127 |
|
| /* Takes over the caller's reference to `n` and returns a node the caller |
| 128 |
|
| * owns one reference to, which may be `n` itself. NULL means no memory: `n` |
| 129 |
|
| * is untouched and still the caller's. */ |
| 130 |
3879434 |
| static hamt_node *hamt_node_put_mut(void *cx, hamt_node *n, unsigned depth, |
| 131 |
|
| hamt_entry *e, int *added, hamt_entry **dead) { |
| 132 |
3879434 |
| int edit = HAMT_EDITABLE(n); |
| 133 |
|
| uint32_t bit, i; |
| 134 |
|
| hamt_node *r; |
| 135 |
|
| |
| 136 |
3879434 |
▶ | if (n->ncoll) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n->ncoll) { |
169825 |
3709609 |
3879434 |
|
| 137 |
1963391 |
▶ | for (i = 0; i < n->ncoll; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n->ncoll; i++) { |
1813559 |
149832 |
1963391 |
|
| 138 |
1813559 |
| hamt_entry *old = (hamt_entry *)n->slot[i]; |
| 139 |
1813559 |
▶ | if (!hamt_entry_key_eq(old, e->key, e->klen, e->utf8, e->hash)) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!hamt_entry_key_eq(old, e->key, e->klen, e->utf8, e->hash)) continue; |
1793566 |
19993 |
1813559 |
|
| 140 |
19993 |
▶ | if (old == e || old->val == e->val) return n; /* # uncoverable condition left count:1 */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (old == e || old->val == e->val) return n; /* # uncoverable condition left count:1 */ |
19993 |
0 |
19993 |
| if (old == e || old->val == e->val) return n; /* # uncoverable condition left count:1 */ |
109 |
19884 |
19993 |
|
| 141 |
19884 |
▶ | if (edit) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (edit) { |
5602 |
14282 |
19884 |
|
| 142 |
5602 |
| n->slot[i] = hamt_entry_retain(e); |
| 143 |
5602 |
| *dead = old; |
| 144 |
5602 |
| return n; |
| 145 |
|
| } |
| 146 |
14282 |
| r = hamt_node_with_slot(cx, n, i, e, HAMT_NO_SKIP); |
| 147 |
14282 |
▶ | if (!r) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!r) return NULL; |
1 |
14281 |
14282 |
|
| 148 |
14281 |
| *dead = hamt_entry_retain(old); |
| 149 |
14281 |
| hamt_node_release(cx, n); |
| 150 |
14281 |
| return r; |
| 151 |
|
| } |
| 152 |
|
| /* A collision node is rare enough to grow by copying whether or not |
| 153 |
|
| * it is ours. The copy holds every entry, so letting the old node |
| 154 |
|
| * go releases nothing that matters. */ |
| 155 |
149832 |
| r = hamt_node_coll_add(cx, n, e); |
| 156 |
149832 |
▶ | if (!r) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!r) return NULL; |
16 |
149816 |
149832 |
|
| 157 |
149816 |
| *added = 1; |
| 158 |
149816 |
| hamt_node_release(cx, n); |
| 159 |
149816 |
| return r; |
| 160 |
|
| } |
| 161 |
|
| |
| 162 |
3709609 |
| bit = HAMT_BIT(e->hash, depth); |
| 163 |
|
| |
| 164 |
3709609 |
▶ | if (n->datamap & bit) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n->datamap & bit) { |
294804 |
3414805 |
3709609 |
|
| 165 |
294804 |
| uint32_t di = hamt_popcnt32(n->datamap & (bit - 1)); |
| 166 |
294804 |
| hamt_entry *old = (hamt_entry *)n->slot[di]; |
| 167 |
|
| hamt_node *sub; |
| 168 |
|
| |
| 169 |
294804 |
▶ | if (hamt_entry_key_eq(old, e->key, e->klen, e->utf8, e->hash)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (hamt_entry_key_eq(old, e->key, e->klen, e->utf8, e->hash)) { |
45225 |
249579 |
294804 |
|
| 170 |
45225 |
▶ | if (old == e || old->val == e->val) return n; /* # uncoverable condition left count:1 */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (old == e || old->val == e->val) return n; /* # uncoverable condition left count:1 */ |
45225 |
0 |
45225 |
| if (old == e || old->val == e->val) return n; /* # uncoverable condition left count:1 */ |
410 |
44815 |
45225 |
|
| 171 |
44815 |
▶ | if (edit) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (edit) { |
14901 |
29914 |
44815 |
|
| 172 |
14901 |
| n->slot[di] = hamt_entry_retain(e); |
| 173 |
14901 |
| *dead = old; |
| 174 |
14901 |
| return n; |
| 175 |
|
| } |
| 176 |
29914 |
| r = hamt_node_with_slot(cx, n, di, e, HAMT_NO_SKIP); |
| 177 |
29914 |
▶ | if (!r) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!r) return NULL; |
54 |
29860 |
29914 |
|
| 178 |
29860 |
| *dead = hamt_entry_retain(old); |
| 179 |
29860 |
| hamt_node_release(cx, n); |
| 180 |
29860 |
| return r; |
| 181 |
|
| } |
| 182 |
|
| |
| 183 |
|
| /* Two keys at one position. The subtree that holds both takes its |
| 184 |
|
| * own reference to each, so the entry leaving this node is not |
| 185 |
|
| * displaced, only moved down. */ |
| 186 |
249579 |
| sub = hamt_node_pair(cx, old, e, depth + 1); |
| 187 |
249579 |
▶ | if (!sub) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sub) return NULL; |
521 |
249058 |
249579 |
|
| 188 |
249058 |
▶ | if (edit) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (edit) { |
238754 |
10304 |
249058 |
|
| 189 |
238754 |
| hamt_entry_release(cx, hamt_node_entry_to_child_here(n, bit, sub)); |
| 190 |
238754 |
| *added = 1; |
| 191 |
238754 |
| return n; |
| 192 |
|
| } |
| 193 |
10304 |
| r = hamt_node_entry_to_child(cx, n, bit, sub); |
| 194 |
10304 |
▶ | if (!r) { hamt_node_release(cx, sub); return NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!r) { hamt_node_release(cx, sub); return NULL; } |
24 |
10280 |
10304 |
|
| 195 |
10280 |
| *added = 1; |
| 196 |
10280 |
| hamt_node_release(cx, n); |
| 197 |
10280 |
| return r; |
| 198 |
|
| } |
| 199 |
|
| |
| 200 |
3414805 |
▶ | if (n->nodemap & bit) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n->nodemap & bit) { |
2757063 |
657742 |
3414805 |
|
| 201 |
2757063 |
| uint32_t ci = hamt_node_nslots(n) - 1 - hamt_popcnt32(n->nodemap & (bit - 1)); |
| 202 |
2757063 |
| hamt_node *child = (hamt_node *)n->slot[ci]; |
| 203 |
|
| hamt_node *sub; |
| 204 |
|
| |
| 205 |
2757063 |
▶ | if (edit) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (edit) { |
2456126 |
300937 |
2757063 |
|
| 206 |
|
| /* This node's reference to the child is the one handed down. */ |
| 207 |
2456126 |
| sub = hamt_node_put_mut(cx, child, depth + 1, e, added, dead); |
| 208 |
2456126 |
▶ | if (!sub) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sub) return NULL; |
906 |
2455220 |
2456126 |
|
| 209 |
2455220 |
| n->slot[ci] = sub; |
| 210 |
2455220 |
| return n; |
| 211 |
|
| } |
| 212 |
|
| |
| 213 |
|
| /* A shared node's reference to its child is not ours to hand down, |
| 214 |
|
| * so the child goes down with one of its own - which makes it |
| 215 |
|
| * shared, and so copied rather than changed. */ |
| 216 |
300937 |
| hamt_node_retain(child); |
| 217 |
300937 |
| sub = hamt_node_put_mut(cx, child, depth + 1, e, added, dead); |
| 218 |
300937 |
▶ | if (!sub) { hamt_node_release(cx, child); return NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sub) { hamt_node_release(cx, child); return NULL; } |
272 |
300665 |
300937 |
|
| 219 |
300665 |
▶ | if (sub == child) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (sub == child) { |
1467 |
299198 |
300665 |
|
| 220 |
1467 |
| hamt_node_release(cx, sub); |
| 221 |
1467 |
| return n; |
| 222 |
|
| } |
| 223 |
299198 |
| r = hamt_node_with_slot(cx, n, ci, sub, ci); |
| 224 |
299198 |
▶ | if (!r) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!r) { |
56 |
299142 |
299198 |
|
| 225 |
|
| /* Everything below was a copy, so it can all be let go. */ |
| 226 |
56 |
| hamt_node_release(cx, sub); |
| 227 |
56 |
▶ | if (*dead) { hamt_entry_release(cx, *dead); *dead = NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (*dead) { hamt_entry_release(cx, *dead); *dead = NULL; } |
18 |
38 |
56 |
|
| 228 |
56 |
| *added = 0; |
| 229 |
56 |
| return NULL; |
| 230 |
|
| } |
| 231 |
299142 |
| hamt_node_release(cx, n); |
| 232 |
299142 |
| return r; |
| 233 |
|
| } |
| 234 |
|
| |
| 235 |
626106 |
| r = edit ? hamt_node_add_entry_move(cx, n, bit, e) |
| 236 |
657742 |
▶ | : hamt_node_add_entry(cx, n, bit, e); |
Branch: 1 branch
| Branch | True | False | Total |
| : hamt_node_add_entry(cx, n, bit, e); |
626106 |
31636 |
657742 |
|
| 237 |
657742 |
▶ | if (!r) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!r) return NULL; |
400 |
657342 |
657742 |
|
| 238 |
657342 |
| *added = 1; |
| 239 |
657342 |
▶ | if (!edit) hamt_node_release(cx, n); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!edit) hamt_node_release(cx, n); |
31545 |
625797 |
657342 |
|
| 240 |
657342 |
| return r; |
| 241 |
|
| } |
| 242 |
|
| |
| 243 |
|
| /* Puts an entry into the trie at *rootp, which the caller owns one |
| 244 |
|
| * reference to before and after; the pointer may change. The entry is |
| 245 |
|
| * shared: the caller keeps its own reference. |
| 246 |
|
| * |
| 247 |
|
| * `*added` is 1 for a new key. `*dead` is the entry this one displaced, if |
| 248 |
|
| * any, for the caller to release AFTER it has put the new root wherever it |
| 249 |
|
| * keeps its root. |
| 250 |
|
| * |
| 251 |
|
| * 0, or -1 for no memory with everything as it was. */ |
| 252 |
1130203 |
| static int hamt_map_put_mut(void *cx, hamt_node **rootp, hamt_entry *e, |
| 253 |
|
| int *added, hamt_entry **dead) { |
| 254 |
|
| hamt_node *r; |
| 255 |
|
| |
| 256 |
1130203 |
| *added = 0; |
| 257 |
1130203 |
| *dead = NULL; |
| 258 |
1130203 |
▶ | if (!*rootp) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!*rootp) { |
7832 |
1122371 |
1130203 |
|
| 259 |
7832 |
| r = hamt_node_new(cx, 1); |
| 260 |
7832 |
▶ | if (!r) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!r) return -1; |
12 |
7820 |
7832 |
|
| 261 |
7820 |
| r->datamap = HAMT_BIT(e->hash, 0); |
| 262 |
7820 |
| r->slot[0] = hamt_entry_retain(e); |
| 263 |
7820 |
| *added = 1; |
| 264 |
7820 |
| *rootp = r; |
| 265 |
7820 |
| return 0; |
| 266 |
|
| } |
| 267 |
1122371 |
| r = hamt_node_put_mut(cx, *rootp, 0, e, added, dead); |
| 268 |
1122371 |
▶ | if (!r) { *added = 0; return -1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!r) { *added = 0; return -1; } |
1072 |
1121299 |
1122371 |
|
| 269 |
1121299 |
| *rootp = r; |
| 270 |
1121299 |
| return 0; |
| 271 |
|
| } |
| 272 |
|
| |
| 273 |
|
| /* Takes over the caller's reference to `n` when it removes something |
| 274 |
|
| * (HAMT_DEL_DONE) and returns what replaces it, which may be `n` itself, or |
| 275 |
|
| * NULL for a root that is now empty. HAMT_DEL_ABSENT returns `n`, untouched |
| 276 |
|
| * and still the caller's. HAMT_DEL_OOM returns NULL, likewise. */ |
| 277 |
342828 |
| static hamt_node *hamt_node_del_mut(void *cx, hamt_node *n, unsigned depth, |
| 278 |
|
| const char *key, size_t klen, int utf8, |
| 279 |
|
| uint64_t hash, int *status, hamt_entry **dead) { |
| 280 |
342828 |
| int edit = HAMT_EDITABLE(n); |
| 281 |
|
| uint32_t bit, i; |
| 282 |
|
| hamt_node *r; |
| 283 |
|
| |
| 284 |
342828 |
▶ | if (n->ncoll) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n->ncoll) { |
18705 |
324123 |
342828 |
|
| 285 |
334674 |
▶ | for (i = 0; i < n->ncoll; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n->ncoll; i++) { |
327733 |
6941 |
334674 |
|
| 286 |
327733 |
| hamt_entry *old = (hamt_entry *)n->slot[i]; |
| 287 |
327733 |
▶ | if (!hamt_entry_key_eq(old, key, klen, utf8, hash)) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!hamt_entry_key_eq(old, key, klen, utf8, hash)) continue; |
315969 |
11764 |
327733 |
|
| 288 |
11764 |
| r = hamt_node_coll_drop(cx, n, i); |
| 289 |
11764 |
▶ | if (!r) { *status = HAMT_DEL_OOM; return NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!r) { *status = HAMT_DEL_OOM; return NULL; } |
7 |
11757 |
11764 |
|
| 290 |
11757 |
| *dead = hamt_entry_retain(old); |
| 291 |
11757 |
| *status = HAMT_DEL_DONE; |
| 292 |
11757 |
| hamt_node_release(cx, n); |
| 293 |
11757 |
| return r; |
| 294 |
|
| } |
| 295 |
6941 |
| *status = HAMT_DEL_ABSENT; |
| 296 |
6941 |
| return n; |
| 297 |
|
| } |
| 298 |
|
| |
| 299 |
324123 |
| bit = HAMT_BIT(hash, depth); |
| 300 |
|
| |
| 301 |
324123 |
▶ | if (n->datamap & bit) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n->datamap & bit) { |
36393 |
287730 |
324123 |
|
| 302 |
36393 |
| hamt_entry *old = (hamt_entry *) |
| 303 |
36393 |
| n->slot[hamt_popcnt32(n->datamap & (bit - 1))]; |
| 304 |
|
| |
| 305 |
36393 |
▶ | if (!hamt_entry_key_eq(old, key, klen, utf8, hash)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!hamt_entry_key_eq(old, key, klen, utf8, hash)) { |
3983 |
32410 |
36393 |
|
| 306 |
3983 |
| *status = HAMT_DEL_ABSENT; |
| 307 |
3983 |
| return n; |
| 308 |
|
| } |
| 309 |
32410 |
▶ | if (hamt_node_nslots(n) == 1) { /* the last of a root */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (hamt_node_nslots(n) == 1) { /* the last of a root */ |
13 |
32397 |
32410 |
|
| 310 |
13 |
| *dead = hamt_entry_retain(old); |
| 311 |
13 |
| *status = HAMT_DEL_DONE; |
| 312 |
13 |
| hamt_node_release(cx, n); |
| 313 |
13 |
| return NULL; |
| 314 |
|
| } |
| 315 |
32397 |
▶ | if (edit) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (edit) { |
13182 |
19215 |
32397 |
|
| 316 |
13182 |
| r = hamt_node_drop_entry_move(cx, n, bit, dead); |
| 317 |
13182 |
▶ | if (!r) { *status = HAMT_DEL_OOM; return NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!r) { *status = HAMT_DEL_OOM; return NULL; } |
79 |
13103 |
13182 |
|
| 318 |
13103 |
| *status = HAMT_DEL_DONE; |
| 319 |
13103 |
| return r; |
| 320 |
|
| } |
| 321 |
19215 |
| r = hamt_node_drop_entry(cx, n, bit); |
| 322 |
19215 |
▶ | if (!r) { *status = HAMT_DEL_OOM; return NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!r) { *status = HAMT_DEL_OOM; return NULL; } |
134 |
19081 |
19215 |
|
| 323 |
19081 |
| *dead = hamt_entry_retain(old); |
| 324 |
19081 |
| *status = HAMT_DEL_DONE; |
| 325 |
19081 |
| hamt_node_release(cx, n); |
| 326 |
19081 |
| return r; |
| 327 |
|
| } |
| 328 |
|
| |
| 329 |
287730 |
▶ | if (n->nodemap & bit) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n->nodemap & bit) { |
278772 |
8958 |
287730 |
|
| 330 |
278772 |
| uint32_t ci = hamt_node_nslots(n) - 1 - hamt_popcnt32(n->nodemap & (bit - 1)); |
| 331 |
278772 |
| hamt_node *child = (hamt_node *)n->slot[ci]; |
| 332 |
|
| hamt_node *sub; |
| 333 |
|
| |
| 334 |
278772 |
▶ | if (edit) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (edit) { |
95805 |
182967 |
278772 |
|
| 335 |
95805 |
| sub = hamt_node_del_mut(cx, child, depth + 1, key, klen, utf8, hash, |
| 336 |
|
| status, dead); |
| 337 |
95805 |
▶ | if (*status != HAMT_DEL_DONE) return *status == HAMT_DEL_ABSENT ? n : NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (*status != HAMT_DEL_DONE) return *status == HAMT_DEL_ABSENT ? n : NULL; |
31746 |
64059 |
95805 |
| if (*status != HAMT_DEL_DONE) return *status == HAMT_DEL_ABSENT ? n : NULL; |
31399 |
347 |
31746 |
|
| 338 |
|
| /* |
| 339 |
|
| # uncoverable branch true |
| 340 |
|
| # uncoverable condition left count:1 */ |
| 341 |
64059 |
▶ | if (!sub) HAMT_FATAL("a node below the root came back empty"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sub) HAMT_FATAL("a node below the root came back empty"); |
0 |
64059 |
64059 |
|
| 342 |
|
| |
| 343 |
64059 |
▶ | if (hamt_node_is_single(sub)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (hamt_node_is_single(sub)) { |
6110 |
57949 |
64059 |
|
| 344 |
|
| /* The fold, where the node stands. The entry is kept alive |
| 345 |
|
| * by a reference of this node's before the child that held |
| 346 |
|
| * it is let go. */ |
| 347 |
6110 |
| hamt_entry *up = hamt_entry_retain((hamt_entry *)sub->slot[0]); |
| 348 |
6110 |
| n->slot[ci] = sub; |
| 349 |
6110 |
| hamt_node_release(cx, hamt_node_child_to_entry_here(n, bit, up)); |
| 350 |
|
| } |
| 351 |
57949 |
| else n->slot[ci] = sub; |
| 352 |
64059 |
| return n; |
| 353 |
|
| } |
| 354 |
|
| |
| 355 |
182967 |
| hamt_node_retain(child); |
| 356 |
182967 |
| sub = hamt_node_del_mut(cx, child, depth + 1, key, klen, utf8, hash, |
| 357 |
|
| status, dead); |
| 358 |
182967 |
▶ | if (*status == HAMT_DEL_OOM) { hamt_node_release(cx, child); return NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (*status == HAMT_DEL_OOM) { hamt_node_release(cx, child); return NULL; } |
172 |
182795 |
182967 |
|
| 359 |
182795 |
▶ | if (*status == HAMT_DEL_ABSENT) { hamt_node_release(cx, child); return n; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (*status == HAMT_DEL_ABSENT) { hamt_node_release(cx, child); return n; } |
65289 |
117506 |
182795 |
|
| 360 |
|
| /* |
| 361 |
|
| # uncoverable branch true |
| 362 |
|
| # uncoverable condition left count:1 */ |
| 363 |
117506 |
▶ | if (!sub) HAMT_FATAL("a node below the root came back empty"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sub) HAMT_FATAL("a node below the root came back empty"); |
0 |
117506 |
117506 |
|
| 364 |
|
| |
| 365 |
117506 |
| r = hamt_node_is_single(sub) |
| 366 |
24498 |
| ? hamt_node_child_to_entry(cx, n, bit, (hamt_entry *)sub->slot[0]) |
| 367 |
117506 |
▶ | : hamt_node_with_slot(cx, n, ci, sub, HAMT_NO_SKIP); |
Branch: 1 branch
| Branch | True | False | Total |
| : hamt_node_with_slot(cx, n, ci, sub, HAMT_NO_SKIP); |
24498 |
93008 |
117506 |
|
| 368 |
117506 |
| hamt_node_release(cx, sub); |
| 369 |
117506 |
▶ | if (!r) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!r) { |
40 |
117466 |
117506 |
|
| 370 |
|
| /* |
| 371 |
|
| # uncoverable branch false |
| 372 |
|
| # uncoverable condition right count:1 */ |
| 373 |
40 |
▶ | if (*dead) { hamt_entry_release(cx, *dead); *dead = NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (*dead) { hamt_entry_release(cx, *dead); *dead = NULL; } |
40 |
0 |
40 |
|
| 374 |
40 |
| *status = HAMT_DEL_OOM; |
| 375 |
40 |
| return NULL; |
| 376 |
|
| } |
| 377 |
117466 |
| hamt_node_release(cx, n); |
| 378 |
117466 |
| return r; |
| 379 |
|
| } |
| 380 |
|
| |
| 381 |
8958 |
| *status = HAMT_DEL_ABSENT; |
| 382 |
8958 |
| return n; |
| 383 |
|
| } |
| 384 |
|
| |
| 385 |
|
| /* Takes a key out of the trie at *rootp, which the caller owns one |
| 386 |
|
| * reference to before and after; the pointer may change, and is NULL for a |
| 387 |
|
| * trie that is now empty. `*dead` is the entry removed, for the caller to |
| 388 |
|
| * release after it has put the new root wherever it keeps its root. |
| 389 |
|
| * |
| 390 |
|
| * HAMT_DEL_DONE, HAMT_DEL_ABSENT, or HAMT_DEL_OOM with everything as it |
| 391 |
|
| * was. */ |
| 392 |
64064 |
| static int hamt_map_del_mut(void *cx, hamt_node **rootp, const char *key, |
| 393 |
|
| size_t klen, int utf8, uint64_t hash, |
| 394 |
|
| hamt_entry **dead) { |
| 395 |
64064 |
| int status = HAMT_DEL_ABSENT; |
| 396 |
|
| hamt_node *r; |
| 397 |
|
| |
| 398 |
64064 |
| *dead = NULL; |
| 399 |
64064 |
▶ | if (!*rootp) return HAMT_DEL_ABSENT; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!*rootp) return HAMT_DEL_ABSENT; |
8 |
64056 |
64064 |
|
| 400 |
64056 |
| r = hamt_node_del_mut(cx, *rootp, 0, key, klen, utf8, hash, &status, dead); |
| 401 |
64056 |
▶ | if (status == HAMT_DEL_DONE) *rootp = r; |
Branch: 1 branch
| Branch | True | False | Total |
| if (status == HAMT_DEL_DONE) *rootp = r; |
43914 |
20142 |
64056 |
|
| 402 |
64056 |
| return status; |
| 403 |
|
| } |
| 404 |
|
| |
| 405 |
|
| #endif |