]> www.infradead.org Git - mtd-utils.git/commitdiff
mtd-utils: Extract rbtree implementation to common lib
authorZhihao Cheng <chengzhihao1@huawei.com>
Fri, 2 Feb 2024 02:38:52 +0000 (10:38 +0800)
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>
Wed, 25 Sep 2024 13:03:08 +0000 (15:03 +0200)
Current rbtree implementation code is put under jffs utils, extract it
into common lib, and add more rbtree operations(eg. rb_first_postorder,
rb_next_postorder, etc.).

This is a preparation for replacing implementation of UBIFS utils with
linux kernel libs.

Signed-off-by: Zhihao Cheng <chengzhihao1@huawei.com>
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
include/rbtree.h [moved from jffsX-utils/rbtree.h with 79% similarity]
jffsX-utils/Makemodule.am
lib/Makemodule.am
lib/rbtree.c [moved from jffsX-utils/rbtree.c with 90% similarity]

similarity index 79%
rename from jffsX-utils/rbtree.h
rename to include/rbtree.h
index 0d77b65f1748426bc2af8ed3963d3f55c7994683..89926e7426b34c538bed4dfda5163951fd652df6 100644 (file)
@@ -155,6 +155,38 @@ extern struct rb_node *rb_prev(struct rb_node *);
 extern struct rb_node *rb_first(struct rb_root *);
 extern struct rb_node *rb_last(struct rb_root *);
 
+/* 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 *);
+
+#define rb_entry_safe(ptr, type, member) \
+       ({ typeof(ptr) ____ptr = (ptr); \
+          ____ptr ? rb_entry(____ptr, type, member) : NULL; \
+       })
+
+/**
+ * rbtree_postorder_for_each_entry_safe - iterate in post-order over rb_root of
+ * given type allowing the backing memory of @pos to be invalidated
+ *
+ * @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'.
+ *
+ * rbtree_postorder_for_each_entry_safe() provides a similar guarantee as
+ * list_for_each_entry_safe() and allows the iteration to continue independent
+ * of changes to @pos by the body of the loop.
+ *
+ * Note, however, that it cannot handle other modifications that re-order the
+ * rbtree it is iterating over. This includes calling rb_erase() on @pos, as
+ * rb_erase() may rebalance the tree, causing us to miss some nodes.
+ */
+#define rbtree_postorder_for_each_entry_safe(pos, n, root, field) \
+       for (pos = rb_entry_safe(rb_first_postorder(root), typeof(*pos), field); \
+            pos && ({ n = rb_entry_safe(rb_next_postorder(&pos->field), \
+                       typeof(*pos), field); 1; }); \
+            pos = n)
+
 /* Fast replacement of a single node without remove/rebalance/add/rebalance */
 extern void rb_replace_node(struct rb_node *victim, struct rb_node *new,
                            struct rb_root *root);
index 2374b8582e53ee3ce452fdf37614a43f73add9cf..4ae4c57b2e05458f942885c676d4e06278e2370f 100644 (file)
@@ -1,16 +1,14 @@
 mkfs_jffs2_SOURCES = \
        jffsX-utils/mkfs.jffs2.c \
-       jffsX-utils/rbtree.h \
        jffsX-utils/compr.h \
-       jffsX-utils/rbtree.c \
        jffsX-utils/compr.c \
        jffsX-utils/compr_rtime.c \
        jffsX-utils/compr.h \
-       jffsX-utils/rbtree.h \
        jffsX-utils/summary.h \
        include/linux/jffs2.h \
        include/mtd/jffs2-user.h \
-       include/list.h
+       include/list.h \
+       include/rbtree.h
 mkfs_jffs2_LDADD = libmtd.a $(ZLIB_LIBS) $(LZO_LIBS)
 mkfs_jffs2_CPPFLAGS = $(AM_CPPFLAGS) $(ZLIB_CFLAGS) $(LZO_CFLAGS)
 
index 7f890daa4a72b0a580f30fa62e43ad76be277108..441532de094664790b43119ee494fe32446debd8 100644 (file)
@@ -7,6 +7,8 @@ libmtd_a_SOURCES = \
        include/common.h \
        lib/list_sort.c \
        include/list.h \
+       lib/rbtree.c \
+       include/rbtree.h \
        lib/libcrc32.c \
        include/crc32.h \
        lib/libmtd_legacy.c \
similarity index 90%
rename from jffsX-utils/rbtree.c
rename to lib/rbtree.c
index 329e09869ba4b1ba8bdf7c03842cd5c0da7892b0..32c875567bc22aa0db43233aaba9ba4f289cf86b 100644 (file)
@@ -388,3 +388,41 @@ void rb_replace_node(struct rb_node *victim, struct rb_node *new,
        /* Copy the pointers/colour from the victim to the replacement */
        *new = *victim;
 }
+
+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;
+}
+
+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);
+}