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