diff mbox

[v2,16/20] rbtree: add __rb_change_child() helper function

Message ID 20170617093253.3990-17-kpraveen.lkml@gmail.com (mailing list archive)
State New, archived
Headers show

Commit Message

Praveen Kumar June 17, 2017, 9:32 a.m. UTC
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>
[Linux commit 7abc704ae399fcb9c51ca200b0456f8a975a8011]

Ported to Xen.

Signed-off-by: Praveen Kumar <kpraveen.lkml@gmail.com>
---
 xen/common/rbtree.c | 37 +++++++++++--------------------------
 1 file changed, 11 insertions(+), 26 deletions(-)
diff mbox

Patch

diff --git a/xen/common/rbtree.c b/xen/common/rbtree.c
index 8c28ab1967..2063536548 100644
--- a/xen/common/rbtree.c
+++ b/xen/common/rbtree.c
@@ -65,6 +65,13 @@  static inline struct rb_node *rb_red_parent(struct rb_node *red)
     return (struct rb_node *)red->__rb_parent_color;
 }
 
+static inline void
+__rb_change_child(struct rb_node *old, struct rb_node *new,
+                 struct rb_node *parent, struct rb_root *root)
+{
+    __rb_change_child(old, new, parent, root);
+}
+
 /*
  * Helper function for rotations:
  * - old's parent and color get assigned to new
@@ -376,15 +383,7 @@  void rb_erase(struct rb_node *node, struct rb_root *root)
         node = node->rb_right;
         while ((left = node->rb_left) != NULL)
             node = left;
-
-        if (rb_parent(old)) {
-            if (rb_parent(old)->rb_left == old)
-                rb_parent(old)->rb_left = node;
-            else
-                rb_parent(old)->rb_right = node;
-        } else
-            root->rb_node = node;
-
+        __rb_change_child(old, node, rb_parent(old), root);
         child = node->rb_right;
         parent = rb_parent(node);
         color = rb_color(node);
@@ -413,15 +412,8 @@  void rb_erase(struct rb_node *node, struct rb_root *root)
 
     if (child)
         rb_set_parent(child, parent);
-    if (parent) {
-        if (parent->rb_left == node)
-            parent->rb_left = child;
-        else
-            parent->rb_right = child;
-    } else
-        root->rb_node = child;
-
- color:
+    __rb_change_child(node, child, parent, root);
+color:
     if (color == RB_BLACK)
         __rb_erase_color(child, parent, root);
 }
@@ -524,14 +516,7 @@  void rb_replace_node(struct rb_node *victim, struct rb_node *new,
     struct rb_node *parent = rb_parent(victim);
 
     /* Set the surrounding nodes to point to the replacement */
-    if (parent) {
-        if (victim == parent->rb_left)
-            parent->rb_left = new;
-        else
-            parent->rb_right = new;
-    } else {
-        root->rb_node = new;
-    }
+    __rb_change_child(victim, new, parent, root);
     if (victim->rb_left)
         rb_set_parent(victim->rb_left, new);
     if (victim->rb_right)