1
0
mirror of https://sourceware.org/git/glibc.git synced 2025-07-29 11:41:21 +03:00

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>
This commit is contained in:
Florian Weimer
2019-05-02 11:42:51 +02:00
parent 20aa581958
commit 7b807a35a8
37 changed files with 315 additions and 4 deletions

View File

@ -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. */