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