Home | History | Annotate | Line # | Download | only in gen
rb.c revision 1.7
      1 /*	$NetBSD: rb.c,v 1.7 2010/09/24 22:51:51 rmind Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2001 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 #if !defined(_KERNEL) && !defined(_STANDALONE)
     33 #include <sys/types.h>
     34 #include <stddef.h>
     35 #include <assert.h>
     36 #include <stdbool.h>
     37 #ifdef RBDEBUG
     38 #define	KASSERT(s)	assert(s)
     39 #else
     40 #define KASSERT(s)	do { } while (/*CONSTCOND*/ 0)
     41 #endif
     42 #else
     43 #include <lib/libkern/libkern.h>
     44 #endif
     45 
     46 #ifdef _LIBC
     47 __weak_alias(rb_tree_init, _rb_tree_init)
     48 __weak_alias(rb_tree_find_node, _rb_tree_find_node)
     49 __weak_alias(rb_tree_find_node_geq, _rb_tree_find_node_geq)
     50 __weak_alias(rb_tree_find_node_leq, _rb_tree_find_node_leq)
     51 __weak_alias(rb_tree_insert_node, _rb_tree_insert_node)
     52 __weak_alias(rb_tree_remove_node, _rb_tree_remove_node)
     53 __weak_alias(rb_tree_iterate, _rb_tree_iterate)
     54 #ifdef RBDEBUG
     55 __weak_alias(rb_tree_check, _rb_tree_check)
     56 __weak_alias(rb_tree_depths, _rb_tree_depths)
     57 #endif
     58 
     59 #define	rb_tree_init		_rb_tree_init
     60 #define	rb_tree_find_node	_rb_tree_find_node
     61 #define	rb_tree_find_node_geq	_rb_tree_find_node_geq
     62 #define	rb_tree_find_node_leq	_rb_tree_find_node_leq
     63 #define	rb_tree_insert_node	_rb_tree_insert_node
     64 #define	rb_tree_remove_node	_rb_tree_remove_node
     65 #define	rb_tree_iterate		_rb_tree_iterate
     66 #ifdef RBDEBUG
     67 #define	rb_tree_check		_rb_tree_check
     68 #define	rb_tree_depths		_rb_tree_depths
     69 #endif
     70 #endif
     71 
     72 #ifdef RBTEST
     73 #include "rb.h"
     74 #else
     75 #include <sys/rb.h>
     76 #endif
     77 
     78 static void rb_tree_insert_rebalance(struct rb_tree *, struct rb_node *);
     79 static void rb_tree_removal_rebalance(struct rb_tree *, struct rb_node *,
     80 	unsigned int);
     81 #ifdef RBDEBUG
     82 static const struct rb_node *rb_tree_iterate_const(const struct rb_tree *,
     83 	const struct rb_node *, const unsigned int);
     84 static bool rb_tree_check_node(const struct rb_tree *, const struct rb_node *,
     85 	const struct rb_node *, bool);
     86 #else
     87 #define	rb_tree_check_node(a, b, c, d)	true
     88 #endif
     89 
     90 #define	RB_NODETOITEM(rbto, rbn)	\
     91     ((void *)((uintptr_t)(rbn) - (rbto)->rbto_node_offset))
     92 #define	RB_ITEMTONODE(rbto, rbn)	\
     93     ((rb_node_t *)((uintptr_t)(rbn) + (rbto)->rbto_node_offset))
     94 
     95 #define	RB_SENTINEL_NODE	NULL
     96 
     97 void
     98 rb_tree_init(struct rb_tree *rbt, const rb_tree_ops_t *ops)
     99 {
    100 
    101 	rbt->rbt_ops = ops;
    102 	*((const struct rb_node **)&rbt->rbt_root) = RB_SENTINEL_NODE;
    103 	RB_TAILQ_INIT(&rbt->rbt_nodes);
    104 #ifndef RBSMALL
    105 	rbt->rbt_minmax[RB_DIR_LEFT] = rbt->rbt_root;	/* minimum node */
    106 	rbt->rbt_minmax[RB_DIR_RIGHT] = rbt->rbt_root;	/* maximum node */
    107 #endif
    108 #ifdef RBSTATS
    109 	rbt->rbt_count = 0;
    110 	rbt->rbt_insertions = 0;
    111 	rbt->rbt_removals = 0;
    112 	rbt->rbt_insertion_rebalance_calls = 0;
    113 	rbt->rbt_insertion_rebalance_passes = 0;
    114 	rbt->rbt_removal_rebalance_calls = 0;
    115 	rbt->rbt_removal_rebalance_passes = 0;
    116 #endif
    117 }
    118 
    119 void *
    120 rb_tree_find_node(struct rb_tree *rbt, const void *key)
    121 {
    122 	const rb_tree_ops_t *rbto = rbt->rbt_ops;
    123 	rbto_compare_key_fn compare_key = rbto->rbto_compare_key;
    124 	struct rb_node *parent = rbt->rbt_root;
    125 
    126 	while (!RB_SENTINEL_P(parent)) {
    127 		void *pobj = RB_NODETOITEM(rbto, parent);
    128 		const signed int diff = (*compare_key)(rbto->rbto_context,
    129 		    pobj, key);
    130 		if (diff == 0)
    131 			return pobj;
    132 		parent = parent->rb_nodes[diff < 0];
    133 	}
    134 
    135 	return NULL;
    136 }
    137 
    138 void *
    139 rb_tree_find_node_geq(struct rb_tree *rbt, const void *key)
    140 {
    141 	const rb_tree_ops_t *rbto = rbt->rbt_ops;
    142 	rbto_compare_key_fn compare_key = rbto->rbto_compare_key;
    143 	struct rb_node *parent = rbt->rbt_root, *last = NULL;
    144 
    145 	while (!RB_SENTINEL_P(parent)) {
    146 		void *pobj = RB_NODETOITEM(rbto, parent);
    147 		const signed int diff = (*compare_key)(rbto->rbto_context,
    148 		    pobj, key);
    149 		if (diff == 0)
    150 			return pobj;
    151 		if (diff > 0)
    152 			last = parent;
    153 		parent = parent->rb_nodes[diff < 0];
    154 	}
    155 
    156 	return RB_NODETOITEM(rbto, last);
    157 }
    158 
    159 void *
    160 rb_tree_find_node_leq(struct rb_tree *rbt, const void *key)
    161 {
    162 	const rb_tree_ops_t *rbto = rbt->rbt_ops;
    163 	rbto_compare_key_fn compare_key = rbto->rbto_compare_key;
    164 	struct rb_node *parent = rbt->rbt_root, *last = NULL;
    165 
    166 	while (!RB_SENTINEL_P(parent)) {
    167 		void *pobj = RB_NODETOITEM(rbto, parent);
    168 		const signed int diff = (*compare_key)(rbto->rbto_context,
    169 		    pobj, key);
    170 		if (diff == 0)
    171 			return pobj;
    172 		if (diff < 0)
    173 			last = parent;
    174 		parent = parent->rb_nodes[diff < 0];
    175 	}
    176 
    177 	return RB_NODETOITEM(rbto, last);
    178 }
    179 
    180 void *
    181 rb_tree_insert_node(struct rb_tree *rbt, void *object)
    182 {
    183 	const rb_tree_ops_t *rbto = rbt->rbt_ops;
    184 	rbto_compare_nodes_fn compare_nodes = rbto->rbto_compare_nodes;
    185 	struct rb_node *parent, *tmp, *self = RB_ITEMTONODE(rbto, object);
    186 	unsigned int position;
    187 	bool rebalance;
    188 
    189 	RBSTAT_INC(rbt->rbt_insertions);
    190 
    191 	tmp = rbt->rbt_root;
    192 	/*
    193 	 * This is a hack.  Because rbt->rbt_root is just a struct rb_node *,
    194 	 * just like rb_node->rb_nodes[RB_DIR_LEFT], we can use this fact to
    195 	 * avoid a lot of tests for root and know that even at root,
    196 	 * updating RB_FATHER(rb_node)->rb_nodes[RB_POSITION(rb_node)] will
    197 	 * update rbt->rbt_root.
    198 	 */
    199 	parent = (struct rb_node *)(void *)&rbt->rbt_root;
    200 	position = RB_DIR_LEFT;
    201 
    202 	/*
    203 	 * Find out where to place this new leaf.
    204 	 */
    205 	while (!RB_SENTINEL_P(tmp)) {
    206 		void *tobj = RB_NODETOITEM(rbto, tmp);
    207 		const signed int diff = (*compare_nodes)(rbto->rbto_context,
    208 		    tobj, object);
    209 		if (__predict_false(diff == 0)) {
    210 			/*
    211 			 * Node already exists; return it.
    212 			 */
    213 			return tobj;
    214 		}
    215 		parent = tmp;
    216 		position = (diff < 0);
    217 		tmp = parent->rb_nodes[position];
    218 	}
    219 
    220 #ifdef RBDEBUG
    221 	{
    222 		struct rb_node *prev = NULL, *next = NULL;
    223 
    224 		if (position == RB_DIR_RIGHT)
    225 			prev = parent;
    226 		else if (tmp != rbt->rbt_root)
    227 			next = parent;
    228 
    229 		/*
    230 		 * Verify our sequential position
    231 		 */
    232 		KASSERT(prev == NULL || !RB_SENTINEL_P(prev));
    233 		KASSERT(next == NULL || !RB_SENTINEL_P(next));
    234 		if (prev != NULL && next == NULL)
    235 			next = TAILQ_NEXT(prev, rb_link);
    236 		if (prev == NULL && next != NULL)
    237 			prev = TAILQ_PREV(next, rb_node_qh, rb_link);
    238 		KASSERT(prev == NULL || !RB_SENTINEL_P(prev));
    239 		KASSERT(next == NULL || !RB_SENTINEL_P(next));
    240 		KASSERT(prev == NULL || (*compare_nodes)(rbto->rbto_context,
    241 		    RB_NODETOITEM(rbto, prev), RB_NODETOITEM(rbto, self)) < 0);
    242 		KASSERT(next == NULL || (*compare_nodes)(rbto->rbto_context,
    243 		    RB_NODETOITEM(rbto, self), RB_NODETOITEM(rbto, next)) < 0);
    244 	}
    245 #endif
    246 
    247 	/*
    248 	 * Initialize the node and insert as a leaf into the tree.
    249 	 */
    250 	RB_SET_FATHER(self, parent);
    251 	RB_SET_POSITION(self, position);
    252 	if (__predict_false(parent == (struct rb_node *)(void *)&rbt->rbt_root)) {
    253 		RB_MARK_BLACK(self);		/* root is always black */
    254 #ifndef RBSMALL
    255 		rbt->rbt_minmax[RB_DIR_LEFT] = self;
    256 		rbt->rbt_minmax[RB_DIR_RIGHT] = self;
    257 #endif
    258 		rebalance = false;
    259 	} else {
    260 		KASSERT(position == RB_DIR_LEFT || position == RB_DIR_RIGHT);
    261 #ifndef RBSMALL
    262 		/*
    263 		 * Keep track of the minimum and maximum nodes.  If our
    264 		 * parent is a minmax node and we on their min/max side,
    265 		 * we must be the new min/max node.
    266 		 */
    267 		if (parent == rbt->rbt_minmax[position])
    268 			rbt->rbt_minmax[position] = self;
    269 #endif /* !RBSMALL */
    270 		/*
    271 		 * All new nodes are colored red.  We only need to rebalance
    272 		 * if our parent is also red.
    273 		 */
    274 		RB_MARK_RED(self);
    275 		rebalance = RB_RED_P(parent);
    276 	}
    277 	KASSERT(RB_SENTINEL_P(parent->rb_nodes[position]));
    278 	self->rb_left = parent->rb_nodes[position];
    279 	self->rb_right = parent->rb_nodes[position];
    280 	parent->rb_nodes[position] = self;
    281 	KASSERT(RB_CHILDLESS_P(self));
    282 
    283 	/*
    284 	 * Insert the new node into a sorted list for easy sequential access
    285 	 */
    286 	RBSTAT_INC(rbt->rbt_count);
    287 #ifdef RBDEBUG
    288 	if (RB_ROOT_P(rbt, self)) {
    289 		RB_TAILQ_INSERT_HEAD(&rbt->rbt_nodes, self, rb_link);
    290 	} else if (position == RB_DIR_LEFT) {
    291 		KASSERT((*compare_nodes)(rbto->rbto_context,
    292 		    RB_NODETOITEM(rbto, self),
    293 		    RB_NODETOITEM(rbto, RB_FATHER(self))) < 0);
    294 		RB_TAILQ_INSERT_BEFORE(RB_FATHER(self), self, rb_link);
    295 	} else {
    296 		KASSERT((*compare_nodes)(rbto->rbto_context,
    297 		    RB_NODETOITEM(rbto, RB_FATHER(self)),
    298 		    RB_NODETOITEM(rbto, self)) < 0);
    299 		RB_TAILQ_INSERT_AFTER(&rbt->rbt_nodes, RB_FATHER(self),
    300 		    self, rb_link);
    301 	}
    302 #endif
    303 	KASSERT(rb_tree_check_node(rbt, self, NULL, !rebalance));
    304 
    305 	/*
    306 	 * Rebalance tree after insertion
    307 	 */
    308 	if (rebalance) {
    309 		rb_tree_insert_rebalance(rbt, self);
    310 		KASSERT(rb_tree_check_node(rbt, self, NULL, true));
    311 	}
    312 
    313 	/* Succesfully inserted, return our node pointer. */
    314 	return object;
    315 }
    316 
    317 /*
    318  * Swap the location and colors of 'self' and its child @ which.  The child
    319  * can not be a sentinel node.  This is our rotation function.  However,
    320  * since it preserves coloring, it great simplifies both insertion and
    321  * removal since rotation almost always involves the exchanging of colors
    322  * as a separate step.
    323  */
    324 /*ARGSUSED*/
    325 static void
    326 rb_tree_reparent_nodes(struct rb_tree *rbt, struct rb_node *old_father,
    327 	const unsigned int which)
    328 {
    329 	const unsigned int other = which ^ RB_DIR_OTHER;
    330 	struct rb_node * const grandpa = RB_FATHER(old_father);
    331 	struct rb_node * const old_child = old_father->rb_nodes[which];
    332 	struct rb_node * const new_father = old_child;
    333 	struct rb_node * const new_child = old_father;
    334 
    335 	KASSERT(which == RB_DIR_LEFT || which == RB_DIR_RIGHT);
    336 
    337 	KASSERT(!RB_SENTINEL_P(old_child));
    338 	KASSERT(RB_FATHER(old_child) == old_father);
    339 
    340 	KASSERT(rb_tree_check_node(rbt, old_father, NULL, false));
    341 	KASSERT(rb_tree_check_node(rbt, old_child, NULL, false));
    342 	KASSERT(RB_ROOT_P(rbt, old_father) ||
    343 	    rb_tree_check_node(rbt, grandpa, NULL, false));
    344 
    345 	/*
    346 	 * Exchange descendant linkages.
    347 	 */
    348 	grandpa->rb_nodes[RB_POSITION(old_father)] = new_father;
    349 	new_child->rb_nodes[which] = old_child->rb_nodes[other];
    350 	new_father->rb_nodes[other] = new_child;
    351 
    352 	/*
    353 	 * Update ancestor linkages
    354 	 */
    355 	RB_SET_FATHER(new_father, grandpa);
    356 	RB_SET_FATHER(new_child, new_father);
    357 
    358 	/*
    359 	 * Exchange properties between new_father and new_child.  The only
    360 	 * change is that new_child's position is now on the other side.
    361 	 */
    362 #if 0
    363 	{
    364 		struct rb_node tmp;
    365 		tmp.rb_info = 0;
    366 		RB_COPY_PROPERTIES(&tmp, old_child);
    367 		RB_COPY_PROPERTIES(new_father, old_father);
    368 		RB_COPY_PROPERTIES(new_child, &tmp);
    369 	}
    370 #else
    371 	RB_SWAP_PROPERTIES(new_father, new_child);
    372 #endif
    373 	RB_SET_POSITION(new_child, other);
    374 
    375 	/*
    376 	 * Make sure to reparent the new child to ourself.
    377 	 */
    378 	if (!RB_SENTINEL_P(new_child->rb_nodes[which])) {
    379 		RB_SET_FATHER(new_child->rb_nodes[which], new_child);
    380 		RB_SET_POSITION(new_child->rb_nodes[which], which);
    381 	}
    382 
    383 	KASSERT(rb_tree_check_node(rbt, new_father, NULL, false));
    384 	KASSERT(rb_tree_check_node(rbt, new_child, NULL, false));
    385 	KASSERT(RB_ROOT_P(rbt, new_father) ||
    386 	    rb_tree_check_node(rbt, grandpa, NULL, false));
    387 }
    388 
    389 static void
    390 rb_tree_insert_rebalance(struct rb_tree *rbt, struct rb_node *self)
    391 {
    392 	struct rb_node * father = RB_FATHER(self);
    393 	struct rb_node * grandpa = RB_FATHER(father);
    394 	struct rb_node * uncle;
    395 	unsigned int which;
    396 	unsigned int other;
    397 
    398 	KASSERT(!RB_ROOT_P(rbt, self));
    399 	KASSERT(RB_RED_P(self));
    400 	KASSERT(RB_RED_P(father));
    401 	RBSTAT_INC(rbt->rbt_insertion_rebalance_calls);
    402 
    403 	for (;;) {
    404 		KASSERT(!RB_SENTINEL_P(self));
    405 
    406 		KASSERT(RB_RED_P(self));
    407 		KASSERT(RB_RED_P(father));
    408 		/*
    409 		 * We are red and our parent is red, therefore we must have a
    410 		 * grandfather and he must be black.
    411 		 */
    412 		grandpa = RB_FATHER(father);
    413 		KASSERT(RB_BLACK_P(grandpa));
    414 		KASSERT(RB_DIR_RIGHT == 1 && RB_DIR_LEFT == 0);
    415 		which = (father == grandpa->rb_right);
    416 		other = which ^ RB_DIR_OTHER;
    417 		uncle = grandpa->rb_nodes[other];
    418 
    419 		if (RB_BLACK_P(uncle))
    420 			break;
    421 
    422 		RBSTAT_INC(rbt->rbt_insertion_rebalance_passes);
    423 		/*
    424 		 * Case 1: our uncle is red
    425 		 *   Simply invert the colors of our parent and
    426 		 *   uncle and make our grandparent red.  And
    427 		 *   then solve the problem up at his level.
    428 		 */
    429 		RB_MARK_BLACK(uncle);
    430 		RB_MARK_BLACK(father);
    431 		if (__predict_false(RB_ROOT_P(rbt, grandpa))) {
    432 			/*
    433 			 * If our grandpa is root, don't bother
    434 			 * setting him to red, just return.
    435 			 */
    436 			KASSERT(RB_BLACK_P(grandpa));
    437 			return;
    438 		}
    439 		RB_MARK_RED(grandpa);
    440 		self = grandpa;
    441 		father = RB_FATHER(self);
    442 		KASSERT(RB_RED_P(self));
    443 		if (RB_BLACK_P(father)) {
    444 			/*
    445 			 * If our greatgrandpa is black, we're done.
    446 			 */
    447 			KASSERT(RB_BLACK_P(rbt->rbt_root));
    448 			return;
    449 		}
    450 	}
    451 
    452 	KASSERT(!RB_ROOT_P(rbt, self));
    453 	KASSERT(RB_RED_P(self));
    454 	KASSERT(RB_RED_P(father));
    455 	KASSERT(RB_BLACK_P(uncle));
    456 	KASSERT(RB_BLACK_P(grandpa));
    457 	/*
    458 	 * Case 2&3: our uncle is black.
    459 	 */
    460 	if (self == father->rb_nodes[other]) {
    461 		/*
    462 		 * Case 2: we are on the same side as our uncle
    463 		 *   Swap ourselves with our parent so this case
    464 		 *   becomes case 3.  Basically our parent becomes our
    465 		 *   child.
    466 		 */
    467 		rb_tree_reparent_nodes(rbt, father, other);
    468 		KASSERT(RB_FATHER(father) == self);
    469 		KASSERT(self->rb_nodes[which] == father);
    470 		KASSERT(RB_FATHER(self) == grandpa);
    471 		self = father;
    472 		father = RB_FATHER(self);
    473 	}
    474 	KASSERT(RB_RED_P(self) && RB_RED_P(father));
    475 	KASSERT(grandpa->rb_nodes[which] == father);
    476 	/*
    477 	 * Case 3: we are opposite a child of a black uncle.
    478 	 *   Swap our parent and grandparent.  Since our grandfather
    479 	 *   is black, our father will become black and our new sibling
    480 	 *   (former grandparent) will become red.
    481 	 */
    482 	rb_tree_reparent_nodes(rbt, grandpa, which);
    483 	KASSERT(RB_FATHER(self) == father);
    484 	KASSERT(RB_FATHER(self)->rb_nodes[RB_POSITION(self) ^ RB_DIR_OTHER] == grandpa);
    485 	KASSERT(RB_RED_P(self));
    486 	KASSERT(RB_BLACK_P(father));
    487 	KASSERT(RB_RED_P(grandpa));
    488 
    489 	/*
    490 	 * Final step: Set the root to black.
    491 	 */
    492 	RB_MARK_BLACK(rbt->rbt_root);
    493 }
    494 
    495 static void
    496 rb_tree_prune_node(struct rb_tree *rbt, struct rb_node *self, bool rebalance)
    497 {
    498 	const unsigned int which = RB_POSITION(self);
    499 	struct rb_node *father = RB_FATHER(self);
    500 #ifndef RBSMALL
    501 	const bool was_root = RB_ROOT_P(rbt, self);
    502 #endif
    503 
    504 	KASSERT(rebalance || (RB_ROOT_P(rbt, self) || RB_RED_P(self)));
    505 	KASSERT(!rebalance || RB_BLACK_P(self));
    506 	KASSERT(RB_CHILDLESS_P(self));
    507 	KASSERT(rb_tree_check_node(rbt, self, NULL, false));
    508 
    509 	/*
    510 	 * Since we are childless, we know that self->rb_left is pointing
    511 	 * to the sentinel node.
    512 	 */
    513 	father->rb_nodes[which] = self->rb_left;
    514 
    515 	/*
    516 	 * Remove ourselves from the node list, decrement the count,
    517 	 * and update min/max.
    518 	 */
    519 	RB_TAILQ_REMOVE(&rbt->rbt_nodes, self, rb_link);
    520 	RBSTAT_DEC(rbt->rbt_count);
    521 #ifndef RBSMALL
    522 	if (__predict_false(rbt->rbt_minmax[RB_POSITION(self)] == self)) {
    523 		rbt->rbt_minmax[RB_POSITION(self)] = father;
    524 		/*
    525 		 * When removing the root, rbt->rbt_minmax[RB_DIR_LEFT] is
    526 		 * updated automatically, but we also need to update
    527 		 * rbt->rbt_minmax[RB_DIR_RIGHT];
    528 		 */
    529 		if (__predict_false(was_root)) {
    530 			rbt->rbt_minmax[RB_DIR_RIGHT] = father;
    531 		}
    532 	}
    533 	RB_SET_FATHER(self, NULL);
    534 #endif
    535 
    536 	/*
    537 	 * Rebalance if requested.
    538 	 */
    539 	if (rebalance)
    540 		rb_tree_removal_rebalance(rbt, father, which);
    541 	KASSERT(was_root || rb_tree_check_node(rbt, father, NULL, true));
    542 }
    543 
    544 /*
    545  * When deleting an interior node
    546  */
    547 static void
    548 rb_tree_swap_prune_and_rebalance(struct rb_tree *rbt, struct rb_node *self,
    549 	struct rb_node *standin)
    550 {
    551 	const unsigned int standin_which = RB_POSITION(standin);
    552 	unsigned int standin_other = standin_which ^ RB_DIR_OTHER;
    553 	struct rb_node *standin_son;
    554 	struct rb_node *standin_father = RB_FATHER(standin);
    555 	bool rebalance = RB_BLACK_P(standin);
    556 
    557 	if (standin_father == self) {
    558 		/*
    559 		 * As a child of self, any childen would be opposite of
    560 		 * our parent.
    561 		 */
    562 		KASSERT(RB_SENTINEL_P(standin->rb_nodes[standin_other]));
    563 		standin_son = standin->rb_nodes[standin_which];
    564 	} else {
    565 		/*
    566 		 * Since we aren't a child of self, any childen would be
    567 		 * on the same side as our parent.
    568 		 */
    569 		KASSERT(RB_SENTINEL_P(standin->rb_nodes[standin_which]));
    570 		standin_son = standin->rb_nodes[standin_other];
    571 	}
    572 
    573 	/*
    574 	 * the node we are removing must have two children.
    575 	 */
    576 	KASSERT(RB_TWOCHILDREN_P(self));
    577 	/*
    578 	 * If standin has a child, it must be red.
    579 	 */
    580 	KASSERT(RB_SENTINEL_P(standin_son) || RB_RED_P(standin_son));
    581 
    582 	/*
    583 	 * Verify things are sane.
    584 	 */
    585 	KASSERT(rb_tree_check_node(rbt, self, NULL, false));
    586 	KASSERT(rb_tree_check_node(rbt, standin, NULL, false));
    587 
    588 	if (__predict_false(RB_RED_P(standin_son))) {
    589 		/*
    590 		 * We know we have a red child so if we flip it to black
    591 		 * we don't have to rebalance.
    592 		 */
    593 		KASSERT(rb_tree_check_node(rbt, standin_son, NULL, true));
    594 		RB_MARK_BLACK(standin_son);
    595 		rebalance = false;
    596 
    597 		if (standin_father == self) {
    598 			KASSERT(RB_POSITION(standin_son) == standin_which);
    599 		} else {
    600 			KASSERT(RB_POSITION(standin_son) == standin_other);
    601 			/*
    602 			 * Change the son's parentage to point to his grandpa.
    603 			 */
    604 			RB_SET_FATHER(standin_son, standin_father);
    605 			RB_SET_POSITION(standin_son, standin_which);
    606 		}
    607 	}
    608 
    609 	if (standin_father == self) {
    610 		/*
    611 		 * If we are about to delete the standin's father, then when
    612 		 * we call rebalance, we need to use ourselves as our father.
    613 		 * Otherwise remember our original father.  Also, sincef we are
    614 		 * our standin's father we only need to reparent the standin's
    615 		 * brother.
    616 		 *
    617 		 * |    R      -->     S    |
    618 		 * |  Q   S    -->   Q   T  |
    619 		 * |        t  -->          |
    620 		 */
    621 		KASSERT(RB_SENTINEL_P(standin->rb_nodes[standin_other]));
    622 		KASSERT(!RB_SENTINEL_P(self->rb_nodes[standin_other]));
    623 		KASSERT(self->rb_nodes[standin_which] == standin);
    624 		/*
    625 		 * Have our son/standin adopt his brother as his new son.
    626 		 */
    627 		standin_father = standin;
    628 	} else {
    629 		/*
    630 		 * |    R          -->    S       .  |
    631 		 * |   / \  |   T  -->   / \  |  /   |
    632 		 * |  ..... | S    -->  ..... | T    |
    633 		 *
    634 		 * Sever standin's connection to his father.
    635 		 */
    636 		standin_father->rb_nodes[standin_which] = standin_son;
    637 		/*
    638 		 * Adopt the far son.
    639 		 */
    640 		standin->rb_nodes[standin_other] = self->rb_nodes[standin_other];
    641 		RB_SET_FATHER(standin->rb_nodes[standin_other], standin);
    642 		KASSERT(RB_POSITION(self->rb_nodes[standin_other]) == standin_other);
    643 		/*
    644 		 * Use standin_other because we need to preserve standin_which
    645 		 * for the removal_rebalance.
    646 		 */
    647 		standin_other = standin_which;
    648 	}
    649 
    650 	/*
    651 	 * Move the only remaining son to our standin.  If our standin is our
    652 	 * son, this will be the only son needed to be moved.
    653 	 */
    654 	KASSERT(standin->rb_nodes[standin_other] != self->rb_nodes[standin_other]);
    655 	standin->rb_nodes[standin_other] = self->rb_nodes[standin_other];
    656 	RB_SET_FATHER(standin->rb_nodes[standin_other], standin);
    657 
    658 	/*
    659 	 * Now copy the result of self to standin and then replace
    660 	 * self with standin in the tree.
    661 	 */
    662 	RB_COPY_PROPERTIES(standin, self);
    663 	RB_SET_FATHER(standin, RB_FATHER(self));
    664 	RB_FATHER(standin)->rb_nodes[RB_POSITION(standin)] = standin;
    665 
    666 	/*
    667 	 * Remove ourselves from the node list, decrement the count,
    668 	 * and update min/max.
    669 	 */
    670 	RB_TAILQ_REMOVE(&rbt->rbt_nodes, self, rb_link);
    671 	RBSTAT_DEC(rbt->rbt_count);
    672 #ifndef RBSMALL
    673 	if (__predict_false(rbt->rbt_minmax[RB_POSITION(self)] == self))
    674 		rbt->rbt_minmax[RB_POSITION(self)] = RB_FATHER(self);
    675 	RB_SET_FATHER(self, NULL);
    676 #endif
    677 
    678 	KASSERT(rb_tree_check_node(rbt, standin, NULL, false));
    679 	KASSERT(RB_FATHER_SENTINEL_P(standin)
    680 		|| rb_tree_check_node(rbt, standin_father, NULL, false));
    681 	KASSERT(RB_LEFT_SENTINEL_P(standin)
    682 		|| rb_tree_check_node(rbt, standin->rb_left, NULL, false));
    683 	KASSERT(RB_RIGHT_SENTINEL_P(standin)
    684 		|| rb_tree_check_node(rbt, standin->rb_right, NULL, false));
    685 
    686 	if (!rebalance)
    687 		return;
    688 
    689 	rb_tree_removal_rebalance(rbt, standin_father, standin_which);
    690 	KASSERT(rb_tree_check_node(rbt, standin, NULL, true));
    691 }
    692 
    693 /*
    694  * We could do this by doing
    695  *	rb_tree_node_swap(rbt, self, which);
    696  *	rb_tree_prune_node(rbt, self, false);
    697  *
    698  * But it's more efficient to just evalate and recolor the child.
    699  */
    700 static void
    701 rb_tree_prune_blackred_branch(struct rb_tree *rbt, struct rb_node *self,
    702 	unsigned int which)
    703 {
    704 	struct rb_node *father = RB_FATHER(self);
    705 	struct rb_node *son = self->rb_nodes[which];
    706 #ifndef RBSMALL
    707 	const bool was_root = RB_ROOT_P(rbt, self);
    708 #endif
    709 
    710 	KASSERT(which == RB_DIR_LEFT || which == RB_DIR_RIGHT);
    711 	KASSERT(RB_BLACK_P(self) && RB_RED_P(son));
    712 	KASSERT(!RB_TWOCHILDREN_P(son));
    713 	KASSERT(RB_CHILDLESS_P(son));
    714 	KASSERT(rb_tree_check_node(rbt, self, NULL, false));
    715 	KASSERT(rb_tree_check_node(rbt, son, NULL, false));
    716 
    717 	/*
    718 	 * Remove ourselves from the tree and give our former child our
    719 	 * properties (position, color, root).
    720 	 */
    721 	RB_COPY_PROPERTIES(son, self);
    722 	father->rb_nodes[RB_POSITION(son)] = son;
    723 	RB_SET_FATHER(son, father);
    724 
    725 	/*
    726 	 * Remove ourselves from the node list, decrement the count,
    727 	 * and update minmax.
    728 	 */
    729 	RB_TAILQ_REMOVE(&rbt->rbt_nodes, self, rb_link);
    730 	RBSTAT_DEC(rbt->rbt_count);
    731 #ifndef RBSMALL
    732 	if (__predict_false(was_root)) {
    733 		KASSERT(rbt->rbt_minmax[which] == son);
    734 		rbt->rbt_minmax[which ^ RB_DIR_OTHER] = son;
    735 	} else if (rbt->rbt_minmax[RB_POSITION(self)] == self) {
    736 		rbt->rbt_minmax[RB_POSITION(self)] = son;
    737 	}
    738 	RB_SET_FATHER(self, NULL);
    739 #endif
    740 
    741 	KASSERT(was_root || rb_tree_check_node(rbt, father, NULL, true));
    742 	KASSERT(rb_tree_check_node(rbt, son, NULL, true));
    743 }
    744 
    745 void
    746 rb_tree_remove_node(struct rb_tree *rbt, void *object)
    747 {
    748 	const rb_tree_ops_t *rbto = rbt->rbt_ops;
    749 	struct rb_node *standin, *self = RB_ITEMTONODE(rbto, object);
    750 	unsigned int which;
    751 
    752 	KASSERT(!RB_SENTINEL_P(self));
    753 	RBSTAT_INC(rbt->rbt_removals);
    754 
    755 	/*
    756 	 * In the following diagrams, we (the node to be removed) are S.  Red
    757 	 * nodes are lowercase.  T could be either red or black.
    758 	 *
    759 	 * Remember the major axiom of the red-black tree: the number of
    760 	 * black nodes from the root to each leaf is constant across all
    761 	 * leaves, only the number of red nodes varies.
    762 	 *
    763 	 * Thus removing a red leaf doesn't require any other changes to a
    764 	 * red-black tree.  So if we must remove a node, attempt to rearrange
    765 	 * the tree so we can remove a red node.
    766 	 *
    767 	 * The simpliest case is a childless red node or a childless root node:
    768 	 *
    769 	 * |    T  -->    T  |    or    |  R  -->  *  |
    770 	 * |  s    -->  *    |
    771 	 */
    772 	if (RB_CHILDLESS_P(self)) {
    773 		const bool rebalance = RB_BLACK_P(self) && !RB_ROOT_P(rbt, self);
    774 		rb_tree_prune_node(rbt, self, rebalance);
    775 		return;
    776 	}
    777 	KASSERT(!RB_CHILDLESS_P(self));
    778 	if (!RB_TWOCHILDREN_P(self)) {
    779 		/*
    780 		 * The next simpliest case is the node we are deleting is
    781 		 * black and has one red child.
    782 		 *
    783 		 * |      T  -->      T  -->      T  |
    784 		 * |    S    -->  R      -->  R      |
    785 		 * |  r      -->    s    -->    *    |
    786 		 */
    787 		which = RB_LEFT_SENTINEL_P(self) ? RB_DIR_RIGHT : RB_DIR_LEFT;
    788 		KASSERT(RB_BLACK_P(self));
    789 		KASSERT(RB_RED_P(self->rb_nodes[which]));
    790 		KASSERT(RB_CHILDLESS_P(self->rb_nodes[which]));
    791 		rb_tree_prune_blackred_branch(rbt, self, which);
    792 		return;
    793 	}
    794 	KASSERT(RB_TWOCHILDREN_P(self));
    795 
    796 	/*
    797 	 * We invert these because we prefer to remove from the inside of
    798 	 * the tree.
    799 	 */
    800 	which = RB_POSITION(self) ^ RB_DIR_OTHER;
    801 
    802 	/*
    803 	 * Let's find the node closes to us opposite of our parent
    804 	 * Now swap it with ourself, "prune" it, and rebalance, if needed.
    805 	 */
    806 	standin = RB_ITEMTONODE(rbto, rb_tree_iterate(rbt, object, which));
    807 	rb_tree_swap_prune_and_rebalance(rbt, self, standin);
    808 }
    809 
    810 static void
    811 rb_tree_removal_rebalance(struct rb_tree *rbt, struct rb_node *parent,
    812 	unsigned int which)
    813 {
    814 	KASSERT(!RB_SENTINEL_P(parent));
    815 	KASSERT(RB_SENTINEL_P(parent->rb_nodes[which]));
    816 	KASSERT(which == RB_DIR_LEFT || which == RB_DIR_RIGHT);
    817 	RBSTAT_INC(rbt->rbt_removal_rebalance_calls);
    818 
    819 	while (RB_BLACK_P(parent->rb_nodes[which])) {
    820 		unsigned int other = which ^ RB_DIR_OTHER;
    821 		struct rb_node *brother = parent->rb_nodes[other];
    822 
    823 		RBSTAT_INC(rbt->rbt_removal_rebalance_passes);
    824 
    825 		KASSERT(!RB_SENTINEL_P(brother));
    826 		/*
    827 		 * For cases 1, 2a, and 2b, our brother's children must
    828 		 * be black and our father must be black
    829 		 */
    830 		if (RB_BLACK_P(parent)
    831 		    && RB_BLACK_P(brother->rb_left)
    832 		    && RB_BLACK_P(brother->rb_right)) {
    833 			if (RB_RED_P(brother)) {
    834 				/*
    835 				 * Case 1: Our brother is red, swap its
    836 				 * position (and colors) with our parent.
    837 				 * This should now be case 2b (unless C or E
    838 				 * has a red child which is case 3; thus no
    839 				 * explicit branch to case 2b).
    840 				 *
    841 				 *    B         ->        D
    842 				 *  A     d     ->    b     E
    843 				 *      C   E   ->  A   C
    844 				 */
    845 				KASSERT(RB_BLACK_P(parent));
    846 				rb_tree_reparent_nodes(rbt, parent, other);
    847 				brother = parent->rb_nodes[other];
    848 				KASSERT(!RB_SENTINEL_P(brother));
    849 				KASSERT(RB_RED_P(parent));
    850 				KASSERT(RB_BLACK_P(brother));
    851 				KASSERT(rb_tree_check_node(rbt, brother, NULL, false));
    852 				KASSERT(rb_tree_check_node(rbt, parent, NULL, false));
    853 			} else {
    854 				/*
    855 				 * Both our parent and brother are black.
    856 				 * Change our brother to red, advance up rank
    857 				 * and go through the loop again.
    858 				 *
    859 				 *    B         ->   *B
    860 				 * *A     D     ->  A     d
    861 				 *      C   E   ->      C   E
    862 				 */
    863 				RB_MARK_RED(brother);
    864 				KASSERT(RB_BLACK_P(brother->rb_left));
    865 				KASSERT(RB_BLACK_P(brother->rb_right));
    866 				if (RB_ROOT_P(rbt, parent))
    867 					return;	/* root == parent == black */
    868 				KASSERT(rb_tree_check_node(rbt, brother, NULL, false));
    869 				KASSERT(rb_tree_check_node(rbt, parent, NULL, false));
    870 				which = RB_POSITION(parent);
    871 				parent = RB_FATHER(parent);
    872 				continue;
    873 			}
    874 		}
    875 		/*
    876 		 * Avoid an else here so that case 2a above can hit either
    877 		 * case 2b, 3, or 4.
    878 		 */
    879 		if (RB_RED_P(parent)
    880 		    && RB_BLACK_P(brother)
    881 		    && RB_BLACK_P(brother->rb_left)
    882 		    && RB_BLACK_P(brother->rb_right)) {
    883 			KASSERT(RB_RED_P(parent));
    884 			KASSERT(RB_BLACK_P(brother));
    885 			KASSERT(RB_BLACK_P(brother->rb_left));
    886 			KASSERT(RB_BLACK_P(brother->rb_right));
    887 			/*
    888 			 * We are black, our father is red, our brother and
    889 			 * both nephews are black.  Simply invert/exchange the
    890 			 * colors of our father and brother (to black and red
    891 			 * respectively).
    892 			 *
    893 			 *	|    f        -->    F        |
    894 			 *	|  *     B    -->  *     b    |
    895 			 *	|      N   N  -->      N   N  |
    896 			 */
    897 			RB_MARK_BLACK(parent);
    898 			RB_MARK_RED(brother);
    899 			KASSERT(rb_tree_check_node(rbt, brother, NULL, true));
    900 			break;		/* We're done! */
    901 		} else {
    902 			/*
    903 			 * Our brother must be black and have at least one
    904 			 * red child (it may have two).
    905 			 */
    906 			KASSERT(RB_BLACK_P(brother));
    907 			KASSERT(RB_RED_P(brother->rb_nodes[which]) ||
    908 				RB_RED_P(brother->rb_nodes[other]));
    909 			if (RB_BLACK_P(brother->rb_nodes[other])) {
    910 				/*
    911 				 * Case 3: our brother is black, our near
    912 				 * nephew is red, and our far nephew is black.
    913 				 * Swap our brother with our near nephew.
    914 				 * This result in a tree that matches case 4.
    915 				 * (Our father could be red or black).
    916 				 *
    917 				 *	|    F      -->    F      |
    918 				 *	|  x     B  -->  x   B    |
    919 				 *	|      n    -->        n  |
    920 				 */
    921 				KASSERT(RB_RED_P(brother->rb_nodes[which]));
    922 				rb_tree_reparent_nodes(rbt, brother, which);
    923 				KASSERT(RB_FATHER(brother) == parent->rb_nodes[other]);
    924 				brother = parent->rb_nodes[other];
    925 				KASSERT(RB_RED_P(brother->rb_nodes[other]));
    926 			}
    927 			/*
    928 			 * Case 4: our brother is black and our far nephew
    929 			 * is red.  Swap our father and brother locations and
    930 			 * change our far nephew to black.  (these can be
    931 			 * done in either order so we change the color first).
    932 			 * The result is a valid red-black tree and is a
    933 			 * terminal case.  (again we don't care about the
    934 			 * father's color)
    935 			 *
    936 			 * If the father is red, we will get a red-black-black
    937 			 * tree:
    938 			 *	|  f      ->  f      -->    b    |
    939 			 *	|    B    ->    B    -->  F   N  |
    940 			 *	|      n  ->      N  -->         |
    941 			 *
    942 			 * If the father is black, we will get an all black
    943 			 * tree:
    944 			 *	|  F      ->  F      -->    B    |
    945 			 *	|    B    ->    B    -->  F   N  |
    946 			 *	|      n  ->      N  -->         |
    947 			 *
    948 			 * If we had two red nephews, then after the swap,
    949 			 * our former father would have a red grandson.
    950 			 */
    951 			KASSERT(RB_BLACK_P(brother));
    952 			KASSERT(RB_RED_P(brother->rb_nodes[other]));
    953 			RB_MARK_BLACK(brother->rb_nodes[other]);
    954 			rb_tree_reparent_nodes(rbt, parent, other);
    955 			break;		/* We're done! */
    956 		}
    957 	}
    958 	KASSERT(rb_tree_check_node(rbt, parent, NULL, true));
    959 }
    960 
    961 void *
    962 rb_tree_iterate(struct rb_tree *rbt, void *object, const unsigned int direction)
    963 {
    964 	const rb_tree_ops_t *rbto = rbt->rbt_ops;
    965 	const unsigned int other = direction ^ RB_DIR_OTHER;
    966 	struct rb_node *self;
    967 
    968 	KASSERT(direction == RB_DIR_LEFT || direction == RB_DIR_RIGHT);
    969 
    970 	if (object == NULL) {
    971 #ifndef RBSMALL
    972 		if (RB_SENTINEL_P(rbt->rbt_root))
    973 			return NULL;
    974 		return RB_NODETOITEM(rbto, rbt->rbt_minmax[direction]);
    975 #else
    976 		self = rbt->rbt_root;
    977 		if (RB_SENTINEL_P(self))
    978 			return NULL;
    979 		while (!RB_SENTINEL_P(self->rb_nodes[direction]))
    980 			self = self->rb_nodes[direction];
    981 		return RB_NODETOITEM(rbto, self);
    982 #endif /* !RBSMALL */
    983 	}
    984 	self = RB_ITEMTONODE(rbto, object);
    985 	KASSERT(!RB_SENTINEL_P(self));
    986 	/*
    987 	 * We can't go any further in this direction.  We proceed up in the
    988 	 * opposite direction until our parent is in direction we want to go.
    989 	 */
    990 	if (RB_SENTINEL_P(self->rb_nodes[direction])) {
    991 		while (!RB_ROOT_P(rbt, self)) {
    992 			if (other == RB_POSITION(self))
    993 				return RB_NODETOITEM(rbto, RB_FATHER(self));
    994 			self = RB_FATHER(self);
    995 		}
    996 		return NULL;
    997 	}
    998 
    999 	/*
   1000 	 * Advance down one in current direction and go down as far as possible
   1001 	 * in the opposite direction.
   1002 	 */
   1003 	self = self->rb_nodes[direction];
   1004 	KASSERT(!RB_SENTINEL_P(self));
   1005 	while (!RB_SENTINEL_P(self->rb_nodes[other]))
   1006 		self = self->rb_nodes[other];
   1007 	return RB_NODETOITEM(rbto, self);
   1008 }
   1009 
   1010 #ifdef RBDEBUG
   1011 static const struct rb_node *
   1012 rb_tree_iterate_const(const struct rb_tree *rbt, const struct rb_node *self,
   1013 	const unsigned int direction)
   1014 {
   1015 	const unsigned int other = direction ^ RB_DIR_OTHER;
   1016 	KASSERT(direction == RB_DIR_LEFT || direction == RB_DIR_RIGHT);
   1017 
   1018 	if (self == NULL) {
   1019 #ifndef RBSMALL
   1020 		if (RB_SENTINEL_P(rbt->rbt_root))
   1021 			return NULL;
   1022 		return rbt->rbt_minmax[direction];
   1023 #else
   1024 		self = rbt->rbt_root;
   1025 		if (RB_SENTINEL_P(self))
   1026 			return NULL;
   1027 		while (!RB_SENTINEL_P(self->rb_nodes[direction]))
   1028 			self = self->rb_nodes[direction];
   1029 		return self;
   1030 #endif /* !RBSMALL */
   1031 	}
   1032 	KASSERT(!RB_SENTINEL_P(self));
   1033 	/*
   1034 	 * We can't go any further in this direction.  We proceed up in the
   1035 	 * opposite direction until our parent is in direction we want to go.
   1036 	 */
   1037 	if (RB_SENTINEL_P(self->rb_nodes[direction])) {
   1038 		while (!RB_ROOT_P(rbt, self)) {
   1039 			if (other == RB_POSITION(self))
   1040 				return RB_FATHER(self);
   1041 			self = RB_FATHER(self);
   1042 		}
   1043 		return NULL;
   1044 	}
   1045 
   1046 	/*
   1047 	 * Advance down one in current direction and go down as far as possible
   1048 	 * in the opposite direction.
   1049 	 */
   1050 	self = self->rb_nodes[direction];
   1051 	KASSERT(!RB_SENTINEL_P(self));
   1052 	while (!RB_SENTINEL_P(self->rb_nodes[other]))
   1053 		self = self->rb_nodes[other];
   1054 	return self;
   1055 }
   1056 
   1057 static unsigned int
   1058 rb_tree_count_black(const struct rb_node *self)
   1059 {
   1060 	unsigned int left, right;
   1061 
   1062 	if (RB_SENTINEL_P(self))
   1063 		return 0;
   1064 
   1065 	left = rb_tree_count_black(self->rb_left);
   1066 	right = rb_tree_count_black(self->rb_right);
   1067 
   1068 	KASSERT(left == right);
   1069 
   1070 	return left + RB_BLACK_P(self);
   1071 }
   1072 
   1073 static bool
   1074 rb_tree_check_node(const struct rb_tree *rbt, const struct rb_node *self,
   1075 	const struct rb_node *prev, bool red_check)
   1076 {
   1077 	const rb_tree_ops_t *rbto = rbt->rbt_ops;
   1078 	rbto_compare_nodes_fn compare_nodes = rbto->rbto_compare_nodes;
   1079 
   1080 	KASSERT(!RB_SENTINEL_P(self));
   1081 	KASSERT(prev == NULL || (*compare_nodes)(rbto->rbto_context,
   1082 	    RB_NODETOITEM(rbto, prev), RB_NODETOITEM(rbto, self)) < 0);
   1083 
   1084 	/*
   1085 	 * Verify our relationship to our parent.
   1086 	 */
   1087 	if (RB_ROOT_P(rbt, self)) {
   1088 		KASSERT(self == rbt->rbt_root);
   1089 		KASSERT(RB_POSITION(self) == RB_DIR_LEFT);
   1090 		KASSERT(RB_FATHER(self)->rb_nodes[RB_DIR_LEFT] == self);
   1091 		KASSERT(RB_FATHER(self) == (const struct rb_node *) &rbt->rbt_root);
   1092 	} else {
   1093 		int diff = (*compare_nodes)(rbto->rbto_context,
   1094 		    RB_NODETOITEM(rbto, self),
   1095 		    RB_NODETOITEM(rbto, RB_FATHER(self)));
   1096 
   1097 		KASSERT(self != rbt->rbt_root);
   1098 		KASSERT(!RB_FATHER_SENTINEL_P(self));
   1099 		if (RB_POSITION(self) == RB_DIR_LEFT) {
   1100 			KASSERT(diff < 0);
   1101 			KASSERT(RB_FATHER(self)->rb_nodes[RB_DIR_LEFT] == self);
   1102 		} else {
   1103 			KASSERT(diff > 0);
   1104 			KASSERT(RB_FATHER(self)->rb_nodes[RB_DIR_RIGHT] == self);
   1105 		}
   1106 	}
   1107 
   1108 	/*
   1109 	 * Verify our position in the linked list against the tree itself.
   1110 	 */
   1111 	{
   1112 		const struct rb_node *prev0 = rb_tree_iterate_const(rbt, self, RB_DIR_LEFT);
   1113 		const struct rb_node *next0 = rb_tree_iterate_const(rbt, self, RB_DIR_RIGHT);
   1114 		KASSERT(prev0 == TAILQ_PREV(self, rb_node_qh, rb_link));
   1115 		KASSERT(next0 == TAILQ_NEXT(self, rb_link));
   1116 #ifndef RBSMALL
   1117 		KASSERT(prev0 != NULL || self == rbt->rbt_minmax[RB_DIR_LEFT]);
   1118 		KASSERT(next0 != NULL || self == rbt->rbt_minmax[RB_DIR_RIGHT]);
   1119 #endif
   1120 	}
   1121 
   1122 	/*
   1123 	 * The root must be black.
   1124 	 * There can never be two adjacent red nodes.
   1125 	 */
   1126 	if (red_check) {
   1127 		KASSERT(!RB_ROOT_P(rbt, self) || RB_BLACK_P(self));
   1128 		(void) rb_tree_count_black(self);
   1129 		if (RB_RED_P(self)) {
   1130 			const struct rb_node *brother;
   1131 			KASSERT(!RB_ROOT_P(rbt, self));
   1132 			brother = RB_FATHER(self)->rb_nodes[RB_POSITION(self) ^ RB_DIR_OTHER];
   1133 			KASSERT(RB_BLACK_P(RB_FATHER(self)));
   1134 			/*
   1135 			 * I'm red and have no children, then I must either
   1136 			 * have no brother or my brother also be red and
   1137 			 * also have no children.  (black count == 0)
   1138 			 */
   1139 			KASSERT(!RB_CHILDLESS_P(self)
   1140 				|| RB_SENTINEL_P(brother)
   1141 				|| RB_RED_P(brother)
   1142 				|| RB_CHILDLESS_P(brother));
   1143 			/*
   1144 			 * If I'm not childless, I must have two children
   1145 			 * and they must be both be black.
   1146 			 */
   1147 			KASSERT(RB_CHILDLESS_P(self)
   1148 				|| (RB_TWOCHILDREN_P(self)
   1149 				    && RB_BLACK_P(self->rb_left)
   1150 				    && RB_BLACK_P(self->rb_right)));
   1151 			/*
   1152 			 * If I'm not childless, thus I have black children,
   1153 			 * then my brother must either be black or have two
   1154 			 * black children.
   1155 			 */
   1156 			KASSERT(RB_CHILDLESS_P(self)
   1157 				|| RB_BLACK_P(brother)
   1158 				|| (RB_TWOCHILDREN_P(brother)
   1159 				    && RB_BLACK_P(brother->rb_left)
   1160 				    && RB_BLACK_P(brother->rb_right)));
   1161 		} else {
   1162 			/*
   1163 			 * If I'm black and have one child, that child must
   1164 			 * be red and childless.
   1165 			 */
   1166 			KASSERT(RB_CHILDLESS_P(self)
   1167 				|| RB_TWOCHILDREN_P(self)
   1168 				|| (!RB_LEFT_SENTINEL_P(self)
   1169 				    && RB_RIGHT_SENTINEL_P(self)
   1170 				    && RB_RED_P(self->rb_left)
   1171 				    && RB_CHILDLESS_P(self->rb_left))
   1172 				|| (!RB_RIGHT_SENTINEL_P(self)
   1173 				    && RB_LEFT_SENTINEL_P(self)
   1174 				    && RB_RED_P(self->rb_right)
   1175 				    && RB_CHILDLESS_P(self->rb_right)));
   1176 
   1177 			/*
   1178 			 * If I'm a childless black node and my parent is
   1179 			 * black, my 2nd closet relative away from my parent
   1180 			 * is either red or has a red parent or red children.
   1181 			 */
   1182 			if (!RB_ROOT_P(rbt, self)
   1183 			    && RB_CHILDLESS_P(self)
   1184 			    && RB_BLACK_P(RB_FATHER(self))) {
   1185 				const unsigned int which = RB_POSITION(self);
   1186 				const unsigned int other = which ^ RB_DIR_OTHER;
   1187 				const struct rb_node *relative0, *relative;
   1188 
   1189 				relative0 = rb_tree_iterate_const(rbt,
   1190 				    self, other);
   1191 				KASSERT(relative0 != NULL);
   1192 				relative = rb_tree_iterate_const(rbt,
   1193 				    relative0, other);
   1194 				KASSERT(relative != NULL);
   1195 				KASSERT(RB_SENTINEL_P(relative->rb_nodes[which]));
   1196 #if 0
   1197 				KASSERT(RB_RED_P(relative)
   1198 					|| RB_RED_P(relative->rb_left)
   1199 					|| RB_RED_P(relative->rb_right)
   1200 					|| RB_RED_P(RB_FATHER(relative)));
   1201 #endif
   1202 			}
   1203 		}
   1204 		/*
   1205 		 * A grandparent's children must be real nodes and not
   1206 		 * sentinels.  First check out grandparent.
   1207 		 */
   1208 		KASSERT(RB_ROOT_P(rbt, self)
   1209 			|| RB_ROOT_P(rbt, RB_FATHER(self))
   1210 			|| RB_TWOCHILDREN_P(RB_FATHER(RB_FATHER(self))));
   1211 		/*
   1212 		 * If we are have grandchildren on our left, then
   1213 		 * we must have a child on our right.
   1214 		 */
   1215 		KASSERT(RB_LEFT_SENTINEL_P(self)
   1216 			|| RB_CHILDLESS_P(self->rb_left)
   1217 			|| !RB_RIGHT_SENTINEL_P(self));
   1218 		/*
   1219 		 * If we are have grandchildren on our right, then
   1220 		 * we must have a child on our left.
   1221 		 */
   1222 		KASSERT(RB_RIGHT_SENTINEL_P(self)
   1223 			|| RB_CHILDLESS_P(self->rb_right)
   1224 			|| !RB_LEFT_SENTINEL_P(self));
   1225 
   1226 		/*
   1227 		 * If we have a child on the left and it doesn't have two
   1228 		 * children make sure we don't have great-great-grandchildren on
   1229 		 * the right.
   1230 		 */
   1231 		KASSERT(RB_TWOCHILDREN_P(self->rb_left)
   1232 			|| RB_CHILDLESS_P(self->rb_right)
   1233 			|| RB_CHILDLESS_P(self->rb_right->rb_left)
   1234 			|| RB_CHILDLESS_P(self->rb_right->rb_left->rb_left)
   1235 			|| RB_CHILDLESS_P(self->rb_right->rb_left->rb_right)
   1236 			|| RB_CHILDLESS_P(self->rb_right->rb_right)
   1237 			|| RB_CHILDLESS_P(self->rb_right->rb_right->rb_left)
   1238 			|| RB_CHILDLESS_P(self->rb_right->rb_right->rb_right));
   1239 
   1240 		/*
   1241 		 * If we have a child on the right and it doesn't have two
   1242 		 * children make sure we don't have great-great-grandchildren on
   1243 		 * the left.
   1244 		 */
   1245 		KASSERT(RB_TWOCHILDREN_P(self->rb_right)
   1246 			|| RB_CHILDLESS_P(self->rb_left)
   1247 			|| RB_CHILDLESS_P(self->rb_left->rb_left)
   1248 			|| RB_CHILDLESS_P(self->rb_left->rb_left->rb_left)
   1249 			|| RB_CHILDLESS_P(self->rb_left->rb_left->rb_right)
   1250 			|| RB_CHILDLESS_P(self->rb_left->rb_right)
   1251 			|| RB_CHILDLESS_P(self->rb_left->rb_right->rb_left)
   1252 			|| RB_CHILDLESS_P(self->rb_left->rb_right->rb_right));
   1253 
   1254 		/*
   1255 		 * If we are fully interior node, then our predecessors and
   1256 		 * successors must have no children in our direction.
   1257 		 */
   1258 		if (RB_TWOCHILDREN_P(self)) {
   1259 			const struct rb_node *prev0;
   1260 			const struct rb_node *next0;
   1261 
   1262 			prev0 = rb_tree_iterate_const(rbt, self, RB_DIR_LEFT);
   1263 			KASSERT(prev0 != NULL);
   1264 			KASSERT(RB_RIGHT_SENTINEL_P(prev0));
   1265 
   1266 			next0 = rb_tree_iterate_const(rbt, self, RB_DIR_RIGHT);
   1267 			KASSERT(next0 != NULL);
   1268 			KASSERT(RB_LEFT_SENTINEL_P(next0));
   1269 		}
   1270 	}
   1271 
   1272 	return true;
   1273 }
   1274 
   1275 void
   1276 rb_tree_check(const struct rb_tree *rbt, bool red_check)
   1277 {
   1278 	const struct rb_node *self;
   1279 	const struct rb_node *prev;
   1280 #ifdef RBSTATS
   1281 	unsigned int count = 0;
   1282 #endif
   1283 
   1284 	KASSERT(rbt->rbt_root != NULL);
   1285 	KASSERT(RB_LEFT_P(rbt->rbt_root));
   1286 
   1287 #if defined(RBSTATS) && !defined(RBSMALL)
   1288 	KASSERT(rbt->rbt_count > 1
   1289 	    || rbt->rbt_minmax[RB_DIR_LEFT] == rbt->rbt_minmax[RB_DIR_RIGHT]);
   1290 #endif
   1291 
   1292 	prev = NULL;
   1293 	TAILQ_FOREACH(self, &rbt->rbt_nodes, rb_link) {
   1294 		rb_tree_check_node(rbt, self, prev, false);
   1295 #ifdef RBSTATS
   1296 		count++;
   1297 #endif
   1298 	}
   1299 #ifdef RBSTATS
   1300 	KASSERT(rbt->rbt_count == count);
   1301 #endif
   1302 	if (red_check) {
   1303 		KASSERT(RB_BLACK_P(rbt->rbt_root));
   1304 		KASSERT(RB_SENTINEL_P(rbt->rbt_root)
   1305 			|| rb_tree_count_black(rbt->rbt_root));
   1306 
   1307 		/*
   1308 		 * The root must be black.
   1309 		 * There can never be two adjacent red nodes.
   1310 		 */
   1311 		TAILQ_FOREACH(self, &rbt->rbt_nodes, rb_link) {
   1312 			rb_tree_check_node(rbt, self, NULL, true);
   1313 		}
   1314 	}
   1315 }
   1316 #endif /* RBDEBUG */
   1317 
   1318 #ifdef RBSTATS
   1319 static void
   1320 rb_tree_mark_depth(const struct rb_tree *rbt, const struct rb_node *self,
   1321 	size_t *depths, size_t depth)
   1322 {
   1323 	if (RB_SENTINEL_P(self))
   1324 		return;
   1325 
   1326 	if (RB_TWOCHILDREN_P(self)) {
   1327 		rb_tree_mark_depth(rbt, self->rb_left, depths, depth + 1);
   1328 		rb_tree_mark_depth(rbt, self->rb_right, depths, depth + 1);
   1329 		return;
   1330 	}
   1331 	depths[depth]++;
   1332 	if (!RB_LEFT_SENTINEL_P(self)) {
   1333 		rb_tree_mark_depth(rbt, self->rb_left, depths, depth + 1);
   1334 	}
   1335 	if (!RB_RIGHT_SENTINEL_P(self)) {
   1336 		rb_tree_mark_depth(rbt, self->rb_right, depths, depth + 1);
   1337 	}
   1338 }
   1339 
   1340 void
   1341 rb_tree_depths(const struct rb_tree *rbt, size_t *depths)
   1342 {
   1343 	rb_tree_mark_depth(rbt, rbt->rbt_root, depths, 1);
   1344 }
   1345 #endif /* RBSTATS */
   1346