Home | History | Annotate | Line # | Download | only in gen
radixtree.c revision 1.17.2.6
      1 /*	$NetBSD: radixtree.c,v 1.17.2.6 2014/05/22 19:09:50 yamt Exp $	*/
      2 
      3 /*-
      4  * Copyright (c)2011,2012,2013 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  * radixtree.c
     31  *
     32  * Overview:
     33  *
     34  * This is an implementation of radix tree, whose keys are uint64_t and leafs
     35  * are user provided pointers.
     36  *
     37  * Leaf nodes are just void * and this implementation doesn't care about
     38  * what they actually point to.  However, this implementation has an assumption
     39  * about their alignment.  Specifically, this implementation assumes that their
     40  * 2 LSBs are always zero and uses them for internal accounting.
     41  *
     42  * Intermediate nodes and memory allocation:
     43  *
     44  * Intermediate nodes are automatically allocated and freed internally and
     45  * basically users don't need to care about them.  The allocation is done via
     46  * pool_cache_get(9) for _KERNEL, malloc(3) for userland, and alloc() for
     47  * _STANDALONE environment.  Only radix_tree_insert_node function can allocate
     48  * memory for intermediate nodes and thus can fail for ENOMEM.
     49  *
     50  * Memory Efficiency:
     51  *
     52  * It's designed to work efficiently with dense index distribution.
     53  * The memory consumption (number of necessary intermediate nodes) heavily
     54  * depends on the index distribution.  Basically, more dense index distribution
     55  * consumes less nodes per item.  Approximately,
     56  *
     57  *  - the best case: about RADIX_TREE_PTR_PER_NODE items per intermediate node.
     58  *    it would look like the following.
     59  *
     60  *     root (t_height=1)
     61  *      |
     62  *      v
     63  *      [ | | | ]   (intermediate node.  RADIX_TREE_PTR_PER_NODE=4 in this fig)
     64  *       | | | |
     65  *       v v v v
     66  *       p p p p    (items)
     67  *
     68  *  - the worst case: RADIX_TREE_MAX_HEIGHT intermediate nodes per item.
     69  *    it would look like the following if RADIX_TREE_MAX_HEIGHT=3.
     70  *
     71  *     root (t_height=3)
     72  *      |
     73  *      v
     74  *      [ | | | ]
     75  *           |
     76  *           v
     77  *           [ | | | ]
     78  *                |
     79  *                v
     80  *                [ | | | ]
     81  *                   |
     82  *                   v
     83  *                   p
     84  *
     85  * The height of tree (t_height) is dynamic.  It's smaller if only small
     86  * index values are used.  As an extreme case, if only index 0 is used,
     87  * the corresponding value is directly stored in the root of the tree
     88  * (struct radix_tree) without allocating any intermediate nodes.  In that
     89  * case, t_height=0.
     90  *
     91  * Gang lookup:
     92  *
     93  * This implementation provides a way to scan many nodes quickly via
     94  * radix_tree_gang_lookup_node function and its varients.
     95  *
     96  * Tags:
     97  *
     98  * This implementation provides tagging functionality, which allows quick
     99  * scanning of a subset of leaf nodes.  Leaf nodes are untagged when inserted
    100  * into the tree and can be tagged by radix_tree_set_tag function.
    101  * radix_tree_gang_lookup_tagged_node function and its variants returns only
    102  * leaf nodes with the given tag.  To reduce amount of nodes to visit for
    103  * these functions, this implementation keeps tagging information in internal
    104  * intermediate nodes and quickly skips uninterested parts of a tree.
    105  *
    106  * A tree has RADIX_TREE_TAG_ID_MAX independent tag spaces, each of which are
    107  * identified by an zero-origin numbers, tagid.  For the current implementation,
    108  * RADIX_TREE_TAG_ID_MAX is 2.  A set of tags is described as a bitmask tagmask,
    109  * which is a bitwise OR of (1 << tagid).
    110  */
    111 
    112 #include <sys/cdefs.h>
    113 
    114 #if defined(_KERNEL) || defined(_STANDALONE)
    115 __KERNEL_RCSID(0, "$NetBSD: radixtree.c,v 1.17.2.6 2014/05/22 19:09:50 yamt Exp $");
    116 #include <sys/param.h>
    117 #include <sys/errno.h>
    118 #include <sys/pool.h>
    119 #include <sys/radixtree.h>
    120 #include <lib/libkern/libkern.h>
    121 #if defined(_STANDALONE)
    122 #include <lib/libsa/stand.h>
    123 #endif /* defined(_STANDALONE) */
    124 #else /* defined(_KERNEL) || defined(_STANDALONE) */
    125 __RCSID("$NetBSD: radixtree.c,v 1.17.2.6 2014/05/22 19:09:50 yamt Exp $");
    126 #include <assert.h>
    127 #include <errno.h>
    128 #include <stdbool.h>
    129 #include <stdlib.h>
    130 #include <string.h>
    131 #if 1
    132 #define KASSERT assert
    133 #else
    134 #define KASSERT(a)	/* nothing */
    135 #endif
    136 #endif /* defined(_KERNEL) || defined(_STANDALONE) */
    137 
    138 #include <sys/radixtree.h>
    139 
    140 #define	RADIX_TREE_BITS_PER_HEIGHT	4	/* XXX tune */
    141 #define	RADIX_TREE_PTR_PER_NODE		(1 << RADIX_TREE_BITS_PER_HEIGHT)
    142 #define	RADIX_TREE_MAX_HEIGHT		(64 / RADIX_TREE_BITS_PER_HEIGHT)
    143 #define	RADIX_TREE_INVALID_HEIGHT	(RADIX_TREE_MAX_HEIGHT + 1)
    144 __CTASSERT((64 % RADIX_TREE_BITS_PER_HEIGHT) == 0);
    145 
    146 __CTASSERT(((1 << RADIX_TREE_TAG_ID_MAX) & (sizeof(int) - 1)) == 0);
    147 #define	RADIX_TREE_TAG_MASK	((1 << RADIX_TREE_TAG_ID_MAX) - 1)
    148 
    149 static inline void *
    150 entry_ptr(void *p)
    151 {
    152 
    153 	return (void *)((uintptr_t)p & ~RADIX_TREE_TAG_MASK);
    154 }
    155 
    156 static inline unsigned int
    157 entry_tagmask(void *p)
    158 {
    159 
    160 	return (uintptr_t)p & RADIX_TREE_TAG_MASK;
    161 }
    162 
    163 static inline void *
    164 entry_compose(void *p, unsigned int tagmask)
    165 {
    166 
    167 	return (void *)((uintptr_t)p | tagmask);
    168 }
    169 
    170 static inline bool
    171 entry_match_p(void *p, unsigned int tagmask)
    172 {
    173 
    174 	KASSERT(entry_ptr(p) != NULL || entry_tagmask(p) == 0);
    175 	if (p == NULL) {
    176 		return false;
    177 	}
    178 	if (tagmask == 0) {
    179 		return true;
    180 	}
    181 	return (entry_tagmask(p) & tagmask) != 0;
    182 }
    183 
    184 /*
    185  * radix_tree_node: an intermediate node
    186  *
    187  * we don't care the type of leaf nodes.  they are just void *.
    188  */
    189 
    190 struct radix_tree_node {
    191 	void *n_ptrs[RADIX_TREE_PTR_PER_NODE];
    192 	unsigned int n_nptrs;	/* # of non-NULL pointers in n_ptrs */
    193 };
    194 
    195 /*
    196  * any_children_tagmask:
    197  *
    198  * return OR'ed tagmask of the given node's children.
    199  */
    200 
    201 static unsigned int
    202 any_children_tagmask(const struct radix_tree_node *n)
    203 {
    204 	unsigned int mask;
    205 	int i;
    206 
    207 	mask = 0;
    208 	for (i = 0; i < RADIX_TREE_PTR_PER_NODE; i++) {
    209 		mask |= (unsigned int)(uintptr_t)n->n_ptrs[i];
    210 	}
    211 	return mask & RADIX_TREE_TAG_MASK;
    212 }
    213 
    214 /*
    215  * p_refs[0].pptr == &t->t_root
    216  *	:
    217  * p_refs[n].pptr == &(*p_refs[n-1])->n_ptrs[x]
    218  *	:
    219  *	:
    220  * p_refs[t->t_height].pptr == &leaf_pointer
    221  */
    222 
    223 struct radix_tree_path {
    224 	struct radix_tree_node_ref {
    225 		void **pptr;
    226 	} p_refs[RADIX_TREE_MAX_HEIGHT + 1]; /* +1 for the root ptr */
    227 	/*
    228 	 * p_lastidx is either the index of the last valid element of p_refs[]
    229 	 * or RADIX_TREE_INVALID_HEIGHT.
    230 	 * RADIX_TREE_INVALID_HEIGHT means that radix_tree_lookup_ptr found
    231 	 * that the height of the tree is not enough to cover the given index.
    232 	 */
    233 	unsigned int p_lastidx;
    234 };
    235 
    236 static inline void **
    237 path_pptr(const struct radix_tree *t, const struct radix_tree_path *p,
    238     unsigned int height)
    239 {
    240 
    241 	KASSERT(height <= t->t_height);
    242 	return p->p_refs[height].pptr;
    243 }
    244 
    245 static inline struct radix_tree_node *
    246 path_node(const struct radix_tree * t, const struct radix_tree_path *p,
    247     unsigned int height)
    248 {
    249 
    250 	KASSERT(height <= t->t_height);
    251 	return entry_ptr(*path_pptr(t, p, height));
    252 }
    253 
    254 /*
    255  * radix_tree_init_tree:
    256  *
    257  * Initialize a tree.
    258  */
    259 
    260 void
    261 radix_tree_init_tree(struct radix_tree *t)
    262 {
    263 
    264 	t->t_height = 0;
    265 	t->t_root = NULL;
    266 }
    267 
    268 /*
    269  * radix_tree_fini_tree:
    270  *
    271  * Finish using a tree.
    272  */
    273 
    274 void
    275 radix_tree_fini_tree(struct radix_tree *t)
    276 {
    277 
    278 	KASSERT(t->t_root == NULL);
    279 	KASSERT(t->t_height == 0);
    280 }
    281 
    282 /*
    283  * radix_tree_empty_tree_p:
    284  *
    285  * Return if the tree is empty.
    286  */
    287 
    288 bool
    289 radix_tree_empty_tree_p(struct radix_tree *t)
    290 {
    291 
    292 	return t->t_root == NULL;
    293 }
    294 
    295 /*
    296  * radix_tree_empty_tree_p:
    297  *
    298  * Return true if the tree has any nodes with the given tag.  Otherwise
    299  * return false.
    300  *
    301  * It's illegal to call this function with tagmask 0.
    302  */
    303 
    304 bool
    305 radix_tree_empty_tagged_tree_p(struct radix_tree *t, unsigned int tagmask)
    306 {
    307 
    308 	KASSERT(tagmask != 0);
    309 	return (entry_tagmask(t->t_root) & tagmask) == 0;
    310 }
    311 
    312 static void
    313 radix_tree_node_init(struct radix_tree_node *n)
    314 {
    315 
    316 	memset(n, 0, sizeof(*n));
    317 }
    318 
    319 #if defined(_KERNEL)
    320 pool_cache_t radix_tree_node_cache __read_mostly;
    321 
    322 static int
    323 radix_tree_node_ctor(void *dummy, void *item, int flags)
    324 {
    325 	struct radix_tree_node *n = item;
    326 
    327 	KASSERT(dummy == NULL);
    328 	radix_tree_node_init(n);
    329 	return 0;
    330 }
    331 
    332 /*
    333  * radix_tree_init:
    334  *
    335  * initialize the subsystem.
    336  */
    337 
    338 void
    339 radix_tree_init(void)
    340 {
    341 
    342 	radix_tree_node_cache = pool_cache_init(sizeof(struct radix_tree_node),
    343 	    0, 0, 0, "radix_tree_node", NULL, IPL_NONE, radix_tree_node_ctor,
    344 	    NULL, NULL);
    345 	KASSERT(radix_tree_node_cache != NULL);
    346 }
    347 #endif /* defined(_KERNEL) */
    348 
    349 static bool __unused
    350 radix_tree_node_clean_p(const struct radix_tree_node *n)
    351 {
    352 	unsigned int i;
    353 
    354 	if (n->n_nptrs != 0) {
    355 		return false;
    356 	}
    357 	for (i = 0; i < RADIX_TREE_PTR_PER_NODE; i++) {
    358 		if (n->n_ptrs[i] != NULL) {
    359 			return false;
    360 		}
    361 	}
    362 	return true;
    363 }
    364 
    365 static struct radix_tree_node *
    366 radix_tree_alloc_node(void)
    367 {
    368 	struct radix_tree_node *n;
    369 
    370 #if defined(_KERNEL)
    371 	/*
    372 	 * note that pool_cache_get can block.
    373 	 */
    374 	n = pool_cache_get(radix_tree_node_cache, PR_NOWAIT);
    375 #else /* defined(_KERNEL) */
    376 #if defined(_STANDALONE)
    377 	n = alloc(sizeof(*n));
    378 #else /* defined(_STANDALONE) */
    379 	n = malloc(sizeof(*n));
    380 #endif /* defined(_STANDALONE) */
    381 	if (n != NULL) {
    382 		radix_tree_node_init(n);
    383 	}
    384 #endif /* defined(_KERNEL) */
    385 	KASSERT(n == NULL || radix_tree_node_clean_p(n));
    386 	return n;
    387 }
    388 
    389 static void
    390 radix_tree_free_node(struct radix_tree_node *n)
    391 {
    392 
    393 	KASSERT(radix_tree_node_clean_p(n));
    394 #if defined(_KERNEL)
    395 	pool_cache_put(radix_tree_node_cache, n);
    396 #elif defined(_STANDALONE)
    397 	dealloc(n, sizeof(*n));
    398 #else
    399 	free(n);
    400 #endif
    401 }
    402 
    403 static int
    404 radix_tree_grow(struct radix_tree *t, unsigned int newheight)
    405 {
    406 	const unsigned int tagmask = entry_tagmask(t->t_root);
    407 
    408 	KASSERT(newheight <= 64 / RADIX_TREE_BITS_PER_HEIGHT);
    409 	if (t->t_root == NULL) {
    410 		t->t_height = newheight;
    411 		return 0;
    412 	}
    413 	while (t->t_height < newheight) {
    414 		struct radix_tree_node *n;
    415 
    416 		n = radix_tree_alloc_node();
    417 		if (n == NULL) {
    418 			/*
    419 			 * don't bother to revert our changes.
    420 			 * the caller will likely retry.
    421 			 */
    422 			return ENOMEM;
    423 		}
    424 		n->n_nptrs = 1;
    425 		n->n_ptrs[0] = t->t_root;
    426 		t->t_root = entry_compose(n, tagmask);
    427 		t->t_height++;
    428 	}
    429 	return 0;
    430 }
    431 
    432 /*
    433  * radix_tree_lookup_ptr:
    434  *
    435  * an internal helper function used for various exported functions.
    436  *
    437  * return the pointer to store the node for the given index.
    438  *
    439  * if alloc is true, try to allocate the storage.  (note for _KERNEL:
    440  * in that case, this function can block.)  if the allocation failed or
    441  * alloc is false, return NULL.
    442  *
    443  * if path is not NULL, fill it for the caller's investigation.
    444  *
    445  * if tagmask is not zero, search only for nodes with the tag set.
    446  * note that, however, this function doesn't check the tagmask for the leaf
    447  * pointer.  it's a caller's responsibility to investigate the value which
    448  * is pointed by the returned pointer if necessary.
    449  *
    450  * while this function is a bit large, as it's called with some constant
    451  * arguments, inlining might have benefits.  anyway, a compiler will decide.
    452  */
    453 
    454 static inline void **
    455 radix_tree_lookup_ptr(struct radix_tree *t, uint64_t idx,
    456     struct radix_tree_path *path, bool alloc, const unsigned int tagmask)
    457 {
    458 	struct radix_tree_node *n;
    459 	int hshift = RADIX_TREE_BITS_PER_HEIGHT * t->t_height;
    460 	int shift;
    461 	void **vpp;
    462 	const uint64_t mask = (UINT64_C(1) << RADIX_TREE_BITS_PER_HEIGHT) - 1;
    463 	struct radix_tree_node_ref *refs = NULL;
    464 
    465 	/*
    466 	 * check unsupported combinations
    467 	 */
    468 	KASSERT(tagmask == 0 || !alloc);
    469 	KASSERT(path == NULL || !alloc);
    470 	vpp = &t->t_root;
    471 	if (path != NULL) {
    472 		refs = path->p_refs;
    473 		refs->pptr = vpp;
    474 	}
    475 	n = NULL;
    476 	for (shift = 64 - RADIX_TREE_BITS_PER_HEIGHT; shift >= 0;) {
    477 		struct radix_tree_node *c;
    478 		void *entry;
    479 		const uint64_t i = (idx >> shift) & mask;
    480 
    481 		if (shift >= hshift) {
    482 			unsigned int newheight;
    483 
    484 			KASSERT(vpp == &t->t_root);
    485 			if (i == 0) {
    486 				shift -= RADIX_TREE_BITS_PER_HEIGHT;
    487 				continue;
    488 			}
    489 			if (!alloc) {
    490 				if (path != NULL) {
    491 					KASSERT((refs - path->p_refs) == 0);
    492 					path->p_lastidx =
    493 					    RADIX_TREE_INVALID_HEIGHT;
    494 				}
    495 				return NULL;
    496 			}
    497 			newheight = shift / RADIX_TREE_BITS_PER_HEIGHT + 1;
    498 			if (radix_tree_grow(t, newheight)) {
    499 				return NULL;
    500 			}
    501 			hshift = RADIX_TREE_BITS_PER_HEIGHT * t->t_height;
    502 		}
    503 		entry = *vpp;
    504 		c = entry_ptr(entry);
    505 		if (c == NULL ||
    506 		    (tagmask != 0 &&
    507 		    (entry_tagmask(entry) & tagmask) == 0)) {
    508 			if (!alloc) {
    509 				if (path != NULL) {
    510 					path->p_lastidx = refs - path->p_refs;
    511 				}
    512 				return NULL;
    513 			}
    514 			c = radix_tree_alloc_node();
    515 			if (c == NULL) {
    516 				return NULL;
    517 			}
    518 			*vpp = c;
    519 			if (n != NULL) {
    520 				KASSERT(n->n_nptrs < RADIX_TREE_PTR_PER_NODE);
    521 				n->n_nptrs++;
    522 			}
    523 		}
    524 		n = c;
    525 		vpp = &n->n_ptrs[i];
    526 		if (path != NULL) {
    527 			refs++;
    528 			refs->pptr = vpp;
    529 		}
    530 		shift -= RADIX_TREE_BITS_PER_HEIGHT;
    531 	}
    532 	if (alloc) {
    533 		KASSERT(*vpp == NULL);
    534 		if (n != NULL) {
    535 			KASSERT(n->n_nptrs < RADIX_TREE_PTR_PER_NODE);
    536 			n->n_nptrs++;
    537 		}
    538 	}
    539 	if (path != NULL) {
    540 		path->p_lastidx = refs - path->p_refs;
    541 	}
    542 	return vpp;
    543 }
    544 
    545 /*
    546  * radix_tree_insert_node:
    547  *
    548  * Insert the node at the given index.
    549  *
    550  * It's illegal to insert NULL.  It's illegal to insert a non-aligned pointer.
    551  *
    552  * This function returns ENOMEM if necessary memory allocation failed.
    553  * Otherwise, this function returns 0.
    554  *
    555  * Note that inserting a node can involves memory allocation for intermediate
    556  * nodes.  If _KERNEL, it's done with no-sleep IPL_NONE memory allocation.
    557  *
    558  * For the newly inserted node, all tags are cleared.
    559  */
    560 
    561 int
    562 radix_tree_insert_node(struct radix_tree *t, uint64_t idx, void *p)
    563 {
    564 	void **vpp;
    565 
    566 	KASSERT(p != NULL);
    567 	KASSERT(entry_tagmask(entry_compose(p, 0)) == 0);
    568 	vpp = radix_tree_lookup_ptr(t, idx, NULL, true, 0);
    569 	if (vpp == NULL) {
    570 		return ENOMEM;
    571 	}
    572 	KASSERT(*vpp == NULL);
    573 	*vpp = p;
    574 	return 0;
    575 }
    576 
    577 /*
    578  * radix_tree_replace_node:
    579  *
    580  * Replace a node at the given index with the given node and return the
    581  * replaced one.
    582  *
    583  * It's illegal to try to replace a node which has not been inserted.
    584  *
    585  * This function keeps tags intact.
    586  */
    587 
    588 void *
    589 radix_tree_replace_node(struct radix_tree *t, uint64_t idx, void *p)
    590 {
    591 	void **vpp;
    592 	void *oldp;
    593 
    594 	KASSERT(p != NULL);
    595 	KASSERT(entry_tagmask(entry_compose(p, 0)) == 0);
    596 	vpp = radix_tree_lookup_ptr(t, idx, NULL, false, 0);
    597 	KASSERT(vpp != NULL);
    598 	oldp = *vpp;
    599 	KASSERT(oldp != NULL);
    600 	*vpp = entry_compose(p, entry_tagmask(*vpp));
    601 	return entry_ptr(oldp);
    602 }
    603 
    604 /*
    605  * radix_tree_remove_node:
    606  *
    607  * Remove the node at the given index.
    608  *
    609  * It's illegal to try to remove a node which has not been inserted.
    610  */
    611 
    612 void *
    613 radix_tree_remove_node(struct radix_tree *t, uint64_t idx)
    614 {
    615 	struct radix_tree_path path;
    616 	void **vpp;
    617 	void *oldp;
    618 	int i;
    619 
    620 	vpp = radix_tree_lookup_ptr(t, idx, &path, false, 0);
    621 	KASSERT(vpp != NULL);
    622 	oldp = *vpp;
    623 	KASSERT(oldp != NULL);
    624 	KASSERT(path.p_lastidx == t->t_height);
    625 	KASSERT(vpp == path_pptr(t, &path, path.p_lastidx));
    626 	*vpp = NULL;
    627 	for (i = t->t_height - 1; i >= 0; i--) {
    628 		void *entry;
    629 		struct radix_tree_node ** const pptr =
    630 		    (struct radix_tree_node **)path_pptr(t, &path, i);
    631 		struct radix_tree_node *n;
    632 
    633 		KASSERT(pptr != NULL);
    634 		entry = *pptr;
    635 		n = entry_ptr(entry);
    636 		KASSERT(n != NULL);
    637 		KASSERT(n->n_nptrs > 0);
    638 		n->n_nptrs--;
    639 		if (n->n_nptrs > 0) {
    640 			break;
    641 		}
    642 		radix_tree_free_node(n);
    643 		*pptr = NULL;
    644 	}
    645 	/*
    646 	 * fix up height
    647 	 */
    648 	if (i < 0) {
    649 		KASSERT(t->t_root == NULL);
    650 		t->t_height = 0;
    651 	}
    652 	/*
    653 	 * update tags
    654 	 */
    655 	for (; i >= 0; i--) {
    656 		void *entry;
    657 		struct radix_tree_node ** const pptr =
    658 		    (struct radix_tree_node **)path_pptr(t, &path, i);
    659 		struct radix_tree_node *n;
    660 		unsigned int newmask;
    661 
    662 		KASSERT(pptr != NULL);
    663 		entry = *pptr;
    664 		n = entry_ptr(entry);
    665 		KASSERT(n != NULL);
    666 		KASSERT(n->n_nptrs > 0);
    667 		newmask = any_children_tagmask(n);
    668 		if (newmask == entry_tagmask(entry)) {
    669 			break;
    670 		}
    671 		*pptr = entry_compose(n, newmask);
    672 	}
    673 	/*
    674 	 * XXX is it worth to try to reduce height?
    675 	 * if we do that, make radix_tree_grow rollback its change as well.
    676 	 */
    677 	return entry_ptr(oldp);
    678 }
    679 
    680 /*
    681  * radix_tree_lookup_node:
    682  *
    683  * Returns the node at the given index.
    684  * Returns NULL if nothing is found at the given index.
    685  */
    686 
    687 void *
    688 radix_tree_lookup_node(struct radix_tree *t, uint64_t idx)
    689 {
    690 	void **vpp;
    691 
    692 	vpp = radix_tree_lookup_ptr(t, idx, NULL, false, 0);
    693 	if (vpp == NULL) {
    694 		return NULL;
    695 	}
    696 	return entry_ptr(*vpp);
    697 }
    698 
    699 static inline void
    700 gang_lookup_init(struct radix_tree *t, uint64_t idx,
    701     struct radix_tree_path *path, const unsigned int tagmask)
    702 {
    703 	void **vpp __unused;
    704 
    705 #if defined(DIAGNOSTIC)
    706 	vpp =
    707 #endif /* defined(DIAGNOSTIC) */
    708 	radix_tree_lookup_ptr(t, idx, path, false, tagmask);
    709 	KASSERT(vpp == NULL ||
    710 	    vpp == path_pptr(t, path, path->p_lastidx));
    711 	KASSERT(&t->t_root == path_pptr(t, path, 0));
    712 	KASSERT(path->p_lastidx == RADIX_TREE_INVALID_HEIGHT ||
    713 	   path->p_lastidx == t->t_height ||
    714 	   !entry_match_p(*path_pptr(t, path, path->p_lastidx), tagmask));
    715 }
    716 
    717 /*
    718  * gang_lookup_scan:
    719  *
    720  * a helper routine for radix_tree_gang_lookup_node and its variants.
    721  */
    722 
    723 static inline unsigned int
    724 __attribute__((__always_inline__))
    725 gang_lookup_scan(struct radix_tree *t, struct radix_tree_path *path,
    726     void **results, const unsigned int maxresults, const unsigned int tagmask,
    727     const bool reverse, const bool dense)
    728 {
    729 
    730 	/*
    731 	 * we keep the path updated only for lastidx-1.
    732 	 * vpp is what path_pptr(t, path, lastidx) would be.
    733 	 */
    734 	void **vpp;
    735 	unsigned int nfound;
    736 	unsigned int lastidx;
    737 	/*
    738 	 * set up scan direction dependant constants so that we can iterate
    739 	 * n_ptrs as the following.
    740 	 *
    741 	 *	for (i = first; i != guard; i += step)
    742 	 *		visit n->n_ptrs[i];
    743 	 */
    744 	const int step = reverse ? -1 : 1;
    745 	const unsigned int first = reverse ? RADIX_TREE_PTR_PER_NODE - 1 : 0;
    746 	const unsigned int last = reverse ? 0 : RADIX_TREE_PTR_PER_NODE - 1;
    747 	const unsigned int guard = last + step;
    748 
    749 	KASSERT(maxresults > 0);
    750 	KASSERT(&t->t_root == path_pptr(t, path, 0));
    751 	lastidx = path->p_lastidx;
    752 	KASSERT(lastidx == RADIX_TREE_INVALID_HEIGHT ||
    753 	   lastidx == t->t_height ||
    754 	   !entry_match_p(*path_pptr(t, path, lastidx), tagmask));
    755 	nfound = 0;
    756 	if (lastidx == RADIX_TREE_INVALID_HEIGHT) {
    757 		/*
    758 		 * requested idx is beyond the right-most node.
    759 		 */
    760 		if (reverse && !dense) {
    761 			lastidx = 0;
    762 			vpp = path_pptr(t, path, lastidx);
    763 			goto descend;
    764 		}
    765 		return 0;
    766 	}
    767 	vpp = path_pptr(t, path, lastidx);
    768 	while (/*CONSTCOND*/true) {
    769 		struct radix_tree_node *n;
    770 		unsigned int i;
    771 
    772 		if (entry_match_p(*vpp, tagmask)) {
    773 			KASSERT(lastidx == t->t_height);
    774 			/*
    775 			 * record the matching non-NULL leaf.
    776 			 */
    777 			results[nfound] = entry_ptr(*vpp);
    778 			nfound++;
    779 			if (nfound == maxresults) {
    780 				return nfound;
    781 			}
    782 		} else if (dense) {
    783 			return nfound;
    784 		}
    785 scan_siblings:
    786 		/*
    787 		 * try to find the next matching non-NULL sibling.
    788 		 */
    789 		if (lastidx == 0) {
    790 			/*
    791 			 * the root has no siblings.
    792 			 * we've done.
    793 			 */
    794 			KASSERT(vpp == &t->t_root);
    795 			break;
    796 		}
    797 		n = path_node(t, path, lastidx - 1);
    798 		if (*vpp != NULL && n->n_nptrs == 1) {
    799 			/*
    800 			 * optimization; if the node has only a single pointer
    801 			 * and we've already visited it, there's no point to
    802 			 * keep scanning in this node.
    803 			 */
    804 			goto no_siblings;
    805 		}
    806 		for (i = vpp - n->n_ptrs + step; i != guard; i += step) {
    807 			KASSERT(i < RADIX_TREE_PTR_PER_NODE);
    808 			if (entry_match_p(n->n_ptrs[i], tagmask)) {
    809 				vpp = &n->n_ptrs[i];
    810 				break;
    811 			}
    812 		}
    813 		if (i == guard) {
    814 no_siblings:
    815 			/*
    816 			 * not found.  go to parent.
    817 			 */
    818 			lastidx--;
    819 			vpp = path_pptr(t, path, lastidx);
    820 			goto scan_siblings;
    821 		}
    822 descend:
    823 		/*
    824 		 * following the left-most (or right-most in the case of
    825 		 * reverse scan) child node, decend until reaching the leaf or
    826 		 * an non-matching entry.
    827 		 */
    828 		while (entry_match_p(*vpp, tagmask) && lastidx < t->t_height) {
    829 			/*
    830 			 * save vpp in the path so that we can come back to this
    831 			 * node after finishing visiting children.
    832 			 */
    833 			path->p_refs[lastidx].pptr = vpp;
    834 			n = entry_ptr(*vpp);
    835 			vpp = &n->n_ptrs[first];
    836 			lastidx++;
    837 		}
    838 	}
    839 	return nfound;
    840 }
    841 
    842 /*
    843  * radix_tree_gang_lookup_node:
    844  *
    845  * Scan the tree starting from the given index in the ascending order and
    846  * return found nodes.
    847  *
    848  * results should be an array large enough to hold maxresults pointers.
    849  * This function returns the number of nodes found, up to maxresults.
    850  * Returning less than maxresults means there are no more nodes in the tree.
    851  *
    852  * If dense == true, this function stops scanning when it founds a hole of
    853  * indexes.  I.e. an index for which radix_tree_lookup_node would returns NULL.
    854  * If dense == false, this function skips holes and continue scanning until
    855  * maxresults nodes are found or it reaches the limit of the index range.
    856  *
    857  * The result of this function is semantically equivalent to what could be
    858  * obtained by repeated calls of radix_tree_lookup_node with increasing index.
    859  * but this function is expected to be computationally cheaper when looking up
    860  * multiple nodes at once.  Especially, it's expected to be much cheaper when
    861  * node indexes are distributed sparsely.
    862  *
    863  * Note that this function doesn't return index values of found nodes.
    864  * Thus, in the case of dense == false, if index values are important for
    865  * a caller, it's the caller's responsibility to check them, typically
    866  * by examinining the returned nodes using some caller-specific knowledge
    867  * about them.
    868  * In the case of dense == true, a node returned via results[N] is always for
    869  * the index (idx + N).
    870  */
    871 
    872 unsigned int
    873 radix_tree_gang_lookup_node(struct radix_tree *t, uint64_t idx,
    874     void **results, unsigned int maxresults, bool dense)
    875 {
    876 	struct radix_tree_path path;
    877 
    878 	gang_lookup_init(t, idx, &path, 0);
    879 	return gang_lookup_scan(t, &path, results, maxresults, 0, false, dense);
    880 }
    881 
    882 /*
    883  * radix_tree_gang_lookup_node_reverse:
    884  *
    885  * Same as radix_tree_gang_lookup_node except that this one scans the
    886  * tree in the reverse order.  I.e. descending index values.
    887  */
    888 
    889 unsigned int
    890 radix_tree_gang_lookup_node_reverse(struct radix_tree *t, uint64_t idx,
    891     void **results, unsigned int maxresults, bool dense)
    892 {
    893 	struct radix_tree_path path;
    894 
    895 	gang_lookup_init(t, idx, &path, 0);
    896 	return gang_lookup_scan(t, &path, results, maxresults, 0, true, dense);
    897 }
    898 
    899 /*
    900  * radix_tree_gang_lookup_tagged_node:
    901  *
    902  * Same as radix_tree_gang_lookup_node except that this one only returns
    903  * nodes tagged with tagid.
    904  *
    905  * It's illegal to call this function with tagmask 0.
    906  */
    907 
    908 unsigned int
    909 radix_tree_gang_lookup_tagged_node(struct radix_tree *t, uint64_t idx,
    910     void **results, unsigned int maxresults, bool dense, unsigned int tagmask)
    911 {
    912 	struct radix_tree_path path;
    913 
    914 	KASSERT(tagmask != 0);
    915 	gang_lookup_init(t, idx, &path, tagmask);
    916 	return gang_lookup_scan(t, &path, results, maxresults, tagmask, false,
    917 	    dense);
    918 }
    919 
    920 /*
    921  * radix_tree_gang_lookup_tagged_node_reverse:
    922  *
    923  * Same as radix_tree_gang_lookup_tagged_node except that this one scans the
    924  * tree in the reverse order.  I.e. descending index values.
    925  */
    926 
    927 unsigned int
    928 radix_tree_gang_lookup_tagged_node_reverse(struct radix_tree *t, uint64_t idx,
    929     void **results, unsigned int maxresults, bool dense, unsigned int tagmask)
    930 {
    931 	struct radix_tree_path path;
    932 
    933 	KASSERT(tagmask != 0);
    934 	gang_lookup_init(t, idx, &path, tagmask);
    935 	return gang_lookup_scan(t, &path, results, maxresults, tagmask, true,
    936 	    dense);
    937 }
    938 
    939 /*
    940  * radix_tree_get_tag:
    941  *
    942  * Return the tagmask for the node at the given index.
    943  *
    944  * It's illegal to call this function for a node which has not been inserted.
    945  */
    946 
    947 unsigned int
    948 radix_tree_get_tag(struct radix_tree *t, uint64_t idx, unsigned int tagmask)
    949 {
    950 	/*
    951 	 * the following two implementations should behave same.
    952 	 * the former one was chosen because it seems faster.
    953 	 */
    954 #if 1
    955 	void **vpp;
    956 
    957 	vpp = radix_tree_lookup_ptr(t, idx, NULL, false, tagmask);
    958 	if (vpp == NULL) {
    959 		return false;
    960 	}
    961 	KASSERT(*vpp != NULL);
    962 	return (entry_tagmask(*vpp) & tagmask);
    963 #else
    964 	void **vpp;
    965 
    966 	vpp = radix_tree_lookup_ptr(t, idx, NULL, false, 0);
    967 	KASSERT(vpp != NULL);
    968 	return (entry_tagmask(*vpp) & tagmask);
    969 #endif
    970 }
    971 
    972 /*
    973  * radix_tree_set_tag:
    974  *
    975  * Set the tag for the node at the given index.
    976  *
    977  * It's illegal to call this function for a node which has not been inserted.
    978  * It's illegal to call this function with tagmask 0.
    979  */
    980 
    981 void
    982 radix_tree_set_tag(struct radix_tree *t, uint64_t idx, unsigned int tagmask)
    983 {
    984 	struct radix_tree_path path;
    985 	void **vpp __unused;
    986 	int i;
    987 
    988 	KASSERT(tagmask != 0);
    989 #if defined(DIAGNOSTIC)
    990 	vpp =
    991 #endif /* defined(DIAGNOSTIC) */
    992 	radix_tree_lookup_ptr(t, idx, &path, false, 0);
    993 	KASSERT(vpp != NULL);
    994 	KASSERT(*vpp != NULL);
    995 	KASSERT(path.p_lastidx == t->t_height);
    996 	KASSERT(vpp == path_pptr(t, &path, path.p_lastidx));
    997 	for (i = t->t_height; i >= 0; i--) {
    998 		void ** const pptr = (void **)path_pptr(t, &path, i);
    999 		void *entry;
   1000 
   1001 		KASSERT(pptr != NULL);
   1002 		entry = *pptr;
   1003 		if ((entry_tagmask(entry) & tagmask) != 0) {
   1004 			break;
   1005 		}
   1006 		*pptr = (void *)((uintptr_t)entry | tagmask);
   1007 	}
   1008 }
   1009 
   1010 /*
   1011  * radix_tree_clear_tag:
   1012  *
   1013  * Clear the tag for the node at the given index.
   1014  *
   1015  * It's illegal to call this function for a node which has not been inserted.
   1016  * It's illegal to call this function with tagmask 0.
   1017  */
   1018 
   1019 void
   1020 radix_tree_clear_tag(struct radix_tree *t, uint64_t idx, unsigned int tagmask)
   1021 {
   1022 	struct radix_tree_path path;
   1023 	void **vpp;
   1024 	int i;
   1025 
   1026 	KASSERT(tagmask != 0);
   1027 	vpp = radix_tree_lookup_ptr(t, idx, &path, false, 0);
   1028 	KASSERT(vpp != NULL);
   1029 	KASSERT(*vpp != NULL);
   1030 	KASSERT(path.p_lastidx == t->t_height);
   1031 	KASSERT(vpp == path_pptr(t, &path, path.p_lastidx));
   1032 	/*
   1033 	 * if already cleared, nothing to do
   1034 	 */
   1035 	if ((entry_tagmask(*vpp) & tagmask) == 0) {
   1036 		return;
   1037 	}
   1038 	/*
   1039 	 * clear the tag only if no children have the tag.
   1040 	 */
   1041 	for (i = t->t_height; i >= 0; i--) {
   1042 		void ** const pptr = (void **)path_pptr(t, &path, i);
   1043 		void *entry;
   1044 
   1045 		KASSERT(pptr != NULL);
   1046 		entry = *pptr;
   1047 		KASSERT((entry_tagmask(entry) & tagmask) != 0);
   1048 		*pptr = entry_compose(entry_ptr(entry),
   1049 		    entry_tagmask(entry) & ~tagmask);
   1050 		/*
   1051 		 * check if we should proceed to process the next level.
   1052 		 */
   1053 		if (0 < i) {
   1054 			struct radix_tree_node *n = path_node(t, &path, i - 1);
   1055 
   1056 			if ((any_children_tagmask(n) & tagmask) != 0) {
   1057 				break;
   1058 			}
   1059 		}
   1060 	}
   1061 }
   1062 
   1063 #if defined(UNITTEST)
   1064 
   1065 #include <inttypes.h>
   1066 #include <stdio.h>
   1067 
   1068 static void
   1069 radix_tree_dump_node(const struct radix_tree *t, void *vp,
   1070     uint64_t offset, unsigned int height)
   1071 {
   1072 	struct radix_tree_node *n;
   1073 	unsigned int i;
   1074 
   1075 	for (i = 0; i < t->t_height - height; i++) {
   1076 		printf(" ");
   1077 	}
   1078 	if (entry_tagmask(vp) == 0) {
   1079 		printf("[%" PRIu64 "] %p", offset, entry_ptr(vp));
   1080 	} else {
   1081 		printf("[%" PRIu64 "] %p (tagmask=0x%x)", offset, entry_ptr(vp),
   1082 		    entry_tagmask(vp));
   1083 	}
   1084 	if (height == 0) {
   1085 		printf(" (leaf)\n");
   1086 		return;
   1087 	}
   1088 	n = entry_ptr(vp);
   1089 	assert(any_children_tagmask(n) == entry_tagmask(vp));
   1090 	printf(" (%u children)\n", n->n_nptrs);
   1091 	for (i = 0; i < __arraycount(n->n_ptrs); i++) {
   1092 		void *c;
   1093 
   1094 		c = n->n_ptrs[i];
   1095 		if (c == NULL) {
   1096 			continue;
   1097 		}
   1098 		radix_tree_dump_node(t, c,
   1099 		    offset + i * (UINT64_C(1) <<
   1100 		    (RADIX_TREE_BITS_PER_HEIGHT * (height - 1))), height - 1);
   1101 	}
   1102 }
   1103 
   1104 void radix_tree_dump(const struct radix_tree *);
   1105 
   1106 void
   1107 radix_tree_dump(const struct radix_tree *t)
   1108 {
   1109 
   1110 	printf("tree %p height=%u\n", t, t->t_height);
   1111 	radix_tree_dump_node(t, t->t_root, 0, t->t_height);
   1112 }
   1113 
   1114 static void
   1115 test1(void)
   1116 {
   1117 	struct radix_tree s;
   1118 	struct radix_tree *t = &s;
   1119 	void *results[3];
   1120 
   1121 	radix_tree_init_tree(t);
   1122 	radix_tree_dump(t);
   1123 	assert(radix_tree_lookup_node(t, 0) == NULL);
   1124 	assert(radix_tree_lookup_node(t, 1000) == NULL);
   1125 	assert(radix_tree_gang_lookup_node(t, 0, results, 3, false) == 0);
   1126 	assert(radix_tree_gang_lookup_node(t, 0, results, 3, true) == 0);
   1127 	assert(radix_tree_gang_lookup_node(t, 1000, results, 3, false) == 0);
   1128 	assert(radix_tree_gang_lookup_node(t, 1000, results, 3, true) == 0);
   1129 	assert(radix_tree_gang_lookup_node_reverse(t, 0, results, 3, false) ==
   1130 	    0);
   1131 	assert(radix_tree_gang_lookup_node_reverse(t, 0, results, 3, true) ==
   1132 	    0);
   1133 	assert(radix_tree_gang_lookup_node_reverse(t, 1000, results, 3, false)
   1134 	    == 0);
   1135 	assert(radix_tree_gang_lookup_node_reverse(t, 1000, results, 3, true)
   1136 	    == 0);
   1137 	assert(radix_tree_gang_lookup_tagged_node(t, 0, results, 3, false, 1)
   1138 	    == 0);
   1139 	assert(radix_tree_gang_lookup_tagged_node(t, 0, results, 3, true, 1)
   1140 	    == 0);
   1141 	assert(radix_tree_gang_lookup_tagged_node(t, 1000, results, 3, false, 1)
   1142 	    == 0);
   1143 	assert(radix_tree_gang_lookup_tagged_node(t, 1000, results, 3, true, 1)
   1144 	    == 0);
   1145 	assert(radix_tree_gang_lookup_tagged_node_reverse(t, 0, results, 3,
   1146 	    false, 1) == 0);
   1147 	assert(radix_tree_gang_lookup_tagged_node_reverse(t, 0, results, 3,
   1148 	    true, 1) == 0);
   1149 	assert(radix_tree_gang_lookup_tagged_node_reverse(t, 1000, results, 3,
   1150 	    false, 1) == 0);
   1151 	assert(radix_tree_gang_lookup_tagged_node_reverse(t, 1000, results, 3,
   1152 	    true, 1) == 0);
   1153 	assert(radix_tree_empty_tree_p(t));
   1154 	assert(radix_tree_empty_tagged_tree_p(t, 1));
   1155 	assert(radix_tree_empty_tagged_tree_p(t, 2));
   1156 	assert(radix_tree_insert_node(t, 0, (void *)0xdeadbea0) == 0);
   1157 	assert(!radix_tree_empty_tree_p(t));
   1158 	assert(radix_tree_empty_tagged_tree_p(t, 1));
   1159 	assert(radix_tree_empty_tagged_tree_p(t, 2));
   1160 	assert(radix_tree_lookup_node(t, 0) == (void *)0xdeadbea0);
   1161 	assert(radix_tree_lookup_node(t, 1000) == NULL);
   1162 	memset(results, 0, sizeof(results));
   1163 	assert(radix_tree_gang_lookup_node(t, 0, results, 3, false) == 1);
   1164 	assert(results[0] == (void *)0xdeadbea0);
   1165 	memset(results, 0, sizeof(results));
   1166 	assert(radix_tree_gang_lookup_node(t, 0, results, 3, true) == 1);
   1167 	assert(results[0] == (void *)0xdeadbea0);
   1168 	assert(radix_tree_gang_lookup_node(t, 1000, results, 3, false) == 0);
   1169 	assert(radix_tree_gang_lookup_node(t, 1000, results, 3, true) == 0);
   1170 	memset(results, 0, sizeof(results));
   1171 	assert(radix_tree_gang_lookup_node_reverse(t, 0, results, 3, false) ==
   1172 	    1);
   1173 	assert(results[0] == (void *)0xdeadbea0);
   1174 	memset(results, 0, sizeof(results));
   1175 	assert(radix_tree_gang_lookup_node_reverse(t, 0, results, 3, true) ==
   1176 	    1);
   1177 	assert(results[0] == (void *)0xdeadbea0);
   1178 	memset(results, 0, sizeof(results));
   1179 	assert(radix_tree_gang_lookup_node_reverse(t, 1000, results, 3, false)
   1180 	    == 1);
   1181 	assert(results[0] == (void *)0xdeadbea0);
   1182 	assert(radix_tree_gang_lookup_node_reverse(t, 1000, results, 3, true)
   1183 	    == 0);
   1184 	assert(radix_tree_gang_lookup_tagged_node(t, 0, results, 3, false, 1)
   1185 	    == 0);
   1186 	assert(radix_tree_gang_lookup_tagged_node(t, 0, results, 3, true, 1)
   1187 	    == 0);
   1188 	assert(radix_tree_gang_lookup_tagged_node_reverse(t, 0, results, 3,
   1189 	    false, 1) == 0);
   1190 	assert(radix_tree_gang_lookup_tagged_node_reverse(t, 0, results, 3,
   1191 	    true, 1) == 0);
   1192 	assert(radix_tree_insert_node(t, 1000, (void *)0xdeadbea0) == 0);
   1193 	assert(radix_tree_remove_node(t, 0) == (void *)0xdeadbea0);
   1194 	assert(!radix_tree_empty_tree_p(t));
   1195 	radix_tree_dump(t);
   1196 	assert(radix_tree_lookup_node(t, 0) == NULL);
   1197 	assert(radix_tree_lookup_node(t, 1000) == (void *)0xdeadbea0);
   1198 	memset(results, 0, sizeof(results));
   1199 	assert(radix_tree_gang_lookup_node(t, 0, results, 3, false) == 1);
   1200 	assert(results[0] == (void *)0xdeadbea0);
   1201 	assert(radix_tree_gang_lookup_node(t, 0, results, 3, true) == 0);
   1202 	memset(results, 0, sizeof(results));
   1203 	assert(radix_tree_gang_lookup_node(t, 1000, results, 3, false) == 1);
   1204 	assert(results[0] == (void *)0xdeadbea0);
   1205 	memset(results, 0, sizeof(results));
   1206 	assert(radix_tree_gang_lookup_node(t, 1000, results, 3, true) == 1);
   1207 	assert(results[0] == (void *)0xdeadbea0);
   1208 	assert(radix_tree_gang_lookup_node_reverse(t, 0, results, 3, false)
   1209 	    == 0);
   1210 	assert(radix_tree_gang_lookup_node_reverse(t, 0, results, 3, true)
   1211 	    == 0);
   1212 	memset(results, 0, sizeof(results));
   1213 	assert(radix_tree_gang_lookup_node_reverse(t, 1000, results, 3, false)
   1214 	    == 1);
   1215 	memset(results, 0, sizeof(results));
   1216 	assert(radix_tree_gang_lookup_node_reverse(t, 1000, results, 3, true)
   1217 	    == 1);
   1218 	assert(results[0] == (void *)0xdeadbea0);
   1219 	assert(radix_tree_gang_lookup_tagged_node(t, 0, results, 3, false, 1)
   1220 	    == 0);
   1221 	assert(radix_tree_gang_lookup_tagged_node(t, 0, results, 3, true, 1)
   1222 	    == 0);
   1223 	assert(radix_tree_gang_lookup_tagged_node_reverse(t, 0, results, 3,
   1224 	    false, 1) == 0);
   1225 	assert(radix_tree_gang_lookup_tagged_node_reverse(t, 0, results, 3,
   1226 	    true, 1) == 0);
   1227 	assert(!radix_tree_get_tag(t, 1000, 1));
   1228 	assert(!radix_tree_get_tag(t, 1000, 2));
   1229 	assert(radix_tree_get_tag(t, 1000, 2 | 1) == 0);
   1230 	assert(radix_tree_empty_tagged_tree_p(t, 1));
   1231 	assert(radix_tree_empty_tagged_tree_p(t, 2));
   1232 	radix_tree_set_tag(t, 1000, 2);
   1233 	assert(!radix_tree_get_tag(t, 1000, 1));
   1234 	assert(radix_tree_get_tag(t, 1000, 2));
   1235 	assert(radix_tree_get_tag(t, 1000, 2 | 1) == 2);
   1236 	assert(radix_tree_empty_tagged_tree_p(t, 1));
   1237 	assert(!radix_tree_empty_tagged_tree_p(t, 2));
   1238 	radix_tree_dump(t);
   1239 	assert(radix_tree_lookup_node(t, 1000) == (void *)0xdeadbea0);
   1240 	assert(radix_tree_insert_node(t, 0, (void *)0xbea0) == 0);
   1241 	radix_tree_dump(t);
   1242 	assert(radix_tree_lookup_node(t, 0) == (void *)0xbea0);
   1243 	assert(radix_tree_lookup_node(t, 1000) == (void *)0xdeadbea0);
   1244 	assert(radix_tree_insert_node(t, UINT64_C(10000000000), (void *)0xdea0)
   1245 	    == 0);
   1246 	radix_tree_dump(t);
   1247 	assert(radix_tree_lookup_node(t, 0) == (void *)0xbea0);
   1248 	assert(radix_tree_lookup_node(t, 1000) == (void *)0xdeadbea0);
   1249 	assert(radix_tree_lookup_node(t, UINT64_C(10000000000)) ==
   1250 	    (void *)0xdea0);
   1251 	radix_tree_dump(t);
   1252 	assert(!radix_tree_get_tag(t, 0, 2));
   1253 	assert(radix_tree_get_tag(t, 1000, 2));
   1254 	assert(!radix_tree_get_tag(t, UINT64_C(10000000000), 1));
   1255 	radix_tree_set_tag(t, 0, 2);;
   1256 	radix_tree_set_tag(t, UINT64_C(10000000000), 2);
   1257 	radix_tree_dump(t);
   1258 	assert(radix_tree_get_tag(t, 0, 2));
   1259 	assert(radix_tree_get_tag(t, 1000, 2));
   1260 	assert(radix_tree_get_tag(t, UINT64_C(10000000000), 2));
   1261 	radix_tree_clear_tag(t, 0, 2);;
   1262 	radix_tree_clear_tag(t, UINT64_C(10000000000), 2);
   1263 	radix_tree_dump(t);
   1264 	assert(!radix_tree_get_tag(t, 0, 2));
   1265 	assert(radix_tree_get_tag(t, 1000, 2));
   1266 	assert(!radix_tree_get_tag(t, UINT64_C(10000000000), 2));
   1267 	radix_tree_dump(t);
   1268 	assert(radix_tree_replace_node(t, 1000, (void *)0x12345678) ==
   1269 	    (void *)0xdeadbea0);
   1270 	assert(!radix_tree_get_tag(t, 1000, 1));
   1271 	assert(radix_tree_get_tag(t, 1000, 2));
   1272 	assert(radix_tree_get_tag(t, 1000, 2 | 1) == 2);
   1273 	memset(results, 0, sizeof(results));
   1274 	assert(radix_tree_gang_lookup_node(t, 0, results, 3, false) == 3);
   1275 	assert(results[0] == (void *)0xbea0);
   1276 	assert(results[1] == (void *)0x12345678);
   1277 	assert(results[2] == (void *)0xdea0);
   1278 	memset(results, 0, sizeof(results));
   1279 	assert(radix_tree_gang_lookup_node(t, 0, results, 3, true) == 1);
   1280 	assert(results[0] == (void *)0xbea0);
   1281 	memset(results, 0, sizeof(results));
   1282 	assert(radix_tree_gang_lookup_node(t, 1, results, 3, false) == 2);
   1283 	assert(results[0] == (void *)0x12345678);
   1284 	assert(results[1] == (void *)0xdea0);
   1285 	assert(radix_tree_gang_lookup_node(t, 1, results, 3, true) == 0);
   1286 	memset(results, 0, sizeof(results));
   1287 	assert(radix_tree_gang_lookup_node(t, 1001, results, 3, false) == 1);
   1288 	assert(results[0] == (void *)0xdea0);
   1289 	assert(radix_tree_gang_lookup_node(t, 1001, results, 3, true) == 0);
   1290 	assert(radix_tree_gang_lookup_node(t, UINT64_C(10000000001), results, 3,
   1291 	    false) == 0);
   1292 	assert(radix_tree_gang_lookup_node(t, UINT64_C(10000000001), results, 3,
   1293 	    true) == 0);
   1294 	assert(radix_tree_gang_lookup_node(t, UINT64_C(1000000000000), results,
   1295 	    3, false) == 0);
   1296 	assert(radix_tree_gang_lookup_node(t, UINT64_C(1000000000000), results,
   1297 	    3, true) == 0);
   1298 	memset(results, 0, sizeof(results));
   1299 	assert(radix_tree_gang_lookup_tagged_node(t, 0, results, 100, false, 2)
   1300 	    == 1);
   1301 	assert(results[0] == (void *)0x12345678);
   1302 	assert(radix_tree_gang_lookup_tagged_node(t, 0, results, 100, true, 2)
   1303 	    == 0);
   1304 	assert(entry_tagmask(t->t_root) != 0);
   1305 	assert(radix_tree_remove_node(t, 1000) == (void *)0x12345678);
   1306 	assert(entry_tagmask(t->t_root) == 0);
   1307 	radix_tree_dump(t);
   1308 	assert(radix_tree_insert_node(t, UINT64_C(10000000001), (void *)0xfff0)
   1309 	    == 0);
   1310 	memset(results, 0, sizeof(results));
   1311 	assert(radix_tree_gang_lookup_node(t, UINT64_C(10000000000), results, 3,
   1312 	    false) == 2);
   1313 	assert(results[0] == (void *)0xdea0);
   1314 	assert(results[1] == (void *)0xfff0);
   1315 	memset(results, 0, sizeof(results));
   1316 	assert(radix_tree_gang_lookup_node(t, UINT64_C(10000000000), results, 3,
   1317 	    true) == 2);
   1318 	assert(results[0] == (void *)0xdea0);
   1319 	assert(results[1] == (void *)0xfff0);
   1320 	memset(results, 0, sizeof(results));
   1321 	assert(radix_tree_gang_lookup_node_reverse(t, UINT64_C(10000000001),
   1322 	    results, 3, false) == 3);
   1323 	assert(results[0] == (void *)0xfff0);
   1324 	assert(results[1] == (void *)0xdea0);
   1325 	assert(results[2] == (void *)0xbea0);
   1326 	memset(results, 0, sizeof(results));
   1327 	assert(radix_tree_gang_lookup_node_reverse(t, UINT64_C(10000000001),
   1328 	    results, 3, true) == 2);
   1329 	assert(results[0] == (void *)0xfff0);
   1330 	assert(results[1] == (void *)0xdea0);
   1331 	assert(radix_tree_remove_node(t, UINT64_C(10000000000)) ==
   1332 	    (void *)0xdea0);
   1333 	assert(radix_tree_remove_node(t, UINT64_C(10000000001)) ==
   1334 	    (void *)0xfff0);
   1335 	radix_tree_dump(t);
   1336 	assert(radix_tree_remove_node(t, 0) == (void *)0xbea0);
   1337 	radix_tree_dump(t);
   1338 	radix_tree_fini_tree(t);
   1339 }
   1340 
   1341 #include <sys/time.h>
   1342 
   1343 struct testnode {
   1344 	uint64_t idx;
   1345 	bool tagged[RADIX_TREE_TAG_ID_MAX];
   1346 };
   1347 
   1348 static void
   1349 printops(const char *title, const char *name, int tag, unsigned int n,
   1350     const struct timeval *stv, const struct timeval *etv)
   1351 {
   1352 	uint64_t s = stv->tv_sec * 1000000 + stv->tv_usec;
   1353 	uint64_t e = etv->tv_sec * 1000000 + etv->tv_usec;
   1354 
   1355 	printf("RESULT %s %s %d %lf op/s\n", title, name, tag,
   1356 	    (double)n / (e - s) * 1000000);
   1357 }
   1358 
   1359 #define	TEST2_GANG_LOOKUP_NODES	16
   1360 
   1361 static bool
   1362 test2_should_tag(unsigned int i, unsigned int tagid)
   1363 {
   1364 
   1365 	if (tagid == 0) {
   1366 		return (i % 4) == 0;	/* 25% */
   1367 	} else {
   1368 		return (i % 7) == 0;	/* 14% */
   1369 	}
   1370 	return 1;
   1371 }
   1372 
   1373 static void
   1374 check_tag_count(const unsigned int *ntagged, unsigned int tagmask,
   1375     unsigned int count)
   1376 {
   1377 	unsigned int tag;
   1378 
   1379 	for (tag = 0; tag < RADIX_TREE_TAG_ID_MAX; tag++) {
   1380 		if ((tagmask & (1 << tag)) == 0) {
   1381 			continue;
   1382 		}
   1383 		if (((tagmask - 1) & tagmask) == 0) {
   1384 			assert(count == ntagged[tag]);
   1385 		} else {
   1386 			assert(count >= ntagged[tag]);
   1387 		}
   1388 	}
   1389 }
   1390 
   1391 static void
   1392 test2(const char *title, bool dense)
   1393 {
   1394 	struct radix_tree s;
   1395 	struct radix_tree *t = &s;
   1396 	struct testnode *n;
   1397 	unsigned int i;
   1398 	unsigned int nnodes = 100000;
   1399 	unsigned int removed;
   1400 	unsigned int tag;
   1401 	unsigned int tagmask;
   1402 	unsigned int ntagged[RADIX_TREE_TAG_ID_MAX];
   1403 	struct testnode *nodes;
   1404 	struct timeval stv;
   1405 	struct timeval etv;
   1406 
   1407 	nodes = malloc(nnodes * sizeof(*nodes));
   1408 	for (tag = 0; tag < RADIX_TREE_TAG_ID_MAX; tag++) {
   1409 		ntagged[tag] = 0;
   1410 	}
   1411 	radix_tree_init_tree(t);
   1412 	for (i = 0; i < nnodes; i++) {
   1413 		n = &nodes[i];
   1414 		n->idx = random();
   1415 		if (sizeof(long) == 4) {
   1416 			n->idx <<= 32;
   1417 			n->idx |= (uint32_t)random();
   1418 		}
   1419 		if (dense) {
   1420 			n->idx %= nnodes * 2;
   1421 		}
   1422 		while (radix_tree_lookup_node(t, n->idx) != NULL) {
   1423 			n->idx++;
   1424 		}
   1425 		radix_tree_insert_node(t, n->idx, n);
   1426 		for (tag = 0; tag < RADIX_TREE_TAG_ID_MAX; tag++) {
   1427 			tagmask = 1 << tag;
   1428 
   1429 			n->tagged[tag] = test2_should_tag(i, tag);
   1430 			if (n->tagged[tag]) {
   1431 				radix_tree_set_tag(t, n->idx, tagmask);
   1432 				ntagged[tag]++;
   1433 			}
   1434 			assert((n->tagged[tag] ? tagmask : 0) ==
   1435 			    radix_tree_get_tag(t, n->idx, tagmask));
   1436 		}
   1437 	}
   1438 
   1439 	gettimeofday(&stv, NULL);
   1440 	for (i = 0; i < nnodes; i++) {
   1441 		n = &nodes[i];
   1442 		assert(radix_tree_lookup_node(t, n->idx) == n);
   1443 	}
   1444 	gettimeofday(&etv, NULL);
   1445 	printops(title, "lookup", 0, nnodes, &stv, &etv);
   1446 
   1447 	for (tagmask = 1; tagmask <= RADIX_TREE_TAG_MASK; tagmask ++) {
   1448 		unsigned int count = 0;
   1449 
   1450 		gettimeofday(&stv, NULL);
   1451 		for (i = 0; i < nnodes; i++) {
   1452 			unsigned int tagged;
   1453 
   1454 			n = &nodes[i];
   1455 			tagged = radix_tree_get_tag(t, n->idx, tagmask);
   1456 			assert((tagged & ~tagmask) == 0);
   1457 			for (tag = 0; tag < RADIX_TREE_TAG_ID_MAX; tag++) {
   1458 				assert((tagmask & (1 << tag)) == 0 ||
   1459 				    n->tagged[tag] == !!(tagged & (1 << tag)));
   1460 			}
   1461 			if (tagged) {
   1462 				count++;
   1463 			}
   1464 		}
   1465 		gettimeofday(&etv, NULL);
   1466 		check_tag_count(ntagged, tagmask, count);
   1467 		printops(title, "get_tag", tagmask, nnodes, &stv, &etv);
   1468 	}
   1469 
   1470 	gettimeofday(&stv, NULL);
   1471 	for (i = 0; i < nnodes; i++) {
   1472 		n = &nodes[i];
   1473 		radix_tree_remove_node(t, n->idx);
   1474 	}
   1475 	gettimeofday(&etv, NULL);
   1476 	printops(title, "remove", 0, nnodes, &stv, &etv);
   1477 
   1478 	gettimeofday(&stv, NULL);
   1479 	for (i = 0; i < nnodes; i++) {
   1480 		n = &nodes[i];
   1481 		radix_tree_insert_node(t, n->idx, n);
   1482 	}
   1483 	gettimeofday(&etv, NULL);
   1484 	printops(title, "insert", 0, nnodes, &stv, &etv);
   1485 
   1486 	for (tag = 0; tag < RADIX_TREE_TAG_ID_MAX; tag++) {
   1487 		tagmask = 1 << tag;
   1488 
   1489 		ntagged[tag] = 0;
   1490 		gettimeofday(&stv, NULL);
   1491 		for (i = 0; i < nnodes; i++) {
   1492 			n = &nodes[i];
   1493 			if (n->tagged[tag]) {
   1494 				radix_tree_set_tag(t, n->idx, tagmask);
   1495 				ntagged[tag]++;
   1496 			}
   1497 		}
   1498 		gettimeofday(&etv, NULL);
   1499 		printops(title, "set_tag", tag, ntagged[tag], &stv, &etv);
   1500 	}
   1501 
   1502 	gettimeofday(&stv, NULL);
   1503 	{
   1504 		struct testnode *results[TEST2_GANG_LOOKUP_NODES];
   1505 		uint64_t nextidx;
   1506 		unsigned int nfound;
   1507 		unsigned int total;
   1508 
   1509 		nextidx = 0;
   1510 		total = 0;
   1511 		while ((nfound = radix_tree_gang_lookup_node(t, nextidx,
   1512 		    (void *)results, __arraycount(results), false)) > 0) {
   1513 			nextidx = results[nfound - 1]->idx + 1;
   1514 			total += nfound;
   1515 			if (nextidx == 0) {
   1516 				break;
   1517 			}
   1518 		}
   1519 		assert(total == nnodes);
   1520 	}
   1521 	gettimeofday(&etv, NULL);
   1522 	printops(title, "ganglookup", 0, nnodes, &stv, &etv);
   1523 
   1524 	gettimeofday(&stv, NULL);
   1525 	{
   1526 		struct testnode *results[TEST2_GANG_LOOKUP_NODES];
   1527 		uint64_t nextidx;
   1528 		unsigned int nfound;
   1529 		unsigned int total;
   1530 
   1531 		nextidx = UINT64_MAX;
   1532 		total = 0;
   1533 		while ((nfound = radix_tree_gang_lookup_node_reverse(t, nextidx,
   1534 		    (void *)results, __arraycount(results), false)) > 0) {
   1535 			nextidx = results[nfound - 1]->idx - 1;
   1536 			total += nfound;
   1537 			if (nextidx == UINT64_MAX) {
   1538 				break;
   1539 			}
   1540 		}
   1541 		assert(total == nnodes);
   1542 	}
   1543 	gettimeofday(&etv, NULL);
   1544 	printops(title, "ganglookup_reverse", 0, nnodes, &stv, &etv);
   1545 
   1546 	for (tagmask = 1; tagmask <= RADIX_TREE_TAG_MASK; tagmask ++) {
   1547 		unsigned int total = 0;
   1548 
   1549 		gettimeofday(&stv, NULL);
   1550 		{
   1551 			struct testnode *results[TEST2_GANG_LOOKUP_NODES];
   1552 			uint64_t nextidx;
   1553 			unsigned int nfound;
   1554 
   1555 			nextidx = 0;
   1556 			while ((nfound = radix_tree_gang_lookup_tagged_node(t,
   1557 			    nextidx, (void *)results, __arraycount(results),
   1558 			    false, tagmask)) > 0) {
   1559 				nextidx = results[nfound - 1]->idx + 1;
   1560 				total += nfound;
   1561 			}
   1562 		}
   1563 		gettimeofday(&etv, NULL);
   1564 		check_tag_count(ntagged, tagmask, total);
   1565 		assert(tagmask != 0 || total == 0);
   1566 		printops(title, "ganglookup_tag", tagmask, total, &stv, &etv);
   1567 	}
   1568 
   1569 	for (tagmask = 1; tagmask <= RADIX_TREE_TAG_MASK; tagmask ++) {
   1570 		unsigned int total = 0;
   1571 
   1572 		gettimeofday(&stv, NULL);
   1573 		{
   1574 			struct testnode *results[TEST2_GANG_LOOKUP_NODES];
   1575 			uint64_t nextidx;
   1576 			unsigned int nfound;
   1577 
   1578 			nextidx = UINT64_MAX;
   1579 			while ((nfound =
   1580 			    radix_tree_gang_lookup_tagged_node_reverse(t,
   1581 			    nextidx, (void *)results, __arraycount(results),
   1582 			    false, tagmask)) > 0) {
   1583 				nextidx = results[nfound - 1]->idx - 1;
   1584 				total += nfound;
   1585 				if (nextidx == UINT64_MAX) {
   1586 					break;
   1587 				}
   1588 			}
   1589 		}
   1590 		gettimeofday(&etv, NULL);
   1591 		check_tag_count(ntagged, tagmask, total);
   1592 		assert(tagmask != 0 || total == 0);
   1593 		printops(title, "ganglookup_tag_reverse", tagmask, total,
   1594 		    &stv, &etv);
   1595 	}
   1596 
   1597 	removed = 0;
   1598 	for (tag = 0; tag < RADIX_TREE_TAG_ID_MAX; tag++) {
   1599 		unsigned int total;
   1600 
   1601 		total = 0;
   1602 		tagmask = 1 << tag;
   1603 		gettimeofday(&stv, NULL);
   1604 		{
   1605 			struct testnode *results[TEST2_GANG_LOOKUP_NODES];
   1606 			uint64_t nextidx;
   1607 			unsigned int nfound;
   1608 
   1609 			nextidx = 0;
   1610 			while ((nfound = radix_tree_gang_lookup_tagged_node(t,
   1611 			    nextidx, (void *)results, __arraycount(results),
   1612 			    false, tagmask)) > 0) {
   1613 				for (i = 0; i < nfound; i++) {
   1614 					radix_tree_remove_node(t,
   1615 					    results[i]->idx);
   1616 				}
   1617 				nextidx = results[nfound - 1]->idx + 1;
   1618 				total += nfound;
   1619 				if (nextidx == 0) {
   1620 					break;
   1621 				}
   1622 			}
   1623 		}
   1624 		gettimeofday(&etv, NULL);
   1625 		if (tag == 0) {
   1626 			check_tag_count(ntagged, tagmask, total);
   1627 		} else {
   1628 			assert(total <= ntagged[tag]);
   1629 		}
   1630 		printops(title, "ganglookup_tag+remove", tagmask, total, &stv,
   1631 		    &etv);
   1632 		removed += total;
   1633 	}
   1634 
   1635 	gettimeofday(&stv, NULL);
   1636 	{
   1637 		struct testnode *results[TEST2_GANG_LOOKUP_NODES];
   1638 		uint64_t nextidx;
   1639 		unsigned int nfound;
   1640 		unsigned int total;
   1641 
   1642 		nextidx = 0;
   1643 		total = 0;
   1644 		while ((nfound = radix_tree_gang_lookup_node(t, nextidx,
   1645 		    (void *)results, __arraycount(results), false)) > 0) {
   1646 			for (i = 0; i < nfound; i++) {
   1647 				assert(results[i] == radix_tree_remove_node(t,
   1648 				    results[i]->idx));
   1649 			}
   1650 			nextidx = results[nfound - 1]->idx + 1;
   1651 			total += nfound;
   1652 			if (nextidx == 0) {
   1653 				break;
   1654 			}
   1655 		}
   1656 		assert(total == nnodes - removed);
   1657 	}
   1658 	gettimeofday(&etv, NULL);
   1659 	printops(title, "ganglookup+remove", 0, nnodes - removed, &stv, &etv);
   1660 
   1661 	assert(radix_tree_empty_tree_p(t));
   1662 	for (tagmask = 1; tagmask <= RADIX_TREE_TAG_MASK; tagmask ++) {
   1663 		assert(radix_tree_empty_tagged_tree_p(t, tagmask));
   1664 	}
   1665 	radix_tree_fini_tree(t);
   1666 	free(nodes);
   1667 }
   1668 
   1669 int
   1670 main(int argc, char *argv[])
   1671 {
   1672 
   1673 	test1();
   1674 	test2("dense", true);
   1675 	test2("sparse", false);
   1676 	return 0;
   1677 }
   1678 
   1679 #endif /* defined(UNITTEST) */
   1680