Skip to content

Commit 99d4d26

Browse files
Peter ZijlstraIngo Molnar
authored andcommitted
rbtree: Add rb_add_augmented_cached() helper
While slightly sub-optimal, updating the augmented data while going down the tree during lookup would be faster -- alas the augment interface does not currently allow for that, provide a generic helper to add a node to an augmented cached tree. Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Signed-off-by: Ingo Molnar <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 86bfbb7 commit 99d4d26

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

include/linux/rbtree_augmented.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,32 @@ rb_insert_augmented_cached(struct rb_node *node,
6060
rb_insert_augmented(node, &root->rb_root, augment);
6161
}
6262

63+
static __always_inline struct rb_node *
64+
rb_add_augmented_cached(struct rb_node *node, struct rb_root_cached *tree,
65+
bool (*less)(struct rb_node *, const struct rb_node *),
66+
const struct rb_augment_callbacks *augment)
67+
{
68+
struct rb_node **link = &tree->rb_root.rb_node;
69+
struct rb_node *parent = NULL;
70+
bool leftmost = true;
71+
72+
while (*link) {
73+
parent = *link;
74+
if (less(node, parent)) {
75+
link = &parent->rb_left;
76+
} else {
77+
link = &parent->rb_right;
78+
leftmost = false;
79+
}
80+
}
81+
82+
rb_link_node(node, parent, link);
83+
augment->propagate(parent, NULL); /* suboptimal */
84+
rb_insert_augmented_cached(node, tree, leftmost, augment);
85+
86+
return leftmost ? node : NULL;
87+
}
88+
6389
/*
6490
* Template for declaring augmented rbtree callbacks (generic case)
6591
*

0 commit comments

Comments
 (0)