SYSENTER/SYSCALL support
[minix.git] / common / lib / libprop / prop_rb.c
blob83657bc288f435bd18d8d804563603ac36084198
1 /* $NetBSD: prop_rb.c,v 1.9 2008/06/17 21:29:47 thorpej Exp $ */
3 /*-
4 * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Matt Thomas <matt@3am-software.com>.
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.
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.
32 #include <prop/proplib.h>
34 #include "prop_object_impl.h"
35 #include "prop_rb_impl.h"
37 #undef KASSERT
38 #ifdef RBDEBUG
39 #define KASSERT(x) _PROP_ASSERT(x)
40 #else
41 #define KASSERT(x) /* nothing */
42 #endif
44 #ifndef __predict_false
45 #define __predict_false(x) (x)
46 #endif
48 static void rb_tree_reparent_nodes(struct rb_tree *, struct rb_node *,
49 unsigned int);
50 static void rb_tree_insert_rebalance(struct rb_tree *, struct rb_node *);
51 static void rb_tree_removal_rebalance(struct rb_tree *, struct rb_node *,
52 unsigned int);
53 #ifdef RBDEBUG
54 static const struct rb_node *rb_tree_iterate_const(const struct rb_tree *,
55 const struct rb_node *, unsigned int);
56 static bool rb_tree_check_node(const struct rb_tree *, const struct rb_node *,
57 const struct rb_node *, bool);
58 #endif
60 #ifdef RBDEBUG
61 #define RBT_COUNT_INCR(rbt) (rbt)->rbt_count++
62 #define RBT_COUNT_DECR(rbt) (rbt)->rbt_count--
63 #else
64 #define RBT_COUNT_INCR(rbt) /* nothing */
65 #define RBT_COUNT_DECR(rbt) /* nothing */
66 #endif
68 #define RBUNCONST(a) ((void *)(unsigned long)(const void *)(a))
71 * Rather than testing for the NULL everywhere, all terminal leaves are
72 * pointed to this node (and that includes itself). Note that by setting
73 * it to be const, that on some architectures trying to write to it will
74 * cause a fault.
76 static const struct rb_node sentinel_node = {
77 .rb_nodes = { RBUNCONST(&sentinel_node),
78 RBUNCONST(&sentinel_node),
79 NULL },
80 .rb_u = { .u_s = { .s_sentinel = 1 } },
83 void
84 _prop_rb_tree_init(struct rb_tree *rbt, const struct rb_tree_ops *ops)
86 RB_TAILQ_INIT(&rbt->rbt_nodes);
87 #ifdef RBDEBUG
88 rbt->rbt_count = 0;
89 #endif
90 rbt->rbt_ops = ops;
91 *((const struct rb_node **)&rbt->rbt_root) = &sentinel_node;
95 * Swap the location and colors of 'self' and its child @ which. The child
96 * can not be a sentinel node.
98 /*ARGSUSED*/
99 static void
100 rb_tree_reparent_nodes(struct rb_tree *rbt _PROP_ARG_UNUSED,
101 struct rb_node *old_father, unsigned int which)
103 const unsigned int other = which ^ RB_NODE_OTHER;
104 struct rb_node * const grandpa = old_father->rb_parent;
105 struct rb_node * const old_child = old_father->rb_nodes[which];
106 struct rb_node * const new_father = old_child;
107 struct rb_node * const new_child = old_father;
108 unsigned int properties;
110 KASSERT(which == RB_NODE_LEFT || which == RB_NODE_RIGHT);
112 KASSERT(!RB_SENTINEL_P(old_child));
113 KASSERT(old_child->rb_parent == old_father);
115 KASSERT(rb_tree_check_node(rbt, old_father, NULL, false));
116 KASSERT(rb_tree_check_node(rbt, old_child, NULL, false));
117 KASSERT(RB_ROOT_P(old_father) || rb_tree_check_node(rbt, grandpa, NULL, false));
120 * Exchange descendant linkages.
122 grandpa->rb_nodes[old_father->rb_position] = new_father;
123 new_child->rb_nodes[which] = old_child->rb_nodes[other];
124 new_father->rb_nodes[other] = new_child;
127 * Update ancestor linkages
129 new_father->rb_parent = grandpa;
130 new_child->rb_parent = new_father;
133 * Exchange properties between new_father and new_child. The only
134 * change is that new_child's position is now on the other side.
136 properties = old_child->rb_properties;
137 new_father->rb_properties = old_father->rb_properties;
138 new_child->rb_properties = properties;
139 new_child->rb_position = other;
142 * Make sure to reparent the new child to ourself.
144 if (!RB_SENTINEL_P(new_child->rb_nodes[which])) {
145 new_child->rb_nodes[which]->rb_parent = new_child;
146 new_child->rb_nodes[which]->rb_position = which;
149 KASSERT(rb_tree_check_node(rbt, new_father, NULL, false));
150 KASSERT(rb_tree_check_node(rbt, new_child, NULL, false));
151 KASSERT(RB_ROOT_P(new_father) || rb_tree_check_node(rbt, grandpa, NULL, false));
154 bool
155 _prop_rb_tree_insert_node(struct rb_tree *rbt, struct rb_node *self)
157 struct rb_node *parent, *tmp;
158 rb_compare_nodes_fn compare_nodes = rbt->rbt_ops->rbto_compare_nodes;
159 unsigned int position;
161 self->rb_properties = 0;
162 tmp = rbt->rbt_root;
164 * This is a hack. Because rbt->rbt_root is just a struct rb_node *,
165 * just like rb_node->rb_nodes[RB_NODE_LEFT], we can use this fact to
166 * avoid a lot of tests for root and know that even at root,
167 * updating rb_node->rb_parent->rb_nodes[rb_node->rb_position] will
168 * rbt->rbt_root.
170 /* LINTED: see above */
171 parent = (struct rb_node *)&rbt->rbt_root;
172 position = RB_NODE_LEFT;
175 * Find out where to place this new leaf.
177 while (!RB_SENTINEL_P(tmp)) {
178 const int diff = (*compare_nodes)(tmp, self);
179 if (__predict_false(diff == 0)) {
181 * Node already exists; don't insert.
183 return false;
185 parent = tmp;
186 KASSERT(diff != 0);
187 if (diff < 0) {
188 position = RB_NODE_LEFT;
189 } else {
190 position = RB_NODE_RIGHT;
192 tmp = parent->rb_nodes[position];
195 #ifdef RBDEBUG
197 struct rb_node *prev = NULL, *next = NULL;
199 if (position == RB_NODE_RIGHT)
200 prev = parent;
201 else if (tmp != rbt->rbt_root)
202 next = parent;
205 * Verify our sequential position
207 KASSERT(prev == NULL || !RB_SENTINEL_P(prev));
208 KASSERT(next == NULL || !RB_SENTINEL_P(next));
209 if (prev != NULL && next == NULL)
210 next = TAILQ_NEXT(prev, rb_link);
211 if (prev == NULL && next != NULL)
212 prev = TAILQ_PREV(next, rb_node_qh, rb_link);
213 KASSERT(prev == NULL || !RB_SENTINEL_P(prev));
214 KASSERT(next == NULL || !RB_SENTINEL_P(next));
215 KASSERT(prev == NULL
216 || (*compare_nodes)(prev, self) > 0);
217 KASSERT(next == NULL
218 || (*compare_nodes)(self, next) > 0);
220 #endif
223 * Initialize the node and insert as a leaf into the tree.
225 self->rb_parent = parent;
226 self->rb_position = position;
227 /* LINTED: rbt_root hack */
228 if (__predict_false(parent == (struct rb_node *) &rbt->rbt_root)) {
229 RB_MARK_ROOT(self);
230 } else {
231 KASSERT(position == RB_NODE_LEFT || position == RB_NODE_RIGHT);
232 KASSERT(!RB_ROOT_P(self)); /* Already done */
234 KASSERT(RB_SENTINEL_P(parent->rb_nodes[position]));
235 self->rb_left = parent->rb_nodes[position];
236 self->rb_right = parent->rb_nodes[position];
237 parent->rb_nodes[position] = self;
238 KASSERT(self->rb_left == &sentinel_node &&
239 self->rb_right == &sentinel_node);
242 * Insert the new node into a sorted list for easy sequential access
244 RBT_COUNT_INCR(rbt);
245 #ifdef RBDEBUG
246 if (RB_ROOT_P(self)) {
247 RB_TAILQ_INSERT_HEAD(&rbt->rbt_nodes, self, rb_link);
248 } else if (position == RB_NODE_LEFT) {
249 KASSERT((*compare_nodes)(self, self->rb_parent) > 0);
250 RB_TAILQ_INSERT_BEFORE(self->rb_parent, self, rb_link);
251 } else {
252 KASSERT((*compare_nodes)(self->rb_parent, self) > 0);
253 RB_TAILQ_INSERT_AFTER(&rbt->rbt_nodes, self->rb_parent,
254 self, rb_link);
256 #endif
258 #if 0
260 * Validate the tree before we rebalance
262 _prop_rb_tree_check(rbt, false);
263 #endif
266 * Rebalance tree after insertion
268 rb_tree_insert_rebalance(rbt, self);
270 #if 0
272 * Validate the tree after we rebalanced
274 _prop_rb_tree_check(rbt, true);
275 #endif
277 return true;
280 static void
281 rb_tree_insert_rebalance(struct rb_tree *rbt, struct rb_node *self)
283 RB_MARK_RED(self);
285 while (!RB_ROOT_P(self) && RB_RED_P(self->rb_parent)) {
286 const unsigned int which =
287 (self->rb_parent == self->rb_parent->rb_parent->rb_left
288 ? RB_NODE_LEFT
289 : RB_NODE_RIGHT);
290 const unsigned int other = which ^ RB_NODE_OTHER;
291 struct rb_node * father = self->rb_parent;
292 struct rb_node * grandpa = father->rb_parent;
293 struct rb_node * const uncle = grandpa->rb_nodes[other];
295 KASSERT(!RB_SENTINEL_P(self));
297 * We are red and our parent is red, therefore we must have a
298 * grandfather and he must be black.
300 KASSERT(RB_RED_P(self)
301 && RB_RED_P(father)
302 && RB_BLACK_P(grandpa));
304 if (RB_RED_P(uncle)) {
306 * Case 1: our uncle is red
307 * Simply invert the colors of our parent and
308 * uncle and make our grandparent red. And
309 * then solve the problem up at his level.
311 RB_MARK_BLACK(uncle);
312 RB_MARK_BLACK(father);
313 RB_MARK_RED(grandpa);
314 self = grandpa;
315 continue;
318 * Case 2&3: our uncle is black.
320 if (self == father->rb_nodes[other]) {
322 * Case 2: we are on the same side as our uncle
323 * Swap ourselves with our parent so this case
324 * becomes case 3. Basically our parent becomes our
325 * child.
327 rb_tree_reparent_nodes(rbt, father, other);
328 KASSERT(father->rb_parent == self);
329 KASSERT(self->rb_nodes[which] == father);
330 KASSERT(self->rb_parent == grandpa);
331 self = father;
332 father = self->rb_parent;
334 KASSERT(RB_RED_P(self) && RB_RED_P(father));
335 KASSERT(grandpa->rb_nodes[which] == father);
337 * Case 3: we are opposite a child of a black uncle.
338 * Swap our parent and grandparent. Since our grandfather
339 * is black, our father will become black and our new sibling
340 * (former grandparent) will become red.
342 rb_tree_reparent_nodes(rbt, grandpa, which);
343 KASSERT(self->rb_parent == father);
344 KASSERT(self->rb_parent->rb_nodes[self->rb_position ^ RB_NODE_OTHER] == grandpa);
345 KASSERT(RB_RED_P(self));
346 KASSERT(RB_BLACK_P(father));
347 KASSERT(RB_RED_P(grandpa));
348 break;
352 * Final step: Set the root to black.
354 RB_MARK_BLACK(rbt->rbt_root);
357 struct rb_node *
358 _prop_rb_tree_find(struct rb_tree *rbt, const void *key)
360 struct rb_node *parent = rbt->rbt_root;
361 rb_compare_key_fn compare_key = rbt->rbt_ops->rbto_compare_key;
363 while (!RB_SENTINEL_P(parent)) {
364 const int diff = (*compare_key)(parent, key);
365 if (diff == 0)
366 return parent;
367 parent = parent->rb_nodes[diff > 0];
370 return NULL;
373 static void
374 rb_tree_prune_node(struct rb_tree *rbt, struct rb_node *self, int rebalance)
376 const unsigned int which = self->rb_position;
377 struct rb_node *father = self->rb_parent;
379 KASSERT(rebalance || (RB_ROOT_P(self) || RB_RED_P(self)));
380 KASSERT(!rebalance || RB_BLACK_P(self));
381 KASSERT(RB_CHILDLESS_P(self));
382 KASSERT(rb_tree_check_node(rbt, self, NULL, false));
384 father->rb_nodes[which] = self->rb_left;
387 * Remove ourselves from the node list and decrement the count.
389 RB_TAILQ_REMOVE(&rbt->rbt_nodes, self, rb_link);
390 RBT_COUNT_DECR(rbt);
392 if (rebalance)
393 rb_tree_removal_rebalance(rbt, father, which);
394 KASSERT(RB_ROOT_P(self) || rb_tree_check_node(rbt, father, NULL, true));
397 static void
398 rb_tree_swap_prune_and_rebalance(struct rb_tree *rbt, struct rb_node *self,
399 struct rb_node *standin)
401 unsigned int standin_which = standin->rb_position;
402 unsigned int standin_other = standin_which ^ RB_NODE_OTHER;
403 struct rb_node *standin_child;
404 struct rb_node *standin_father;
405 bool rebalance = RB_BLACK_P(standin);
407 if (standin->rb_parent == self) {
409 * As a child of self, any childen would be opposite of
410 * our parent (self).
412 KASSERT(RB_SENTINEL_P(standin->rb_nodes[standin_other]));
413 standin_child = standin->rb_nodes[standin_which];
414 } else {
416 * Since we aren't a child of self, any childen would be
417 * on the same side as our parent (self).
419 KASSERT(RB_SENTINEL_P(standin->rb_nodes[standin_which]));
420 standin_child = standin->rb_nodes[standin_other];
424 * the node we are removing must have two children.
426 KASSERT(RB_TWOCHILDREN_P(self));
428 * If standin has a child, it must be red.
430 KASSERT(RB_SENTINEL_P(standin_child) || RB_RED_P(standin_child));
433 * Verify things are sane.
435 KASSERT(rb_tree_check_node(rbt, self, NULL, false));
436 KASSERT(rb_tree_check_node(rbt, standin, NULL, false));
438 if (!RB_SENTINEL_P(standin_child)) {
440 * We know we have a red child so if we swap them we can
441 * void flipping standin's child to black afterwards.
443 KASSERT(rb_tree_check_node(rbt, standin_child, NULL, true));
444 rb_tree_reparent_nodes(rbt, standin,
445 standin_child->rb_position);
446 KASSERT(rb_tree_check_node(rbt, standin, NULL, true));
447 KASSERT(rb_tree_check_node(rbt, standin_child, NULL, true));
449 * Since we are removing a red leaf, no need to rebalance.
451 rebalance = false;
453 * We know that standin can not be a child of self, so
454 * update before of that.
456 KASSERT(standin->rb_parent != self);
457 standin_which = standin->rb_position;
458 standin_other = standin_which ^ RB_NODE_OTHER;
460 KASSERT(RB_CHILDLESS_P(standin));
463 * If we are about to delete the standin's father, then when we call
464 * rebalance, we need to use ourselves as our father. Otherwise
465 * remember our original father. Also, if we are our standin's father
466 * we only need to reparent the standin's brother.
468 if (standin->rb_parent == self) {
470 * | R --> S |
471 * | Q S --> Q * |
472 * | --> |
474 standin_father = standin;
475 KASSERT(RB_SENTINEL_P(standin->rb_nodes[standin_other]));
476 KASSERT(!RB_SENTINEL_P(self->rb_nodes[standin_other]));
477 KASSERT(self->rb_nodes[standin_which] == standin);
479 * Make our brother our son.
481 standin->rb_nodes[standin_other] = self->rb_nodes[standin_other];
482 standin->rb_nodes[standin_other]->rb_parent = standin;
483 KASSERT(standin->rb_nodes[standin_other]->rb_position == standin_other);
484 } else {
486 * | P --> P |
487 * | S --> Q |
488 * | Q --> |
490 standin_father = standin->rb_parent;
491 standin_father->rb_nodes[standin_which] =
492 standin->rb_nodes[standin_which];
493 standin->rb_left = self->rb_left;
494 standin->rb_right = self->rb_right;
495 standin->rb_left->rb_parent = standin;
496 standin->rb_right->rb_parent = standin;
500 * Now copy the result of self to standin and then replace
501 * self with standin in the tree.
503 standin->rb_parent = self->rb_parent;
504 standin->rb_properties = self->rb_properties;
505 standin->rb_parent->rb_nodes[standin->rb_position] = standin;
508 * Remove ourselves from the node list and decrement the count.
510 RB_TAILQ_REMOVE(&rbt->rbt_nodes, self, rb_link);
511 RBT_COUNT_DECR(rbt);
513 KASSERT(rb_tree_check_node(rbt, standin, NULL, false));
514 KASSERT(rb_tree_check_node(rbt, standin_father, NULL, false));
516 if (!rebalance)
517 return;
519 rb_tree_removal_rebalance(rbt, standin_father, standin_which);
520 KASSERT(rb_tree_check_node(rbt, standin, NULL, true));
524 * We could do this by doing
525 * rb_tree_node_swap(rbt, self, which);
526 * rb_tree_prune_node(rbt, self, false);
528 * But it's more efficient to just evalate and recolor the child.
530 /*ARGSUSED*/
531 static void
532 rb_tree_prune_blackred_branch(struct rb_tree *rbt _PROP_ARG_UNUSED,
533 struct rb_node *self, unsigned int which)
535 struct rb_node *parent = self->rb_parent;
536 struct rb_node *child = self->rb_nodes[which];
538 KASSERT(which == RB_NODE_LEFT || which == RB_NODE_RIGHT);
539 KASSERT(RB_BLACK_P(self) && RB_RED_P(child));
540 KASSERT(!RB_TWOCHILDREN_P(child));
541 KASSERT(RB_CHILDLESS_P(child));
542 KASSERT(rb_tree_check_node(rbt, self, NULL, false));
543 KASSERT(rb_tree_check_node(rbt, child, NULL, false));
546 * Remove ourselves from the tree and give our former child our
547 * properties (position, color, root).
549 parent->rb_nodes[self->rb_position] = child;
550 child->rb_parent = parent;
551 child->rb_properties = self->rb_properties;
554 * Remove ourselves from the node list and decrement the count.
556 RB_TAILQ_REMOVE(&rbt->rbt_nodes, self, rb_link);
557 RBT_COUNT_DECR(rbt);
559 KASSERT(RB_ROOT_P(self) || rb_tree_check_node(rbt, parent, NULL, true));
560 KASSERT(rb_tree_check_node(rbt, child, NULL, true));
565 void
566 _prop_rb_tree_remove_node(struct rb_tree *rbt, struct rb_node *self)
568 struct rb_node *standin;
569 unsigned int which;
571 * In the following diagrams, we (the node to be removed) are S. Red
572 * nodes are lowercase. T could be either red or black.
574 * Remember the major axiom of the red-black tree: the number of
575 * black nodes from the root to each leaf is constant across all
576 * leaves, only the number of red nodes varies.
578 * Thus removing a red leaf doesn't require any other changes to a
579 * red-black tree. So if we must remove a node, attempt to rearrange
580 * the tree so we can remove a red node.
582 * The simpliest case is a childless red node or a childless root node:
584 * | T --> T | or | R --> * |
585 * | s --> * |
587 if (RB_CHILDLESS_P(self)) {
588 if (RB_RED_P(self) || RB_ROOT_P(self)) {
589 rb_tree_prune_node(rbt, self, false);
590 return;
592 rb_tree_prune_node(rbt, self, true);
593 return;
595 KASSERT(!RB_CHILDLESS_P(self));
596 if (!RB_TWOCHILDREN_P(self)) {
598 * The next simpliest case is the node we are deleting is
599 * black and has one red child.
601 * | T --> T --> T |
602 * | S --> R --> R |
603 * | r --> s --> * |
605 which = RB_LEFT_SENTINEL_P(self) ? RB_NODE_RIGHT : RB_NODE_LEFT;
606 KASSERT(RB_BLACK_P(self));
607 KASSERT(RB_RED_P(self->rb_nodes[which]));
608 KASSERT(RB_CHILDLESS_P(self->rb_nodes[which]));
609 rb_tree_prune_blackred_branch(rbt, self, which);
610 return;
612 KASSERT(RB_TWOCHILDREN_P(self));
615 * We invert these because we prefer to remove from the inside of
616 * the tree.
618 which = self->rb_position ^ RB_NODE_OTHER;
621 * Let's find the node closes to us opposite of our parent
622 * Now swap it with ourself, "prune" it, and rebalance, if needed.
624 standin = _prop_rb_tree_iterate(rbt, self, which);
625 rb_tree_swap_prune_and_rebalance(rbt, self, standin);
628 static void
629 rb_tree_removal_rebalance(struct rb_tree *rbt, struct rb_node *parent,
630 unsigned int which)
632 KASSERT(!RB_SENTINEL_P(parent));
633 KASSERT(RB_SENTINEL_P(parent->rb_nodes[which]));
634 KASSERT(which == RB_NODE_LEFT || which == RB_NODE_RIGHT);
636 while (RB_BLACK_P(parent->rb_nodes[which])) {
637 unsigned int other = which ^ RB_NODE_OTHER;
638 struct rb_node *brother = parent->rb_nodes[other];
640 KASSERT(!RB_SENTINEL_P(brother));
642 * For cases 1, 2a, and 2b, our brother's children must
643 * be black and our father must be black
645 if (RB_BLACK_P(parent)
646 && RB_BLACK_P(brother->rb_left)
647 && RB_BLACK_P(brother->rb_right)) {
649 * Case 1: Our brother is red, swap its position
650 * (and colors) with our parent. This is now case 2b.
652 * B -> D
653 * x d -> b E
654 * C E -> x C
656 if (RB_RED_P(brother)) {
657 KASSERT(RB_BLACK_P(parent));
658 rb_tree_reparent_nodes(rbt, parent, other);
659 brother = parent->rb_nodes[other];
660 KASSERT(!RB_SENTINEL_P(brother));
661 KASSERT(RB_BLACK_P(brother));
662 KASSERT(RB_RED_P(parent));
663 KASSERT(rb_tree_check_node(rbt, brother, NULL, false));
664 KASSERT(rb_tree_check_node(rbt, parent, NULL, false));
665 } else {
667 * Both our parent and brother are black.
668 * Change our brother to red, advance up rank
669 * and go through the loop again.
671 * B -> B
672 * A D -> A d
673 * C E -> C E
675 RB_MARK_RED(brother);
676 KASSERT(RB_BLACK_P(brother->rb_left));
677 KASSERT(RB_BLACK_P(brother->rb_right));
678 if (RB_ROOT_P(parent))
679 return;
680 KASSERT(rb_tree_check_node(rbt, brother, NULL, false));
681 KASSERT(rb_tree_check_node(rbt, parent, NULL, false));
682 which = parent->rb_position;
683 parent = parent->rb_parent;
685 } else if (RB_RED_P(parent)
686 && RB_BLACK_P(brother)
687 && RB_BLACK_P(brother->rb_left)
688 && RB_BLACK_P(brother->rb_right)) {
689 KASSERT(RB_BLACK_P(brother));
690 KASSERT(RB_BLACK_P(brother->rb_left));
691 KASSERT(RB_BLACK_P(brother->rb_right));
692 RB_MARK_BLACK(parent);
693 RB_MARK_RED(brother);
694 KASSERT(rb_tree_check_node(rbt, brother, NULL, true));
695 break; /* We're done! */
696 } else {
697 KASSERT(RB_BLACK_P(brother));
698 KASSERT(!RB_CHILDLESS_P(brother));
700 * Case 3: our brother is black, our left nephew is
701 * red, and our right nephew is black. Swap our
702 * brother with our left nephew. This result in a
703 * tree that matches case 4.
705 * B -> D
706 * A D -> B E
707 * c e -> A C
709 if (RB_BLACK_P(brother->rb_nodes[other])) {
710 KASSERT(RB_RED_P(brother->rb_nodes[which]));
711 rb_tree_reparent_nodes(rbt, brother, which);
712 KASSERT(brother->rb_parent == parent->rb_nodes[other]);
713 brother = parent->rb_nodes[other];
714 KASSERT(RB_RED_P(brother->rb_nodes[other]));
717 * Case 4: our brother is black and our right nephew
718 * is red. Swap our parent and brother locations and
719 * change our right nephew to black. (these can be
720 * done in either order so we change the color first).
721 * The result is a valid red-black tree and is a
722 * terminal case.
724 * B -> D
725 * A D -> B E
726 * c e -> A C
728 RB_MARK_BLACK(brother->rb_nodes[other]);
729 rb_tree_reparent_nodes(rbt, parent, other);
730 break; /* We're done! */
733 KASSERT(rb_tree_check_node(rbt, parent, NULL, true));
736 struct rb_node *
737 _prop_rb_tree_iterate(struct rb_tree *rbt, struct rb_node *self,
738 unsigned int direction)
740 const unsigned int other = direction ^ RB_NODE_OTHER;
741 KASSERT(direction == RB_NODE_LEFT || direction == RB_NODE_RIGHT);
743 if (self == NULL) {
744 self = rbt->rbt_root;
745 if (RB_SENTINEL_P(self))
746 return NULL;
747 while (!RB_SENTINEL_P(self->rb_nodes[other]))
748 self = self->rb_nodes[other];
749 return self;
751 KASSERT(!RB_SENTINEL_P(self));
753 * We can't go any further in this direction. We proceed up in the
754 * opposite direction until our parent is in direction we want to go.
756 if (RB_SENTINEL_P(self->rb_nodes[direction])) {
757 while (!RB_ROOT_P(self)) {
758 if (other == self->rb_position)
759 return self->rb_parent;
760 self = self->rb_parent;
762 return NULL;
766 * Advance down one in current direction and go down as far as possible
767 * in the opposite direction.
769 self = self->rb_nodes[direction];
770 KASSERT(!RB_SENTINEL_P(self));
771 while (!RB_SENTINEL_P(self->rb_nodes[other]))
772 self = self->rb_nodes[other];
773 return self;
776 #ifdef RBDEBUG
777 static const struct rb_node *
778 rb_tree_iterate_const(const struct rb_tree *rbt, const struct rb_node *self,
779 unsigned int direction)
781 const unsigned int other = direction ^ RB_NODE_OTHER;
782 KASSERT(direction == RB_NODE_LEFT || direction == RB_NODE_RIGHT);
784 if (self == NULL) {
785 self = rbt->rbt_root;
786 if (RB_SENTINEL_P(self))
787 return NULL;
788 while (!RB_SENTINEL_P(self->rb_nodes[other]))
789 self = self->rb_nodes[other];
790 return self;
792 KASSERT(!RB_SENTINEL_P(self));
794 * We can't go any further in this direction. We proceed up in the
795 * opposite direction until our parent is in direction we want to go.
797 if (RB_SENTINEL_P(self->rb_nodes[direction])) {
798 while (!RB_ROOT_P(self)) {
799 if (other == self->rb_position)
800 return self->rb_parent;
801 self = self->rb_parent;
803 return NULL;
807 * Advance down one in current direction and go down as far as possible
808 * in the opposite direction.
810 self = self->rb_nodes[direction];
811 KASSERT(!RB_SENTINEL_P(self));
812 while (!RB_SENTINEL_P(self->rb_nodes[other]))
813 self = self->rb_nodes[other];
814 return self;
817 static bool
818 rb_tree_check_node(const struct rb_tree *rbt, const struct rb_node *self,
819 const struct rb_node *prev, bool red_check)
821 KASSERT(!self->rb_sentinel);
822 KASSERT(self->rb_left);
823 KASSERT(self->rb_right);
824 KASSERT(prev == NULL ||
825 (*rbt->rbt_ops->rbto_compare_nodes)(prev, self) > 0);
828 * Verify our relationship to our parent.
830 if (RB_ROOT_P(self)) {
831 KASSERT(self == rbt->rbt_root);
832 KASSERT(self->rb_position == RB_NODE_LEFT);
833 KASSERT(self->rb_parent->rb_nodes[RB_NODE_LEFT] == self);
834 KASSERT(self->rb_parent == (const struct rb_node *) &rbt->rbt_root);
835 } else {
836 KASSERT(self != rbt->rbt_root);
837 KASSERT(!RB_PARENT_SENTINEL_P(self));
838 if (self->rb_position == RB_NODE_LEFT) {
839 KASSERT((*rbt->rbt_ops->rbto_compare_nodes)(self, self->rb_parent) > 0);
840 KASSERT(self->rb_parent->rb_nodes[RB_NODE_LEFT] == self);
841 } else {
842 KASSERT((*rbt->rbt_ops->rbto_compare_nodes)(self, self->rb_parent) < 0);
843 KASSERT(self->rb_parent->rb_nodes[RB_NODE_RIGHT] == self);
848 * Verify our position in the linked list against the tree itself.
851 const struct rb_node *prev0 = rb_tree_iterate_const(rbt, self, RB_NODE_LEFT);
852 const struct rb_node *next0 = rb_tree_iterate_const(rbt, self, RB_NODE_RIGHT);
853 KASSERT(prev0 == TAILQ_PREV(self, rb_node_qh, rb_link));
854 if (next0 != TAILQ_NEXT(self, rb_link))
855 next0 = rb_tree_iterate_const(rbt, self, RB_NODE_RIGHT);
856 KASSERT(next0 == TAILQ_NEXT(self, rb_link));
860 * The root must be black.
861 * There can never be two adjacent red nodes.
863 if (red_check) {
864 KASSERT(!RB_ROOT_P(self) || RB_BLACK_P(self));
865 if (RB_RED_P(self)) {
866 const struct rb_node *brother;
867 KASSERT(!RB_ROOT_P(self));
868 brother = self->rb_parent->rb_nodes[self->rb_position ^ RB_NODE_OTHER];
869 KASSERT(RB_BLACK_P(self->rb_parent));
871 * I'm red and have no children, then I must either
872 * have no brother or my brother also be red and
873 * also have no children. (black count == 0)
875 KASSERT(!RB_CHILDLESS_P(self)
876 || RB_SENTINEL_P(brother)
877 || RB_RED_P(brother)
878 || RB_CHILDLESS_P(brother));
880 * If I'm not childless, I must have two children
881 * and they must be both be black.
883 KASSERT(RB_CHILDLESS_P(self)
884 || (RB_TWOCHILDREN_P(self)
885 && RB_BLACK_P(self->rb_left)
886 && RB_BLACK_P(self->rb_right)));
888 * If I'm not childless, thus I have black children,
889 * then my brother must either be black or have two
890 * black children.
892 KASSERT(RB_CHILDLESS_P(self)
893 || RB_BLACK_P(brother)
894 || (RB_TWOCHILDREN_P(brother)
895 && RB_BLACK_P(brother->rb_left)
896 && RB_BLACK_P(brother->rb_right)));
897 } else {
899 * If I'm black and have one child, that child must
900 * be red and childless.
902 KASSERT(RB_CHILDLESS_P(self)
903 || RB_TWOCHILDREN_P(self)
904 || (!RB_LEFT_SENTINEL_P(self)
905 && RB_RIGHT_SENTINEL_P(self)
906 && RB_RED_P(self->rb_left)
907 && RB_CHILDLESS_P(self->rb_left))
908 || (!RB_RIGHT_SENTINEL_P(self)
909 && RB_LEFT_SENTINEL_P(self)
910 && RB_RED_P(self->rb_right)
911 && RB_CHILDLESS_P(self->rb_right)));
914 * If I'm a childless black node and my parent is
915 * black, my 2nd closet relative away from my parent
916 * is either red or has a red parent or red children.
918 if (!RB_ROOT_P(self)
919 && RB_CHILDLESS_P(self)
920 && RB_BLACK_P(self->rb_parent)) {
921 const unsigned int which = self->rb_position;
922 const unsigned int other = which ^ RB_NODE_OTHER;
923 const struct rb_node *relative0, *relative;
925 relative0 = rb_tree_iterate_const(rbt,
926 self, other);
927 KASSERT(relative0 != NULL);
928 relative = rb_tree_iterate_const(rbt,
929 relative0, other);
930 KASSERT(relative != NULL);
931 KASSERT(RB_SENTINEL_P(relative->rb_nodes[which]));
932 #if 0
933 KASSERT(RB_RED_P(relative)
934 || RB_RED_P(relative->rb_left)
935 || RB_RED_P(relative->rb_right)
936 || RB_RED_P(relative->rb_parent));
937 #endif
941 * A grandparent's children must be real nodes and not
942 * sentinels. First check out grandparent.
944 KASSERT(RB_ROOT_P(self)
945 || RB_ROOT_P(self->rb_parent)
946 || RB_TWOCHILDREN_P(self->rb_parent->rb_parent));
948 * If we are have grandchildren on our left, then
949 * we must have a child on our right.
951 KASSERT(RB_LEFT_SENTINEL_P(self)
952 || RB_CHILDLESS_P(self->rb_left)
953 || !RB_RIGHT_SENTINEL_P(self));
955 * If we are have grandchildren on our right, then
956 * we must have a child on our left.
958 KASSERT(RB_RIGHT_SENTINEL_P(self)
959 || RB_CHILDLESS_P(self->rb_right)
960 || !RB_LEFT_SENTINEL_P(self));
963 * If we have a child on the left and it doesn't have two
964 * children make sure we don't have great-great-grandchildren on
965 * the right.
967 KASSERT(RB_TWOCHILDREN_P(self->rb_left)
968 || RB_CHILDLESS_P(self->rb_right)
969 || RB_CHILDLESS_P(self->rb_right->rb_left)
970 || RB_CHILDLESS_P(self->rb_right->rb_left->rb_left)
971 || RB_CHILDLESS_P(self->rb_right->rb_left->rb_right)
972 || RB_CHILDLESS_P(self->rb_right->rb_right)
973 || RB_CHILDLESS_P(self->rb_right->rb_right->rb_left)
974 || RB_CHILDLESS_P(self->rb_right->rb_right->rb_right));
977 * If we have a child on the right and it doesn't have two
978 * children make sure we don't have great-great-grandchildren on
979 * the left.
981 KASSERT(RB_TWOCHILDREN_P(self->rb_right)
982 || RB_CHILDLESS_P(self->rb_left)
983 || RB_CHILDLESS_P(self->rb_left->rb_left)
984 || RB_CHILDLESS_P(self->rb_left->rb_left->rb_left)
985 || RB_CHILDLESS_P(self->rb_left->rb_left->rb_right)
986 || RB_CHILDLESS_P(self->rb_left->rb_right)
987 || RB_CHILDLESS_P(self->rb_left->rb_right->rb_left)
988 || RB_CHILDLESS_P(self->rb_left->rb_right->rb_right));
991 * If we are fully interior node, then our predecessors and
992 * successors must have no children in our direction.
994 if (RB_TWOCHILDREN_P(self)) {
995 const struct rb_node *prev0;
996 const struct rb_node *next0;
998 prev0 = rb_tree_iterate_const(rbt, self, RB_NODE_LEFT);
999 KASSERT(prev0 != NULL);
1000 KASSERT(RB_RIGHT_SENTINEL_P(prev0));
1002 next0 = rb_tree_iterate_const(rbt, self, RB_NODE_RIGHT);
1003 KASSERT(next0 != NULL);
1004 KASSERT(RB_LEFT_SENTINEL_P(next0));
1008 return true;
1011 static unsigned int
1012 rb_tree_count_black(const struct rb_node *self)
1014 unsigned int left, right;
1016 if (RB_SENTINEL_P(self))
1017 return 0;
1019 left = rb_tree_count_black(self->rb_left);
1020 right = rb_tree_count_black(self->rb_right);
1022 KASSERT(left == right);
1024 return left + RB_BLACK_P(self);
1027 void
1028 _prop_rb_tree_check(const struct rb_tree *rbt, bool red_check)
1030 const struct rb_node *self;
1031 const struct rb_node *prev;
1032 unsigned int count;
1034 KASSERT(rbt->rbt_root == NULL || rbt->rbt_root->rb_position == RB_NODE_LEFT);
1036 prev = NULL;
1037 count = 0;
1038 TAILQ_FOREACH(self, &rbt->rbt_nodes, rb_link) {
1039 rb_tree_check_node(rbt, self, prev, false);
1040 count++;
1042 KASSERT(rbt->rbt_count == count);
1043 KASSERT(RB_SENTINEL_P(rbt->rbt_root)
1044 || rb_tree_count_black(rbt->rbt_root));
1047 * The root must be black.
1048 * There can never be two adjacent red nodes.
1050 if (red_check) {
1051 KASSERT(rbt->rbt_root == NULL || RB_BLACK_P(rbt->rbt_root));
1052 TAILQ_FOREACH(self, &rbt->rbt_nodes, rb_link) {
1053 rb_tree_check_node(rbt, self, NULL, true);
1057 #endif /* RBDEBUG */