[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-devel] [Resend][PATCH 17/17] rbtree: add postorder iteration functions



Postorder iteration yields all of a node's children prior to yielding the
node itself, and this particular implementation also avoids examining the
leaf links in a node after that node has been yielded.

In what I expect will be its most common usage, postorder iteration allows
the deletion of every node in an rbtree without modifying the rbtree nodes
(no _requirement_ that they be nulled) while avoiding referencing child
nodes after they have been "deleted" (most commonly, freed).

I have only updated zswap to use this functionality at this point, but
numerous bits of code (most notably in the filesystem drivers) use a hand
rolled postorder iteration that NULLs child links as it traverses the
tree.  Each of those instances could be replaced with this common
implementation.

1 & 2 add rbtree postorder iteration functions.
3 adds testing of the iteration to the rbtree runtime tests
4 allows building the rbtree runtime tests as builtins
5 updates zswap.

This patch:

Add postorder iteration functions for rbtree.  These are useful for safely
freeing an entire rbtree without modifying the tree at all.

commit 9dee5c51516d2c3fff22633c1272c5652e68075a from Linux tree

Signed-off-by: Praveen Kumar <kpraveen.lkml@xxxxxxxxx>
---
 xen/common/rbtree.c      | 45 +++++++++++++++++++++++++++++++++++++++++++++
 xen/include/xen/rbtree.h |  4 ++++
 2 files changed, 49 insertions(+)

diff --git a/xen/common/rbtree.c b/xen/common/rbtree.c
index 83b4892f54..3c994dcc0c 100644
--- a/xen/common/rbtree.c
+++ b/xen/common/rbtree.c
@@ -584,3 +584,48 @@ void rb_replace_node(struct rb_node *victim, struct 
rb_node *new,
     *new = *victim;
 }
 EXPORT_SYMBOL(rb_replace_node);
+
+static struct rb_node *rb_left_deepest_node(const struct rb_node *node)
+{
+    for (;;)
+    { 
+        if (node->rb_left)
+            node = node->rb_left;
+        else if (node->rb_right)
+            node = node->rb_right;
+        else
+            return (struct rb_node *)node;
+    }
+}
+
+struct rb_node *rb_next_postorder(const struct rb_node *node)
+{
+    const struct rb_node *parent;
+    if (!node)
+        return NULL;
+    parent = rb_parent(node);
+
+    /* If we're sitting on node, we've already seen our children */
+    if (parent && node == parent->rb_left && parent->rb_right)
+    {
+        /* If we are the parent's left node, go to the parent's right
+         * node then all the way down to the left
+         */
+        return rb_left_deepest_node(parent->rb_right);
+    } else
+        /* Otherwise we are the parent's right node, and the parent
+         * should be next
+         */
+        return (struct rb_node *)parent;
+}
+EXPORT_SYMBOL(rb_next_postorder);
+
+struct rb_node *rb_first_postorder(const struct rb_root *root)
+{
+    if (!root->rb_node)
+        return NULL;
+
+    return rb_left_deepest_node(root->rb_node);
+}
+EXPORT_SYMBOL(rb_first_postorder);
+
diff --git a/xen/include/xen/rbtree.h b/xen/include/xen/rbtree.h
index 107f1b12f2..24650a5cd8 100644
--- a/xen/include/xen/rbtree.h
+++ b/xen/include/xen/rbtree.h
@@ -66,4 +66,8 @@ static inline void rb_link_node(struct rb_node * node, struct 
rb_node * parent,
     *rb_link = node;
 }
 
+/* Postorder iteration - always visit the parent after its children */
+extern struct rb_node *rb_first_postorder(const struct rb_root *);
+extern struct rb_node *rb_next_postorder(const struct rb_node *);
+
 #endif /* __RBTREE_H__ */
-- 
2.12.0


_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxx
https://lists.xen.org/xen-devel

 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.