1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 Red Black Trees
4 (C) 1999 Andrea Arcangeli <andrea@suse.de>
5 (C) 2002 David Woodhouse <dwmw2@infradead.org>
6 (C) 2012 Michel Lespinasse <walken@google.com>
7
8
9 linux/include/linux/rbtree_augmented.h
10 */
11
12 #ifndef _LINUX_RBTREE_AUGMENTED_H
13 #define _LINUX_RBTREE_AUGMENTED_H
14
15 #include <linux/compiler.h>
16 #include <linux/rbtree.h>
17 #include <linux/rcupdate.h>
18
19 /*
20 * Please note - only struct rb_augment_callbacks and the prototypes for
21 * rb_insert_augmented() and rb_erase_augmented() are intended to be public.
22 * The rest are implementation details you are not expected to depend on.
23 *
24 * See Documentation/core-api/rbtree.rst for documentation and samples.
25 */
26
27 struct rb_augment_callbacks {
28 void (*propagate)(struct rb_node *node, struct rb_node *stop);
29 void (*copy)(struct rb_node *old, struct rb_node *new);
30 void (*rotate)(struct rb_node *old, struct rb_node *new);
31 };
32
33 extern void __rb_insert_augmented(struct rb_node *node, struct rb_root *root,
34 void (*augment_rotate)(struct rb_node *old, struct rb_node *new));
35
36 /*
37 * Fixup the rbtree and update the augmented information when rebalancing.
38 *
39 * On insertion, the user must update the augmented information on the path
40 * leading to the inserted node, then call rb_link_node() as usual and
41 * rb_insert_augmented() instead of the usual rb_insert_color() call.
42 * If rb_insert_augmented() rebalances the rbtree, it will callback into
43 * a user provided function to update the augmented information on the
44 * affected subtrees.
45 */
46 static inline void
rb_insert_augmented(struct rb_node * node,struct rb_root * root,const struct rb_augment_callbacks * augment)47 rb_insert_augmented(struct rb_node *node, struct rb_root *root,
48 const struct rb_augment_callbacks *augment)
49 {
50 __rb_insert_augmented(node, root, augment->rotate);
51 }
52
53 static inline void
rb_insert_augmented_cached(struct rb_node * node,struct rb_root_cached * root,bool newleft,const struct rb_augment_callbacks * augment)54 rb_insert_augmented_cached(struct rb_node *node,
55 struct rb_root_cached *root, bool newleft,
56 const struct rb_augment_callbacks *augment)
57 {
58 if (newleft)
59 root->rb_leftmost = node;
60 rb_insert_augmented(node, &root->rb_root, augment);
61 }
62
63 static __always_inline struct rb_node *
rb_add_augmented_cached(struct rb_node * node,struct rb_root_cached * tree,bool (* less)(struct rb_node *,const struct rb_node *),const struct rb_augment_callbacks * augment)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
89 /*
90 * Template for declaring augmented rbtree callbacks (generic multi fields)
91 *
92 * RBSTATIC: 'static' or empty
93 * RBNAME: name of the rb_augment_callbacks structure
94 * RBSTRUCT: struct type of the tree nodes
95 * RBFIELD: name of struct rb_node field within RBSTRUCT
96 * RBCOPY: name of function that copies the RBAUGMENTED datas
97 * RBCOMPUTE: name of function that recomputes the RBAUGMENTED datas
98 */
99
100 #define RB_DECLARE_CALLBACKS_MULTI(RBSTATIC, RBNAME, \
101 RBSTRUCT, RBFIELD, RBCOPY, RBCOMPUTE) \
102 static inline void \
103 RBNAME ## _propagate(struct rb_node *rb, struct rb_node *stop) \
104 { \
105 while (rb != stop) { \
106 RBSTRUCT *node = rb_entry(rb, RBSTRUCT, RBFIELD); \
107 if (RBCOMPUTE(node, true)) \
108 break; \
109 rb = rb_parent(&node->RBFIELD); \
110 } \
111 } \
112 static inline void \
113 RBNAME ## _copy(struct rb_node *rb_old, struct rb_node *rb_new) \
114 { \
115 RBSTRUCT *old = rb_entry(rb_old, RBSTRUCT, RBFIELD); \
116 RBSTRUCT *new = rb_entry(rb_new, RBSTRUCT, RBFIELD); \
117 RBCOPY(new, old); \
118 } \
119 static void \
120 RBNAME ## _rotate(struct rb_node *rb_old, struct rb_node *rb_new) \
121 { \
122 RBSTRUCT *old = rb_entry(rb_old, RBSTRUCT, RBFIELD); \
123 RBSTRUCT *new = rb_entry(rb_new, RBSTRUCT, RBFIELD); \
124 RBCOPY(new, old); \
125 RBCOMPUTE(old, false); \
126 } \
127 RBSTATIC const struct rb_augment_callbacks RBNAME = { \
128 .propagate = RBNAME ## _propagate, \
129 .copy = RBNAME ## _copy, \
130 .rotate = RBNAME ## _rotate \
131 };
132
133 /*
134 * Template for declaring augmented rbtree callbacks (generic single field)
135 *
136 * RBSTATIC: 'static' or empty
137 * RBNAME: name of the rb_augment_callbacks structure
138 * RBSTRUCT: struct type of the tree nodes
139 * RBFIELD: name of struct rb_node field within RBSTRUCT
140 * RBAUGMENTED: name of field within RBSTRUCT holding data for subtree
141 * RBCOMPUTE: name of function that recomputes the RBAUGMENTED data
142 */
143
144 #define RB_DECLARE_CALLBACKS(RBSTATIC, RBNAME, \
145 RBSTRUCT, RBFIELD, RBAUGMENTED, RBCOMPUTE) \
146 static inline void \
147 RBNAME ## _copy_single(RBSTRUCT *new, RBSTRUCT *old) \
148 { \
149 new->RBAUGMENTED = old->RBAUGMENTED; \
150 } \
151 RB_DECLARE_CALLBACKS_MULTI(RBSTATIC, RBNAME, \
152 RBSTRUCT, RBFIELD, RBNAME ## _copy_single, RBCOMPUTE)
153
154 /*
155 * Template for declaring augmented rbtree callbacks,
156 * computing RBAUGMENTED scalar as max(RBCOMPUTE(node)) for all subtree nodes.
157 *
158 * RBSTATIC: 'static' or empty
159 * RBNAME: name of the rb_augment_callbacks structure
160 * RBSTRUCT: struct type of the tree nodes
161 * RBFIELD: name of struct rb_node field within RBSTRUCT
162 * RBTYPE: type of the RBAUGMENTED field
163 * RBAUGMENTED: name of RBTYPE field within RBSTRUCT holding data for subtree
164 * RBCOMPUTE: name of function that returns the per-node RBTYPE scalar
165 */
166
167 #define RB_DECLARE_CALLBACKS_MAX(RBSTATIC, RBNAME, RBSTRUCT, RBFIELD, \
168 RBTYPE, RBAUGMENTED, RBCOMPUTE) \
169 static inline bool RBNAME ## _compute_max(RBSTRUCT *node, bool exit) \
170 { \
171 RBSTRUCT *child; \
172 RBTYPE max = RBCOMPUTE(node); \
173 if (node->RBFIELD.rb_left) { \
174 child = rb_entry(node->RBFIELD.rb_left, RBSTRUCT, RBFIELD); \
175 if (child->RBAUGMENTED > max) \
176 max = child->RBAUGMENTED; \
177 } \
178 if (node->RBFIELD.rb_right) { \
179 child = rb_entry(node->RBFIELD.rb_right, RBSTRUCT, RBFIELD); \
180 if (child->RBAUGMENTED > max) \
181 max = child->RBAUGMENTED; \
182 } \
183 if (exit && node->RBAUGMENTED == max) \
184 return true; \
185 node->RBAUGMENTED = max; \
186 return false; \
187 } \
188 RB_DECLARE_CALLBACKS(RBSTATIC, RBNAME, \
189 RBSTRUCT, RBFIELD, RBAUGMENTED, RBNAME ## _compute_max)
190
191
192 #define RB_RED 0
193 #define RB_BLACK 1
194
195 #define __rb_parent(pc) ((struct rb_node *)(pc & ~3))
196
197 #define __rb_color(pc) ((pc) & 1)
198 #define __rb_is_black(pc) __rb_color(pc)
199 #define __rb_is_red(pc) (!__rb_color(pc))
200 #define rb_color(rb) __rb_color((rb)->__rb_parent_color)
201 #define rb_is_red(rb) __rb_is_red((rb)->__rb_parent_color)
202 #define rb_is_black(rb) __rb_is_black((rb)->__rb_parent_color)
203
rb_set_parent(struct rb_node * rb,struct rb_node * p)204 static inline void rb_set_parent(struct rb_node *rb, struct rb_node *p)
205 {
206 rb->__rb_parent_color = rb_color(rb) + (unsigned long)p;
207 }
208
rb_set_parent_color(struct rb_node * rb,struct rb_node * p,int color)209 static inline void rb_set_parent_color(struct rb_node *rb,
210 struct rb_node *p, int color)
211 {
212 rb->__rb_parent_color = (unsigned long)p + color;
213 }
214
215 static inline void
__rb_change_child(struct rb_node * old,struct rb_node * new,struct rb_node * parent,struct rb_root * root)216 __rb_change_child(struct rb_node *old, struct rb_node *new,
217 struct rb_node *parent, struct rb_root *root)
218 {
219 if (parent) {
220 if (parent->rb_left == old)
221 WRITE_ONCE(parent->rb_left, new);
222 else
223 WRITE_ONCE(parent->rb_right, new);
224 } else
225 WRITE_ONCE(root->rb_node, new);
226 }
227
228 static inline void
__rb_change_child_rcu(struct rb_node * old,struct rb_node * new,struct rb_node * parent,struct rb_root * root)229 __rb_change_child_rcu(struct rb_node *old, struct rb_node *new,
230 struct rb_node *parent, struct rb_root *root)
231 {
232 if (parent) {
233 if (parent->rb_left == old)
234 rcu_assign_pointer(parent->rb_left, new);
235 else
236 rcu_assign_pointer(parent->rb_right, new);
237 } else
238 rcu_assign_pointer(root->rb_node, new);
239 }
240
241 extern void __rb_erase_color(struct rb_node *parent, struct rb_root *root,
242 void (*augment_rotate)(struct rb_node *old, struct rb_node *new));
243
244 static __always_inline struct rb_node *
__rb_erase_augmented(struct rb_node * node,struct rb_root * root,const struct rb_augment_callbacks * augment)245 __rb_erase_augmented(struct rb_node *node, struct rb_root *root,
246 const struct rb_augment_callbacks *augment)
247 {
248 struct rb_node *child = node->rb_right;
249 struct rb_node *tmp = node->rb_left;
250 struct rb_node *parent, *rebalance;
251 unsigned long pc;
252
253 if (!tmp) {
254 /*
255 * Case 1: node to erase has no more than 1 child (easy!)
256 *
257 * Note that if there is one child it must be red due to 5)
258 * and node must be black due to 4). We adjust colors locally
259 * so as to bypass __rb_erase_color() later on.
260 */
261 pc = node->__rb_parent_color;
262 parent = __rb_parent(pc);
263 __rb_change_child(node, child, parent, root);
264 if (child) {
265 child->__rb_parent_color = pc;
266 rebalance = NULL;
267 } else
268 rebalance = __rb_is_black(pc) ? parent : NULL;
269 tmp = parent;
270 } else if (!child) {
271 /* Still case 1, but this time the child is node->rb_left */
272 tmp->__rb_parent_color = pc = node->__rb_parent_color;
273 parent = __rb_parent(pc);
274 __rb_change_child(node, tmp, parent, root);
275 rebalance = NULL;
276 tmp = parent;
277 } else {
278 struct rb_node *successor = child, *child2;
279
280 tmp = child->rb_left;
281 if (!tmp) {
282 /*
283 * Case 2: node's successor is its right child
284 *
285 * (n) (s)
286 * / \ / \
287 * (x) (s) -> (x) (c)
288 * \
289 * (c)
290 */
291 parent = successor;
292 child2 = successor->rb_right;
293
294 augment->copy(node, successor);
295 } else {
296 /*
297 * Case 3: node's successor is leftmost under
298 * node's right child subtree
299 *
300 * (n) (s)
301 * / \ / \
302 * (x) (y) -> (x) (y)
303 * / /
304 * (p) (p)
305 * / /
306 * (s) (c)
307 * \
308 * (c)
309 */
310 do {
311 parent = successor;
312 successor = tmp;
313 tmp = tmp->rb_left;
314 } while (tmp);
315 child2 = successor->rb_right;
316 WRITE_ONCE(parent->rb_left, child2);
317 WRITE_ONCE(successor->rb_right, child);
318 rb_set_parent(child, successor);
319
320 augment->copy(node, successor);
321 augment->propagate(parent, successor);
322 }
323
324 tmp = node->rb_left;
325 WRITE_ONCE(successor->rb_left, tmp);
326 rb_set_parent(tmp, successor);
327
328 pc = node->__rb_parent_color;
329 tmp = __rb_parent(pc);
330 __rb_change_child(node, successor, tmp, root);
331
332 if (child2) {
333 rb_set_parent_color(child2, parent, RB_BLACK);
334 rebalance = NULL;
335 } else {
336 rebalance = rb_is_black(successor) ? parent : NULL;
337 }
338 successor->__rb_parent_color = pc;
339 tmp = successor;
340 }
341
342 augment->propagate(tmp, NULL);
343 return rebalance;
344 }
345
346 static __always_inline void
rb_erase_augmented(struct rb_node * node,struct rb_root * root,const struct rb_augment_callbacks * augment)347 rb_erase_augmented(struct rb_node *node, struct rb_root *root,
348 const struct rb_augment_callbacks *augment)
349 {
350 struct rb_node *rebalance = __rb_erase_augmented(node, root, augment);
351 if (rebalance)
352 __rb_erase_color(rebalance, root, augment->rotate);
353 }
354
355 static __always_inline void
rb_erase_augmented_cached(struct rb_node * node,struct rb_root_cached * root,const struct rb_augment_callbacks * augment)356 rb_erase_augmented_cached(struct rb_node *node, struct rb_root_cached *root,
357 const struct rb_augment_callbacks *augment)
358 {
359 if (root->rb_leftmost == node)
360 root->rb_leftmost = rb_next(node);
361 rb_erase_augmented(node, &root->rb_root, augment);
362 }
363
364 #endif /* _LINUX_RBTREE_AUGMENTED_H */
365