radixtree.c revision 1.11 1 1.11 yamt /* $NetBSD: radixtree.c,v 1.11 2011/10/14 15:31:35 yamt Exp $ */
2 1.1 yamt
3 1.1 yamt /*-
4 1.1 yamt * Copyright (c)2011 YAMAMOTO Takashi,
5 1.1 yamt * All rights reserved.
6 1.1 yamt *
7 1.1 yamt * Redistribution and use in source and binary forms, with or without
8 1.1 yamt * modification, are permitted provided that the following conditions
9 1.1 yamt * are met:
10 1.1 yamt * 1. Redistributions of source code must retain the above copyright
11 1.1 yamt * notice, this list of conditions and the following disclaimer.
12 1.1 yamt * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 yamt * notice, this list of conditions and the following disclaimer in the
14 1.1 yamt * documentation and/or other materials provided with the distribution.
15 1.1 yamt *
16 1.1 yamt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.1 yamt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1 yamt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1 yamt * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.1 yamt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1 yamt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1 yamt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 yamt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1 yamt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 yamt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 yamt * SUCH DAMAGE.
27 1.1 yamt */
28 1.1 yamt
29 1.1 yamt /*
30 1.1 yamt * radix tree
31 1.1 yamt *
32 1.1 yamt * it's designed to work efficiently with dense index distribution.
33 1.1 yamt * the memory consumption (number of necessary intermediate nodes)
34 1.1 yamt * heavily depends on index distribution. basically, more dense index
35 1.1 yamt * distribution consumes less nodes per item.
36 1.1 yamt * approximately,
37 1.1 yamt * the best case: about RADIX_TREE_PTR_PER_NODE items per node.
38 1.1 yamt * the worst case: RADIX_TREE_MAX_HEIGHT nodes per item.
39 1.1 yamt */
40 1.1 yamt
41 1.1 yamt #include <sys/cdefs.h>
42 1.1 yamt
43 1.2 yamt #if defined(_KERNEL) || defined(_STANDALONE)
44 1.11 yamt __KERNEL_RCSID(0, "$NetBSD: radixtree.c,v 1.11 2011/10/14 15:31:35 yamt Exp $");
45 1.1 yamt #include <sys/param.h>
46 1.3 yamt #include <sys/errno.h>
47 1.1 yamt #include <sys/pool.h>
48 1.1 yamt #include <sys/radixtree.h>
49 1.3 yamt #include <lib/libkern/libkern.h>
50 1.3 yamt #if defined(_STANDALONE)
51 1.3 yamt #include <lib/libsa/stand.h>
52 1.3 yamt #endif /* defined(_STANDALONE) */
53 1.2 yamt #else /* defined(_KERNEL) || defined(_STANDALONE) */
54 1.11 yamt __RCSID("$NetBSD: radixtree.c,v 1.11 2011/10/14 15:31:35 yamt Exp $");
55 1.1 yamt #include <assert.h>
56 1.1 yamt #include <errno.h>
57 1.1 yamt #include <stdbool.h>
58 1.1 yamt #include <stdlib.h>
59 1.8 yamt #include <string.h>
60 1.1 yamt #if 1
61 1.1 yamt #define KASSERT assert
62 1.1 yamt #else
63 1.1 yamt #define KASSERT(a) /* nothing */
64 1.1 yamt #endif
65 1.2 yamt #endif /* defined(_KERNEL) || defined(_STANDALONE) */
66 1.1 yamt
67 1.1 yamt #include <sys/radixtree.h>
68 1.1 yamt
69 1.1 yamt #define RADIX_TREE_BITS_PER_HEIGHT 4 /* XXX tune */
70 1.1 yamt #define RADIX_TREE_PTR_PER_NODE (1 << RADIX_TREE_BITS_PER_HEIGHT)
71 1.1 yamt #define RADIX_TREE_MAX_HEIGHT (64 / RADIX_TREE_BITS_PER_HEIGHT)
72 1.2 yamt __CTASSERT((64 % RADIX_TREE_BITS_PER_HEIGHT) == 0);
73 1.1 yamt
74 1.2 yamt __CTASSERT(((1 << RADIX_TREE_TAG_ID_MAX) & (sizeof(int) - 1)) == 0);
75 1.1 yamt #define RADIX_TREE_TAG_MASK ((1 << RADIX_TREE_TAG_ID_MAX) - 1)
76 1.1 yamt
77 1.1 yamt static inline void *
78 1.1 yamt entry_ptr(void *p)
79 1.1 yamt {
80 1.1 yamt
81 1.1 yamt return (void *)((uintptr_t)p & ~RADIX_TREE_TAG_MASK);
82 1.1 yamt }
83 1.1 yamt
84 1.1 yamt static inline unsigned int
85 1.1 yamt entry_tagmask(void *p)
86 1.1 yamt {
87 1.1 yamt
88 1.1 yamt return (uintptr_t)p & RADIX_TREE_TAG_MASK;
89 1.1 yamt }
90 1.1 yamt
91 1.1 yamt static inline void *
92 1.1 yamt entry_compose(void *p, unsigned int tagmask)
93 1.1 yamt {
94 1.1 yamt
95 1.1 yamt return (void *)((uintptr_t)p | tagmask);
96 1.1 yamt }
97 1.1 yamt
98 1.1 yamt static inline bool
99 1.1 yamt entry_match_p(void *p, unsigned int tagmask)
100 1.1 yamt {
101 1.1 yamt
102 1.1 yamt KASSERT(entry_ptr(p) != NULL || entry_tagmask(p) == 0);
103 1.1 yamt if (p == NULL) {
104 1.1 yamt return false;
105 1.1 yamt }
106 1.1 yamt if (tagmask == 0) {
107 1.1 yamt return true;
108 1.1 yamt }
109 1.1 yamt return (entry_tagmask(p) & tagmask) != 0;
110 1.1 yamt }
111 1.1 yamt
112 1.1 yamt static inline unsigned int
113 1.1 yamt tagid_to_mask(radix_tree_tagid_t id)
114 1.1 yamt {
115 1.1 yamt
116 1.6 yamt KASSERT(id >= 0);
117 1.6 yamt KASSERT(id < RADIX_TREE_TAG_ID_MAX);
118 1.1 yamt return 1U << id;
119 1.1 yamt }
120 1.1 yamt
121 1.1 yamt /*
122 1.1 yamt * radix_tree_node: an intermediate node
123 1.1 yamt *
124 1.1 yamt * we don't care the type of leaf nodes. they are just void *.
125 1.1 yamt */
126 1.1 yamt
127 1.1 yamt struct radix_tree_node {
128 1.1 yamt void *n_ptrs[RADIX_TREE_PTR_PER_NODE];
129 1.1 yamt unsigned int n_nptrs; /* # of non-NULL pointers in n_ptrs */
130 1.1 yamt };
131 1.1 yamt
132 1.7 yamt /*
133 1.7 yamt * any_children_tagmask:
134 1.7 yamt *
135 1.7 yamt * return OR'ed tagmask of the given node's children.
136 1.7 yamt */
137 1.7 yamt
138 1.1 yamt static unsigned int
139 1.1 yamt any_children_tagmask(struct radix_tree_node *n)
140 1.1 yamt {
141 1.1 yamt unsigned int mask;
142 1.1 yamt int i;
143 1.1 yamt
144 1.1 yamt mask = 0;
145 1.1 yamt for (i = 0; i < RADIX_TREE_PTR_PER_NODE; i++) {
146 1.1 yamt mask |= (unsigned int)(uintptr_t)n->n_ptrs[i];
147 1.1 yamt }
148 1.1 yamt return mask & RADIX_TREE_TAG_MASK;
149 1.1 yamt }
150 1.1 yamt
151 1.1 yamt /*
152 1.1 yamt * p_refs[0].pptr == &t->t_root
153 1.1 yamt * :
154 1.1 yamt * p_refs[n].pptr == &(*p_refs[n-1])->n_ptrs[x]
155 1.1 yamt * :
156 1.1 yamt * :
157 1.1 yamt * p_refs[t->t_height].pptr == &leaf_pointer
158 1.1 yamt */
159 1.1 yamt
160 1.1 yamt struct radix_tree_path {
161 1.1 yamt struct radix_tree_node_ref {
162 1.1 yamt void **pptr;
163 1.1 yamt } p_refs[RADIX_TREE_MAX_HEIGHT + 1]; /* +1 for the root ptr */
164 1.10 yamt unsigned int p_lastidx;
165 1.1 yamt };
166 1.1 yamt
167 1.1 yamt static inline void **
168 1.1 yamt path_pptr(struct radix_tree *t, struct radix_tree_path *p,
169 1.1 yamt unsigned int height)
170 1.1 yamt {
171 1.1 yamt
172 1.1 yamt KASSERT(height <= t->t_height);
173 1.1 yamt return p->p_refs[height].pptr;
174 1.1 yamt }
175 1.1 yamt
176 1.1 yamt static inline struct radix_tree_node *
177 1.1 yamt path_node(struct radix_tree * t, struct radix_tree_path *p, unsigned int height)
178 1.1 yamt {
179 1.1 yamt
180 1.1 yamt KASSERT(height <= t->t_height);
181 1.1 yamt return entry_ptr(*path_pptr(t, p, height));
182 1.1 yamt }
183 1.1 yamt
184 1.1 yamt static inline unsigned int
185 1.1 yamt path_idx(struct radix_tree * t, struct radix_tree_path *p, unsigned int height)
186 1.1 yamt {
187 1.1 yamt
188 1.1 yamt KASSERT(height <= t->t_height);
189 1.1 yamt return path_pptr(t, p, height + 1) - path_node(t, p, height)->n_ptrs;
190 1.1 yamt }
191 1.1 yamt
192 1.1 yamt /*
193 1.1 yamt * radix_tree_init_tree:
194 1.1 yamt *
195 1.1 yamt * initialize a tree.
196 1.1 yamt */
197 1.1 yamt
198 1.1 yamt void
199 1.1 yamt radix_tree_init_tree(struct radix_tree *t)
200 1.1 yamt {
201 1.1 yamt
202 1.1 yamt t->t_height = 0;
203 1.1 yamt t->t_root = NULL;
204 1.1 yamt }
205 1.1 yamt
206 1.1 yamt /*
207 1.1 yamt * radix_tree_init_tree:
208 1.1 yamt *
209 1.1 yamt * clean up a tree.
210 1.1 yamt */
211 1.1 yamt
212 1.1 yamt void
213 1.1 yamt radix_tree_fini_tree(struct radix_tree *t)
214 1.1 yamt {
215 1.1 yamt
216 1.1 yamt KASSERT(t->t_root == NULL);
217 1.1 yamt KASSERT(t->t_height == 0);
218 1.1 yamt }
219 1.1 yamt
220 1.9 yamt bool
221 1.9 yamt radix_tree_empty_tree_p(struct radix_tree *t)
222 1.9 yamt {
223 1.9 yamt
224 1.9 yamt return t->t_root == NULL;
225 1.9 yamt }
226 1.9 yamt
227 1.3 yamt static void
228 1.3 yamt radix_tree_node_init(struct radix_tree_node *n)
229 1.3 yamt {
230 1.3 yamt
231 1.3 yamt memset(n, 0, sizeof(*n));
232 1.3 yamt }
233 1.3 yamt
234 1.1 yamt #if defined(_KERNEL)
235 1.2 yamt pool_cache_t radix_tree_node_cache __read_mostly;
236 1.1 yamt
237 1.1 yamt static int
238 1.1 yamt radix_tree_node_ctor(void *dummy, void *item, int flags)
239 1.1 yamt {
240 1.1 yamt struct radix_tree_node *n = item;
241 1.1 yamt
242 1.1 yamt KASSERT(dummy == NULL);
243 1.3 yamt radix_tree_node_init(n);
244 1.1 yamt return 0;
245 1.1 yamt }
246 1.1 yamt
247 1.1 yamt /*
248 1.1 yamt * radix_tree_init:
249 1.1 yamt *
250 1.1 yamt * initialize the subsystem.
251 1.1 yamt */
252 1.1 yamt
253 1.1 yamt void
254 1.1 yamt radix_tree_init(void)
255 1.1 yamt {
256 1.1 yamt
257 1.1 yamt radix_tree_node_cache = pool_cache_init(sizeof(struct radix_tree_node),
258 1.1 yamt 0, 0, 0, "radix_tree_node", NULL, IPL_NONE, radix_tree_node_ctor,
259 1.1 yamt NULL, NULL);
260 1.1 yamt KASSERT(radix_tree_node_cache != NULL);
261 1.1 yamt }
262 1.1 yamt #endif /* defined(_KERNEL) */
263 1.1 yamt
264 1.1 yamt static bool __unused
265 1.1 yamt radix_tree_node_clean_p(const struct radix_tree_node *n)
266 1.1 yamt {
267 1.1 yamt unsigned int i;
268 1.1 yamt
269 1.1 yamt if (n->n_nptrs != 0) {
270 1.1 yamt return false;
271 1.1 yamt }
272 1.1 yamt for (i = 0; i < RADIX_TREE_PTR_PER_NODE; i++) {
273 1.1 yamt if (n->n_ptrs[i] != NULL) {
274 1.1 yamt return false;
275 1.1 yamt }
276 1.1 yamt }
277 1.1 yamt return true;
278 1.1 yamt }
279 1.1 yamt
280 1.1 yamt static struct radix_tree_node *
281 1.1 yamt radix_tree_alloc_node(void)
282 1.1 yamt {
283 1.1 yamt struct radix_tree_node *n;
284 1.1 yamt
285 1.1 yamt #if defined(_KERNEL)
286 1.1 yamt n = pool_cache_get(radix_tree_node_cache, PR_NOWAIT);
287 1.1 yamt #else /* defined(_KERNEL) */
288 1.3 yamt #if defined(_STANDALONE)
289 1.3 yamt n = alloc(sizeof(*n));
290 1.3 yamt #else /* defined(_STANDALONE) */
291 1.3 yamt n = malloc(sizeof(*n));
292 1.3 yamt #endif /* defined(_STANDALONE) */
293 1.3 yamt if (n != NULL) {
294 1.3 yamt radix_tree_node_init(n);
295 1.3 yamt }
296 1.1 yamt #endif /* defined(_KERNEL) */
297 1.1 yamt KASSERT(n == NULL || radix_tree_node_clean_p(n));
298 1.1 yamt return n;
299 1.1 yamt }
300 1.1 yamt
301 1.1 yamt static void
302 1.1 yamt radix_tree_free_node(struct radix_tree_node *n)
303 1.1 yamt {
304 1.1 yamt
305 1.1 yamt KASSERT(radix_tree_node_clean_p(n));
306 1.1 yamt #if defined(_KERNEL)
307 1.1 yamt pool_cache_put(radix_tree_node_cache, n);
308 1.3 yamt #elif defined(_STANDALONE)
309 1.3 yamt dealloc(n, sizeof(*n));
310 1.3 yamt #else
311 1.1 yamt free(n);
312 1.3 yamt #endif
313 1.1 yamt }
314 1.1 yamt
315 1.1 yamt static int
316 1.1 yamt radix_tree_grow(struct radix_tree *t, unsigned int newheight)
317 1.1 yamt {
318 1.1 yamt const unsigned int tagmask = entry_tagmask(t->t_root);
319 1.1 yamt
320 1.1 yamt KASSERT(newheight <= 64 / RADIX_TREE_BITS_PER_HEIGHT);
321 1.1 yamt if (t->t_root == NULL) {
322 1.1 yamt t->t_height = newheight;
323 1.1 yamt return 0;
324 1.1 yamt }
325 1.1 yamt while (t->t_height < newheight) {
326 1.1 yamt struct radix_tree_node *n;
327 1.1 yamt
328 1.1 yamt n = radix_tree_alloc_node();
329 1.1 yamt if (n == NULL) {
330 1.1 yamt /*
331 1.1 yamt * don't bother to revert our changes.
332 1.1 yamt * the caller will likely retry.
333 1.1 yamt */
334 1.1 yamt return ENOMEM;
335 1.1 yamt }
336 1.1 yamt n->n_nptrs = 1;
337 1.1 yamt n->n_ptrs[0] = t->t_root;
338 1.1 yamt t->t_root = entry_compose(n, tagmask);
339 1.1 yamt t->t_height++;
340 1.1 yamt }
341 1.1 yamt return 0;
342 1.1 yamt }
343 1.1 yamt
344 1.5 yamt /*
345 1.5 yamt * radix_tree_lookup_ptr:
346 1.5 yamt *
347 1.5 yamt * an internal helper function used for various exported functions.
348 1.5 yamt *
349 1.5 yamt * return the pointer to store the node for the given index.
350 1.5 yamt *
351 1.5 yamt * if alloc is true, try to allocate the storage. (note for _KERNEL:
352 1.5 yamt * in that case, this function can block.) if the allocation failed or
353 1.5 yamt * alloc is false, return NULL.
354 1.5 yamt *
355 1.5 yamt * if path is not NULL, fill it for the caller's investigation.
356 1.5 yamt *
357 1.5 yamt * if tagmask is not zero, search only for nodes with the tag set.
358 1.5 yamt *
359 1.5 yamt * while this function is a bit large, as it's called with some constant
360 1.5 yamt * arguments, inlining might have benefits. anyway, a compiler will decide.
361 1.5 yamt */
362 1.5 yamt
363 1.1 yamt static inline void **
364 1.1 yamt radix_tree_lookup_ptr(struct radix_tree *t, uint64_t idx,
365 1.1 yamt struct radix_tree_path *path, bool alloc, const unsigned int tagmask)
366 1.1 yamt {
367 1.1 yamt struct radix_tree_node *n;
368 1.1 yamt int hshift = RADIX_TREE_BITS_PER_HEIGHT * t->t_height;
369 1.1 yamt int shift;
370 1.1 yamt void **vpp;
371 1.1 yamt const uint64_t mask = (UINT64_C(1) << RADIX_TREE_BITS_PER_HEIGHT) - 1;
372 1.1 yamt struct radix_tree_node_ref *refs = NULL;
373 1.1 yamt
374 1.5 yamt /*
375 1.5 yamt * check unsupported combinations
376 1.5 yamt */
377 1.1 yamt KASSERT(tagmask == 0 || !alloc);
378 1.1 yamt KASSERT(path == NULL || !alloc);
379 1.1 yamt vpp = &t->t_root;
380 1.1 yamt if (path != NULL) {
381 1.1 yamt refs = path->p_refs;
382 1.1 yamt refs->pptr = vpp;
383 1.1 yamt }
384 1.1 yamt n = NULL;
385 1.1 yamt for (shift = 64 - RADIX_TREE_BITS_PER_HEIGHT; shift >= 0;) {
386 1.1 yamt struct radix_tree_node *c;
387 1.1 yamt void *entry;
388 1.1 yamt const uint64_t i = (idx >> shift) & mask;
389 1.1 yamt
390 1.1 yamt if (shift >= hshift) {
391 1.1 yamt unsigned int newheight;
392 1.1 yamt
393 1.1 yamt KASSERT(vpp == &t->t_root);
394 1.1 yamt if (i == 0) {
395 1.1 yamt shift -= RADIX_TREE_BITS_PER_HEIGHT;
396 1.1 yamt continue;
397 1.1 yamt }
398 1.1 yamt if (!alloc) {
399 1.1 yamt if (path != NULL) {
400 1.1 yamt KASSERT((refs - path->p_refs) == 0);
401 1.1 yamt path->p_lastidx = 0;
402 1.1 yamt }
403 1.1 yamt return NULL;
404 1.1 yamt }
405 1.1 yamt newheight = shift / RADIX_TREE_BITS_PER_HEIGHT + 1;
406 1.1 yamt if (radix_tree_grow(t, newheight)) {
407 1.1 yamt return NULL;
408 1.1 yamt }
409 1.1 yamt hshift = RADIX_TREE_BITS_PER_HEIGHT * t->t_height;
410 1.1 yamt }
411 1.1 yamt entry = *vpp;
412 1.1 yamt c = entry_ptr(entry);
413 1.1 yamt if (c == NULL ||
414 1.1 yamt (tagmask != 0 &&
415 1.1 yamt (entry_tagmask(entry) & tagmask) == 0)) {
416 1.1 yamt if (!alloc) {
417 1.1 yamt if (path != NULL) {
418 1.1 yamt path->p_lastidx = refs - path->p_refs;
419 1.1 yamt }
420 1.1 yamt return NULL;
421 1.1 yamt }
422 1.1 yamt c = radix_tree_alloc_node();
423 1.1 yamt if (c == NULL) {
424 1.1 yamt return NULL;
425 1.1 yamt }
426 1.1 yamt *vpp = c;
427 1.1 yamt if (n != NULL) {
428 1.1 yamt KASSERT(n->n_nptrs < RADIX_TREE_PTR_PER_NODE);
429 1.1 yamt n->n_nptrs++;
430 1.1 yamt }
431 1.1 yamt }
432 1.1 yamt n = c;
433 1.1 yamt vpp = &n->n_ptrs[i];
434 1.1 yamt if (path != NULL) {
435 1.1 yamt refs++;
436 1.1 yamt refs->pptr = vpp;
437 1.1 yamt }
438 1.1 yamt shift -= RADIX_TREE_BITS_PER_HEIGHT;
439 1.1 yamt }
440 1.1 yamt if (alloc) {
441 1.1 yamt KASSERT(*vpp == NULL);
442 1.1 yamt if (n != NULL) {
443 1.1 yamt KASSERT(n->n_nptrs < RADIX_TREE_PTR_PER_NODE);
444 1.1 yamt n->n_nptrs++;
445 1.1 yamt }
446 1.1 yamt }
447 1.1 yamt if (path != NULL) {
448 1.1 yamt path->p_lastidx = refs - path->p_refs;
449 1.1 yamt }
450 1.1 yamt return vpp;
451 1.1 yamt }
452 1.1 yamt
453 1.1 yamt /*
454 1.1 yamt * radix_tree_insert_node:
455 1.1 yamt *
456 1.1 yamt * insert the node at idx.
457 1.1 yamt * it's illegal to insert NULL.
458 1.1 yamt * it's illegal to insert a non-aligned pointer.
459 1.1 yamt *
460 1.1 yamt * this function returns ENOMEM if necessary memory allocation failed.
461 1.1 yamt * otherwise, this function returns 0.
462 1.1 yamt *
463 1.1 yamt * note that inserting a node can involves memory allocation for intermediate
464 1.1 yamt * nodes. if _KERNEL, it's done with non-blocking IPL_NONE memory allocation.
465 1.4 yamt *
466 1.4 yamt * for the newly inserted node, all tags are cleared.
467 1.1 yamt */
468 1.1 yamt
469 1.1 yamt int
470 1.1 yamt radix_tree_insert_node(struct radix_tree *t, uint64_t idx, void *p)
471 1.1 yamt {
472 1.1 yamt void **vpp;
473 1.1 yamt
474 1.1 yamt KASSERT(p != NULL);
475 1.1 yamt KASSERT(entry_compose(p, 0) == p);
476 1.1 yamt vpp = radix_tree_lookup_ptr(t, idx, NULL, true, 0);
477 1.1 yamt if (vpp == NULL) {
478 1.1 yamt return ENOMEM;
479 1.1 yamt }
480 1.1 yamt KASSERT(*vpp == NULL);
481 1.1 yamt *vpp = p;
482 1.1 yamt return 0;
483 1.1 yamt }
484 1.1 yamt
485 1.4 yamt /*
486 1.4 yamt * radix_tree_replace_node:
487 1.4 yamt *
488 1.4 yamt * replace a node at the given index with the given node.
489 1.4 yamt * return the old node.
490 1.4 yamt * it's illegal to try to replace a node which has not been inserted.
491 1.4 yamt *
492 1.4 yamt * this function doesn't change tags.
493 1.4 yamt */
494 1.4 yamt
495 1.1 yamt void *
496 1.1 yamt radix_tree_replace_node(struct radix_tree *t, uint64_t idx, void *p)
497 1.1 yamt {
498 1.1 yamt void **vpp;
499 1.1 yamt void *oldp;
500 1.1 yamt
501 1.1 yamt KASSERT(p != NULL);
502 1.1 yamt KASSERT(entry_compose(p, 0) == p);
503 1.1 yamt vpp = radix_tree_lookup_ptr(t, idx, NULL, false, 0);
504 1.1 yamt KASSERT(vpp != NULL);
505 1.1 yamt oldp = *vpp;
506 1.1 yamt KASSERT(oldp != NULL);
507 1.1 yamt *vpp = entry_compose(p, entry_tagmask(*vpp));
508 1.1 yamt return entry_ptr(oldp);
509 1.1 yamt }
510 1.1 yamt
511 1.1 yamt /*
512 1.1 yamt * radix_tree_remove_node:
513 1.1 yamt *
514 1.1 yamt * remove the node at idx.
515 1.1 yamt * it's illegal to try to remove a node which has not been inserted.
516 1.1 yamt */
517 1.1 yamt
518 1.1 yamt void *
519 1.1 yamt radix_tree_remove_node(struct radix_tree *t, uint64_t idx)
520 1.1 yamt {
521 1.1 yamt struct radix_tree_path path;
522 1.1 yamt void **vpp;
523 1.1 yamt void *oldp;
524 1.1 yamt int i;
525 1.1 yamt
526 1.1 yamt vpp = radix_tree_lookup_ptr(t, idx, &path, false, 0);
527 1.1 yamt KASSERT(vpp != NULL);
528 1.1 yamt oldp = *vpp;
529 1.1 yamt KASSERT(oldp != NULL);
530 1.1 yamt KASSERT(path.p_lastidx == t->t_height);
531 1.1 yamt KASSERT(vpp == path_pptr(t, &path, path.p_lastidx));
532 1.1 yamt *vpp = NULL;
533 1.1 yamt for (i = t->t_height - 1; i >= 0; i--) {
534 1.1 yamt void *entry;
535 1.1 yamt struct radix_tree_node ** const pptr =
536 1.1 yamt (struct radix_tree_node **)path_pptr(t, &path, i);
537 1.1 yamt struct radix_tree_node *n;
538 1.1 yamt
539 1.1 yamt KASSERT(pptr != NULL);
540 1.1 yamt entry = *pptr;
541 1.1 yamt n = entry_ptr(entry);
542 1.1 yamt KASSERT(n != NULL);
543 1.1 yamt KASSERT(n->n_nptrs > 0);
544 1.1 yamt n->n_nptrs--;
545 1.1 yamt if (n->n_nptrs > 0) {
546 1.1 yamt break;
547 1.1 yamt }
548 1.1 yamt radix_tree_free_node(n);
549 1.1 yamt *pptr = NULL;
550 1.1 yamt }
551 1.1 yamt /*
552 1.1 yamt * fix up height
553 1.1 yamt */
554 1.1 yamt if (i < 0) {
555 1.1 yamt KASSERT(t->t_root == NULL);
556 1.1 yamt t->t_height = 0;
557 1.1 yamt }
558 1.1 yamt /*
559 1.1 yamt * update tags
560 1.1 yamt */
561 1.1 yamt for (; i >= 0; i--) {
562 1.1 yamt void *entry;
563 1.1 yamt struct radix_tree_node ** const pptr =
564 1.1 yamt (struct radix_tree_node **)path_pptr(t, &path, i);
565 1.1 yamt struct radix_tree_node *n;
566 1.1 yamt unsigned int newmask;
567 1.1 yamt
568 1.1 yamt KASSERT(pptr != NULL);
569 1.1 yamt entry = *pptr;
570 1.1 yamt n = entry_ptr(entry);
571 1.1 yamt KASSERT(n != NULL);
572 1.1 yamt KASSERT(n->n_nptrs > 0);
573 1.1 yamt newmask = any_children_tagmask(n);
574 1.1 yamt if (newmask == entry_tagmask(entry)) {
575 1.1 yamt break;
576 1.1 yamt }
577 1.1 yamt *pptr = entry_compose(n, newmask);
578 1.1 yamt }
579 1.1 yamt /*
580 1.1 yamt * XXX is it worth to try to reduce height?
581 1.1 yamt * if we do that, make radix_tree_grow rollback its change as well.
582 1.1 yamt */
583 1.1 yamt return entry_ptr(oldp);
584 1.1 yamt }
585 1.1 yamt
586 1.1 yamt /*
587 1.1 yamt * radix_tree_lookup_node:
588 1.1 yamt *
589 1.1 yamt * returns the node at idx.
590 1.1 yamt * returns NULL if nothing is found at idx.
591 1.1 yamt */
592 1.1 yamt
593 1.1 yamt void *
594 1.1 yamt radix_tree_lookup_node(struct radix_tree *t, uint64_t idx)
595 1.1 yamt {
596 1.1 yamt void **vpp;
597 1.1 yamt
598 1.1 yamt vpp = radix_tree_lookup_ptr(t, idx, NULL, false, 0);
599 1.1 yamt if (vpp == NULL) {
600 1.1 yamt return NULL;
601 1.1 yamt }
602 1.1 yamt return entry_ptr(*vpp);
603 1.1 yamt }
604 1.1 yamt
605 1.1 yamt static inline void
606 1.1 yamt gang_lookup_init(struct radix_tree *t, uint64_t idx,
607 1.1 yamt struct radix_tree_path *path, const unsigned int tagmask)
608 1.1 yamt {
609 1.1 yamt void **vpp;
610 1.1 yamt
611 1.1 yamt vpp = radix_tree_lookup_ptr(t, idx, path, false, tagmask);
612 1.1 yamt KASSERT(vpp == NULL ||
613 1.1 yamt vpp == path_pptr(t, path, path->p_lastidx));
614 1.1 yamt KASSERT(&t->t_root == path_pptr(t, path, 0));
615 1.1 yamt }
616 1.1 yamt
617 1.1 yamt static inline unsigned int
618 1.1 yamt gang_lookup_scan(struct radix_tree *t, struct radix_tree_path *path,
619 1.1 yamt void **results, unsigned int maxresults, const unsigned int tagmask)
620 1.1 yamt {
621 1.1 yamt void **vpp;
622 1.10 yamt unsigned int nfound;
623 1.1 yamt unsigned int lastidx;
624 1.1 yamt
625 1.1 yamt KASSERT(maxresults > 0);
626 1.1 yamt lastidx = path->p_lastidx;
627 1.1 yamt if (lastidx == 0) {
628 1.1 yamt return 0;
629 1.1 yamt }
630 1.1 yamt nfound = 0;
631 1.1 yamt vpp = path_pptr(t, path, lastidx);
632 1.1 yamt while (/*CONSTCOND*/true) {
633 1.1 yamt struct radix_tree_node *n;
634 1.10 yamt unsigned int i;
635 1.1 yamt
636 1.1 yamt if (entry_match_p(*vpp, tagmask)) {
637 1.1 yamt KASSERT(lastidx == t->t_height);
638 1.1 yamt /*
639 1.1 yamt * record the non-NULL leaf.
640 1.1 yamt */
641 1.1 yamt results[nfound] = entry_ptr(*vpp);
642 1.1 yamt nfound++;
643 1.1 yamt if (nfound == maxresults) {
644 1.1 yamt return nfound;
645 1.1 yamt }
646 1.1 yamt }
647 1.1 yamt scan_siblings:
648 1.1 yamt /*
649 1.1 yamt * try to find the next non-NULL sibling.
650 1.1 yamt */
651 1.1 yamt n = path_node(t, path, lastidx - 1);
652 1.1 yamt if (*vpp != NULL && n->n_nptrs == 1) {
653 1.1 yamt /*
654 1.1 yamt * optimization
655 1.1 yamt */
656 1.1 yamt goto no_siblings;
657 1.1 yamt }
658 1.1 yamt for (i = path_idx(t, path, lastidx - 1) + 1;
659 1.1 yamt i < RADIX_TREE_PTR_PER_NODE;
660 1.1 yamt i++) {
661 1.1 yamt if (entry_match_p(n->n_ptrs[i], tagmask)) {
662 1.1 yamt vpp = &n->n_ptrs[i];
663 1.1 yamt path->p_refs[lastidx].pptr = vpp;
664 1.1 yamt KASSERT(path_idx(t, path, lastidx - 1)
665 1.1 yamt == i);
666 1.1 yamt break;
667 1.1 yamt }
668 1.1 yamt }
669 1.1 yamt if (i == RADIX_TREE_PTR_PER_NODE) {
670 1.1 yamt no_siblings:
671 1.1 yamt /*
672 1.1 yamt * not found. go to parent.
673 1.1 yamt */
674 1.1 yamt lastidx--;
675 1.1 yamt if (lastidx == 0) {
676 1.1 yamt return nfound;
677 1.1 yamt }
678 1.1 yamt vpp = path_pptr(t, path, lastidx);
679 1.1 yamt goto scan_siblings;
680 1.1 yamt }
681 1.1 yamt /*
682 1.1 yamt * descending the left-most child node, upto the leaf or NULL.
683 1.1 yamt */
684 1.1 yamt while (entry_match_p(*vpp, tagmask) && lastidx < t->t_height) {
685 1.1 yamt n = entry_ptr(*vpp);
686 1.1 yamt vpp = &n->n_ptrs[0];
687 1.1 yamt lastidx++;
688 1.1 yamt path->p_refs[lastidx].pptr = vpp;
689 1.1 yamt }
690 1.1 yamt }
691 1.1 yamt }
692 1.1 yamt
693 1.1 yamt /*
694 1.1 yamt * radix_tree_gang_lookup_node:
695 1.1 yamt *
696 1.1 yamt * search nodes starting from idx in the ascending order.
697 1.1 yamt * results should be an array large enough to hold maxresults pointers.
698 1.1 yamt * returns the number of nodes found, up to maxresults.
699 1.1 yamt * returning less than maxresults means there are no more nodes.
700 1.1 yamt *
701 1.1 yamt * the result of this function is semantically equivalent to what could be
702 1.1 yamt * obtained by repeated calls of radix_tree_lookup_node with increasing index.
703 1.1 yamt * but this function is much faster when node indexes are distributed sparsely.
704 1.1 yamt *
705 1.1 yamt * note that this function doesn't return exact values of node indexes of
706 1.1 yamt * found nodes. if they are important for a caller, it's the caller's
707 1.1 yamt * responsibility to check them, typically by examinining the returned nodes
708 1.1 yamt * using some caller-specific knowledge about them.
709 1.1 yamt */
710 1.1 yamt
711 1.1 yamt unsigned int
712 1.1 yamt radix_tree_gang_lookup_node(struct radix_tree *t, uint64_t idx,
713 1.1 yamt void **results, unsigned int maxresults)
714 1.1 yamt {
715 1.1 yamt struct radix_tree_path path;
716 1.1 yamt
717 1.1 yamt gang_lookup_init(t, idx, &path, 0);
718 1.1 yamt return gang_lookup_scan(t, &path, results, maxresults, 0);
719 1.1 yamt }
720 1.1 yamt
721 1.1 yamt /*
722 1.1 yamt * radix_tree_gang_lookup_tagged_node:
723 1.1 yamt *
724 1.1 yamt * same as radix_tree_gang_lookup_node except that this one only returns
725 1.1 yamt * nodes tagged with tagid.
726 1.1 yamt */
727 1.1 yamt
728 1.1 yamt unsigned int
729 1.1 yamt radix_tree_gang_lookup_tagged_node(struct radix_tree *t, uint64_t idx,
730 1.1 yamt void **results, unsigned int maxresults, radix_tree_tagid_t tagid)
731 1.1 yamt {
732 1.1 yamt struct radix_tree_path path;
733 1.1 yamt const unsigned int tagmask = tagid_to_mask(tagid);
734 1.1 yamt
735 1.1 yamt gang_lookup_init(t, idx, &path, tagmask);
736 1.1 yamt return gang_lookup_scan(t, &path, results, maxresults, tagmask);
737 1.1 yamt }
738 1.1 yamt
739 1.4 yamt /*
740 1.4 yamt * radix_tree_get_tag:
741 1.4 yamt *
742 1.4 yamt * return if the tag is set for the node at the given index. (true if set)
743 1.4 yamt * it's illegal to call this function for a node which has not been inserted.
744 1.4 yamt */
745 1.4 yamt
746 1.1 yamt bool
747 1.1 yamt radix_tree_get_tag(struct radix_tree *t, uint64_t idx,
748 1.1 yamt radix_tree_tagid_t tagid)
749 1.1 yamt {
750 1.1 yamt #if 1
751 1.1 yamt const unsigned int tagmask = tagid_to_mask(tagid);
752 1.1 yamt void **vpp;
753 1.1 yamt
754 1.1 yamt vpp = radix_tree_lookup_ptr(t, idx, NULL, false, tagmask);
755 1.1 yamt if (vpp == NULL) {
756 1.1 yamt return false;
757 1.1 yamt }
758 1.1 yamt KASSERT(*vpp != NULL);
759 1.1 yamt return (entry_tagmask(*vpp) & tagmask) != 0;
760 1.1 yamt #else
761 1.1 yamt const unsigned int tagmask = tagid_to_mask(tagid);
762 1.1 yamt void **vpp;
763 1.1 yamt
764 1.1 yamt vpp = radix_tree_lookup_ptr(t, idx, NULL, false, 0);
765 1.1 yamt KASSERT(vpp != NULL);
766 1.1 yamt return (entry_tagmask(*vpp) & tagmask) != 0;
767 1.1 yamt #endif
768 1.1 yamt }
769 1.1 yamt
770 1.4 yamt /*
771 1.4 yamt * radix_tree_set_tag:
772 1.4 yamt *
773 1.4 yamt * set the tag for the node at the given index.
774 1.4 yamt * it's illegal to call this function for a node which has not been inserted.
775 1.4 yamt */
776 1.4 yamt
777 1.1 yamt void
778 1.1 yamt radix_tree_set_tag(struct radix_tree *t, uint64_t idx,
779 1.1 yamt radix_tree_tagid_t tagid)
780 1.1 yamt {
781 1.1 yamt struct radix_tree_path path;
782 1.1 yamt const unsigned int tagmask = tagid_to_mask(tagid);
783 1.1 yamt void **vpp;
784 1.1 yamt int i;
785 1.1 yamt
786 1.1 yamt vpp = radix_tree_lookup_ptr(t, idx, &path, false, 0);
787 1.1 yamt KASSERT(vpp != NULL);
788 1.1 yamt KASSERT(*vpp != NULL);
789 1.1 yamt KASSERT(path.p_lastidx == t->t_height);
790 1.1 yamt KASSERT(vpp == path_pptr(t, &path, path.p_lastidx));
791 1.1 yamt for (i = t->t_height; i >= 0; i--) {
792 1.1 yamt void ** const pptr = (void **)path_pptr(t, &path, i);
793 1.1 yamt void *entry;
794 1.1 yamt
795 1.1 yamt KASSERT(pptr != NULL);
796 1.1 yamt entry = *pptr;
797 1.1 yamt if ((entry_tagmask(entry) & tagmask) != 0) {
798 1.1 yamt break;
799 1.1 yamt }
800 1.1 yamt *pptr = (void *)((uintptr_t)entry | tagmask);
801 1.1 yamt }
802 1.1 yamt }
803 1.1 yamt
804 1.4 yamt /*
805 1.4 yamt * radix_tree_clear_tag:
806 1.4 yamt *
807 1.4 yamt * clear the tag for the node at the given index.
808 1.4 yamt * it's illegal to call this function for a node which has not been inserted.
809 1.4 yamt */
810 1.4 yamt
811 1.1 yamt void
812 1.1 yamt radix_tree_clear_tag(struct radix_tree *t, uint64_t idx,
813 1.1 yamt radix_tree_tagid_t tagid)
814 1.1 yamt {
815 1.1 yamt struct radix_tree_path path;
816 1.1 yamt const unsigned int tagmask = tagid_to_mask(tagid);
817 1.1 yamt void **vpp;
818 1.1 yamt int i;
819 1.1 yamt
820 1.1 yamt vpp = radix_tree_lookup_ptr(t, idx, &path, false, 0);
821 1.1 yamt KASSERT(vpp != NULL);
822 1.1 yamt KASSERT(*vpp != NULL);
823 1.1 yamt KASSERT(path.p_lastidx == t->t_height);
824 1.1 yamt KASSERT(vpp == path_pptr(t, &path, path.p_lastidx));
825 1.7 yamt /*
826 1.7 yamt * if already cleared, nothing to do
827 1.7 yamt */
828 1.1 yamt if ((entry_tagmask(*vpp) & tagmask) == 0) {
829 1.1 yamt return;
830 1.1 yamt }
831 1.7 yamt /*
832 1.7 yamt * clear the tag only if no children have the tag.
833 1.7 yamt */
834 1.1 yamt for (i = t->t_height; i >= 0; i--) {
835 1.1 yamt void ** const pptr = (void **)path_pptr(t, &path, i);
836 1.1 yamt void *entry;
837 1.1 yamt
838 1.1 yamt KASSERT(pptr != NULL);
839 1.1 yamt entry = *pptr;
840 1.1 yamt KASSERT((entry_tagmask(entry) & tagmask) != 0);
841 1.1 yamt *pptr = entry_compose(entry_ptr(entry),
842 1.1 yamt entry_tagmask(entry) & ~tagmask);
843 1.7 yamt /*
844 1.7 yamt * check if we should proceed to process the next level.
845 1.7 yamt */
846 1.7 yamt if (0 < i) {
847 1.1 yamt struct radix_tree_node *n = path_node(t, &path, i - 1);
848 1.1 yamt
849 1.1 yamt if ((any_children_tagmask(n) & tagmask) != 0) {
850 1.1 yamt break;
851 1.1 yamt }
852 1.1 yamt }
853 1.1 yamt }
854 1.1 yamt }
855 1.1 yamt
856 1.1 yamt #if defined(UNITTEST)
857 1.1 yamt
858 1.1 yamt #include <inttypes.h>
859 1.1 yamt #include <stdio.h>
860 1.1 yamt
861 1.1 yamt static void
862 1.1 yamt radix_tree_dump_node(const struct radix_tree *t, void *vp,
863 1.1 yamt uint64_t offset, unsigned int height)
864 1.1 yamt {
865 1.1 yamt struct radix_tree_node *n;
866 1.1 yamt unsigned int i;
867 1.1 yamt
868 1.1 yamt for (i = 0; i < t->t_height - height; i++) {
869 1.1 yamt printf(" ");
870 1.1 yamt }
871 1.1 yamt if (entry_tagmask(vp) == 0) {
872 1.1 yamt printf("[%" PRIu64 "] %p", offset, entry_ptr(vp));
873 1.1 yamt } else {
874 1.1 yamt printf("[%" PRIu64 "] %p (tagmask=0x%x)", offset, entry_ptr(vp),
875 1.1 yamt entry_tagmask(vp));
876 1.1 yamt }
877 1.1 yamt if (height == 0) {
878 1.1 yamt printf(" (leaf)\n");
879 1.1 yamt return;
880 1.1 yamt }
881 1.1 yamt n = entry_ptr(vp);
882 1.1 yamt assert(any_children_tagmask(n) == entry_tagmask(vp));
883 1.1 yamt printf(" (%u children)\n", n->n_nptrs);
884 1.1 yamt for (i = 0; i < __arraycount(n->n_ptrs); i++) {
885 1.1 yamt void *c;
886 1.1 yamt
887 1.1 yamt c = n->n_ptrs[i];
888 1.1 yamt if (c == NULL) {
889 1.1 yamt continue;
890 1.1 yamt }
891 1.1 yamt radix_tree_dump_node(t, c,
892 1.1 yamt offset + i * (UINT64_C(1) <<
893 1.1 yamt (RADIX_TREE_BITS_PER_HEIGHT * (height - 1))), height - 1);
894 1.1 yamt }
895 1.1 yamt }
896 1.1 yamt
897 1.1 yamt void radix_tree_dump(const struct radix_tree *);
898 1.1 yamt
899 1.1 yamt void
900 1.1 yamt radix_tree_dump(const struct radix_tree *t)
901 1.1 yamt {
902 1.1 yamt
903 1.1 yamt printf("tree %p height=%u\n", t, t->t_height);
904 1.1 yamt radix_tree_dump_node(t, t->t_root, 0, t->t_height);
905 1.1 yamt }
906 1.1 yamt
907 1.1 yamt static void
908 1.1 yamt test1(void)
909 1.1 yamt {
910 1.1 yamt struct radix_tree s;
911 1.1 yamt struct radix_tree *t = &s;
912 1.1 yamt void *results[3];
913 1.1 yamt
914 1.1 yamt radix_tree_init_tree(t);
915 1.1 yamt radix_tree_dump(t);
916 1.1 yamt assert(radix_tree_lookup_node(t, 0) == NULL);
917 1.1 yamt assert(radix_tree_lookup_node(t, 1000) == NULL);
918 1.1 yamt assert(radix_tree_insert_node(t, 1000, (void *)0xdeadbea0) == 0);
919 1.1 yamt radix_tree_dump(t);
920 1.1 yamt assert(!radix_tree_get_tag(t, 1000, 0));
921 1.1 yamt assert(!radix_tree_get_tag(t, 1000, 1));
922 1.1 yamt radix_tree_set_tag(t, 1000, 1);
923 1.1 yamt assert(!radix_tree_get_tag(t, 1000, 0));
924 1.1 yamt assert(radix_tree_get_tag(t, 1000, 1));
925 1.1 yamt radix_tree_dump(t);
926 1.1 yamt assert(radix_tree_lookup_node(t, 1000) == (void *)0xdeadbea0);
927 1.1 yamt assert(radix_tree_insert_node(t, 0, (void *)0xbea0) == 0);
928 1.1 yamt radix_tree_dump(t);
929 1.1 yamt assert(radix_tree_lookup_node(t, 0) == (void *)0xbea0);
930 1.1 yamt assert(radix_tree_lookup_node(t, 1000) == (void *)0xdeadbea0);
931 1.1 yamt assert(radix_tree_insert_node(t, UINT64_C(10000000000), (void *)0xdea0)
932 1.1 yamt == 0);
933 1.1 yamt radix_tree_dump(t);
934 1.1 yamt assert(radix_tree_lookup_node(t, 0) == (void *)0xbea0);
935 1.1 yamt assert(radix_tree_lookup_node(t, 1000) == (void *)0xdeadbea0);
936 1.1 yamt assert(radix_tree_lookup_node(t, UINT64_C(10000000000)) ==
937 1.1 yamt (void *)0xdea0);
938 1.1 yamt radix_tree_dump(t);
939 1.1 yamt assert(!radix_tree_get_tag(t, 0, 1));
940 1.1 yamt assert(radix_tree_get_tag(t, 1000, 1));
941 1.1 yamt assert(!radix_tree_get_tag(t, UINT64_C(10000000000), 1));
942 1.1 yamt radix_tree_set_tag(t, 0, 1);;
943 1.1 yamt radix_tree_set_tag(t, UINT64_C(10000000000), 1);
944 1.1 yamt radix_tree_dump(t);
945 1.1 yamt assert(radix_tree_get_tag(t, 0, 1));
946 1.1 yamt assert(radix_tree_get_tag(t, 1000, 1));
947 1.1 yamt assert(radix_tree_get_tag(t, UINT64_C(10000000000), 1));
948 1.1 yamt radix_tree_clear_tag(t, 0, 1);;
949 1.1 yamt radix_tree_clear_tag(t, UINT64_C(10000000000), 1);
950 1.1 yamt radix_tree_dump(t);
951 1.1 yamt assert(!radix_tree_get_tag(t, 0, 1));
952 1.1 yamt assert(radix_tree_get_tag(t, 1000, 1));
953 1.1 yamt assert(!radix_tree_get_tag(t, UINT64_C(10000000000), 1));
954 1.1 yamt radix_tree_dump(t);
955 1.1 yamt assert(radix_tree_replace_node(t, 1000, (void *)0x12345678) ==
956 1.1 yamt (void *)0xdeadbea0);
957 1.1 yamt assert(!radix_tree_get_tag(t, 1000, 0));
958 1.1 yamt assert(radix_tree_get_tag(t, 1000, 1));
959 1.1 yamt assert(radix_tree_gang_lookup_node(t, 0, results, 3) == 3);
960 1.1 yamt assert(results[0] == (void *)0xbea0);
961 1.1 yamt assert(results[1] == (void *)0x12345678);
962 1.1 yamt assert(results[2] == (void *)0xdea0);
963 1.1 yamt assert(radix_tree_gang_lookup_node(t, 1, results, 3) == 2);
964 1.1 yamt assert(results[0] == (void *)0x12345678);
965 1.1 yamt assert(results[1] == (void *)0xdea0);
966 1.1 yamt assert(radix_tree_gang_lookup_node(t, 1001, results, 3) == 1);
967 1.1 yamt assert(results[0] == (void *)0xdea0);
968 1.1 yamt assert(radix_tree_gang_lookup_node(t, UINT64_C(10000000001), results, 3)
969 1.1 yamt == 0);
970 1.1 yamt assert(radix_tree_gang_lookup_node(t, UINT64_C(1000000000000), results,
971 1.1 yamt 3) == 0);
972 1.1 yamt assert(radix_tree_gang_lookup_tagged_node(t, 0, results, 100, 1) == 1);
973 1.1 yamt assert(results[0] == (void *)0x12345678);
974 1.1 yamt assert(entry_tagmask(t->t_root) != 0);
975 1.1 yamt assert(radix_tree_remove_node(t, 1000) == (void *)0x12345678);
976 1.1 yamt assert(entry_tagmask(t->t_root) == 0);
977 1.1 yamt radix_tree_dump(t);
978 1.1 yamt assert(radix_tree_remove_node(t, UINT64_C(10000000000)) ==
979 1.1 yamt (void *)0xdea0);
980 1.1 yamt radix_tree_dump(t);
981 1.1 yamt assert(radix_tree_remove_node(t, 0) == (void *)0xbea0);
982 1.1 yamt radix_tree_dump(t);
983 1.1 yamt radix_tree_fini_tree(t);
984 1.1 yamt }
985 1.1 yamt
986 1.1 yamt #include <sys/time.h>
987 1.1 yamt
988 1.1 yamt struct testnode {
989 1.1 yamt uint64_t idx;
990 1.1 yamt };
991 1.1 yamt
992 1.1 yamt static void
993 1.11 yamt printops(const char *title, const char *name, int tag, unsigned int n,
994 1.11 yamt const struct timeval *stv, const struct timeval *etv)
995 1.1 yamt {
996 1.1 yamt uint64_t s = stv->tv_sec * 1000000 + stv->tv_usec;
997 1.1 yamt uint64_t e = etv->tv_sec * 1000000 + etv->tv_usec;
998 1.1 yamt
999 1.11 yamt printf("RESULT %s %s %d %lf op/s\n", title, name, tag,
1000 1.11 yamt (double)n / (e - s) * 1000000);
1001 1.1 yamt }
1002 1.1 yamt
1003 1.1 yamt #define TEST2_GANG_LOOKUP_NODES 16
1004 1.1 yamt
1005 1.1 yamt static bool
1006 1.1 yamt test2_should_tag(unsigned int i, radix_tree_tagid_t tagid)
1007 1.1 yamt {
1008 1.1 yamt
1009 1.1 yamt if (tagid == 0) {
1010 1.11 yamt return (i & 0x3) == 0; /* 25% */
1011 1.1 yamt } else {
1012 1.11 yamt return (i % 7) == 0; /* 14% */
1013 1.1 yamt }
1014 1.1 yamt }
1015 1.1 yamt
1016 1.1 yamt static void
1017 1.11 yamt test2(const char *title, bool dense)
1018 1.1 yamt {
1019 1.1 yamt struct radix_tree s;
1020 1.1 yamt struct radix_tree *t = &s;
1021 1.1 yamt struct testnode *n;
1022 1.1 yamt unsigned int i;
1023 1.1 yamt unsigned int nnodes = 100000;
1024 1.1 yamt unsigned int removed;
1025 1.1 yamt radix_tree_tagid_t tag;
1026 1.1 yamt unsigned int ntagged[RADIX_TREE_TAG_ID_MAX];
1027 1.1 yamt struct testnode *nodes;
1028 1.1 yamt struct timeval stv;
1029 1.1 yamt struct timeval etv;
1030 1.1 yamt
1031 1.1 yamt nodes = malloc(nnodes * sizeof(*nodes));
1032 1.1 yamt for (tag = 0; tag < RADIX_TREE_TAG_ID_MAX; tag++) {
1033 1.1 yamt ntagged[tag] = 0;
1034 1.1 yamt }
1035 1.1 yamt radix_tree_init_tree(t);
1036 1.1 yamt for (i = 0; i < nnodes; i++) {
1037 1.1 yamt n = &nodes[i];
1038 1.1 yamt n->idx = random();
1039 1.1 yamt if (sizeof(long) == 4) {
1040 1.1 yamt n->idx <<= 32;
1041 1.1 yamt n->idx |= (uint32_t)random();
1042 1.1 yamt }
1043 1.1 yamt if (dense) {
1044 1.1 yamt n->idx %= nnodes * 2;
1045 1.1 yamt }
1046 1.1 yamt while (radix_tree_lookup_node(t, n->idx) != NULL) {
1047 1.1 yamt n->idx++;
1048 1.1 yamt }
1049 1.1 yamt radix_tree_insert_node(t, n->idx, n);
1050 1.1 yamt for (tag = 0; tag < RADIX_TREE_TAG_ID_MAX; tag++) {
1051 1.1 yamt if (test2_should_tag(i, tag)) {
1052 1.1 yamt radix_tree_set_tag(t, n->idx, tag);
1053 1.1 yamt ntagged[tag]++;
1054 1.1 yamt }
1055 1.1 yamt assert(test2_should_tag(i, tag) ==
1056 1.1 yamt radix_tree_get_tag(t, n->idx, tag));
1057 1.1 yamt }
1058 1.1 yamt }
1059 1.1 yamt
1060 1.1 yamt gettimeofday(&stv, NULL);
1061 1.1 yamt for (i = 0; i < nnodes; i++) {
1062 1.1 yamt n = &nodes[i];
1063 1.1 yamt assert(radix_tree_lookup_node(t, n->idx) == n);
1064 1.1 yamt }
1065 1.1 yamt gettimeofday(&etv, NULL);
1066 1.11 yamt printops(title, "lookup", 0, nnodes, &stv, &etv);
1067 1.1 yamt
1068 1.1 yamt for (tag = 0; tag < RADIX_TREE_TAG_ID_MAX; tag++) {
1069 1.1 yamt gettimeofday(&stv, NULL);
1070 1.1 yamt for (i = 0; i < nnodes; i++) {
1071 1.1 yamt n = &nodes[i];
1072 1.1 yamt assert(test2_should_tag(i, tag) ==
1073 1.1 yamt radix_tree_get_tag(t, n->idx, tag));
1074 1.1 yamt }
1075 1.1 yamt gettimeofday(&etv, NULL);
1076 1.11 yamt printops(title, "get_tag", tag, ntagged[tag], &stv, &etv);
1077 1.1 yamt }
1078 1.1 yamt
1079 1.1 yamt gettimeofday(&stv, NULL);
1080 1.1 yamt for (i = 0; i < nnodes; i++) {
1081 1.1 yamt n = &nodes[i];
1082 1.1 yamt radix_tree_remove_node(t, n->idx);
1083 1.1 yamt }
1084 1.1 yamt gettimeofday(&etv, NULL);
1085 1.11 yamt printops(title, "remove", 0, nnodes, &stv, &etv);
1086 1.1 yamt
1087 1.1 yamt gettimeofday(&stv, NULL);
1088 1.1 yamt for (i = 0; i < nnodes; i++) {
1089 1.1 yamt n = &nodes[i];
1090 1.1 yamt radix_tree_insert_node(t, n->idx, n);
1091 1.1 yamt }
1092 1.1 yamt gettimeofday(&etv, NULL);
1093 1.11 yamt printops(title, "insert", 0, nnodes, &stv, &etv);
1094 1.1 yamt
1095 1.1 yamt for (tag = 0; tag < RADIX_TREE_TAG_ID_MAX; tag++) {
1096 1.1 yamt ntagged[tag] = 0;
1097 1.1 yamt gettimeofday(&stv, NULL);
1098 1.1 yamt for (i = 0; i < nnodes; i++) {
1099 1.1 yamt n = &nodes[i];
1100 1.1 yamt if (test2_should_tag(i, tag)) {
1101 1.1 yamt radix_tree_set_tag(t, n->idx, tag);
1102 1.1 yamt ntagged[tag]++;
1103 1.1 yamt }
1104 1.1 yamt }
1105 1.1 yamt gettimeofday(&etv, NULL);
1106 1.11 yamt printops(title, "set_tag", tag, ntagged[tag], &stv, &etv);
1107 1.1 yamt }
1108 1.1 yamt
1109 1.1 yamt gettimeofday(&stv, NULL);
1110 1.1 yamt {
1111 1.1 yamt struct testnode *results[TEST2_GANG_LOOKUP_NODES];
1112 1.1 yamt uint64_t nextidx;
1113 1.1 yamt unsigned int nfound;
1114 1.1 yamt unsigned int total;
1115 1.1 yamt
1116 1.1 yamt nextidx = 0;
1117 1.1 yamt total = 0;
1118 1.1 yamt while ((nfound = radix_tree_gang_lookup_node(t, nextidx,
1119 1.1 yamt (void *)results, __arraycount(results))) > 0) {
1120 1.1 yamt nextidx = results[nfound - 1]->idx + 1;
1121 1.1 yamt total += nfound;
1122 1.1 yamt }
1123 1.1 yamt assert(total == nnodes);
1124 1.1 yamt }
1125 1.1 yamt gettimeofday(&etv, NULL);
1126 1.11 yamt printops(title, "ganglookup", 0, nnodes, &stv, &etv);
1127 1.1 yamt
1128 1.1 yamt for (tag = 0; tag < RADIX_TREE_TAG_ID_MAX; tag++) {
1129 1.1 yamt gettimeofday(&stv, NULL);
1130 1.1 yamt {
1131 1.1 yamt struct testnode *results[TEST2_GANG_LOOKUP_NODES];
1132 1.1 yamt uint64_t nextidx;
1133 1.1 yamt unsigned int nfound;
1134 1.1 yamt unsigned int total;
1135 1.1 yamt
1136 1.1 yamt nextidx = 0;
1137 1.1 yamt total = 0;
1138 1.1 yamt while ((nfound = radix_tree_gang_lookup_tagged_node(t,
1139 1.1 yamt nextidx, (void *)results, __arraycount(results),
1140 1.1 yamt tag)) > 0) {
1141 1.1 yamt nextidx = results[nfound - 1]->idx + 1;
1142 1.1 yamt total += nfound;
1143 1.1 yamt }
1144 1.1 yamt assert(total == ntagged[tag]);
1145 1.1 yamt }
1146 1.1 yamt gettimeofday(&etv, NULL);
1147 1.11 yamt printops(title, "ganglookup_tag", tag, ntagged[tag], &stv,
1148 1.11 yamt &etv);
1149 1.1 yamt }
1150 1.1 yamt
1151 1.1 yamt removed = 0;
1152 1.1 yamt for (tag = 0; tag < RADIX_TREE_TAG_ID_MAX; tag++) {
1153 1.1 yamt unsigned int total;
1154 1.1 yamt
1155 1.1 yamt total = 0;
1156 1.1 yamt gettimeofday(&stv, NULL);
1157 1.1 yamt {
1158 1.1 yamt struct testnode *results[TEST2_GANG_LOOKUP_NODES];
1159 1.1 yamt uint64_t nextidx;
1160 1.1 yamt unsigned int nfound;
1161 1.1 yamt
1162 1.1 yamt nextidx = 0;
1163 1.1 yamt while ((nfound = radix_tree_gang_lookup_tagged_node(t,
1164 1.1 yamt nextidx, (void *)results, __arraycount(results),
1165 1.1 yamt tag)) > 0) {
1166 1.1 yamt for (i = 0; i < nfound; i++) {
1167 1.1 yamt radix_tree_remove_node(t,
1168 1.1 yamt results[i]->idx);
1169 1.1 yamt }
1170 1.1 yamt nextidx = results[nfound - 1]->idx + 1;
1171 1.1 yamt total += nfound;
1172 1.1 yamt }
1173 1.1 yamt assert(tag != 0 || total == ntagged[tag]);
1174 1.1 yamt assert(total <= ntagged[tag]);
1175 1.1 yamt }
1176 1.1 yamt gettimeofday(&etv, NULL);
1177 1.11 yamt printops(title, "ganglookup_tag+remove", tag, total, &stv,
1178 1.11 yamt &etv);
1179 1.1 yamt removed += total;
1180 1.1 yamt }
1181 1.1 yamt
1182 1.1 yamt gettimeofday(&stv, NULL);
1183 1.1 yamt {
1184 1.1 yamt struct testnode *results[TEST2_GANG_LOOKUP_NODES];
1185 1.1 yamt uint64_t nextidx;
1186 1.1 yamt unsigned int nfound;
1187 1.1 yamt unsigned int total;
1188 1.1 yamt
1189 1.1 yamt nextidx = 0;
1190 1.1 yamt total = 0;
1191 1.1 yamt while ((nfound = radix_tree_gang_lookup_node(t, nextidx,
1192 1.1 yamt (void *)results, __arraycount(results))) > 0) {
1193 1.1 yamt for (i = 0; i < nfound; i++) {
1194 1.1 yamt assert(results[i] == radix_tree_remove_node(t,
1195 1.1 yamt results[i]->idx));
1196 1.1 yamt }
1197 1.1 yamt nextidx = results[nfound - 1]->idx + 1;
1198 1.1 yamt total += nfound;
1199 1.1 yamt }
1200 1.1 yamt assert(total == nnodes - removed);
1201 1.1 yamt }
1202 1.1 yamt gettimeofday(&etv, NULL);
1203 1.11 yamt printops(title, "ganglookup+remove", 0, nnodes - removed, &stv, &etv);
1204 1.1 yamt
1205 1.1 yamt radix_tree_fini_tree(t);
1206 1.1 yamt free(nodes);
1207 1.1 yamt }
1208 1.1 yamt
1209 1.1 yamt int
1210 1.1 yamt main(int argc, char *argv[])
1211 1.1 yamt {
1212 1.1 yamt
1213 1.1 yamt test1();
1214 1.11 yamt test2("dense", true);
1215 1.11 yamt test2("sparse", false);
1216 1.1 yamt return 0;
1217 1.1 yamt }
1218 1.1 yamt
1219 1.1 yamt #endif /* defined(UNITTEST) */
1220