Home | History | Annotate | Line # | Download | only in gen
ptree.c revision 1.2
      1 /* $NetBSD: ptree.c,v 1.2 2008/11/21 01:58:41 matt Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Matt Thomas <matt (at) 3am-software.com>.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #define _PT_PRIVATE
     33 
     34 #if defined(PTCHECK) && !defined(PTDEBUG)
     35 #define PTDEBUG
     36 #endif
     37 
     38 #if defined(_KERNEL) || defined(_STANDALONE)
     39 #include <sys/param.h>
     40 #include <sys/types.h>
     41 #include <sys/systm.h>
     42 __KERNEL_RCSID(0, "$NetBSD: ptree.c,v 1.2 2008/11/21 01:58:41 matt Exp $");
     43 #else
     44 #include <stddef.h>
     45 #include <stdint.h>
     46 #include <limits.h>
     47 #include <stdbool.h>
     48 #include <string.h>
     49 #ifdef PTDEBUG
     50 #include <assert.h>
     51 #define	KASSERT(e)	assert(e)
     52 #else
     53 #define	KASSERT(e)	do { } while (/*CONSTCOND*/ 0)
     54 #endif
     55 __RCSID("$NetBSD: ptree.c,v 1.2 2008/11/21 01:58:41 matt Exp $");
     56 #endif /* _KERNEL || _STANDALONE */
     57 
     58 #ifdef _LIBC
     59 #include "namespace.h"
     60 #endif
     61 
     62 #ifdef PTTEST
     63 #include "ptree.h"
     64 #else
     65 #include <sys/ptree.h>
     66 #endif
     67 
     68 /*
     69  * This is an implementation of a radix / PATRICIA tree.  As in a traditional
     70  * patricia tree, all the data is at the leaves of the tree.  An N-value
     71  * tree would have N leaves, N-1 branching nodes, and a root pointer.  Each
     72  * branching node would have left(0) and right(1) pointers that either point
     73  * to another branching node or a leaf node.  The root pointer would also
     74  * point to either the first branching node or a leaf node.  Leaf nodes
     75  * have no need for pointers.
     76  *
     77  * However, allocation for these branching nodes is problematic since the
     78  * allocation could fail.  This would cause insertions to fail for reasons
     79  * beyond the users control.  So to prevent this, in this implementation
     80  * each node has two identities: its leaf identity and its branch identity.
     81  * Each is separate from the other.  Every branch is tagged as to whether
     82  * it points to a leaf or a branch.  This is not an attribute of the object
     83  * but of the pointer to the object.  The low bit of the pointer is used as
     84  * the tag to determine wether it points to a leaf or branch identity, with
     85  * branch identities having the low bit set.
     86  *
     87  * A node's branch identity has one rule: when traversing the tree from the
     88  * root to the node's leaf identity, one of the branches traversed will be via
     89  * the node's branch identity.  Of course, that has an exception: since to
     90  * store N leaves, you need N-1 branches.  That one node whose branch identity
     91  * isn't used is stored as "oddman"-out in the root.
     92  *
     93  * Branching nodes also has a bit offset and a bit length which determines
     94  * which branch slot is used.  The bit length can be zero resulting in a
     95  * one-way branch.  This is happens in two special cases: the root and
     96  * interior mask nodes.
     97  *
     98  * To support longest match first lookups, when a mask node (one that only
     99  * match the first N bits) has children who first N bits match the mask nodes,
    100  * that mask node is converted from being a leaf node to being a one-way
    101  * branch-node.  The mask becomes fixed in position in the tree.  The mask
    102  * will alaways be the longest mask match for its descendants (unless they
    103  * traverse an even longer match).
    104  */
    105 
    106 #define	NODETOITEM(pt, ptn)	\
    107 	((void *)((uintptr_t)(ptn) - (pt)->pt_node_offset))
    108 #define	NODETOKEY(pt, ptn)	\
    109 	((void *)((uintptr_t)(ptn) - (pt)->pt_node_offset + pt->pt_key_offset))
    110 #define	ITEMTONODE(pt, ptn)	\
    111 	((pt_node_t *)((uintptr_t)(ptn) + (pt)->pt_node_offset))
    112 
    113 bool ptree_check(const pt_tree_t *);
    114 #if PTCHECK > 1
    115 #define	PTREE_CHECK(pt)		ptree_check(pt)
    116 #else
    117 #define	PTREE_CHECK(pt)		do { } while (/*CONSTCOND*/ 0)
    118 #endif
    119 
    120 static inline bool
    121 ptree_matchnode(const pt_tree_t *pt, const pt_node_t *target,
    122 	const pt_node_t *ptn, pt_bitoff_t max_bitoff,
    123 	pt_bitoff_t *bitoff_p, pt_slot_t *slots_p)
    124 {
    125 	return (*pt->pt_ops->ptto_matchnode)(NODETOKEY(pt, target),
    126 	    (ptn != NULL ? NODETOKEY(pt, ptn) : NULL), max_bitoff,
    127 	    bitoff_p, slots_p);
    128 }
    129 
    130 static inline pt_slot_t
    131 ptree_testnode(const pt_tree_t *pt, const pt_node_t *target,
    132 	const pt_node_t *ptn)
    133 {
    134 	const pt_bitlen_t bitlen = PTN_BRANCH_BITLEN(ptn);
    135 	if (bitlen == 0)
    136 		return PT_SLOT_ROOT;
    137 	return (*pt->pt_ops->ptto_testnode)(NODETOKEY(pt, target),
    138 	     PTN_BRANCH_BITOFF(ptn),
    139 	     bitlen);
    140 }
    141 
    142 static inline bool
    143 ptree_matchkey(const pt_tree_t *pt, const void *key,
    144 	const pt_node_t *ptn, pt_bitoff_t bitoff, pt_bitlen_t bitlen)
    145 {
    146 	return (*pt->pt_ops->ptto_matchkey)(key, NODETOKEY(pt, ptn),
    147 	    bitoff, bitlen);
    148 }
    149 
    150 static inline pt_slot_t
    151 ptree_testkey(const pt_tree_t *pt, const void *key, const pt_node_t *ptn)
    152 {
    153 	return (*pt->pt_ops->ptto_testkey)(key,
    154 	    PTN_BRANCH_BITOFF(ptn),
    155 	    PTN_BRANCH_BITLEN(ptn));
    156 }
    157 
    158 static inline void
    159 ptree_set_position(uintptr_t node, pt_slot_t position)
    160 {
    161 	if (PT_LEAF_P(node))
    162 		PTN_SET_LEAF_POSITION(PT_NODE(node), position);
    163 	else
    164 		PTN_SET_BRANCH_POSITION(PT_NODE(node), position);
    165 }
    166 
    167 void
    168 ptree_init(pt_tree_t *pt, const pt_tree_ops_t *ops, size_t node_offset,
    169 	size_t key_offset)
    170 {
    171 	memset(pt, 0, sizeof(*pt));
    172 	pt->pt_node_offset = node_offset;
    173 	pt->pt_key_offset = key_offset;
    174 	pt->pt_ops = ops;
    175 }
    176 
    177 typedef struct {
    178 	uintptr_t *id_insertp;
    179 	pt_node_t *id_parent;
    180 	uintptr_t id_node;
    181 	pt_slot_t id_parent_slot;
    182 	pt_bitoff_t id_bitoff;
    183 	pt_slot_t id_slot;
    184 } pt_insertdata_t;
    185 
    186 typedef bool (*pt_insertfunc_t)(pt_tree_t *, pt_node_t *, pt_insertdata_t *);
    187 
    188 /*
    189  * Move a branch identify from src to dst.  The leaves don't care since
    190  * nothing for them has changed.
    191  */
    192 /*ARGSUSED*/
    193 static uintptr_t
    194 ptree_move_branch(pt_tree_t * const pt, pt_node_t * const dst,
    195 	const pt_node_t * const src)
    196 {
    197 	KASSERT(PTN_BRANCH_BITLEN(src) == 1);
    198 	/* set branch bitlen and bitoff in one step.  */
    199 	dst->ptn_branchdata = src->ptn_branchdata;
    200 	PTN_SET_BRANCH_POSITION(dst, PTN_BRANCH_POSITION(src));
    201 	PTN_COPY_BRANCH_SLOTS(dst, src);
    202 	return PTN_BRANCH(dst);
    203 }
    204 
    205 #ifndef PTNOMASK
    206 static inline uintptr_t *
    207 ptree_find_branch(pt_tree_t * const pt, uintptr_t branch_node)
    208 {
    209 	pt_node_t * const branch = PT_NODE(branch_node);
    210 	pt_node_t *parent;
    211 
    212 	for (parent = &pt->pt_rootnode;;) {
    213 		uintptr_t *nodep =
    214 		    &PTN_BRANCH_SLOT(parent, ptree_testnode(pt, branch, parent));
    215 		if (*nodep == branch_node)
    216 			return nodep;
    217 		if (PT_LEAF_P(*nodep))
    218 			return NULL;
    219 		parent = PT_NODE(*nodep);
    220 	}
    221 }
    222 
    223 static bool
    224 ptree_insert_leaf_after_mask(pt_tree_t * const pt, pt_node_t * const target,
    225 	pt_insertdata_t * const id)
    226 {
    227 	const uintptr_t target_node = PTN_LEAF(target);
    228 	const uintptr_t mask_node = id->id_node;
    229 	pt_node_t * const mask = PT_NODE(mask_node);
    230 	const pt_bitlen_t mask_len = PTN_MASK_BITLEN(mask);
    231 
    232 	KASSERT(PT_LEAF_P(mask_node));
    233 	KASSERT(PTN_LEAF_POSITION(mask) == id->id_parent_slot);
    234 	KASSERT(mask_len <= id->id_bitoff);
    235 	KASSERT(PTN_ISMASK_P(mask));
    236 	KASSERT(!PTN_ISMASK_P(target) || mask_len < PTN_MASK_BITLEN(target));
    237 
    238 	if (mask_node == PTN_BRANCH_ODDMAN_SLOT(&pt->pt_rootnode)) {
    239 		KASSERT(id->id_parent != mask);
    240 		/*
    241 		 * Nice, mask was an oddman.  So just set the oddman to target.
    242 		 */
    243 		PTN_BRANCH_ODDMAN_SLOT(&pt->pt_rootnode) = target_node;
    244 	} else {
    245 		/*
    246 		 * We need to find out who's pointing to mask's branch
    247 		 * identity.  We know that between root and the leaf identity,
    248 		 * we must traverse the node's branch identity.
    249 		 */
    250 		uintptr_t * const mask_nodep = ptree_find_branch(pt, PTN_BRANCH(mask));
    251 		KASSERT(mask_nodep != NULL);
    252 		KASSERT(*mask_nodep == PTN_BRANCH(mask));
    253 		KASSERT(PTN_BRANCH_BITLEN(mask) == 1);
    254 
    255 		/*
    256 		 * Alas, mask was used as a branch.  Since the mask is becoming
    257 		 * a one-way branch, we need make target take over mask's
    258 		 * branching responsibilities.  Only then can we change it.
    259 		 */
    260 		*mask_nodep = ptree_move_branch(pt, target, mask);
    261 
    262 		/*
    263 		 * However, it's possible that mask's parent is itself.  If
    264 		 * that's true, update the insert point to use target since it
    265 		 * has taken over mask's branching duties.
    266 		 */
    267 		if (id->id_parent == mask)
    268 			id->id_insertp = &PTN_BRANCH_SLOT(target,
    269 			    id->id_parent_slot);
    270 	}
    271 
    272 	PTN_SET_BRANCH_BITLEN(mask, 0);
    273 	PTN_SET_BRANCH_BITOFF(mask, mask_len);
    274 
    275 	PTN_BRANCH_ROOT_SLOT(mask) = target_node;
    276 	PTN_BRANCH_ODDMAN_SLOT(mask) = PT_NULL;
    277 	PTN_SET_LEAF_POSITION(target, PT_SLOT_ROOT);
    278 	PTN_SET_BRANCH_POSITION(mask, id->id_parent_slot);
    279 
    280 	/*
    281 	 * Now that everything is done, to make target visible we need to
    282 	 * change mask from a leaf to a branch.
    283 	 */
    284 	*id->id_insertp = PTN_BRANCH(mask);
    285 	PTREE_CHECK(pt);
    286 	return true;
    287 }
    288 
    289 /*ARGSUSED*/
    290 static bool
    291 ptree_insert_mask_before_node(pt_tree_t * const pt, pt_node_t * const target,
    292 	pt_insertdata_t * const id)
    293 {
    294 	const uintptr_t node = id->id_node;
    295 	pt_node_t * const ptn = PT_NODE(node);
    296 	const pt_slot_t mask_len = PTN_MASK_BITLEN(target);
    297 	const pt_bitlen_t node_mask_len = PTN_MASK_BITLEN(ptn);
    298 
    299 	KASSERT(PT_LEAF_P(node) || id->id_parent_slot == PTN_BRANCH_POSITION(ptn));
    300 	KASSERT(PT_BRANCH_P(node) || id->id_parent_slot == PTN_LEAF_POSITION(ptn));
    301 	KASSERT(PTN_ISMASK_P(target));
    302 
    303 	/*
    304 	 * If the node we are placing ourself in front is a mask with the
    305 	 * same mask length as us, return failure.
    306 	 */
    307 	if (PTN_ISMASK_P(ptn) && node_mask_len == mask_len)
    308 		return false;
    309 
    310 	PTN_SET_BRANCH_BITLEN(target, 0);
    311 	PTN_SET_BRANCH_BITOFF(target, mask_len);
    312 
    313 	PTN_BRANCH_SLOT(target, PT_SLOT_ROOT) = node;
    314 	*id->id_insertp = PTN_BRANCH(target);
    315 
    316 	PTN_SET_BRANCH_POSITION(target, id->id_parent_slot);
    317 	ptree_set_position(node, PT_SLOT_ROOT);
    318 
    319 	PTREE_CHECK(pt);
    320 	return true;
    321 }
    322 #endif /* !PTNOMASK */
    323 
    324 /*ARGSUSED*/
    325 static bool
    326 ptree_insert_branch_at_node(pt_tree_t * const pt, pt_node_t * const target,
    327 	pt_insertdata_t * const id)
    328 {
    329 	const uintptr_t target_node = PTN_LEAF(target);
    330 	const uintptr_t node = id->id_node;
    331 	const pt_slot_t other_slot = id->id_slot ^ PT_SLOT_OTHER;
    332 
    333 	KASSERT(PT_BRANCH_P(node) || id->id_parent_slot == PTN_LEAF_POSITION(PT_NODE(node)));
    334 	KASSERT(PT_LEAF_P(node) || id->id_parent_slot == PTN_BRANCH_POSITION(PT_NODE(node)));
    335 	KASSERT((node == pt->pt_root) == (id->id_parent == &pt->pt_rootnode));
    336 #ifndef PTNOMASK
    337 	KASSERT(!PTN_ISMASK_P(target) || id->id_bitoff <= PTN_MASK_BITLEN(target));
    338 #endif
    339 	KASSERT(node == pt->pt_root || PTN_BRANCH_BITOFF(id->id_parent) + PTN_BRANCH_BITLEN(id->id_parent) <= id->id_bitoff);
    340 
    341 	PTN_SET_BRANCH_BITOFF(target, id->id_bitoff);
    342 	PTN_SET_BRANCH_BITLEN(target, 1);
    343 
    344 	PTN_BRANCH_SLOT(target, id->id_slot) = target_node;
    345 	PTN_BRANCH_SLOT(target, other_slot) = node;
    346 	*id->id_insertp = PTN_BRANCH(target);
    347 
    348 	PTN_SET_LEAF_POSITION(target, id->id_slot);
    349 	ptree_set_position(node, other_slot);
    350 
    351 	PTN_SET_BRANCH_POSITION(target, id->id_parent_slot);
    352 	PTREE_CHECK(pt);
    353 	return true;
    354 }
    355 
    356 static bool
    357 ptree_insert_leaf(pt_tree_t * const pt, pt_node_t * const target,
    358 	pt_insertdata_t * const id)
    359 {
    360 	const uintptr_t leaf_node = id->id_node;
    361 	pt_node_t * const leaf = PT_NODE(leaf_node);
    362 #ifdef PTNOMASK
    363 	const bool inserting_mask = false;
    364 	const bool at_mask = false;
    365 #else
    366 	const bool inserting_mask = PTN_ISMASK_P(target);
    367 	const bool at_mask = PTN_ISMASK_P(leaf);
    368 	const pt_bitlen_t leaf_masklen = PTN_MASK_BITLEN(leaf);
    369 	const pt_bitlen_t target_masklen = PTN_MASK_BITLEN(target);
    370 #endif
    371 	pt_insertfunc_t insertfunc = ptree_insert_branch_at_node;
    372 	bool matched;
    373 
    374 	/*
    375 	 * In all likelyhood we are going simply going to insert a branch
    376 	 * where this leaf is which will point to the old and new leaves.
    377 	 */
    378 	KASSERT(PT_LEAF_P(leaf_node));
    379 	KASSERT(PTN_LEAF_POSITION(leaf) == id->id_parent_slot);
    380 	matched = ptree_matchnode(pt, target, leaf, UINT_MAX,
    381 	    &id->id_bitoff, &id->id_slot);
    382 	if (__predict_false(!inserting_mask)) {
    383 		/*
    384 		 * We aren't inserting a mask nor is the leaf a mask, which
    385 		 * means we are trying to insert a duplicate leaf.  Can't do
    386 		 * that.
    387 		 */
    388 		if (!at_mask && matched)
    389 			return false;
    390 
    391 #ifndef PTNOMASK
    392 		/*
    393 		 * We are at a mask and the leaf we are about to insert
    394 		 * is at or beyond the mask, we need to convert the mask
    395 		 * from a leaf to a one-way branch interior mask.
    396 		 */
    397 		if (at_mask && id->id_bitoff >= leaf_masklen)
    398 			insertfunc = ptree_insert_leaf_after_mask;
    399 #endif /* PTNOMASK */
    400 	}
    401 #ifndef PTNOMASK
    402 	else {
    403 		/*
    404 		 * We are inserting a mask.
    405 		 */
    406 		if (matched) {
    407 			/*
    408 			 * If the leaf isn't a mask, we obviously have to
    409 			 * insert the new mask before non-mask leaf.  If the
    410 			 * leaf is a mask, and the new node has a LEQ mask
    411 			 * length it too needs to inserted before leaf (*).
    412 			 *
    413 			 * In other cases, we place the new mask as leaf after
    414 			 * leaf mask.  Which mask comes first will be a one-way
    415 			 * branch interior mask node which has the other mask
    416 			 * node as a child.
    417 			 *
    418 			 * (*) ptree_insert_mask_before_node can detect a
    419 			 * duplicate mask and return failure if needed.
    420 			 */
    421 			if (!at_mask || target_masklen <= leaf_masklen)
    422 				insertfunc = ptree_insert_mask_before_node;
    423 			else
    424 				insertfunc = ptree_insert_leaf_after_mask;
    425 		} else if (at_mask && id->id_bitoff >= leaf_masklen) {
    426 			/*
    427 			 * If the new mask has a bit offset GEQ than the leaf's
    428 			 * mask length, convert the left to a one-way branch
    429 			 * interior mask and make that point to the new [leaf]
    430 			 * mask.
    431 			 */
    432 			insertfunc = ptree_insert_leaf_after_mask;
    433 		} else {
    434 			/*
    435 			 * The new mask has a bit offset less than the leaf's
    436 			 * mask length or if the leaf isn't a mask at all, the
    437 			 * new mask deserves to be its own leaf so we use the
    438 			 * default insertfunc to do that.
    439 			 */
    440 		}
    441 	}
    442 #endif /* PTNOMASK */
    443 
    444 	return (*insertfunc)(pt, target, id);
    445 }
    446 
    447 static bool
    448 ptree_insert_node_common(pt_tree_t *pt, void *item)
    449 {
    450 	pt_node_t * const target = ITEMTONODE(pt, item);
    451 #ifndef PTNOMASK
    452 	const bool inserting_mask = PTN_ISMASK_P(target);
    453 	const pt_bitlen_t target_masklen = PTN_MASK_BITLEN(target);
    454 #endif
    455 	pt_insertfunc_t insertfunc;
    456 	pt_insertdata_t id;
    457 
    458 	/*
    459 	 * We need a leaf so we can match against.  Until we get a leaf
    460 	 * we having nothing to test against.
    461 	 */
    462 	if (__predict_false(PT_NULL_P(pt->pt_root))) {
    463 		PTN_BRANCH_ROOT_SLOT(&pt->pt_rootnode) = PTN_LEAF(target);
    464 		PTN_BRANCH_ODDMAN_SLOT(&pt->pt_rootnode) = PTN_LEAF(target);
    465 		PTN_SET_LEAF_POSITION(target, PT_SLOT_ROOT);
    466 		PTREE_CHECK(pt);
    467 		return true;
    468 	}
    469 
    470 	id.id_bitoff = 0;
    471 	id.id_parent = &pt->pt_rootnode;
    472 	id.id_parent_slot = PT_SLOT_ROOT;
    473 	id.id_insertp = &PTN_BRANCH_ROOT_SLOT(id.id_parent);
    474 	for (;;) {
    475 		pt_bitoff_t branch_bitoff;
    476 		pt_node_t * const ptn = PT_NODE(*id.id_insertp);
    477 		id.id_node = *id.id_insertp;
    478 
    479 		/*
    480 		 * If we hit a leaf, try to insert target at leaf.  We could
    481 		 * have inlined ptree_insert_leaf here but that would have
    482 		 * made this routine much harder to understand.  Trust the
    483 		 * compiler to optimize this properly.
    484 		 */
    485 		if (PT_LEAF_P(id.id_node)) {
    486 			KASSERT(PTN_LEAF_POSITION(ptn) == id.id_parent_slot);
    487 			insertfunc = ptree_insert_leaf;
    488 			break;
    489 		}
    490 
    491 		/*
    492 		 * If we aren't a leaf, we must be a branch.  Make sure we are
    493 		 * in the slot we think we are.
    494 		 */
    495 		KASSERT(PT_BRANCH_P(id.id_node));
    496 		KASSERT(PTN_BRANCH_POSITION(ptn) == id.id_parent_slot);
    497 
    498 		/*
    499 		 * Where is this branch?
    500 		 */
    501 		branch_bitoff = PTN_BRANCH_BITOFF(ptn);
    502 
    503 #ifndef PTNOMASK
    504 		/*
    505 		 * If this is a one-way mask node, its offset must equal
    506 		 * its mask's bitlen.
    507 		 */
    508 		KASSERT(!(PTN_ISMASK_P(ptn) && PTN_BRANCH_BITLEN(ptn) == 0) || PTN_MASK_BITLEN(ptn) == branch_bitoff);
    509 
    510 		/*
    511 		 * If we are inserting a mask, and we know that at this point
    512 		 * all bits before the current bit offset match both the target
    513 		 * and the branch.  If the target's mask length is LEQ than
    514 		 * this branch's bit offset, then this is where the mask needs
    515 		 * to added to the tree.
    516 		 */
    517 		if (__predict_false(inserting_mask)
    518 		    && (PTN_ISROOT_P(pt, id.id_parent)
    519 			|| id.id_bitoff < target_masklen)
    520 		    && target_masklen <= branch_bitoff) {
    521 			/*
    522 			 * We don't know about the bits (if any) between
    523 			 * id.id_bitoff and the target's mask length match
    524 			 * both the target and the branch.  If the target's
    525 			 * mask length is greater than the current bit offset
    526 			 * make sure the untested bits match both the target
    527 			 * and the branch.
    528 			 */
    529 			if (target_masklen == id.id_bitoff
    530 			    || ptree_matchnode(pt, target, ptn, target_masklen,
    531 				    &id.id_bitoff, &id.id_slot)) {
    532 				/*
    533 				 * The bits matched, so insert the mask as a
    534 				 * one-way branch.
    535 				 */
    536 				insertfunc = ptree_insert_mask_before_node;
    537 				break;
    538 			} else if (id.id_bitoff < branch_bitoff) {
    539 				/*
    540 				 * They didn't match, so create a normal branch
    541 				 * because this mask needs to a be a new leaf.
    542 				 */
    543 				insertfunc = ptree_insert_branch_at_node;
    544 				break;
    545 			}
    546 		}
    547 #endif /* PTNOMASK */
    548 
    549 		/*
    550 		 * If we are skipping some bits, verify they match the node.
    551 		 * If they don't match, it means we have a leaf to insert.
    552 		 * Note that if we are advancing bit by bit, we'll skip
    553 		 * doing matchnode and walk the tree bit by bit via testnode.
    554 		 */
    555 		if (id.id_bitoff < branch_bitoff
    556 		    && !ptree_matchnode(pt, target, ptn, branch_bitoff,
    557 					&id.id_bitoff, &id.id_slot)) {
    558 			KASSERT(id.id_bitoff < branch_bitoff);
    559 			insertfunc = ptree_insert_branch_at_node;
    560 			break;
    561 		}
    562 
    563 		/*
    564 		 * At this point, all bits before branch_bitoff are known
    565 		 * to match the target.
    566 		 */
    567 		KASSERT(id.id_bitoff >= branch_bitoff);
    568 
    569 		/*
    570 		 * Decend the tree one level.
    571 		 */
    572 		id.id_parent = ptn;
    573 		id.id_parent_slot = ptree_testnode(pt, target, id.id_parent);
    574 		id.id_bitoff += PTN_BRANCH_BITLEN(id.id_parent);
    575 		id.id_insertp = &PTN_BRANCH_SLOT(id.id_parent, id.id_parent_slot);
    576 	}
    577 
    578 	/*
    579 	 * Do the actual insertion.
    580 	 */
    581 	return (*insertfunc)(pt, target, &id);
    582 }
    583 
    584 bool
    585 ptree_insert_node(pt_tree_t *pt, void *item)
    586 {
    587 	pt_node_t * const target = ITEMTONODE(pt, item);
    588 
    589 	memset(target, 0, sizeof(*target));
    590 	return ptree_insert_node_common(pt, target);
    591 }
    592 
    593 #ifndef PTNOMASK
    594 bool
    595 ptree_insert_mask_node(pt_tree_t *pt, void *item, pt_bitlen_t mask_len)
    596 {
    597 	pt_node_t * const target = ITEMTONODE(pt, item);
    598 	pt_bitoff_t bitoff = mask_len;
    599 	pt_slot_t slot;
    600 
    601 	memset(target, 0, sizeof(*target));
    602 	KASSERT(mask_len == 0 || (~PT__MASK(PTN_MASK_BITLEN) & mask_len) == 0);
    603 	/*
    604 	 * Only the first <mask_len> bits can be non-zero.
    605 	 * All other bits must be 0.
    606 	 */
    607 	if (!ptree_matchnode(pt, target, NULL, UINT_MAX, &bitoff, &slot))
    608 		return false;
    609 	PTN_SET_MASK_BITLEN(target, mask_len);
    610 	PTN_MARK_MASK(target);
    611 	return ptree_insert_node_common(pt, target);
    612 }
    613 #endif /* !PTNOMASH */
    614 
    615 void *
    616 ptree_find_filtered_node(pt_tree_t *pt, void *key, pt_filter_t filter,
    617 	void *filter_arg)
    618 {
    619 #ifndef PTNOMASK
    620 	pt_node_t *mask = NULL;
    621 #endif
    622 	bool at_mask = false;
    623 	pt_node_t *ptn, *parent;
    624 	pt_bitoff_t bitoff;
    625 	pt_slot_t parent_slot;
    626 
    627 	if (PT_NULL_P(PTN_BRANCH_ROOT_SLOT(&pt->pt_rootnode)))
    628 		return NULL;
    629 
    630 	bitoff = 0;
    631 	parent = &pt->pt_rootnode;
    632 	parent_slot = PT_SLOT_ROOT;
    633 	for (;;) {
    634 		const uintptr_t node = PTN_BRANCH_SLOT(parent, parent_slot);
    635 		const pt_slot_t branch_bitoff = PTN_BRANCH_BITOFF(PT_NODE(node));
    636 		ptn = PT_NODE(node);
    637 
    638 		if (PT_LEAF_P(node)) {
    639 #ifndef PTNOMASK
    640 			at_mask = PTN_ISMASK_P(ptn);
    641 #endif
    642 			break;
    643 		}
    644 
    645 		if (bitoff < branch_bitoff) {
    646 			if (!ptree_matchkey(pt, key, ptn, bitoff, branch_bitoff - bitoff)) {
    647 #ifndef PTNOMASK
    648 				if (mask != NULL)
    649 					return NODETOITEM(pt, mask);
    650 #endif
    651 				return NULL;
    652 			}
    653 			bitoff = branch_bitoff;
    654 		}
    655 
    656 #ifndef PTNOMASK
    657 		if (PTN_ISMASK_P(ptn) && PTN_BRANCH_BITLEN(ptn) == 0
    658 		    && (!filter
    659 		        || (*filter)(filter_arg, NODETOITEM(pt, ptn),
    660 				     PT_FILTER_MASK)))
    661 			mask = ptn;
    662 #endif
    663 
    664 		parent = ptn;
    665 		parent_slot = ptree_testkey(pt, key, parent);
    666 		bitoff += PTN_BRANCH_BITLEN(parent);
    667 	}
    668 
    669 	KASSERT(PTN_ISROOT_P(pt, parent) || PTN_BRANCH_BITOFF(parent) + PTN_BRANCH_BITLEN(parent) == bitoff);
    670 	if (!filter || (*filter)(filter_arg, NODETOITEM(pt, ptn), at_mask ? PT_FILTER_MASK : 0)) {
    671 #ifndef PTNOMASK
    672 		if (PTN_ISMASK_P(ptn)) {
    673 			const pt_bitlen_t mask_len = PTN_MASK_BITLEN(ptn);
    674 			if (bitoff == PTN_MASK_BITLEN(ptn))
    675 				return NODETOITEM(pt, ptn);
    676 			if (ptree_matchkey(pt, key, ptn, bitoff, mask_len - bitoff))
    677 				return NODETOITEM(pt, ptn);
    678 		} else
    679 #endif /* !PTNOMASK */
    680 		if (ptree_matchkey(pt, key, ptn, bitoff, UINT_MAX))
    681 			return NODETOITEM(pt, ptn);
    682 	}
    683 
    684 #ifndef PTNOMASK
    685 	/*
    686 	 * By virtue of how the mask was placed in the tree,
    687 	 * all nodes descended from it will match it.  But the bits
    688 	 * before the mask still need to be checked and since the
    689 	 * mask was a branch, that was done implicitly.
    690 	 */
    691 	if (mask != NULL) {
    692 		KASSERT(ptree_matchkey(pt, key, mask, 0, PTN_MASK_BITLEN(mask)));
    693 		return NODETOITEM(pt, mask);
    694 	}
    695 #endif /* !PTNOMASK */
    696 
    697 	/*
    698 	 * Nothing matched.
    699 	 */
    700 	return NULL;
    701 }
    702 
    703 void *
    704 ptree_iterate(pt_tree_t *pt, const void *item, pt_direction_t direction)
    705 {
    706 	const pt_node_t * const target = ITEMTONODE(pt, item);
    707 	uintptr_t node, next_node;
    708 
    709 	if (direction != PT_ASCENDING && direction != PT_DESCENDING)
    710 		return NULL;
    711 
    712 	node = PTN_BRANCH_ROOT_SLOT(&pt->pt_rootnode);
    713 	if (PT_NULL_P(node))
    714 		return NULL;
    715 
    716 	if (item == NULL) {
    717 		pt_node_t * const ptn = PT_NODE(node);
    718 		if (direction == PT_ASCENDING
    719 		    && PTN_ISMASK_P(ptn) && PTN_BRANCH_BITLEN(ptn) == 0)
    720 			return NODETOITEM(pt, ptn);
    721 		next_node = node;
    722 	} else {
    723 #ifndef PTNOMASK
    724 		uintptr_t mask_node = PT_NULL;
    725 #endif /* !PTNOMASK */
    726 		next_node = PT_NULL;
    727 		while (!PT_LEAF_P(node)) {
    728 			pt_node_t * const ptn = PT_NODE(node);
    729 			pt_slot_t slot;
    730 #ifndef PTNOMASK
    731 			if (PTN_ISMASK_P(ptn) && PTN_BRANCH_BITLEN(ptn) == 0) {
    732 				if (ptn == target)
    733 					break;
    734 				if (direction == PT_DESCENDING) {
    735 					mask_node = node;
    736 					next_node = PT_NULL;
    737 				}
    738 			}
    739 #endif /* !PTNOMASK */
    740 			slot = ptree_testnode(pt, target, ptn);
    741 			node = PTN_BRANCH_SLOT(ptn, slot);
    742 			if (direction == PT_ASCENDING) {
    743 				if (slot != (1 << PTN_BRANCH_BITLEN(ptn)) - 1)
    744 					next_node = PTN_BRANCH_SLOT(ptn, slot + 1);
    745 			} else {
    746 				if (slot > 0) {
    747 #ifndef PTNOMASK
    748 					mask_node = PT_NULL;
    749 #endif /* !PTNOMASK */
    750 					next_node = PTN_BRANCH_SLOT(ptn, slot - 1);
    751 				}
    752 			}
    753 		}
    754 		if (PT_NODE(node) != target)
    755 			return NULL;
    756 #ifndef PTNOMASK
    757 		if (PT_BRANCH_P(node)) {
    758 			pt_node_t *ptn = PT_NODE(node);
    759 			KASSERT(PTN_ISMASK_P(PT_NODE(node)) && PTN_BRANCH_BITLEN(PT_NODE(node)) == 0);
    760 			if (direction == PT_ASCENDING) {
    761 				next_node = PTN_BRANCH_ROOT_SLOT(ptn);
    762 				ptn = PT_NODE(next_node);
    763 			}
    764 		}
    765 		/*
    766 		 * When descending, if we countered a mask node then that's
    767 		 * we want to return.
    768 		 */
    769 		if (direction == PT_DESCENDING && !PT_NULL_P(mask_node)) {
    770 			KASSERT(PT_NULL_P(next_node));
    771 			return NODETOITEM(pt, PT_NODE(mask_node));
    772 		}
    773 #endif /* !PTNOMASK */
    774 	}
    775 
    776 	node = next_node;
    777 	if (PT_NULL_P(node))
    778 		return NULL;
    779 
    780 	while (!PT_LEAF_P(node)) {
    781 		pt_node_t * const ptn = PT_NODE(node);
    782 		pt_slot_t slot;
    783 		if (direction == PT_ASCENDING) {
    784 #ifndef PTNOMASK
    785 			if (PT_BRANCH_P(node)
    786 			    && PTN_ISMASK_P(ptn)
    787 			    && PTN_BRANCH_BITLEN(ptn) == 0)
    788 				return NODETOITEM(pt, ptn);
    789 #endif /* !PTNOMASK */
    790 			slot = PT_SLOT_LEFT;
    791 		} else {
    792 			slot = (1 << PTN_BRANCH_BITLEN(ptn)) - 1;
    793 		}
    794 		node = PTN_BRANCH_SLOT(ptn, slot);
    795 	}
    796 	return NODETOITEM(pt, PT_NODE(node));
    797 }
    798 
    799 void
    800 ptree_remove_node(pt_tree_t *pt, void *item)
    801 {
    802 	pt_node_t * const target = ITEMTONODE(pt, item);
    803 	const pt_slot_t leaf_slot = PTN_LEAF_POSITION(target);
    804 	const pt_slot_t branch_slot = PTN_BRANCH_POSITION(target);
    805 	pt_node_t *ptn, *parent;
    806 	uintptr_t node;
    807 	uintptr_t *removep;
    808 	uintptr_t *nodep;
    809 	pt_bitoff_t bitoff;
    810 	pt_slot_t parent_slot;
    811 #ifndef PTNOMASK
    812 	bool at_mask;
    813 #endif
    814 
    815 	if (PT_NULL_P(PTN_BRANCH_ROOT_SLOT(&pt->pt_rootnode))) {
    816 		KASSERT(!PT_NULL_P(PTN_BRANCH_ROOT_SLOT(&pt->pt_rootnode)));
    817 		return;
    818 	}
    819 
    820 	bitoff = 0;
    821 	removep = NULL;
    822 	nodep = NULL;
    823 	parent = &pt->pt_rootnode;
    824 	parent_slot = PT_SLOT_ROOT;
    825 	for (;;) {
    826 		node = PTN_BRANCH_SLOT(parent, parent_slot);
    827 		ptn = PT_NODE(node);
    828 #ifndef PTNOMASK
    829 		at_mask = PTN_ISMASK_P(ptn);
    830 #endif
    831 
    832 		if (PT_LEAF_P(node))
    833 			break;
    834 
    835 		/*
    836 		 * If we are at the target, then we are looking at its branch
    837 		 * identity.  We need to remember who's pointing at it so we
    838 		 * stop them from doing that.
    839 		 */
    840 		if (__predict_false(ptn == target)) {
    841 			KASSERT(nodep == NULL);
    842 #ifndef PTNOMASK
    843 			/*
    844 			 * Interior mask nodes are trivial to get rid of.
    845 			 */
    846 			if (at_mask && PTN_BRANCH_BITLEN(ptn) == 0) {
    847 				PTN_BRANCH_SLOT(parent, parent_slot) =
    848 				    PTN_BRANCH_ROOT_SLOT(ptn);
    849 				KASSERT(PT_NULL_P(PTN_BRANCH_ODDMAN_SLOT(ptn)));
    850 				PTREE_CHECK(pt);
    851 				return;
    852 			}
    853 #endif /* !PTNOMASK */
    854 			nodep = &PTN_BRANCH_SLOT(parent, parent_slot);
    855 			KASSERT(*nodep == PTN_BRANCH(target));
    856 		}
    857 		/*
    858 		 * We need also need to know who's pointing at our parent.
    859 		 * After we remove ourselves from our parent, he'll only
    860 		 * have one child and that's unacceptable.  So we replace
    861 		 * the pointer to the parent with our abadoned sibling.
    862 		 */
    863 		removep = &PTN_BRANCH_SLOT(parent, parent_slot);
    864 
    865 		/*
    866 		 * Descend into the tree.
    867 		 */
    868 		parent = ptn;
    869 		parent_slot = ptree_testnode(pt, target, parent);
    870 		bitoff += PTN_BRANCH_BITLEN(parent);
    871 	}
    872 
    873 	/*
    874 	 * We better have found that the leaf we are looking for is target.
    875 	 */
    876 	if (target != ptn) {
    877 		KASSERT(target == ptn);
    878 		return;
    879 	}
    880 
    881 	/*
    882 	 * If we didn't encounter target as branch, then target must be the
    883 	 * oddman-out.
    884 	 */
    885 	if (nodep == NULL) {
    886 		KASSERT(PTN_BRANCH_ODDMAN_SLOT(&pt->pt_rootnode) == PTN_LEAF(target));
    887 		KASSERT(nodep == NULL);
    888 		nodep = &PTN_BRANCH_ODDMAN_SLOT(&pt->pt_rootnode);
    889 	}
    890 
    891 	KASSERT((removep == NULL) == (parent == &pt->pt_rootnode));
    892 
    893 	/*
    894 	 * We have to special remove the last leaf from the root since
    895 	 * the only time the tree can a PT_NULL node is when it's empty.
    896 	 */
    897 	if (__predict_false(PTN_ISROOT_P(pt, parent))) {
    898 		KASSERT(removep == NULL);
    899 		KASSERT(parent == &pt->pt_rootnode);
    900 		KASSERT(nodep == &PTN_BRANCH_ODDMAN_SLOT(&pt->pt_rootnode));
    901 		KASSERT(*nodep == PTN_LEAF(target));
    902 		PTN_BRANCH_ROOT_SLOT(&pt->pt_rootnode) = PT_NULL;
    903 		PTN_BRANCH_ODDMAN_SLOT(&pt->pt_rootnode) = PT_NULL;
    904 		return;
    905 	}
    906 
    907 	KASSERT((parent == target) == (removep == nodep));
    908 	if (PTN_BRANCH(parent) == PTN_BRANCH_SLOT(target, PTN_BRANCH_POSITION(parent))) {
    909 		/*
    910 		 * The pointer to the parent actually lives in the target's
    911 		 * branch identity.  We can't just move the target's branch
    912 		 * identity since that would result in the parent pointing
    913 		 * to its own branch identity and that's fobidden.
    914 		 */
    915 		const pt_slot_t slot = PTN_BRANCH_POSITION(parent);
    916 		const pt_slot_t other_slot = slot ^ PT_SLOT_OTHER;
    917 		const pt_bitlen_t parent_bitlen = PTN_BRANCH_BITLEN(parent);
    918 
    919 		KASSERT(PTN_BRANCH_BITOFF(target) < PTN_BRANCH_BITOFF(parent));
    920 
    921 		/*
    922 		 * This gets so confusing.  The target's branch identity
    923 		 * points to the branch identity of the parent of the target's
    924 		 * leaf identity:
    925 		 *
    926 		 * 	TB = { X, PB = { TL, Y } }
    927 		 *   or TB = { X, PB = { TL } }
    928 		 *
    929 		 * So we can't move the target's branch identity to the parent
    930 		 * because that would corrupt the tree.
    931 		 */
    932 		if (__predict_true(parent_bitlen > 0)) {
    933 			/*
    934 			 * The parent is a two-way branch.  We have to have
    935 			 * do to this chang in two steps to keep internally
    936 			 * consistent.  First step is to copy our sibling from
    937 			 * our parent to where we are pointing to parent's
    938 			 * branch identiy.  This remove all references to his
    939 			 * branch identity from the tree.  We then simply make
    940 			 * the parent assume the target's branching duties.
    941 			 *
    942 			 *   TB = { X, PB = { Y, TL } } --> PB = { X, Y }.
    943 			 *   TB = { X, PB = { TL, Y } } --> PB = { X, Y }.
    944 			 *   TB = { PB = { Y, TL }, X } --> PB = { Y, X }.
    945 			 *   TB = { PB = { TL, Y }, X } --> PB = { Y, X }.
    946 			 */
    947 			PTN_BRANCH_SLOT(target, slot) =
    948 			    PTN_BRANCH_SLOT(parent, parent_slot ^ PT_SLOT_OTHER);
    949 			*nodep = ptree_move_branch(pt, parent, target);
    950 			PTREE_CHECK(pt);
    951 			return;
    952 		} else {
    953 			/*
    954 			 * If parent was a one-way branch, it must have been
    955 			 * mask which pointed to a single leaf which we are
    956 			 * removing.  This means we have to convert the
    957 			 * parent back to a leaf node.  So in the same
    958 			 * position that target pointed to parent, we place
    959 			 * leaf pointer to parent.  In the other position,
    960 			 * we just put the other node from target.
    961 			 *
    962 			 *   TB = { X, PB = { TL } } --> PB = { X, PL }
    963 			 */
    964 			KASSERT(PTN_ISMASK_P(parent));
    965 			KASSERT(slot == ptree_testnode(pt, parent, target));
    966 			PTN_BRANCH_SLOT(parent, slot) = PTN_LEAF(parent);
    967 			PTN_BRANCH_SLOT(parent, other_slot) =
    968 			   PTN_BRANCH_SLOT(target, other_slot);
    969 			PTN_SET_LEAF_POSITION(parent,slot);
    970 			PTN_SET_BRANCH_BITLEN(parent, 1);
    971 		}
    972 		PTN_SET_BRANCH_BITOFF(parent, PTN_BRANCH_BITOFF(target));
    973 		PTN_SET_BRANCH_POSITION(parent, PTN_BRANCH_POSITION(target));
    974 
    975 		*nodep = PTN_BRANCH(parent);
    976 		PTREE_CHECK(pt);
    977 		return;
    978 	}
    979 
    980 #ifndef PTNOMASK
    981 	if (__predict_false(PTN_BRANCH_BITLEN(parent) == 0)) {
    982 		/*
    983 		 * Parent was a one-way branch which is changing back to a leaf.
    984 		 * Since parent is no longer a one-way branch, it can take over
    985 		 * target's branching duties.
    986 		 *
    987 		 *  GB = { PB = { TL } }	--> GB = { PL }
    988 		 *  TB = { X, Y }		--> PB = { X, Y }
    989 		 */
    990 		KASSERT(PTN_ISMASK_P(parent));
    991 		KASSERT(parent != target);
    992 		*removep = PTN_LEAF(parent);
    993 	} else
    994 #endif /* !PTNOMASK */
    995 	{
    996 		/*
    997 		 * Now we are the normal removal case.  Since after the
    998 		 * target's leaf identity is removed from the its parent,
    999 		 * that parent will only have one decendent.  So we can
   1000 		 * just as easily replace the node that has the parent's
   1001 		 * branch identity with the surviving node.  This freeing
   1002 		 * parent from its branching duties which means it can
   1003 		 * take over target's branching duties.
   1004 		 *
   1005 		 *  GB = { PB = { X, TL } }	--> GB = { X }
   1006 		 *  TB = { V, W }		--> PB = { V, W }
   1007 		 */
   1008 		const pt_slot_t other_slot = parent_slot ^ PT_SLOT_OTHER;
   1009 		uintptr_t other_node = PTN_BRANCH_SLOT(parent, other_slot);
   1010 		const pt_slot_t target_slot = (parent == target ? branch_slot : leaf_slot);
   1011 
   1012 		*removep = other_node;
   1013 
   1014 		ptree_set_position(other_node, target_slot);
   1015 
   1016 		/*
   1017 		 * If target's branch identity contained its leaf identity, we
   1018 		 * have nothing left to do.  We've already moved 'X' so there
   1019 		 * is no longer anything in the target's branch identiy that
   1020 		 * has to be preserved.
   1021 		 */
   1022 		if (parent == target) {
   1023 			/*
   1024 			 *  GB = { TB = { X, TL } }	--> GB = { X }
   1025 			 *  TB = { X, TL }		--> don't care
   1026 			 */
   1027 			PTREE_CHECK(pt);
   1028 			return;
   1029 		}
   1030 	}
   1031 
   1032 	/*
   1033 	 * If target wasn't used as a branch, then it must have been the
   1034 	 * oddman-out of the tree (the one node that doesn't have a branch
   1035 	 * identity).  This makes parent the new oddman-out.
   1036 	 */
   1037 	if (*nodep == PTN_LEAF(target)) {
   1038 		KASSERT(nodep == &PTN_BRANCH_ODDMAN_SLOT(&pt->pt_rootnode));
   1039 		PTN_BRANCH_ODDMAN_SLOT(&pt->pt_rootnode) = PTN_LEAF(parent);
   1040 		PTREE_CHECK(pt);
   1041 		return;
   1042 	}
   1043 
   1044 	/*
   1045 	 * Finally move the target's branching duties to the parent.
   1046 	 */
   1047 	KASSERT(PTN_BRANCH_BITOFF(parent) > PTN_BRANCH_BITOFF(target));
   1048 	*nodep = ptree_move_branch(pt, parent, target);
   1049 	PTREE_CHECK(pt);
   1050 }
   1051 
   1052 #ifdef PTCHECK
   1053 static const pt_node_t *
   1054 ptree_check_find_node2(const pt_tree_t *pt, const pt_node_t *parent,
   1055 	uintptr_t target)
   1056 {
   1057 	const pt_bitlen_t slots = 1 << PTN_BRANCH_BITLEN(parent);
   1058 	pt_slot_t slot;
   1059 
   1060 	for (slot = 0; slot < slots; slot++) {
   1061 		const uintptr_t node = PTN_BRANCH_SLOT(parent, slot);
   1062 		if (PTN_BRANCH_SLOT(parent, slot) == node)
   1063 			return parent;
   1064 	}
   1065 	for (slot = 0; slot < slots; slot++) {
   1066 		const uintptr_t node = PTN_BRANCH_SLOT(parent, slot);
   1067 		const pt_node_t *branch;
   1068 		if (!PT_BRANCH_P(node))
   1069 			continue;
   1070 		branch = ptree_check_find_node2(pt, PT_NODE(node), target);
   1071 		if (branch != NULL)
   1072 			return branch;
   1073 	}
   1074 
   1075 	return NULL;
   1076 }
   1077 
   1078 static bool
   1079 ptree_check_leaf(const pt_tree_t *pt, const pt_node_t *parent,
   1080 	const pt_node_t *ptn)
   1081 {
   1082 	const pt_bitoff_t leaf_position = PTN_LEAF_POSITION(ptn);
   1083 	const pt_bitlen_t bitlen = PTN_BRANCH_BITLEN(ptn);
   1084 	const pt_bitlen_t mask_len = PTN_MASK_BITLEN(ptn);
   1085 	const uintptr_t leaf_node = PTN_LEAF(ptn);
   1086 	const bool is_parent_root = (parent == &pt->pt_rootnode);
   1087 	const bool is_mask = PTN_ISMASK_P(ptn);
   1088 	bool ok = true;
   1089 
   1090 	if (is_parent_root) {
   1091 		ok = ok && PTN_BRANCH_ODDMAN_SLOT(parent) == leaf_node;
   1092 		KASSERT(ok);
   1093 		return ok;
   1094 	}
   1095 
   1096 	if (is_mask && PTN_ISMASK_P(parent) && PTN_BRANCH_BITLEN(parent) == 0) {
   1097 		ok = ok && PTN_MASK_BITLEN(parent) < mask_len;
   1098 		KASSERT(ok);
   1099 		ok = ok && PTN_BRANCH_BITOFF(parent) < mask_len;
   1100 		KASSERT(ok);
   1101 	}
   1102 	ok = ok && PTN_BRANCH_SLOT(parent, leaf_position) == leaf_node;
   1103 	KASSERT(ok);
   1104 	ok = ok && leaf_position == ptree_testnode(pt, ptn, parent);
   1105 	KASSERT(ok);
   1106 	if (PTN_BRANCH_ODDMAN_SLOT(&pt->pt_rootnode) != leaf_node) {
   1107 		ok = ok && bitlen > 0;
   1108 		KASSERT(ok);
   1109 		ok = ok && ptn == ptree_check_find_node2(pt, ptn, PTN_LEAF(ptn));
   1110 		KASSERT(ok);
   1111 	}
   1112 	return ok;
   1113 }
   1114 
   1115 static bool
   1116 ptree_check_branch(const pt_tree_t *pt, const pt_node_t *parent,
   1117 	const pt_node_t *ptn)
   1118 {
   1119 	const bool is_parent_root = (parent == &pt->pt_rootnode);
   1120 	const pt_slot_t branch_slot = PTN_BRANCH_POSITION(ptn);
   1121 	const pt_bitoff_t bitoff = PTN_BRANCH_BITOFF(ptn);
   1122 	const pt_bitoff_t bitlen = PTN_BRANCH_BITLEN(ptn);
   1123 	const pt_bitoff_t parent_bitoff = PTN_BRANCH_BITOFF(parent);
   1124 	const pt_bitoff_t parent_bitlen = PTN_BRANCH_BITLEN(parent);
   1125 	const bool is_parent_mask = PTN_ISMASK_P(parent) && parent_bitlen == 0;
   1126 	const bool is_mask = PTN_ISMASK_P(ptn) && bitlen == 0;
   1127 	const pt_bitoff_t parent_mask_len = PTN_MASK_BITLEN(parent);
   1128 	const pt_bitoff_t mask_len = PTN_MASK_BITLEN(ptn);
   1129 	const pt_bitlen_t slots = 1 << bitlen;
   1130 	pt_slot_t slot;
   1131 	bool ok = true;
   1132 
   1133 	ok = ok && PTN_BRANCH_SLOT(parent, branch_slot) == PTN_BRANCH(ptn);
   1134 	KASSERT(ok);
   1135 	ok = ok && branch_slot == ptree_testnode(pt, ptn, parent);
   1136 	KASSERT(ok);
   1137 
   1138 	if (is_mask) {
   1139 		ok = ok && bitoff == mask_len;
   1140 		KASSERT(ok);
   1141 		if (is_parent_mask) {
   1142 			ok = ok && parent_mask_len < mask_len;
   1143 			KASSERT(ok);
   1144 			ok = ok && parent_bitoff < bitoff;
   1145 			KASSERT(ok);
   1146 		}
   1147 	} else {
   1148 		if (is_parent_mask) {
   1149 			ok = ok && parent_bitoff <= bitoff;
   1150 		} else if (!is_parent_root) {
   1151 			ok = ok && parent_bitoff < bitoff;
   1152 		}
   1153 		KASSERT(ok);
   1154 	}
   1155 
   1156 	for (slot = 0; slot < slots; slot++) {
   1157 		const uintptr_t node = PTN_BRANCH_SLOT(ptn, slot);
   1158 		pt_bitoff_t tmp_bitoff = 0;
   1159 		pt_slot_t tmp_slot;
   1160 		ok = ok && node != PTN_BRANCH(ptn);
   1161 		KASSERT(ok);
   1162 		if (bitlen > 0) {
   1163 			ok = ok && ptree_matchnode(pt, PT_NODE(node), ptn, bitoff, &tmp_bitoff, &tmp_slot);
   1164 			KASSERT(ok);
   1165 			tmp_slot = ptree_testnode(pt, PT_NODE(node), ptn);
   1166 			ok = ok && slot == tmp_slot;
   1167 			KASSERT(ok);
   1168 		}
   1169 		if (PT_LEAF_P(node))
   1170 			ok = ok && ptree_check_leaf(pt, ptn, PT_NODE(node));
   1171 		else
   1172 			ok = ok && ptree_check_branch(pt, ptn, PT_NODE(node));
   1173 	}
   1174 
   1175 	return ok;
   1176 }
   1177 #endif /* PTCHECK */
   1178 
   1179 /*ARGSUSED*/
   1180 bool
   1181 ptree_check(const pt_tree_t *pt)
   1182 {
   1183 	bool ok = true;
   1184 #ifdef PTCHECK
   1185 	const pt_node_t * const parent = &pt->pt_rootnode;
   1186 	const uintptr_t node = pt->pt_root;
   1187 	const pt_node_t * const ptn = PT_NODE(node);
   1188 
   1189 	ok = ok && PTN_BRANCH_BITOFF(parent) == 0;
   1190 	ok = ok && !PTN_ISMASK_P(parent);
   1191 
   1192 	if (PT_NULL_P(node))
   1193 		return ok;
   1194 
   1195 	if (PT_LEAF_P(node))
   1196 		ok = ok && ptree_check_leaf(pt, parent, ptn);
   1197 	else
   1198 		ok = ok && ptree_check_branch(pt, parent, ptn);
   1199 #endif
   1200 	return ok;
   1201 }
   1202