Home | History | Annotate | Line # | Download | only in gen
ptree.c revision 1.1
      1 /* $NetBSD: ptree.c,v 1.1 2008/11/20 23:50:08 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.1 2008/11/20 23:50:08 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.1 2008/11/20 23:50:08 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)		(void) 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 static uintptr_t
    193 ptree_move_branch(pt_tree_t * const pt, pt_node_t * const dst,
    194 	const pt_node_t * const src)
    195 {
    196 	KASSERT(PTN_BRANCH_BITLEN(src) == 1);
    197 	/* set branch bitlen and bitoff in one step.  */
    198 	dst->ptn_branchdata = src->ptn_branchdata;
    199 	PTN_SET_BRANCH_POSITION(dst, PTN_BRANCH_POSITION(src));
    200 	PTN_COPY_BRANCH_SLOTS(dst, src);
    201 	return PTN_BRANCH(dst);
    202 }
    203 
    204 #ifndef PTNOMASK
    205 static inline uintptr_t *
    206 ptree_find_branch(pt_tree_t * const pt, uintptr_t branch_node)
    207 {
    208 	pt_node_t * const branch = PT_NODE(branch_node);
    209 	pt_node_t *parent;
    210 
    211 	for (parent = &pt->pt_rootnode;;) {
    212 		uintptr_t *nodep =
    213 		    &PTN_BRANCH_SLOT(parent, ptree_testnode(pt, branch, parent));
    214 		if (*nodep == branch_node)
    215 			return nodep;
    216 		if (PT_LEAF_P(*nodep))
    217 			return NULL;
    218 		parent = PT_NODE(*nodep);
    219 	}
    220 }
    221 
    222 static bool
    223 ptree_insert_leaf_after_mask(pt_tree_t * const pt, pt_node_t * const target,
    224 	pt_insertdata_t * const id)
    225 {
    226 	const uintptr_t target_node = PTN_LEAF(target);
    227 	const uintptr_t mask_node = id->id_node;
    228 	pt_node_t * const mask = PT_NODE(mask_node);
    229 	const pt_bitlen_t mask_len = PTN_MASK_BITLEN(mask);
    230 
    231 	KASSERT(PT_LEAF_P(mask_node));
    232 	KASSERT(PTN_LEAF_POSITION(mask) == id->id_parent_slot);
    233 	KASSERT(mask_len <= id->id_bitoff);
    234 	KASSERT(PTN_ISMASK_P(mask));
    235 	KASSERT(!PTN_ISMASK_P(target) || mask_len < PTN_MASK_BITLEN(target));
    236 
    237 	if (mask_node == PTN_BRANCH_ODDMAN_SLOT(&pt->pt_rootnode)) {
    238 		KASSERT(id->id_parent != mask);
    239 		/*
    240 		 * Nice, mask was an oddman.  So just set the oddman to target.
    241 		 */
    242 		PTN_BRANCH_ODDMAN_SLOT(&pt->pt_rootnode) = target_node;
    243 	} else {
    244 		/*
    245 		 * We need to find out who's pointing to mask's branch
    246 		 * identity.  We know that between root and the leaf identity,
    247 		 * we must traverse the node's branch identity.
    248 		 */
    249 		uintptr_t * const mask_nodep = ptree_find_branch(pt, PTN_BRANCH(mask));
    250 		KASSERT(mask_nodep != NULL);
    251 		KASSERT(*mask_nodep == PTN_BRANCH(mask));
    252 		KASSERT(PTN_BRANCH_BITLEN(mask) == 1);
    253 
    254 		/*
    255 		 * Alas, mask was used as a branch.  Since the mask is becoming
    256 		 * a one-way branch, we need make target take over mask's
    257 		 * branching responsibilities.  Only then can we change it.
    258 		 */
    259 		*mask_nodep = ptree_move_branch(pt, target, mask);
    260 
    261 		/*
    262 		 * However, it's possible that mask's parent is itself.  If
    263 		 * that's true, update the insert point to use target since it
    264 		 * has taken over mask's branching duties.
    265 		 */
    266 		if (id->id_parent == mask)
    267 			id->id_insertp = &PTN_BRANCH_SLOT(target,
    268 			    id->id_parent_slot);
    269 	}
    270 
    271 	PTN_SET_BRANCH_BITLEN(mask, 0);
    272 	PTN_SET_BRANCH_BITOFF(mask, mask_len);
    273 
    274 	PTN_BRANCH_ROOT_SLOT(mask) = target_node;
    275 	PTN_BRANCH_ODDMAN_SLOT(mask) = PT_NULL;
    276 	PTN_SET_LEAF_POSITION(target, PT_SLOT_ROOT);
    277 	PTN_SET_BRANCH_POSITION(mask, id->id_parent_slot);
    278 
    279 	/*
    280 	 * Now that everything is done, to make target visible we need to
    281 	 * change mask from a leaf to a branch.
    282 	 */
    283 	*id->id_insertp = PTN_BRANCH(mask);
    284 	PTREE_CHECK(pt);
    285 	return true;
    286 }
    287 
    288 static bool
    289 ptree_insert_mask_before_node(pt_tree_t * const pt, pt_node_t * const target,
    290 	pt_insertdata_t * const id)
    291 {
    292 	const uintptr_t node = id->id_node;
    293 	pt_node_t * const ptn = PT_NODE(node);
    294 	const pt_slot_t mask_len = PTN_MASK_BITLEN(target);
    295 	const pt_bitlen_t node_mask_len = PTN_MASK_BITLEN(ptn);
    296 
    297 	KASSERT(PT_LEAF_P(node) || id->id_parent_slot == PTN_BRANCH_POSITION(ptn));
    298 	KASSERT(PT_BRANCH_P(node) || id->id_parent_slot == PTN_LEAF_POSITION(ptn));
    299 	KASSERT(PTN_ISMASK_P(target));
    300 
    301 	/*
    302 	 * If the node we are placing ourself in front is a mask with the
    303 	 * same mask length as us, return failure.
    304 	 */
    305 	if (PTN_ISMASK_P(ptn) && node_mask_len == mask_len)
    306 		return false;
    307 
    308 	PTN_SET_BRANCH_BITLEN(target, 0);
    309 	PTN_SET_BRANCH_BITOFF(target, mask_len);
    310 
    311 	PTN_BRANCH_SLOT(target, PT_SLOT_ROOT) = node;
    312 	*id->id_insertp = PTN_BRANCH(target);
    313 
    314 	PTN_SET_BRANCH_POSITION(target, id->id_parent_slot);
    315 	ptree_set_position(node, PT_SLOT_ROOT);
    316 
    317 	PTREE_CHECK(pt);
    318 	return true;
    319 }
    320 #endif /* !PTNOMASK */
    321 
    322 static bool
    323 ptree_insert_branch_at_node(pt_tree_t * const pt, pt_node_t * const target,
    324 	pt_insertdata_t * const id)
    325 {
    326 	const uintptr_t target_node = PTN_LEAF(target);
    327 	const uintptr_t node = id->id_node;
    328 	const pt_slot_t other_slot = id->id_slot ^ PT_SLOT_OTHER;
    329 
    330 	KASSERT(PT_BRANCH_P(node) || id->id_parent_slot == PTN_LEAF_POSITION(PT_NODE(node)));
    331 	KASSERT(PT_LEAF_P(node) || id->id_parent_slot == PTN_BRANCH_POSITION(PT_NODE(node)));
    332 	KASSERT((node == pt->pt_root) == (id->id_parent == &pt->pt_rootnode));
    333 #ifndef PTNOMASK
    334 	KASSERT(!PTN_ISMASK_P(target) || id->id_bitoff <= PTN_MASK_BITLEN(target));
    335 #endif
    336 	KASSERT(node == pt->pt_root || PTN_BRANCH_BITOFF(id->id_parent) + PTN_BRANCH_BITLEN(id->id_parent) <= id->id_bitoff);
    337 
    338 	PTN_SET_BRANCH_BITOFF(target, id->id_bitoff);
    339 	PTN_SET_BRANCH_BITLEN(target, 1);
    340 
    341 	PTN_BRANCH_SLOT(target, id->id_slot) = target_node;
    342 	PTN_BRANCH_SLOT(target, other_slot) = node;
    343 	*id->id_insertp = PTN_BRANCH(target);
    344 
    345 	PTN_SET_LEAF_POSITION(target, id->id_slot);
    346 	ptree_set_position(node, other_slot);
    347 
    348 	PTN_SET_BRANCH_POSITION(target, id->id_parent_slot);
    349 	PTREE_CHECK(pt);
    350 	return true;
    351 }
    352 
    353 static bool
    354 ptree_insert_leaf(pt_tree_t * const pt, pt_node_t * const target,
    355 	pt_insertdata_t * const id)
    356 {
    357 	const uintptr_t leaf_node = id->id_node;
    358 	pt_node_t * const leaf = PT_NODE(leaf_node);
    359 #ifdef PTNOMASK
    360 	const bool inserting_mask = false;
    361 	const bool at_mask = false;
    362 #else
    363 	const bool inserting_mask = PTN_ISMASK_P(target);
    364 	const bool at_mask = PTN_ISMASK_P(leaf);
    365 	const pt_bitlen_t leaf_masklen = PTN_MASK_BITLEN(leaf);
    366 	const pt_bitlen_t target_masklen = PTN_MASK_BITLEN(target);
    367 #endif
    368 	pt_insertfunc_t insertfunc = ptree_insert_branch_at_node;
    369 	bool matched;
    370 
    371 	/*
    372 	 * In all likelyhood we are going simply going to insert a branch
    373 	 * where this leaf is which will point to the old and new leaves.
    374 	 */
    375 	KASSERT(PT_LEAF_P(leaf_node));
    376 	KASSERT(PTN_LEAF_POSITION(leaf) == id->id_parent_slot);
    377 	matched = ptree_matchnode(pt, target, leaf, UINT_MAX,
    378 	    &id->id_bitoff, &id->id_slot);
    379 	if (__predict_false(!inserting_mask)) {
    380 		/*
    381 		 * We aren't inserting a mask nor is the leaf a mask, which
    382 		 * means we are trying to insert a duplicate leaf.  Can't do
    383 		 * that.
    384 		 */
    385 		if (!at_mask && matched)
    386 			return false;
    387 
    388 #ifndef PTNOMASK
    389 		/*
    390 		 * We are at a mask and the leaf we are about to insert
    391 		 * is at or beyond the mask, we need to convert the mask
    392 		 * from a leaf to a one-way branch interior mask.
    393 		 */
    394 		if (at_mask && id->id_bitoff >= leaf_masklen)
    395 			insertfunc = ptree_insert_leaf_after_mask;
    396 #endif /* PTNOMASK */
    397 	}
    398 #ifndef PTNOMASK
    399 	else {
    400 		/*
    401 		 * We are inserting a mask.
    402 		 */
    403 		if (matched) {
    404 			/*
    405 			 * If the leaf isn't a mask, we obviously have to
    406 			 * insert the new mask before non-mask leaf.  If the
    407 			 * leaf is a mask, and the new node has a LEQ mask
    408 			 * length it too needs to inserted before leaf (*).
    409 			 *
    410 			 * In other cases, we place the new mask as leaf after
    411 			 * leaf mask.  Which mask comes first will be a one-way
    412 			 * branch interior mask node which has the other mask
    413 			 * node as a child.
    414 			 *
    415 			 * (*) ptree_insert_mask_before_node can detect a
    416 			 * duplicate mask and return failure if needed.
    417 			 */
    418 			if (!at_mask || target_masklen <= leaf_masklen)
    419 				insertfunc = ptree_insert_mask_before_node;
    420 			else
    421 				insertfunc = ptree_insert_leaf_after_mask;
    422 		} else if (at_mask && id->id_bitoff >= leaf_masklen) {
    423 			/*
    424 			 * If the new mask has a bit offset GEQ than the leaf's
    425 			 * mask length, convert the left to a one-way branch
    426 			 * interior mask and make that point to the new [leaf]
    427 			 * mask.
    428 			 */
    429 			insertfunc = ptree_insert_leaf_after_mask;
    430 		} else {
    431 			/*
    432 			 * The new mask has a bit offset less than the leaf's
    433 			 * mask length or if the leaf isn't a mask at all, the
    434 			 * new mask deserves to be its own leaf so we use the
    435 			 * default insertfunc to do that.
    436 			 */
    437 		}
    438 	}
    439 #endif /* PTNOMASK */
    440 
    441 	return (*insertfunc)(pt, target, id);
    442 }
    443 
    444 static bool
    445 ptree_insert_node_common(pt_tree_t *pt, void *item)
    446 {
    447 	pt_node_t * const target = ITEMTONODE(pt, item);
    448 #ifndef PTNOMASK
    449 	const bool inserting_mask = PTN_ISMASK_P(target);
    450 	const pt_bitlen_t target_masklen = PTN_MASK_BITLEN(target);
    451 #endif
    452 	pt_insertfunc_t insertfunc;
    453 	pt_insertdata_t id;
    454 
    455 	/*
    456 	 * We need a leaf so we can match against.  Until we get a leaf
    457 	 * we having nothing to test against.
    458 	 */
    459 	if (__predict_false(PT_NULL_P(pt->pt_root))) {
    460 		PTN_BRANCH_ROOT_SLOT(&pt->pt_rootnode) = PTN_LEAF(target);
    461 		PTN_BRANCH_ODDMAN_SLOT(&pt->pt_rootnode) = PTN_LEAF(target);
    462 		PTN_SET_LEAF_POSITION(target, PT_SLOT_ROOT);
    463 		PTREE_CHECK(pt);
    464 		return true;
    465 	}
    466 
    467 	id.id_bitoff = 0;
    468 	id.id_parent = &pt->pt_rootnode;
    469 	id.id_parent_slot = PT_SLOT_ROOT;
    470 	id.id_insertp = &PTN_BRANCH_ROOT_SLOT(id.id_parent);
    471 	for (;;) {
    472 		pt_bitoff_t branch_bitoff;
    473 		pt_node_t * const ptn = PT_NODE(*id.id_insertp);
    474 		id.id_node = *id.id_insertp;
    475 
    476 		/*
    477 		 * If we hit a leaf, try to insert target at leaf.  We could
    478 		 * have inlined ptree_insert_leaf here but that would have
    479 		 * made this routine much harder to understand.  Trust the
    480 		 * compiler to optimize this properly.
    481 		 */
    482 		if (PT_LEAF_P(id.id_node)) {
    483 			KASSERT(PTN_LEAF_POSITION(ptn) == id.id_parent_slot);
    484 			insertfunc = ptree_insert_leaf;
    485 			break;
    486 		}
    487 
    488 		/*
    489 		 * If we aren't a leaf, we must be a branch.  Make sure we are
    490 		 * in the slot we think we are.
    491 		 */
    492 		KASSERT(PT_BRANCH_P(id.id_node));
    493 		KASSERT(PTN_BRANCH_POSITION(ptn) == id.id_parent_slot);
    494 
    495 		/*
    496 		 * Where is this branch?
    497 		 */
    498 		branch_bitoff = PTN_BRANCH_BITOFF(ptn);
    499 
    500 #ifndef PTNOMASK
    501 		/*
    502 		 * If this is a one-way mask node, its offset must equal
    503 		 * its mask's bitlen.
    504 		 */
    505 		KASSERT(!(PTN_ISMASK_P(ptn) && PTN_BRANCH_BITLEN(ptn) == 0) || PTN_MASK_BITLEN(ptn) == branch_bitoff);
    506 
    507 		/*
    508 		 * If we are inserting a mask, and we know that at this point
    509 		 * all bits before the current bit offset match both the target
    510 		 * and the branch.  If the target's mask length is LEQ than
    511 		 * this branch's bit offset, then this is where the mask needs
    512 		 * to added to the tree.
    513 		 */
    514 		if (__predict_false(inserting_mask)
    515 		    && (PTN_ISROOT_P(pt, id.id_parent)
    516 			|| id.id_bitoff < target_masklen)
    517 		    && target_masklen <= branch_bitoff) {
    518 			/*
    519 			 * We don't know about the bits (if any) between
    520 			 * id.id_bitoff and the target's mask length match
    521 			 * both the target and the branch.  If the target's
    522 			 * mask length is greater than the current bit offset
    523 			 * make sure the untested bits match both the target
    524 			 * and the branch.
    525 			 */
    526 			if (target_masklen == id.id_bitoff
    527 			    || ptree_matchnode(pt, target, ptn, target_masklen,
    528 				    &id.id_bitoff, &id.id_slot)) {
    529 				/*
    530 				 * The bits matched, so insert the mask as a
    531 				 * one-way branch.
    532 				 */
    533 				insertfunc = ptree_insert_mask_before_node;
    534 				break;
    535 			} else if (id.id_bitoff < branch_bitoff) {
    536 				/*
    537 				 * They didn't match, so create a normal branch
    538 				 * because this mask needs to a be a new leaf.
    539 				 */
    540 				insertfunc = ptree_insert_branch_at_node;
    541 				break;
    542 			}
    543 		}
    544 #endif /* PTNOMASK */
    545 
    546 		/*
    547 		 * If we are skipping some bits, verify they match the node.
    548 		 * If they don't match, it means we have a leaf to insert.
    549 		 * Note that if we are advancing bit by bit, we'll skip
    550 		 * doing matchnode and walk the tree bit by bit via testnode.
    551 		 */
    552 		if (id.id_bitoff < branch_bitoff
    553 		    && !ptree_matchnode(pt, target, ptn, branch_bitoff,
    554 					&id.id_bitoff, &id.id_slot)) {
    555 			KASSERT(id.id_bitoff < branch_bitoff);
    556 			insertfunc = ptree_insert_branch_at_node;
    557 			break;
    558 		}
    559 
    560 		/*
    561 		 * At this point, all bits before branch_bitoff are known
    562 		 * to match the target.
    563 		 */
    564 		KASSERT(id.id_bitoff >= branch_bitoff);
    565 
    566 		/*
    567 		 * Decend the tree one level.
    568 		 */
    569 		id.id_parent = ptn;
    570 		id.id_parent_slot = ptree_testnode(pt, target, id.id_parent);
    571 		id.id_bitoff += PTN_BRANCH_BITLEN(id.id_parent);
    572 		id.id_insertp = &PTN_BRANCH_SLOT(id.id_parent, id.id_parent_slot);
    573 	}
    574 
    575 	/*
    576 	 * Do the actual insertion.
    577 	 */
    578 	return (*insertfunc)(pt, target, &id);
    579 }
    580 
    581 bool
    582 ptree_insert_node(pt_tree_t *pt, void *item)
    583 {
    584 	pt_node_t * const target = ITEMTONODE(pt, item);
    585 
    586 	memset(target, 0, sizeof(*target));
    587 	return ptree_insert_node_common(pt, target);
    588 }
    589 
    590 #ifndef PTNOMASK
    591 bool
    592 ptree_insert_mask_node(pt_tree_t *pt, void *item, pt_bitlen_t mask_len)
    593 {
    594 	pt_node_t * const target = ITEMTONODE(pt, item);
    595 	pt_bitoff_t bitoff = mask_len;
    596 	pt_slot_t slot;
    597 
    598 	memset(target, 0, sizeof(*target));
    599 	KASSERT(mask_len == 0 || (~PT__MASK(PTN_MASK_BITLEN) & mask_len) == 0);
    600 	/*
    601 	 * Only the first <mask_len> bits can be non-zero.
    602 	 * All other bits must be 0.
    603 	 */
    604 	if (!ptree_matchnode(pt, target, NULL, UINT_MAX, &bitoff, &slot))
    605 		return false;
    606 	PTN_SET_MASK_BITLEN(target, mask_len);
    607 	PTN_MARK_MASK(target);
    608 	return ptree_insert_node_common(pt, target);
    609 }
    610 #endif /* !PTNOMASH */
    611 
    612 void *
    613 ptree_find_filtered_node(pt_tree_t *pt, void *key, pt_filter_t filter,
    614 	void *filter_arg)
    615 {
    616 #ifndef PTNOMASK
    617 	pt_node_t *mask = NULL;
    618 #endif
    619 	bool at_mask = false;
    620 	pt_node_t *ptn, *parent;
    621 	pt_bitoff_t bitoff;
    622 	pt_slot_t parent_slot;
    623 
    624 	if (PT_NULL_P(PTN_BRANCH_ROOT_SLOT(&pt->pt_rootnode)))
    625 		return NULL;
    626 
    627 	bitoff = 0;
    628 	parent = &pt->pt_rootnode;
    629 	parent_slot = PT_SLOT_ROOT;
    630 	for (;;) {
    631 		const uintptr_t node = PTN_BRANCH_SLOT(parent, parent_slot);
    632 		const pt_slot_t branch_bitoff = PTN_BRANCH_BITOFF(PT_NODE(node));
    633 		ptn = PT_NODE(node);
    634 
    635 		if (PT_LEAF_P(node)) {
    636 #ifndef PTNOMASK
    637 			at_mask = PTN_ISMASK_P(ptn);
    638 #endif
    639 			break;
    640 		}
    641 
    642 		if (bitoff < branch_bitoff) {
    643 			if (!ptree_matchkey(pt, key, ptn, bitoff, branch_bitoff - bitoff)) {
    644 #ifndef PTNOMASK
    645 				if (mask != NULL)
    646 					return NODETOITEM(pt, mask);
    647 #endif
    648 				return NULL;
    649 			}
    650 			bitoff = branch_bitoff;
    651 		}
    652 
    653 #ifndef PTNOMASK
    654 		if (PTN_ISMASK_P(ptn) && PTN_BRANCH_BITLEN(ptn) == 0
    655 		    && (!filter
    656 		        || (*filter)(filter_arg, NODETOITEM(pt, ptn),
    657 				     PT_FILTER_MASK)))
    658 			mask = ptn;
    659 #endif
    660 
    661 		parent = ptn;
    662 		parent_slot = ptree_testkey(pt, key, parent);
    663 		bitoff += PTN_BRANCH_BITLEN(parent);
    664 	}
    665 
    666 	KASSERT(PTN_ISROOT_P(pt, parent) || PTN_BRANCH_BITOFF(parent) + PTN_BRANCH_BITLEN(parent) == bitoff);
    667 	if (!filter || (*filter)(filter_arg, NODETOITEM(pt, ptn), at_mask ? PT_FILTER_MASK : 0)) {
    668 #ifndef PTNOMASK
    669 		if (PTN_ISMASK_P(ptn)) {
    670 			const pt_bitlen_t mask_len = PTN_MASK_BITLEN(ptn);
    671 			if (bitoff == PTN_MASK_BITLEN(ptn))
    672 				return NODETOITEM(pt, ptn);
    673 			if (ptree_matchkey(pt, key, ptn, bitoff, mask_len - bitoff))
    674 				return NODETOITEM(pt, ptn);
    675 		} else
    676 #endif /* !PTNOMASK */
    677 		if (ptree_matchkey(pt, key, ptn, bitoff, UINT_MAX))
    678 			return NODETOITEM(pt, ptn);
    679 	}
    680 
    681 #ifndef PTNOMASK
    682 	/*
    683 	 * By virtue of how the mask was placed in the tree,
    684 	 * all nodes descended from it will match it.  But the bits
    685 	 * before the mask still need to be checked and since the
    686 	 * mask was a branch, that was done implicitly.
    687 	 */
    688 	if (mask != NULL) {
    689 		KASSERT(ptree_matchkey(pt, key, mask, 0, PTN_MASK_BITLEN(mask)));
    690 		return NODETOITEM(pt, mask);
    691 	}
    692 #endif /* !PTNOMASK */
    693 
    694 	/*
    695 	 * Nothing matched.
    696 	 */
    697 	return NULL;
    698 }
    699 
    700 void *
    701 ptree_iterate(pt_tree_t *pt, const void *item, pt_direction_t direction)
    702 {
    703 	const pt_node_t * const target = ITEMTONODE(pt, item);
    704 	uintptr_t node, next_node;
    705 
    706 	if (direction != PT_ASCENDING && direction != PT_DESCENDING)
    707 		return NULL;
    708 
    709 	node = PTN_BRANCH_ROOT_SLOT(&pt->pt_rootnode);
    710 	if (PT_NULL_P(node))
    711 		return NULL;
    712 
    713 	if (item == NULL) {
    714 		pt_node_t * const ptn = PT_NODE(node);
    715 		if (direction == PT_ASCENDING
    716 		    && PTN_ISMASK_P(ptn) && PTN_BRANCH_BITLEN(ptn) == 0)
    717 			return NODETOITEM(pt, ptn);
    718 		next_node = node;
    719 	} else {
    720 #ifndef PTNOMASK
    721 		uintptr_t mask_node = PT_NULL;
    722 #endif /* !PTNOMASK */
    723 		next_node = PT_NULL;
    724 		while (!PT_LEAF_P(node)) {
    725 			pt_node_t * const ptn = PT_NODE(node);
    726 			pt_slot_t slot;
    727 #ifndef PTNOMASK
    728 			if (PTN_ISMASK_P(ptn) && PTN_BRANCH_BITLEN(ptn) == 0) {
    729 				if (ptn == target)
    730 					break;
    731 				if (direction == PT_DESCENDING) {
    732 					mask_node = node;
    733 					next_node = PT_NULL;
    734 				}
    735 			}
    736 #endif /* !PTNOMASK */
    737 			slot = ptree_testnode(pt, target, ptn);
    738 			node = PTN_BRANCH_SLOT(ptn, slot);
    739 			if (direction == PT_ASCENDING) {
    740 				if (slot != (1 << PTN_BRANCH_BITLEN(ptn)) - 1)
    741 					next_node = PTN_BRANCH_SLOT(ptn, slot + 1);
    742 			} else {
    743 				if (slot > 0) {
    744 #ifndef PTNOMASK
    745 					mask_node = PT_NULL;
    746 #endif /* !PTNOMASK */
    747 					next_node = PTN_BRANCH_SLOT(ptn, slot - 1);
    748 				}
    749 			}
    750 		}
    751 		if (PT_NODE(node) != target)
    752 			return NULL;
    753 #ifndef PTNOMASK
    754 		if (PT_BRANCH_P(node)) {
    755 			pt_node_t *ptn = PT_NODE(node);
    756 			KASSERT(PTN_ISMASK_P(PT_NODE(node)) && PTN_BRANCH_BITLEN(PT_NODE(node)) == 0);
    757 			if (direction == PT_ASCENDING) {
    758 				next_node = PTN_BRANCH_ROOT_SLOT(ptn);
    759 				ptn = PT_NODE(next_node);
    760 			}
    761 		}
    762 		/*
    763 		 * When descending, if we countered a mask node then that's
    764 		 * we want to return.
    765 		 */
    766 		if (direction == PT_DESCENDING && !PT_NULL_P(mask_node)) {
    767 			KASSERT(PT_NULL_P(next_node));
    768 			return NODETOITEM(pt, PT_NODE(mask_node));
    769 		}
    770 #endif /* !PTNOMASK */
    771 	}
    772 
    773 	node = next_node;
    774 	if (PT_NULL_P(node))
    775 		return NULL;
    776 
    777 	while (!PT_LEAF_P(node)) {
    778 		pt_node_t * const ptn = PT_NODE(node);
    779 		pt_slot_t slot;
    780 		if (direction == PT_ASCENDING) {
    781 #ifndef PTNOMASK
    782 			if (PT_BRANCH_P(node)
    783 			    && PTN_ISMASK_P(ptn)
    784 			    && PTN_BRANCH_BITLEN(ptn) == 0)
    785 				return NODETOITEM(pt, ptn);
    786 #endif /* !PTNOMASK */
    787 			slot = PT_SLOT_LEFT;
    788 		} else {
    789 			slot = (1 << PTN_BRANCH_BITLEN(ptn)) - 1;
    790 		}
    791 		node = PTN_BRANCH_SLOT(ptn, slot);
    792 	}
    793 	return NODETOITEM(pt, PT_NODE(node));
    794 }
    795 
    796 void
    797 ptree_remove_node(pt_tree_t *pt, void *item)
    798 {
    799 	pt_node_t * const target = ITEMTONODE(pt, item);
    800 	const pt_slot_t leaf_slot = PTN_LEAF_POSITION(target);
    801 	const pt_slot_t branch_slot = PTN_BRANCH_POSITION(target);
    802 	pt_node_t *ptn, *parent;
    803 	uintptr_t node;
    804 	uintptr_t *removep;
    805 	uintptr_t *nodep;
    806 	pt_bitoff_t bitoff;
    807 	pt_slot_t parent_slot;
    808 #ifndef PTNOMASK
    809 	bool at_mask;
    810 #endif
    811 
    812 	if (PT_NULL_P(PTN_BRANCH_ROOT_SLOT(&pt->pt_rootnode))) {
    813 		KASSERT(!PT_NULL_P(PTN_BRANCH_ROOT_SLOT(&pt->pt_rootnode)));
    814 		return;
    815 	}
    816 
    817 	bitoff = 0;
    818 	removep = NULL;
    819 	nodep = NULL;
    820 	parent = &pt->pt_rootnode;
    821 	parent_slot = PT_SLOT_ROOT;
    822 	for (;;) {
    823 		node = PTN_BRANCH_SLOT(parent, parent_slot);
    824 		ptn = PT_NODE(node);
    825 #ifndef PTNOMASK
    826 		at_mask = PTN_ISMASK_P(ptn);
    827 #endif
    828 
    829 		if (PT_LEAF_P(node))
    830 			break;
    831 
    832 		/*
    833 		 * If we are at the target, then we are looking at its branch
    834 		 * identity.  We need to remember who's pointing at it so we
    835 		 * stop them from doing that.
    836 		 */
    837 		if (__predict_false(ptn == target)) {
    838 			KASSERT(nodep == NULL);
    839 #ifndef PTNOMASK
    840 			/*
    841 			 * Interior mask nodes are trivial to get rid of.
    842 			 */
    843 			if (at_mask && PTN_BRANCH_BITLEN(ptn) == 0) {
    844 				PTN_BRANCH_SLOT(parent, parent_slot) =
    845 				    PTN_BRANCH_ROOT_SLOT(ptn);
    846 				KASSERT(PT_NULL_P(PTN_BRANCH_ODDMAN_SLOT(ptn)));
    847 				PTREE_CHECK(pt);
    848 				return;
    849 			}
    850 #endif /* !PTNOMASK */
    851 			nodep = &PTN_BRANCH_SLOT(parent, parent_slot);
    852 			KASSERT(*nodep == PTN_BRANCH(target));
    853 		}
    854 		/*
    855 		 * We need also need to know who's pointing at our parent.
    856 		 * After we remove ourselves from our parent, he'll only
    857 		 * have one child and that's unacceptable.  So we replace
    858 		 * the pointer to the parent with our abadoned sibling.
    859 		 */
    860 		removep = &PTN_BRANCH_SLOT(parent, parent_slot);
    861 
    862 		/*
    863 		 * Descend into the tree.
    864 		 */
    865 		parent = ptn;
    866 		parent_slot = ptree_testnode(pt, target, parent);
    867 		bitoff += PTN_BRANCH_BITLEN(parent);
    868 	}
    869 
    870 	/*
    871 	 * We better have found that the leaf we are looking for is target.
    872 	 */
    873 	if (target != ptn) {
    874 		KASSERT(target == ptn);
    875 		return;
    876 	}
    877 
    878 	/*
    879 	 * If we didn't encounter target as branch, then target must be the
    880 	 * oddman-out.
    881 	 */
    882 	if (nodep == NULL) {
    883 		KASSERT(PTN_BRANCH_ODDMAN_SLOT(&pt->pt_rootnode) == PTN_LEAF(target));
    884 		KASSERT(nodep == NULL);
    885 		nodep = &PTN_BRANCH_ODDMAN_SLOT(&pt->pt_rootnode);
    886 	}
    887 
    888 	KASSERT((removep == NULL) == (parent == &pt->pt_rootnode));
    889 
    890 	/*
    891 	 * We have to special remove the last leaf from the root since
    892 	 * the only time the tree can a PT_NULL node is when it's empty.
    893 	 */
    894 	if (__predict_false(PTN_ISROOT_P(pt, parent))) {
    895 		KASSERT(removep == NULL);
    896 		KASSERT(parent == &pt->pt_rootnode);
    897 		KASSERT(nodep == &PTN_BRANCH_ODDMAN_SLOT(&pt->pt_rootnode));
    898 		KASSERT(*nodep == PTN_LEAF(target));
    899 		PTN_BRANCH_ROOT_SLOT(&pt->pt_rootnode) = PT_NULL;
    900 		PTN_BRANCH_ODDMAN_SLOT(&pt->pt_rootnode) = PT_NULL;
    901 		return;
    902 	}
    903 
    904 	KASSERT((parent == target) == (removep == nodep));
    905 	if (PTN_BRANCH(parent) == PTN_BRANCH_SLOT(target, PTN_BRANCH_POSITION(parent))) {
    906 		/*
    907 		 * The pointer to the parent actually lives in the target's
    908 		 * branch identity.  We can't just move the target's branch
    909 		 * identity since that would result in the parent pointing
    910 		 * to its own branch identity and that's fobidden.
    911 		 */
    912 		const pt_slot_t slot = PTN_BRANCH_POSITION(parent);
    913 		const pt_slot_t other_slot = slot ^ PT_SLOT_OTHER;
    914 		const pt_bitlen_t parent_bitlen = PTN_BRANCH_BITLEN(parent);
    915 
    916 		KASSERT(PTN_BRANCH_BITOFF(target) < PTN_BRANCH_BITOFF(parent));
    917 
    918 		/*
    919 		 * This gets so confusing.  The target's branch identity
    920 		 * points to the branch identity of the parent of the target's
    921 		 * leaf identity:
    922 		 *
    923 		 * 	TB = { X, PB = { TL, Y } }
    924 		 *   or TB = { X, PB = { TL } }
    925 		 *
    926 		 * So we can't move the target's branch identity to the parent
    927 		 * because that would corrupt the tree.
    928 		 */
    929 		if (__predict_true(parent_bitlen > 0)) {
    930 			/*
    931 			 * The parent is a two-way branch.  We have to have
    932 			 * do to this chang in two steps to keep internally
    933 			 * consistent.  First step is to copy our sibling from
    934 			 * our parent to where we are pointing to parent's
    935 			 * branch identiy.  This remove all references to his
    936 			 * branch identity from the tree.  We then simply make
    937 			 * the parent assume the target's branching duties.
    938 			 *
    939 			 *   TB = { X, PB = { Y, TL } } --> PB = { X, Y }.
    940 			 *   TB = { X, PB = { TL, Y } } --> PB = { X, Y }.
    941 			 *   TB = { PB = { Y, TL }, X } --> PB = { Y, X }.
    942 			 *   TB = { PB = { TL, Y }, X } --> PB = { Y, X }.
    943 			 */
    944 			PTN_BRANCH_SLOT(target, slot) =
    945 			    PTN_BRANCH_SLOT(parent, parent_slot ^ PT_SLOT_OTHER);
    946 			*nodep = ptree_move_branch(pt, parent, target);
    947 			PTREE_CHECK(pt);
    948 			return;
    949 		} else {
    950 			/*
    951 			 * If parent was a one-way branch, it must have been
    952 			 * mask which pointed to a single leaf which we are
    953 			 * removing.  This means we have to convert the
    954 			 * parent back to a leaf node.  So in the same
    955 			 * position that target pointed to parent, we place
    956 			 * leaf pointer to parent.  In the other position,
    957 			 * we just put the other node from target.
    958 			 *
    959 			 *   TB = { X, PB = { TL } } --> PB = { X, PL }
    960 			 */
    961 			KASSERT(PTN_ISMASK_P(parent));
    962 			KASSERT(slot == ptree_testnode(pt, parent, target));
    963 			PTN_BRANCH_SLOT(parent, slot) = PTN_LEAF(parent);
    964 			PTN_BRANCH_SLOT(parent, other_slot) =
    965 			   PTN_BRANCH_SLOT(target, other_slot);
    966 			PTN_SET_LEAF_POSITION(parent,slot);
    967 			PTN_SET_BRANCH_BITLEN(parent, 1);
    968 		}
    969 		PTN_SET_BRANCH_BITOFF(parent, PTN_BRANCH_BITOFF(target));
    970 		PTN_SET_BRANCH_POSITION(parent, PTN_BRANCH_POSITION(target));
    971 
    972 		*nodep = PTN_BRANCH(parent);
    973 		PTREE_CHECK(pt);
    974 		return;
    975 	}
    976 
    977 #ifndef PTNOMASK
    978 	if (__predict_false(PTN_BRANCH_BITLEN(parent) == 0)) {
    979 		/*
    980 		 * Parent was a one-way branch which is changing back to a leaf.
    981 		 * Since parent is no longer a one-way branch, it can take over
    982 		 * target's branching duties.
    983 		 *
    984 		 *  GB = { PB = { TL } }	--> GB = { PL }
    985 		 *  TB = { X, Y }		--> PB = { X, Y }
    986 		 */
    987 		KASSERT(PTN_ISMASK_P(parent));
    988 		KASSERT(parent != target);
    989 		*removep = PTN_LEAF(parent);
    990 	} else
    991 #endif /* !PTNOMASK */
    992 	{
    993 		/*
    994 		 * Now we are the normal removal case.  Since after the
    995 		 * target's leaf identity is removed from the its parent,
    996 		 * that parent will only have one decendent.  So we can
    997 		 * just as easily replace the node that has the parent's
    998 		 * branch identity with the surviving node.  This freeing
    999 		 * parent from its branching duties which means it can
   1000 		 * take over target's branching duties.
   1001 		 *
   1002 		 *  GB = { PB = { X, TL } }	--> GB = { X }
   1003 		 *  TB = { V, W }		--> PB = { V, W }
   1004 		 */
   1005 		const pt_slot_t other_slot = parent_slot ^ PT_SLOT_OTHER;
   1006 		uintptr_t other_node = PTN_BRANCH_SLOT(parent, other_slot);
   1007 		const pt_slot_t target_slot = (parent == target ? branch_slot : leaf_slot);
   1008 
   1009 		*removep = other_node;
   1010 
   1011 		ptree_set_position(other_node, target_slot);
   1012 
   1013 		/*
   1014 		 * If target's branch identity contained its leaf identity, we
   1015 		 * have nothing left to do.  We've already moved 'X' so there
   1016 		 * is no longer anything in the target's branch identiy that
   1017 		 * has to be preserved.
   1018 		 */
   1019 		if (parent == target) {
   1020 			/*
   1021 			 *  GB = { TB = { X, TL } }	--> GB = { X }
   1022 			 *  TB = { X, TL }		--> don't care
   1023 			 */
   1024 			PTREE_CHECK(pt);
   1025 			return;
   1026 		}
   1027 	}
   1028 
   1029 	/*
   1030 	 * If target wasn't used as a branch, then it must have been the
   1031 	 * oddman-out of the tree (the one node that doesn't have a branch
   1032 	 * identity).  This makes parent the new oddman-out.
   1033 	 */
   1034 	if (*nodep == PTN_LEAF(target)) {
   1035 		KASSERT(nodep == &PTN_BRANCH_ODDMAN_SLOT(&pt->pt_rootnode));
   1036 		PTN_BRANCH_ODDMAN_SLOT(&pt->pt_rootnode) = PTN_LEAF(parent);
   1037 		PTREE_CHECK(pt);
   1038 		return;
   1039 	}
   1040 
   1041 	/*
   1042 	 * Finally move the target's branching duties to the parent.
   1043 	 */
   1044 	KASSERT(PTN_BRANCH_BITOFF(parent) > PTN_BRANCH_BITOFF(target));
   1045 	*nodep = ptree_move_branch(pt, parent, target);
   1046 	PTREE_CHECK(pt);
   1047 }
   1048 
   1049 #ifdef PTCHECK
   1050 static const pt_node_t *
   1051 ptree_check_find_node2(const pt_tree_t *pt, const pt_node_t *parent,
   1052 	uintptr_t target)
   1053 {
   1054 	const pt_bitlen_t slots = 1 << PTN_BRANCH_BITLEN(parent);
   1055 	pt_slot_t slot;
   1056 
   1057 	for (slot = 0; slot < slots; slot++) {
   1058 		const uintptr_t node = PTN_BRANCH_SLOT(parent, slot);
   1059 		if (PTN_BRANCH_SLOT(parent, slot) == node)
   1060 			return parent;
   1061 	}
   1062 	for (slot = 0; slot < slots; slot++) {
   1063 		const uintptr_t node = PTN_BRANCH_SLOT(parent, slot);
   1064 		const pt_node_t *branch;
   1065 		if (!PT_BRANCH_P(node))
   1066 			continue;
   1067 		branch = ptree_check_find_node2(pt, PT_NODE(node), target);
   1068 		if (branch != NULL)
   1069 			return branch;
   1070 	}
   1071 
   1072 	return NULL;
   1073 }
   1074 
   1075 static bool
   1076 ptree_check_leaf(const pt_tree_t *pt, const pt_node_t *parent,
   1077 	const pt_node_t *ptn)
   1078 {
   1079 	const pt_bitoff_t leaf_position = PTN_LEAF_POSITION(ptn);
   1080 	const pt_bitlen_t bitlen = PTN_BRANCH_BITLEN(ptn);
   1081 	const pt_bitlen_t mask_len = PTN_MASK_BITLEN(ptn);
   1082 	const uintptr_t leaf_node = PTN_LEAF(ptn);
   1083 	const bool is_parent_root = (parent == &pt->pt_rootnode);
   1084 	const bool is_mask = PTN_ISMASK_P(ptn);
   1085 	bool ok = true;
   1086 
   1087 	if (is_parent_root) {
   1088 		ok = ok && PTN_BRANCH_ODDMAN_SLOT(parent) == leaf_node;
   1089 		KASSERT(ok);
   1090 		return ok;
   1091 	}
   1092 
   1093 	if (is_mask && PTN_ISMASK_P(parent) && PTN_BRANCH_BITLEN(parent) == 0) {
   1094 		ok = ok && PTN_MASK_BITLEN(parent) < mask_len;
   1095 		KASSERT(ok);
   1096 		ok = ok && PTN_BRANCH_BITOFF(parent) < mask_len;
   1097 		KASSERT(ok);
   1098 	}
   1099 	ok = ok && PTN_BRANCH_SLOT(parent, leaf_position) == leaf_node;
   1100 	KASSERT(ok);
   1101 	ok = ok && leaf_position == ptree_testnode(pt, ptn, parent);
   1102 	KASSERT(ok);
   1103 	if (PTN_BRANCH_ODDMAN_SLOT(&pt->pt_rootnode) != leaf_node) {
   1104 		ok = ok && bitlen > 0;
   1105 		KASSERT(ok);
   1106 		ok = ok && ptn == ptree_check_find_node2(pt, ptn, PTN_LEAF(ptn));
   1107 		KASSERT(ok);
   1108 	}
   1109 	return ok;
   1110 }
   1111 
   1112 static bool
   1113 ptree_check_branch(const pt_tree_t *pt, const pt_node_t *parent,
   1114 	const pt_node_t *ptn)
   1115 {
   1116 	const bool is_parent_root = (parent == &pt->pt_rootnode);
   1117 	const pt_slot_t branch_slot = PTN_BRANCH_POSITION(ptn);
   1118 	const pt_bitoff_t bitoff = PTN_BRANCH_BITOFF(ptn);
   1119 	const pt_bitoff_t bitlen = PTN_BRANCH_BITLEN(ptn);
   1120 	const pt_bitoff_t parent_bitoff = PTN_BRANCH_BITOFF(parent);
   1121 	const pt_bitoff_t parent_bitlen = PTN_BRANCH_BITLEN(parent);
   1122 	const bool is_parent_mask = PTN_ISMASK_P(parent) && parent_bitlen == 0;
   1123 	const bool is_mask = PTN_ISMASK_P(ptn) && bitlen == 0;
   1124 	const pt_bitoff_t parent_mask_len = PTN_MASK_BITLEN(parent);
   1125 	const pt_bitoff_t mask_len = PTN_MASK_BITLEN(ptn);
   1126 	const pt_bitlen_t slots = 1 << bitlen;
   1127 	pt_slot_t slot;
   1128 	bool ok = true;
   1129 
   1130 	ok = ok && PTN_BRANCH_SLOT(parent, branch_slot) == PTN_BRANCH(ptn);
   1131 	KASSERT(ok);
   1132 	ok = ok && branch_slot == ptree_testnode(pt, ptn, parent);
   1133 	KASSERT(ok);
   1134 
   1135 	if (is_mask) {
   1136 		ok = ok && bitoff == mask_len;
   1137 		KASSERT(ok);
   1138 		if (is_parent_mask) {
   1139 			ok = ok && parent_mask_len < mask_len;
   1140 			KASSERT(ok);
   1141 			ok = ok && parent_bitoff < bitoff;
   1142 			KASSERT(ok);
   1143 		}
   1144 	} else {
   1145 		if (is_parent_mask) {
   1146 			ok = ok && parent_bitoff <= bitoff;
   1147 		} else if (!is_parent_root) {
   1148 			ok = ok && parent_bitoff < bitoff;
   1149 		}
   1150 		KASSERT(ok);
   1151 	}
   1152 
   1153 	for (slot = 0; slot < slots; slot++) {
   1154 		const uintptr_t node = PTN_BRANCH_SLOT(ptn, slot);
   1155 		pt_bitoff_t tmp_bitoff = 0;
   1156 		pt_slot_t tmp_slot;
   1157 		ok = ok && node != PTN_BRANCH(ptn);
   1158 		KASSERT(ok);
   1159 		if (bitlen > 0) {
   1160 			ok = ok && ptree_matchnode(pt, PT_NODE(node), ptn, bitoff, &tmp_bitoff, &tmp_slot);
   1161 			KASSERT(ok);
   1162 			tmp_slot = ptree_testnode(pt, PT_NODE(node), ptn);
   1163 			ok = ok && slot == tmp_slot;
   1164 			KASSERT(ok);
   1165 		}
   1166 		if (PT_LEAF_P(node))
   1167 			ok = ok && ptree_check_leaf(pt, ptn, PT_NODE(node));
   1168 		else
   1169 			ok = ok && ptree_check_branch(pt, ptn, PT_NODE(node));
   1170 	}
   1171 
   1172 	return ok;
   1173 }
   1174 #endif /* PTCHECK */
   1175 
   1176 bool
   1177 ptree_check(const pt_tree_t *pt)
   1178 {
   1179 	bool ok = true;
   1180 #ifdef PTCHECK
   1181 	const pt_node_t * const parent = &pt->pt_rootnode;
   1182 	const uintptr_t node = pt->pt_root;
   1183 	const pt_node_t * const ptn = PT_NODE(node);
   1184 
   1185 	ok = ok && PTN_BRANCH_BITOFF(parent) == 0;
   1186 	ok = ok && !PTN_ISMASK_P(parent);
   1187 
   1188 	if (PT_NULL_P(node))
   1189 		return ok;
   1190 
   1191 	if (PT_LEAF_P(node))
   1192 		ok = ok && ptree_check_leaf(pt, parent, ptn);
   1193 	else
   1194 		ok = ok && ptree_check_branch(pt, parent, ptn);
   1195 #endif
   1196 	return ok;
   1197 }
   1198