about summary refs log tree commit diff
path: root/misc/tsearch.c
diff options
context:
space:
mode:
authorFlorian Weimer <fweimer@redhat.com>2019-05-02 11:42:51 +0200
committerFlorian Weimer <fweimer@redhat.com>2019-05-02 11:42:51 +0200
commit7b807a35a8dc63f9742cecf0fc3db46c30e28b0d (patch)
tree0dce83854245814394b46373b4c8316ab7610b0c /misc/tsearch.c
parent20aa5819586ac7ad11f711bab64feda307965191 (diff)
downloadglibc-7b807a35a8dc63f9742cecf0fc3db46c30e28b0d.tar.gz
glibc-7b807a35a8dc63f9742cecf0fc3db46c30e28b0d.tar.xz
glibc-7b807a35a8dc63f9742cecf0fc3db46c30e28b0d.zip
misc: Add twalk_r function
The twalk function is very difficult to use in a multi-threaded
program because there is no way to pass external state to the
iterator function.

Reviewed-by: Carlos O'Donell <carlos@redhat.com>
Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Diffstat (limited to 'misc/tsearch.c')
-rw-r--r--misc/tsearch.c34
1 files changed, 34 insertions, 0 deletions
diff --git a/misc/tsearch.c b/misc/tsearch.c
index 5c19082a59..edf0fec971 100644
--- a/misc/tsearch.c
+++ b/misc/tsearch.c
@@ -719,7 +719,41 @@ __twalk (const void *vroot, __action_fn_t action)
 libc_hidden_def (__twalk)
 weak_alias (__twalk, twalk)
 
+/* twalk_r is the same as twalk, but with a closure parameter instead
+   of the level.  */
+static void
+trecurse_r (const void *vroot, void (*action) (const void *, VISIT, void *),
+	    void *closure)
+{
+  const_node root = (const_node) vroot;
 
+  if (LEFT(root) == NULL && RIGHT(root) == NULL)
+    (*action) (root, leaf, closure);
+  else
+    {
+      (*action) (root, preorder, closure);
+      if (LEFT(root) != NULL)
+	trecurse_r (LEFT(root), action, closure);
+      (*action) (root, postorder, closure);
+      if (RIGHT(root) != NULL)
+	trecurse_r (RIGHT(root), action, closure);
+      (*action) (root, endorder, closure);
+    }
+}
+
+void
+__twalk_r (const void *vroot, void (*action) (const void *, VISIT, void *),
+	   void *closure)
+{
+  const_node root = (const_node) vroot;
+
+  CHECK_TREE ((node) root);
+
+  if (root != NULL && action != NULL)
+    trecurse_r (root, action, closure);
+}
+libc_hidden_def (__twalk_r)
+weak_alias (__twalk_r, twalk_r)
 
 /* The standardized functions miss an important functionality: the
    tree cannot be removed easily.  We provide a function to do this.  */