1
0
Fork 0
mirror of synced 2025-03-06 20:59:54 +01:00

rbtree: add rb_find_add_cached() to rbtree.h

Adds rb_find_add_cached() as a helper function for use with red-black
trees. Used in btrfs to reduce boilerplate code.

And since it's a new helper, the cmp() function will require both
parameter to be const rb_node pointers.

Suggested-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: Roger L. Beckermeyer III <beckerlee3@gmail.com>
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Reviewed-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: Qu Wenruo <wqu@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
Roger L. Beckermeyer III 2024-12-18 08:28:50 +10:30 committed by David Sterba
parent 57e421867b
commit 90dde9a13c

View file

@ -210,6 +210,43 @@ rb_add(struct rb_node *node, struct rb_root *tree,
rb_insert_color(node, tree);
}
/**
* rb_find_add_cached() - find equivalent @node in @tree, or add @node
* @node: node to look-for / insert
* @tree: tree to search / modify
* @cmp: operator defining the node order
*
* Returns the rb_node matching @node, or NULL when no match is found and @node
* is inserted.
*/
static __always_inline struct rb_node *
rb_find_add_cached(struct rb_node *node, struct rb_root_cached *tree,
int (*cmp)(const struct rb_node *new, const struct rb_node *exist))
{
bool leftmost = true;
struct rb_node **link = &tree->rb_root.rb_node;
struct rb_node *parent = NULL;
int c;
while (*link) {
parent = *link;
c = cmp(node, parent);
if (c < 0) {
link = &parent->rb_left;
} else if (c > 0) {
link = &parent->rb_right;
leftmost = false;
} else {
return parent;
}
}
rb_link_node(node, parent, link);
rb_insert_color_cached(node, tree, leftmost);
return NULL;
}
/**
* rb_find_add() - find equivalent @node in @tree, or add @node
* @node: node to look-for / insert