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