aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorMichel Lespinasse <walken@google.com>2012-10-08 19:30:44 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2012-10-09 03:22:33 -0400
commit6d58452dc066db61acdff7b84671db1b11a3de1c (patch)
tree36df08157d975398c5416d068f83cd31b79ae2c7 /lib
parent1f0528653e41ec230c60f5738820e8a544731399 (diff)
rbtree: adjust root color in rb_insert_color() only when necessary
The root node of an rbtree must always be black. However, rb_insert_color() only needs to maintain this invariant when it has been broken - that is, when it exits the loop due to the current (red) node being the root. In all other cases (exiting after tree rotations, or exiting due to an existing black parent) the invariant is already satisfied, so there is no need to adjust the root node color. Signed-off-by: Michel Lespinasse <walken@google.com> Cc: Andrea Arcangeli <aarcange@redhat.com> Acked-by: David Woodhouse <David.Woodhouse@intel.com> Cc: Rik van Riel <riel@redhat.com> Cc: Peter Zijlstra <a.p.zijlstra@chello.nl> Cc: Daniel Santos <daniel.santos@pobox.com> Cc: Jens Axboe <axboe@kernel.dk> Cc: "Eric W. Biederman" <ebiederm@xmission.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'lib')
-rw-r--r--lib/rbtree.c19
1 files changed, 15 insertions, 4 deletions
diff --git a/lib/rbtree.c b/lib/rbtree.c
index 12abb8abf442..d0be5fcaafe8 100644
--- a/lib/rbtree.c
+++ b/lib/rbtree.c
@@ -91,8 +91,21 @@ void rb_insert_color(struct rb_node *node, struct rb_root *root)
91{ 91{
92 struct rb_node *parent, *gparent; 92 struct rb_node *parent, *gparent;
93 93
94 while ((parent = rb_parent(node)) && rb_is_red(parent)) 94 while (true) {
95 { 95 /*
96 * Loop invariant: node is red
97 *
98 * If there is a black parent, we are done.
99 * Otherwise, take some corrective action as we don't
100 * want a red root or two consecutive red nodes.
101 */
102 parent = rb_parent(node);
103 if (!parent) {
104 rb_set_black(node);
105 break;
106 } else if (rb_is_black(parent))
107 break;
108
96 gparent = rb_parent(parent); 109 gparent = rb_parent(parent);
97 110
98 if (parent == gparent->rb_left) 111 if (parent == gparent->rb_left)
@@ -142,8 +155,6 @@ void rb_insert_color(struct rb_node *node, struct rb_root *root)
142 break; 155 break;
143 } 156 }
144 } 157 }
145
146 rb_set_black(root->rb_node);
147} 158}
148EXPORT_SYMBOL(rb_insert_color); 159EXPORT_SYMBOL(rb_insert_color);
149 160