This repository has been archived by the owner on Mar 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathcritnib.c
304 lines (264 loc) · 6.16 KB
/
critnib.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// SPDX-License-Identifier: BSD-3-Clause
/* Copyright 2018-2020, Intel Corporation */
#include <errno.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "os_thread.h"
#include "util.h"
#include "out.h"
#include "critnib.h"
/*
* WARNING: this implementation fails badly if you try to store two keys
* where one is a prefix of another. Pass a struct { int len; char[] key; }
* or such if such keys are possible.
*/
#define NIB ((1 << SLICE) - 1)
#define KEYLEN(leaf) (leaf->key.ksize + sizeof(size_t))
typedef struct cache_entry critnib_leaf;
/*
* is_leaf -- (internal) check tagged pointer for leafness
*/
static inline bool
is_leaf(struct critnib_node *n)
{
return (uintptr_t)n & 1;
}
/*
* to_leaf -- (internal) untag a leaf pointer
*/
static inline critnib_leaf *
to_leaf(struct critnib_node *n)
{
return (void *)((uintptr_t)n & ~1ULL);
}
/*
* slice_index -- (internal) get index of radix child at a given shift
*/
static inline int
slice_index(char b, bitn_t bit)
{
return (b >> bit) & NIB;
}
/*
* critnib_new -- allocate a new hashmap
*/
struct critnib *
critnib_new(void)
{
struct critnib *c = Zalloc(sizeof(struct critnib));
if (!c)
return NULL;
return c;
}
/*
* delete_node -- (internal) recursively free a subtree
*/
static void
delete_node(struct critnib_node *n, delete_entry_t del)
{
if (!n)
return;
if (is_leaf(n)) {
if (del)
del(to_leaf(n));
return;
}
for (int i = 0; i < SLNODES; i++)
delete_node(n->child[i], del);
Free(n);
}
/*
* critnib_delete -- free a hashmap
*/
void
critnib_delete(struct critnib *c, delete_entry_t del)
{
delete_node(c->root, del);
Free(c);
}
/*
* alloc_node -- (internal) alloc a node
*/
static struct critnib_node *
alloc_node(struct critnib *c)
{
struct critnib_node *n = Zalloc(sizeof(struct critnib_node));
if (!n)
return NULL;
#ifdef STATS_ENABLED
c->node_count++;
#endif
return n;
}
/*
* any_leaf -- (internal) find any leaf in a subtree
*
* We know they're all identical up to the divergence point between a prefix
* shared by all of them vs the new key we're inserting.
*/
static struct critnib_node *
any_leaf(struct critnib_node *n)
{
for (int i = 0; i < SLNODES; i++) {
struct critnib_node *m;
if ((m = n->child[i]))
return is_leaf(m) ? m : any_leaf(m);
}
return NULL;
}
/*
* critnib_set -- insert a new entry
*/
int
critnib_set(struct critnib *c, struct cache_entry *e)
{
const char *key = (void *)&e->key;
byten_t key_len = (byten_t)KEYLEN(e);
critnib_leaf *k = (void *)((uintptr_t)e | 1);
struct critnib_node *n = c->root;
if (!n) {
c->root = (void *)k;
return 0;
}
/*
* Need to descend the tree twice: first to find a leaf that
* represents a subtree whose all keys share a prefix at least as
* long as the one common to the new key and that subtree.
*/
while (!is_leaf(n) && n->byte < key_len) {
struct critnib_node *nn =
n->child[slice_index(key[n->byte], n->bit)];
if (nn)
n = nn;
else {
n = any_leaf(n);
break;
}
}
ASSERT(n);
if (!is_leaf(n))
n = any_leaf(n);
ASSERT(n);
ASSERT(is_leaf(n));
critnib_leaf *nk = to_leaf(n);
const char *nkey = (void *)&nk->key;
/* Find the divergence point, accurate to a byte. */
byten_t common_len = ((byten_t)KEYLEN(nk) < key_len)
? (byten_t)KEYLEN(nk) : key_len;
byten_t diff;
for (diff = 0; diff < common_len; diff++) {
if (nkey[diff] != key[diff])
break;
}
if (diff >= common_len) {
/*
* Either an update or a conflict between keys being a
* prefix of each other.
*/
return EEXIST;
}
/* Calculate the divergence point within the single byte. */
char at = nkey[diff] ^ key[diff];
bitn_t sh = util_mssb_index((uint32_t)(uint8_t)at)
& (bitn_t)~(SLICE - 1);
/* Descend into the tree again. */
n = c->root;
struct critnib_node **parent = &c->root;
while (n && !is_leaf(n) &&
(n->byte < diff || (n->byte == diff && n->bit >= sh))) {
parent = &n->child[slice_index(key[n->byte], n->bit)];
n = *parent;
}
/*
* If the divergence point is at same nib as an existing node, and
* the subtree there is empty, just place our leaf there and we're
* done. Obviously this can't happen if SLICE == 1.
*/
if (!n) {
*parent = (void *)k;
return 0;
}
/* If not, we need to insert a new node in the middle of an edge. */
if (!(n = alloc_node(c)))
return ENOMEM;
n->child[slice_index(nkey[diff], sh)] = *parent;
n->child[slice_index(key[diff], sh)] = (void *)k;
n->byte = diff;
n->bit = sh;
*parent = n;
return 0;
}
/*
* critnib_get -- query a key
*/
void *
critnib_get(struct critnib *c, const struct cache_entry *e)
{
const char *key = (void *)&e->key;
byten_t key_len = (byten_t)KEYLEN(e);
struct critnib_node *n = c->root;
while (n && !is_leaf(n)) {
if (n->byte >= key_len)
return NULL;
n = n->child[slice_index(key[n->byte], n->bit)];
}
if (!n)
return NULL;
critnib_leaf *k = to_leaf(n);
/*
* We checked only nibs at divergence points, have to re-check the
* whole key.
*/
return (key_len != KEYLEN(k) || memcmp(key, (void *)&k->key,
key_len)) ? NULL : k;
}
/*
* critnib_remove -- query and delete a key
*
* Neither the key nor its value are freed, just our private nodes.
*/
void *
critnib_remove(struct critnib *c, const struct cache_entry *e)
{
const char *key = (void *)&e->key;
byten_t key_len = (byten_t)KEYLEN(e);
struct critnib_node **pp = NULL;
struct critnib_node *n = c->root;
struct critnib_node **parent = &c->root;
/* First, do a get. */
while (n && !is_leaf(n)) {
if (n->byte >= key_len)
return NULL;
pp = parent;
parent = &n->child[slice_index(key[n->byte], n->bit)];
n = *parent;
}
if (!n)
return NULL;
critnib_leaf *k = to_leaf(n);
if (key_len != KEYLEN(k) || memcmp(key, (void *)&k->key, key_len))
return NULL;
/* Remove the entry (leaf). */
*parent = NULL;
if (!pp) /* was root */
return k;
/* Check if after deletion the node has just a single child left. */
n = *pp;
struct critnib_node *only_child = NULL;
for (int i = 0; i < SLNODES; i++) {
if (n->child[i]) {
if (only_child) /* Nope. */
return k;
only_child = n->child[i];
}
}
/* Yes -- shorten the tree's edge. */
ASSERT(only_child);
*pp = only_child;
Free(n);
#ifdef STATS_ENABLED
c->node_count--;
#endif
return k;
}