rbtree: add rbtree_postorder_for_each_entry_safe() helper
authorCody P Schafer <cody@linux.vnet.ibm.com>
Wed, 11 Sep 2013 21:25:11 +0000 (14:25 -0700)
committerLinus Torvalds <torvalds@linux-foundation.org>
Wed, 11 Sep 2013 22:59:20 +0000 (15:59 -0700)
Because deletion (of the entire tree) is a relatively common use of the
rbtree_postorder iteration, and because doing it safely means fiddling
with temporary storage, provide a helper to simplify postorder rbtree
iteration.

Signed-off-by: Cody P Schafer <cody@linux.vnet.ibm.com>
Reviewed-by: Seth Jennings <sjenning@linux.vnet.ibm.com>
Cc: David Woodhouse <David.Woodhouse@intel.com>
Cc: Rik van Riel <riel@redhat.com>
Cc: Michel Lespinasse <walken@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
include/linux/rbtree.h

index c467151e99509997fa8b9a14ee53c1cd4f9765b8..aa870a4ddf5428bd120d464f0ffb304ec27b868f 100644 (file)
@@ -85,4 +85,22 @@ static inline void rb_link_node(struct rb_node * node, struct rb_node * parent,
        *rb_link = node;
 }
 
+/**
+ * rbtree_postorder_for_each_entry_safe - iterate over rb_root in post order of
+ * given type safe against removal of rb_node entry
+ *
+ * @pos:       the 'type *' to use as a loop cursor.
+ * @n:         another 'type *' to use as temporary storage
+ * @root:      'rb_root *' of the rbtree.
+ * @field:     the name of the rb_node field within 'type'.
+ */
+#define rbtree_postorder_for_each_entry_safe(pos, n, root, field) \
+       for (pos = rb_entry(rb_first_postorder(root), typeof(*pos), field),\
+               n = rb_entry(rb_next_postorder(&pos->field), \
+                       typeof(*pos), field); \
+            &pos->field; \
+            pos = n, \
+               n = rb_entry(rb_next_postorder(&pos->field), \
+                       typeof(*pos), field))
+
 #endif /* _LINUX_RBTREE_H */