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