diff options
author | Michel Lespinasse <walken@google.com> | 2012-10-08 19:31:07 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2012-10-09 03:22:36 -0400 |
commit | 7abc704ae399fcb9c51ca200b0456f8a975a8011 (patch) | |
tree | 3180bbf50ef3d25f0647362ecc7e7925f884d738 /lib | |
parent | 28d7530928d01638678f63c3c70113540b0e6abe (diff) |
rbtree: add __rb_change_child() helper function
Add __rb_change_child() as an inline helper function to replace code that
would otherwise be duplicated 4 times in the source.
No changes to binary size or speed.
Signed-off-by: Michel Lespinasse <walken@google.com>
Reviewed-by: Rik van Riel <riel@redhat.com>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Andrea Arcangeli <aarcange@redhat.com>
Cc: David Woodhouse <dwmw2@infradead.org>
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.c | 46 |
1 files changed, 17 insertions, 29 deletions
diff --git a/lib/rbtree.c b/lib/rbtree.c index 61cdd0e3e538..de89a614b1ba 100644 --- a/lib/rbtree.c +++ b/lib/rbtree.c | |||
@@ -66,6 +66,19 @@ static inline struct rb_node *rb_red_parent(struct rb_node *red) | |||
66 | return (struct rb_node *)red->__rb_parent_color; | 66 | return (struct rb_node *)red->__rb_parent_color; |
67 | } | 67 | } |
68 | 68 | ||
69 | static inline void | ||
70 | __rb_change_child(struct rb_node *old, struct rb_node *new, | ||
71 | struct rb_node *parent, struct rb_root *root) | ||
72 | { | ||
73 | if (parent) { | ||
74 | if (parent->rb_left == old) | ||
75 | parent->rb_left = new; | ||
76 | else | ||
77 | parent->rb_right = new; | ||
78 | } else | ||
79 | root->rb_node = new; | ||
80 | } | ||
81 | |||
69 | /* | 82 | /* |
70 | * Helper function for rotations: | 83 | * Helper function for rotations: |
71 | * - old's parent and color get assigned to new | 84 | * - old's parent and color get assigned to new |
@@ -78,13 +91,7 @@ __rb_rotate_set_parents(struct rb_node *old, struct rb_node *new, | |||
78 | struct rb_node *parent = rb_parent(old); | 91 | struct rb_node *parent = rb_parent(old); |
79 | new->__rb_parent_color = old->__rb_parent_color; | 92 | new->__rb_parent_color = old->__rb_parent_color; |
80 | rb_set_parent_color(old, new, color); | 93 | rb_set_parent_color(old, new, color); |
81 | if (parent) { | 94 | __rb_change_child(old, new, parent, root); |
82 | if (parent->rb_left == old) | ||
83 | parent->rb_left = new; | ||
84 | else | ||
85 | parent->rb_right = new; | ||
86 | } else | ||
87 | root->rb_node = new; | ||
88 | } | 95 | } |
89 | 96 | ||
90 | void rb_insert_color(struct rb_node *node, struct rb_root *root) | 97 | void rb_insert_color(struct rb_node *node, struct rb_root *root) |
@@ -375,13 +382,7 @@ void rb_erase(struct rb_node *node, struct rb_root *root) | |||
375 | while ((left = node->rb_left) != NULL) | 382 | while ((left = node->rb_left) != NULL) |
376 | node = left; | 383 | node = left; |
377 | 384 | ||
378 | if (rb_parent(old)) { | 385 | __rb_change_child(old, node, rb_parent(old), root); |
379 | if (rb_parent(old)->rb_left == old) | ||
380 | rb_parent(old)->rb_left = node; | ||
381 | else | ||
382 | rb_parent(old)->rb_right = node; | ||
383 | } else | ||
384 | root->rb_node = node; | ||
385 | 386 | ||
386 | child = node->rb_right; | 387 | child = node->rb_right; |
387 | parent = rb_parent(node); | 388 | parent = rb_parent(node); |
@@ -410,13 +411,7 @@ void rb_erase(struct rb_node *node, struct rb_root *root) | |||
410 | 411 | ||
411 | if (child) | 412 | if (child) |
412 | rb_set_parent(child, parent); | 413 | rb_set_parent(child, parent); |
413 | if (parent) { | 414 | __rb_change_child(node, child, parent, root); |
414 | if (parent->rb_left == node) | ||
415 | parent->rb_left = child; | ||
416 | else | ||
417 | parent->rb_right = child; | ||
418 | } else | ||
419 | root->rb_node = child; | ||
420 | 415 | ||
421 | color: | 416 | color: |
422 | if (color == RB_BLACK) | 417 | if (color == RB_BLACK) |
@@ -591,14 +586,7 @@ void rb_replace_node(struct rb_node *victim, struct rb_node *new, | |||
591 | struct rb_node *parent = rb_parent(victim); | 586 | struct rb_node *parent = rb_parent(victim); |
592 | 587 | ||
593 | /* Set the surrounding nodes to point to the replacement */ | 588 | /* Set the surrounding nodes to point to the replacement */ |
594 | if (parent) { | 589 | __rb_change_child(victim, new, parent, root); |
595 | if (victim == parent->rb_left) | ||
596 | parent->rb_left = new; | ||
597 | else | ||
598 | parent->rb_right = new; | ||
599 | } else { | ||
600 | root->rb_node = new; | ||
601 | } | ||
602 | if (victim->rb_left) | 590 | if (victim->rb_left) |
603 | rb_set_parent(victim->rb_left, new); | 591 | rb_set_parent(victim->rb_left, new); |
604 | if (victim->rb_right) | 592 | if (victim->rb_right) |