1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#ifndef _LINUX_RBTREE_H
18#define _LINUX_RBTREE_H
19
20#include <linux/rbtree_types.h>
21
22#include <linux/kernel.h>
23#include <linux/stddef.h>
24#include <linux/rcupdate.h>
25
26#define rb_parent(r) ((struct rb_node *)((r)->__rb_parent_color & ~3))
27
28#define rb_entry(ptr, type, member) container_of(ptr, type, member)
29
30#define RB_EMPTY_ROOT(root) (READ_ONCE((root)->rb_node) == NULL)
31
32
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))
37
38
39extern void rb_insert_color(struct rb_node *, struct rb_root *);
40extern void rb_erase(struct rb_node *, struct rb_root *);
41
42
43
44extern struct rb_node *rb_next(const struct rb_node *);
45extern struct rb_node *rb_prev(const struct rb_node *);
46extern struct rb_node *rb_first(const struct rb_root *);
47extern struct rb_node *rb_last(const struct rb_root *);
48
49
50extern struct rb_node *rb_first_postorder(const struct rb_root *);
51extern struct rb_node *rb_next_postorder(const struct rb_node *);
52
53
54extern void rb_replace_node(struct rb_node *victim, struct rb_node *new,
55 struct rb_root *root);
56extern void rb_replace_node_rcu(struct rb_node *victim, struct rb_node *new,
57 struct rb_root *root);
58
59static inline void rb_link_node(struct rb_node *node, struct rb_node *parent,
60 struct rb_node **rb_link)
61{
62 node->__rb_parent_color = (unsigned long)parent;
63 node->rb_left = node->rb_right = NULL;
64
65 *rb_link = node;
66}
67
68static inline void rb_link_node_rcu(struct rb_node *node, struct rb_node *parent,
69 struct rb_node **rb_link)
70{
71 node->__rb_parent_color = (unsigned long)parent;
72 node->rb_left = node->rb_right = NULL;
73
74 rcu_assign_pointer(*rb_link, node);
75}
76
77#define rb_entry_safe(ptr, type, member) \
78 ({ typeof(ptr) ____ptr = (ptr); \
79 ____ptr ? rb_entry(____ptr, type, member) : NULL; \
80 })
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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; }); \
103 pos = n)
104
105
106#define rb_first_cached(root) (root)->rb_leftmost
107
108static inline void rb_insert_color_cached(struct rb_node *node,
109 struct rb_root_cached *root,
110 bool leftmost)
111{
112 if (leftmost)
113 root->rb_leftmost = node;
114 rb_insert_color(node, &root->rb_root);
115}
116
117
118static inline struct rb_node *
119rb_erase_cached(struct rb_node *node, struct rb_root_cached *root)
120{
121 struct rb_node *leftmost = NULL;
122
123 if (root->rb_leftmost == node)
124 leftmost = root->rb_leftmost = rb_next(node);
125
126 rb_erase(node, &root->rb_root);
127
128 return leftmost;
129}
130
131static inline void rb_replace_node_cached(struct rb_node *victim,
132 struct rb_node *new,
133 struct rb_root_cached *root)
134{
135 if (root->rb_leftmost == victim)
136 root->rb_leftmost = new;
137 rb_replace_node(victim, new, &root->rb_root);
138}
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164static __always_inline struct rb_node *
165rb_add_cached(struct rb_node *node, struct rb_root_cached *tree,
166 bool (*less)(struct rb_node *, const struct rb_node *))
167{
168 struct rb_node **link = &tree->rb_root.rb_node;
169 struct rb_node *parent = NULL;
170 bool leftmost = true;
171
172 while (*link) {
173 parent = *link;
174 if (less(node, parent)) {
175 link = &parent->rb_left;
176 } else {
177 link = &parent->rb_right;
178 leftmost = false;
179 }
180 }
181
182 rb_link_node(node, parent, link);
183 rb_insert_color_cached(node, tree, leftmost);
184
185 return leftmost ? node : NULL;
186}
187
188
189
190
191
192
193
194static __always_inline void
195rb_add(struct rb_node *node, struct rb_root *tree,
196 bool (*less)(struct rb_node *, const struct rb_node *))
197{
198 struct rb_node **link = &tree->rb_node;
199 struct rb_node *parent = NULL;
200
201 while (*link) {
202 parent = *link;
203 if (less(node, parent))
204 link = &parent->rb_left;
205 else
206 link = &parent->rb_right;
207 }
208
209 rb_link_node(node, parent, link);
210 rb_insert_color(node, tree);
211}
212
213
214
215
216
217
218
219
220
221
222static __always_inline struct rb_node *
223rb_find_add(struct rb_node *node, struct rb_root *tree,
224 int (*cmp)(struct rb_node *, const struct rb_node *))
225{
226 struct rb_node **link = &tree->rb_node;
227 struct rb_node *parent = NULL;
228 int c;
229
230 while (*link) {
231 parent = *link;
232 c = cmp(node, parent);
233
234 if (c < 0)
235 link = &parent->rb_left;
236 else if (c > 0)
237 link = &parent->rb_right;
238 else
239 return parent;
240 }
241
242 rb_link_node(node, parent, link);
243 rb_insert_color(node, tree);
244 return NULL;
245}
246
247
248
249
250
251
252
253
254
255static __always_inline struct rb_node *
256rb_find(const void *key, const struct rb_root *tree,
257 int (*cmp)(const void *key, const struct rb_node *))
258{
259 struct rb_node *node = tree->rb_node;
260
261 while (node) {
262 int c = cmp(key, node);
263
264 if (c < 0)
265 node = node->rb_left;
266 else if (c > 0)
267 node = node->rb_right;
268 else
269 return node;
270 }
271
272 return NULL;
273}
274
275
276
277
278
279
280
281
282
283static __always_inline struct rb_node *
284rb_find_first(const void *key, const struct rb_root *tree,
285 int (*cmp)(const void *key, const struct rb_node *))
286{
287 struct rb_node *node = tree->rb_node;
288 struct rb_node *match = NULL;
289
290 while (node) {
291 int c = cmp(key, node);
292
293 if (c <= 0) {
294 if (!c)
295 match = node;
296 node = node->rb_left;
297 } else if (c > 0) {
298 node = node->rb_right;
299 }
300 }
301
302 return match;
303}
304
305
306
307
308
309
310
311
312
313static __always_inline struct rb_node *
314rb_next_match(const void *key, struct rb_node *node,
315 int (*cmp)(const void *key, const struct rb_node *))
316{
317 node = rb_next(node);
318 if (node && cmp(key, node))
319 node = NULL;
320 return node;
321}
322
323
324
325
326
327
328
329
330#define rb_for_each(node, key, tree, cmp) \
331 for ((node) = rb_find_first((key), (tree), (cmp)); \
332 (node); (node) = rb_next_match((key), (node), (cmp)))
333
334#endif
335