Skip to content

Commit

Permalink
core: add rbmap foreach node utility function
Browse files Browse the repository at this point in the history
Co-authored-by: Alexia Ingerson <[email protected]>
Signed-off-by: Stephen Oost <[email protected]>
  • Loading branch information
2 people authored and j-xiong committed Aug 8, 2024
1 parent c55adcf commit 3e8d734
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
4 changes: 4 additions & 0 deletions include/ofi_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ ofi_rbmap_create(int (*compare)(struct ofi_rbmap *map, void *key, void *data));
void ofi_rbmap_destroy(struct ofi_rbmap *map);
void ofi_rbmap_init(struct ofi_rbmap *map,
int (*compare)(struct ofi_rbmap *map, void *key, void *data));
typedef int (*ofi_rbmap_node_func_t)(struct ofi_rbmap *map,
struct ofi_rbnode *node, void *context);
int ofi_rbmap_foreach(struct ofi_rbmap *map, struct ofi_rbnode *root,
ofi_rbmap_node_func_t func, void *context);
void ofi_rbmap_cleanup(struct ofi_rbmap *map);

struct ofi_rbnode *ofi_rbmap_get_root(struct ofi_rbmap *map);
Expand Down
27 changes: 20 additions & 7 deletions src/tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,34 @@ ofi_rbmap_create(int (*compare)(struct ofi_rbmap *map, void *key, void *data))
return map;
}

static void ofi_delete_tree(struct ofi_rbmap *map, struct ofi_rbnode *node)
static int ofi_free_node(struct ofi_rbmap *map, struct ofi_rbnode *node,
void *context)
{
if (node == &map->sentinel)
return;

ofi_delete_tree(map, node->left);
ofi_delete_tree(map, node->right);
free(node);
return FI_SUCCESS;
}

int ofi_rbmap_foreach(struct ofi_rbmap *map, struct ofi_rbnode *root,
ofi_rbmap_node_func_t func, void *context)
{
int ret;
if (root == &map->sentinel)
return FI_SUCCESS;

ret = ofi_rbmap_foreach(map, root->left, func, context);
if (ret)
return ret;
ret = ofi_rbmap_foreach(map, root->right, func, context);
if (ret)
return ret;
return func(map, root, context);
}

void ofi_rbmap_cleanup(struct ofi_rbmap *map)
{
struct ofi_rbnode *node;

ofi_delete_tree(map, map->root);
(void) ofi_rbmap_foreach(map, map->root, ofi_free_node, NULL);
while (map->free_list) {
node = map->free_list;
map->free_list = node->right;
Expand Down

0 comments on commit 3e8d734

Please sign in to comment.