1 /* SPDX-License-Identifier: GPL-2.0-or-later */
4 (C) 1999 Andrea Arcangeli <andrea@suse.de>
7 linux/include/linux/rbtree.h
9 To use rbtrees you'll have to implement your own insert and search cores.
10 This will avoid us to use callbacks and to drop drammatically performances.
11 I know it's not the cleaner way, but in C (not in C++) to get
12 performances and genericity...
14 See Documentation/core-api/rbtree.rst for documentation and samples.
17 #ifndef _LINUX_RBTREE_H
18 #define _LINUX_RBTREE_H
20 #include <linux/container_of.h>
21 #include <linux/rbtree_types.h>
23 #include <linux/stddef.h>
24 #include <linux/rcupdate.h>
26 #define rb_parent(r) ((struct rb_node *)((r)->__rb_parent_color & ~3))
28 #define rb_entry(ptr, type, member) container_of(ptr, type, member)
30 #define RB_EMPTY_ROOT(root) (READ_ONCE((root)->rb_node) == NULL)
32 /* 'empty' nodes are nodes that are known not to be inserted in an rbtree */
33 #define RB_EMPTY_NODE(node) \
34 ((node)->__rb_parent_color == (unsigned long)(node))
35 #define RB_CLEAR_NODE(node) \
36 ((node)->__rb_parent_color = (unsigned long)(node))
39 extern void rb_insert_color(struct rb_node
*, struct rb_root
*);
40 extern void rb_erase(struct rb_node
*, struct rb_root
*);
43 /* Find logical next and previous nodes in a tree */
44 extern struct rb_node
*rb_next(const struct rb_node
*);
45 extern struct rb_node
*rb_prev(const struct rb_node
*);
46 extern struct rb_node
*rb_first(const struct rb_root
*);
47 extern struct rb_node
*rb_last(const struct rb_root
*);
49 /* Postorder iteration - always visit the parent after its children */
50 extern struct rb_node
*rb_first_postorder(const struct rb_root
*);
51 extern struct rb_node
*rb_next_postorder(const struct rb_node
*);
53 /* Fast replacement of a single node without remove/rebalance/add/rebalance */
54 extern void rb_replace_node(struct rb_node
*victim
, struct rb_node
*new,
55 struct rb_root
*root
);
56 extern void rb_replace_node_rcu(struct rb_node
*victim
, struct rb_node
*new,
57 struct rb_root
*root
);
59 static inline void rb_link_node(struct rb_node
*node
, struct rb_node
*parent
,
60 struct rb_node
**rb_link
)
62 node
->__rb_parent_color
= (unsigned long)parent
;
63 node
->rb_left
= node
->rb_right
= NULL
;
68 static inline void rb_link_node_rcu(struct rb_node
*node
, struct rb_node
*parent
,
69 struct rb_node
**rb_link
)
71 node
->__rb_parent_color
= (unsigned long)parent
;
72 node
->rb_left
= node
->rb_right
= NULL
;
74 rcu_assign_pointer(*rb_link
, node
);
77 #define rb_entry_safe(ptr, type, member) \
78 ({ typeof(ptr) ____ptr = (ptr); \
79 ____ptr ? rb_entry(____ptr, type, member) : NULL; \
83 * rbtree_postorder_for_each_entry_safe - iterate in post-order over rb_root of
84 * given type allowing the backing memory of @pos to be invalidated
86 * @pos: the 'type *' to use as a loop cursor.
87 * @n: another 'type *' to use as temporary storage
88 * @root: 'rb_root *' of the rbtree.
89 * @field: the name of the rb_node field within 'type'.
91 * rbtree_postorder_for_each_entry_safe() provides a similar guarantee as
92 * list_for_each_entry_safe() and allows the iteration to continue independent
93 * of changes to @pos by the body of the loop.
95 * Note, however, that it cannot handle other modifications that re-order the
96 * rbtree it is iterating over. This includes calling rb_erase() on @pos, as
97 * rb_erase() may rebalance the tree, causing us to miss some nodes.
99 #define rbtree_postorder_for_each_entry_safe(pos, n, root, field) \
100 for (pos = rb_entry_safe(rb_first_postorder(root), typeof(*pos), field); \
101 pos && ({ n = rb_entry_safe(rb_next_postorder(&pos->field), \
102 typeof(*pos), field); 1; }); \
105 /* Same as rb_first(), but O(1) */
106 #define rb_first_cached(root) (root)->rb_leftmost
108 static inline void rb_insert_color_cached(struct rb_node
*node
,
109 struct rb_root_cached
*root
,
113 root
->rb_leftmost
= node
;
114 rb_insert_color(node
, &root
->rb_root
);
118 static inline struct rb_node
*
119 rb_erase_cached(struct rb_node
*node
, struct rb_root_cached
*root
)
121 struct rb_node
*leftmost
= NULL
;
123 if (root
->rb_leftmost
== node
)
124 leftmost
= root
->rb_leftmost
= rb_next(node
);
126 rb_erase(node
, &root
->rb_root
);
131 static inline void rb_replace_node_cached(struct rb_node
*victim
,
133 struct rb_root_cached
*root
)
135 if (root
->rb_leftmost
== victim
)
136 root
->rb_leftmost
= new;
137 rb_replace_node(victim
, new, &root
->rb_root
);
141 * The below helper functions use 2 operators with 3 different
142 * calling conventions. The operators are related like:
144 * comp(a->key,b) < 0 := less(a,b)
145 * comp(a->key,b) > 0 := less(b,a)
146 * comp(a->key,b) == 0 := !less(a,b) && !less(b,a)
148 * If these operators define a partial order on the elements we make no
149 * guarantee on which of the elements matching the key is found. See
152 * The reason for this is to allow the find() interface without requiring an
153 * on-stack dummy object, which might not be feasible due to object size.
157 * rb_add_cached() - insert @node into the leftmost cached tree @tree
158 * @node: node to insert
159 * @tree: leftmost cached tree to insert @node into
160 * @less: operator defining the (partial) node order
162 * Returns @node when it is the new leftmost, or NULL.
164 static __always_inline
struct rb_node
*
165 rb_add_cached(struct rb_node
*node
, struct rb_root_cached
*tree
,
166 bool (*less
)(struct rb_node
*, const struct rb_node
*))
168 struct rb_node
**link
= &tree
->rb_root
.rb_node
;
169 struct rb_node
*parent
= NULL
;
170 bool leftmost
= true;
174 if (less(node
, parent
)) {
175 link
= &parent
->rb_left
;
177 link
= &parent
->rb_right
;
182 rb_link_node(node
, parent
, link
);
183 rb_insert_color_cached(node
, tree
, leftmost
);
185 return leftmost
? node
: NULL
;
189 * rb_add() - insert @node into @tree
190 * @node: node to insert
191 * @tree: tree to insert @node into
192 * @less: operator defining the (partial) node order
194 static __always_inline
void
195 rb_add(struct rb_node
*node
, struct rb_root
*tree
,
196 bool (*less
)(struct rb_node
*, const struct rb_node
*))
198 struct rb_node
**link
= &tree
->rb_node
;
199 struct rb_node
*parent
= NULL
;
203 if (less(node
, parent
))
204 link
= &parent
->rb_left
;
206 link
= &parent
->rb_right
;
209 rb_link_node(node
, parent
, link
);
210 rb_insert_color(node
, tree
);
214 * rb_find_add() - find equivalent @node in @tree, or add @node
215 * @node: node to look-for / insert
216 * @tree: tree to search / modify
217 * @cmp: operator defining the node order
219 * Returns the rb_node matching @node, or NULL when no match is found and @node
222 static __always_inline
struct rb_node
*
223 rb_find_add(struct rb_node
*node
, struct rb_root
*tree
,
224 int (*cmp
)(struct rb_node
*, const struct rb_node
*))
226 struct rb_node
**link
= &tree
->rb_node
;
227 struct rb_node
*parent
= NULL
;
232 c
= cmp(node
, parent
);
235 link
= &parent
->rb_left
;
237 link
= &parent
->rb_right
;
242 rb_link_node(node
, parent
, link
);
243 rb_insert_color(node
, tree
);
248 * rb_find_add_rcu() - find equivalent @node in @tree, or add @node
249 * @node: node to look-for / insert
250 * @tree: tree to search / modify
251 * @cmp: operator defining the node order
253 * Adds a Store-Release for link_node.
255 * Returns the rb_node matching @node, or NULL when no match is found and @node
258 static __always_inline
struct rb_node
*
259 rb_find_add_rcu(struct rb_node
*node
, struct rb_root
*tree
,
260 int (*cmp
)(struct rb_node
*, const struct rb_node
*))
262 struct rb_node
**link
= &tree
->rb_node
;
263 struct rb_node
*parent
= NULL
;
268 c
= cmp(node
, parent
);
271 link
= &parent
->rb_left
;
273 link
= &parent
->rb_right
;
278 rb_link_node_rcu(node
, parent
, link
);
279 rb_insert_color(node
, tree
);
284 * rb_find() - find @key in tree @tree
286 * @tree: tree to search
287 * @cmp: operator defining the node order
289 * Returns the rb_node matching @key or NULL.
291 static __always_inline
struct rb_node
*
292 rb_find(const void *key
, const struct rb_root
*tree
,
293 int (*cmp
)(const void *key
, const struct rb_node
*))
295 struct rb_node
*node
= tree
->rb_node
;
298 int c
= cmp(key
, node
);
301 node
= node
->rb_left
;
303 node
= node
->rb_right
;
312 * rb_find_rcu() - find @key in tree @tree
314 * @tree: tree to search
315 * @cmp: operator defining the node order
317 * Notably, tree descent vs concurrent tree rotations is unsound and can result
318 * in false-negatives.
320 * Returns the rb_node matching @key or NULL.
322 static __always_inline
struct rb_node
*
323 rb_find_rcu(const void *key
, const struct rb_root
*tree
,
324 int (*cmp
)(const void *key
, const struct rb_node
*))
326 struct rb_node
*node
= tree
->rb_node
;
329 int c
= cmp(key
, node
);
332 node
= rcu_dereference_raw(node
->rb_left
);
334 node
= rcu_dereference_raw(node
->rb_right
);
343 * rb_find_first() - find the first @key in @tree
345 * @tree: tree to search
346 * @cmp: operator defining node order
348 * Returns the leftmost node matching @key, or NULL.
350 static __always_inline
struct rb_node
*
351 rb_find_first(const void *key
, const struct rb_root
*tree
,
352 int (*cmp
)(const void *key
, const struct rb_node
*))
354 struct rb_node
*node
= tree
->rb_node
;
355 struct rb_node
*match
= NULL
;
358 int c
= cmp(key
, node
);
363 node
= node
->rb_left
;
365 node
= node
->rb_right
;
373 * rb_next_match() - find the next @key in @tree
375 * @tree: tree to search
376 * @cmp: operator defining node order
378 * Returns the next node matching @key, or NULL.
380 static __always_inline
struct rb_node
*
381 rb_next_match(const void *key
, struct rb_node
*node
,
382 int (*cmp
)(const void *key
, const struct rb_node
*))
384 node
= rb_next(node
);
385 if (node
&& cmp(key
, node
))
391 * rb_for_each() - iterates a subtree matching @key
394 * @tree: tree to search
395 * @cmp: operator defining node order
397 #define rb_for_each(node, key, tree, cmp) \
398 for ((node) = rb_find_first((key), (tree), (cmp)); \
399 (node); (node) = rb_next_match((key), (node), (cmp)))
401 #endif /* _LINUX_RBTREE_H */