This source file includes following definitions.
- rb_insert_augmented
- rb_insert_augmented_cached
- rb_set_parent
- rb_set_parent_color
- __rb_change_child
- __rb_erase_augmented
- rb_erase_augmented
- rb_erase_augmented_cached
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 #ifndef _TOOLS_LINUX_RBTREE_AUGMENTED_H
16 #define _TOOLS_LINUX_RBTREE_AUGMENTED_H
17
18 #include <linux/compiler.h>
19 #include <linux/rbtree.h>
20
21
22
23
24
25
26
27
28
29 struct rb_augment_callbacks {
30 void (*propagate)(struct rb_node *node, struct rb_node *stop);
31 void (*copy)(struct rb_node *old, struct rb_node *new);
32 void (*rotate)(struct rb_node *old, struct rb_node *new);
33 };
34
35 extern void __rb_insert_augmented(struct rb_node *node, struct rb_root *root,
36 void (*augment_rotate)(struct rb_node *old, struct rb_node *new));
37
38
39
40
41
42
43
44
45
46
47
48 static inline void
49 rb_insert_augmented(struct rb_node *node, struct rb_root *root,
50 const struct rb_augment_callbacks *augment)
51 {
52 __rb_insert_augmented(node, root, augment->rotate);
53 }
54
55 static inline void
56 rb_insert_augmented_cached(struct rb_node *node,
57 struct rb_root_cached *root, bool newleft,
58 const struct rb_augment_callbacks *augment)
59 {
60 if (newleft)
61 root->rb_leftmost = node;
62 rb_insert_augmented(node, &root->rb_root, augment);
63 }
64
65
66
67
68
69
70
71
72
73
74
75
76 #define RB_DECLARE_CALLBACKS(RBSTATIC, RBNAME, \
77 RBSTRUCT, RBFIELD, RBAUGMENTED, RBCOMPUTE) \
78 static inline void \
79 RBNAME ## _propagate(struct rb_node *rb, struct rb_node *stop) \
80 { \
81 while (rb != stop) { \
82 RBSTRUCT *node = rb_entry(rb, RBSTRUCT, RBFIELD); \
83 if (RBCOMPUTE(node, true)) \
84 break; \
85 rb = rb_parent(&node->RBFIELD); \
86 } \
87 } \
88 static inline void \
89 RBNAME ## _copy(struct rb_node *rb_old, struct rb_node *rb_new) \
90 { \
91 RBSTRUCT *old = rb_entry(rb_old, RBSTRUCT, RBFIELD); \
92 RBSTRUCT *new = rb_entry(rb_new, RBSTRUCT, RBFIELD); \
93 new->RBAUGMENTED = old->RBAUGMENTED; \
94 } \
95 static void \
96 RBNAME ## _rotate(struct rb_node *rb_old, struct rb_node *rb_new) \
97 { \
98 RBSTRUCT *old = rb_entry(rb_old, RBSTRUCT, RBFIELD); \
99 RBSTRUCT *new = rb_entry(rb_new, RBSTRUCT, RBFIELD); \
100 new->RBAUGMENTED = old->RBAUGMENTED; \
101 RBCOMPUTE(old, false); \
102 } \
103 RBSTATIC const struct rb_augment_callbacks RBNAME = { \
104 .propagate = RBNAME ## _propagate, \
105 .copy = RBNAME ## _copy, \
106 .rotate = RBNAME ## _rotate \
107 };
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122 #define RB_DECLARE_CALLBACKS_MAX(RBSTATIC, RBNAME, RBSTRUCT, RBFIELD, \
123 RBTYPE, RBAUGMENTED, RBCOMPUTE) \
124 static inline bool RBNAME ## _compute_max(RBSTRUCT *node, bool exit) \
125 { \
126 RBSTRUCT *child; \
127 RBTYPE max = RBCOMPUTE(node); \
128 if (node->RBFIELD.rb_left) { \
129 child = rb_entry(node->RBFIELD.rb_left, RBSTRUCT, RBFIELD); \
130 if (child->RBAUGMENTED > max) \
131 max = child->RBAUGMENTED; \
132 } \
133 if (node->RBFIELD.rb_right) { \
134 child = rb_entry(node->RBFIELD.rb_right, RBSTRUCT, RBFIELD); \
135 if (child->RBAUGMENTED > max) \
136 max = child->RBAUGMENTED; \
137 } \
138 if (exit && node->RBAUGMENTED == max) \
139 return true; \
140 node->RBAUGMENTED = max; \
141 return false; \
142 } \
143 RB_DECLARE_CALLBACKS(RBSTATIC, RBNAME, \
144 RBSTRUCT, RBFIELD, RBAUGMENTED, RBNAME ## _compute_max)
145
146
147 #define RB_RED 0
148 #define RB_BLACK 1
149
150 #define __rb_parent(pc) ((struct rb_node *)(pc & ~3))
151
152 #define __rb_color(pc) ((pc) & 1)
153 #define __rb_is_black(pc) __rb_color(pc)
154 #define __rb_is_red(pc) (!__rb_color(pc))
155 #define rb_color(rb) __rb_color((rb)->__rb_parent_color)
156 #define rb_is_red(rb) __rb_is_red((rb)->__rb_parent_color)
157 #define rb_is_black(rb) __rb_is_black((rb)->__rb_parent_color)
158
159 static inline void rb_set_parent(struct rb_node *rb, struct rb_node *p)
160 {
161 rb->__rb_parent_color = rb_color(rb) | (unsigned long)p;
162 }
163
164 static inline void rb_set_parent_color(struct rb_node *rb,
165 struct rb_node *p, int color)
166 {
167 rb->__rb_parent_color = (unsigned long)p | color;
168 }
169
170 static inline void
171 __rb_change_child(struct rb_node *old, struct rb_node *new,
172 struct rb_node *parent, struct rb_root *root)
173 {
174 if (parent) {
175 if (parent->rb_left == old)
176 WRITE_ONCE(parent->rb_left, new);
177 else
178 WRITE_ONCE(parent->rb_right, new);
179 } else
180 WRITE_ONCE(root->rb_node, new);
181 }
182
183 extern void __rb_erase_color(struct rb_node *parent, struct rb_root *root,
184 void (*augment_rotate)(struct rb_node *old, struct rb_node *new));
185
186 static __always_inline struct rb_node *
187 __rb_erase_augmented(struct rb_node *node, struct rb_root *root,
188 const struct rb_augment_callbacks *augment)
189 {
190 struct rb_node *child = node->rb_right;
191 struct rb_node *tmp = node->rb_left;
192 struct rb_node *parent, *rebalance;
193 unsigned long pc;
194
195 if (!tmp) {
196
197
198
199
200
201
202
203 pc = node->__rb_parent_color;
204 parent = __rb_parent(pc);
205 __rb_change_child(node, child, parent, root);
206 if (child) {
207 child->__rb_parent_color = pc;
208 rebalance = NULL;
209 } else
210 rebalance = __rb_is_black(pc) ? parent : NULL;
211 tmp = parent;
212 } else if (!child) {
213
214 tmp->__rb_parent_color = pc = node->__rb_parent_color;
215 parent = __rb_parent(pc);
216 __rb_change_child(node, tmp, parent, root);
217 rebalance = NULL;
218 tmp = parent;
219 } else {
220 struct rb_node *successor = child, *child2;
221
222 tmp = child->rb_left;
223 if (!tmp) {
224
225
226
227
228
229
230
231
232
233 parent = successor;
234 child2 = successor->rb_right;
235
236 augment->copy(node, successor);
237 } else {
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252 do {
253 parent = successor;
254 successor = tmp;
255 tmp = tmp->rb_left;
256 } while (tmp);
257 child2 = successor->rb_right;
258 WRITE_ONCE(parent->rb_left, child2);
259 WRITE_ONCE(successor->rb_right, child);
260 rb_set_parent(child, successor);
261
262 augment->copy(node, successor);
263 augment->propagate(parent, successor);
264 }
265
266 tmp = node->rb_left;
267 WRITE_ONCE(successor->rb_left, tmp);
268 rb_set_parent(tmp, successor);
269
270 pc = node->__rb_parent_color;
271 tmp = __rb_parent(pc);
272 __rb_change_child(node, successor, tmp, root);
273
274 if (child2) {
275 successor->__rb_parent_color = pc;
276 rb_set_parent_color(child2, parent, RB_BLACK);
277 rebalance = NULL;
278 } else {
279 unsigned long pc2 = successor->__rb_parent_color;
280 successor->__rb_parent_color = pc;
281 rebalance = __rb_is_black(pc2) ? parent : NULL;
282 }
283 tmp = successor;
284 }
285
286 augment->propagate(tmp, NULL);
287 return rebalance;
288 }
289
290 static __always_inline void
291 rb_erase_augmented(struct rb_node *node, struct rb_root *root,
292 const struct rb_augment_callbacks *augment)
293 {
294 struct rb_node *rebalance = __rb_erase_augmented(node, root, augment);
295 if (rebalance)
296 __rb_erase_color(rebalance, root, augment->rotate);
297 }
298
299 static __always_inline void
300 rb_erase_augmented_cached(struct rb_node *node, struct rb_root_cached *root,
301 const struct rb_augment_callbacks *augment)
302 {
303 if (root->rb_leftmost == node)
304 root->rb_leftmost = rb_next(node);
305 rb_erase_augmented(node, &root->rb_root, augment);
306 }
307
308 #endif