augmented rbtree: add new RB_DECLARE_CALLBACKS_MAX macro
[linux-2.6-block.git] / include / linux / rbtree_augmented.h
CommitLineData
1a59d1b8 1/* SPDX-License-Identifier: GPL-2.0-or-later */
9c079add
ML
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
9c079add
ML
8
9 linux/include/linux/rbtree_augmented.h
10*/
11
12#ifndef _LINUX_RBTREE_AUGMENTED_H
13#define _LINUX_RBTREE_AUGMENTED_H
14
29fc7c5a 15#include <linux/compiler.h>
9c079add 16#include <linux/rbtree.h>
2075b16e 17#include <linux/rcupdate.h>
9c079add
ML
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/rbtree.txt for documentation and samples.
25 */
26
27struct 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
9f973cb3 33extern void __rb_insert_augmented(struct rb_node *node, struct rb_root *root,
9c079add 34 void (*augment_rotate)(struct rb_node *old, struct rb_node *new));
9f973cb3 35
a841b659
LJ
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
7e5ca363
WY
41 * rb_insert_augmented() instead of the usual rb_insert_color() call.
42 * If rb_insert_augmented() rebalances the rbtree, it will callback into
a841b659
LJ
43 * a user provided function to update the augmented information on the
44 * affected subtrees.
45 */
9c079add
ML
46static inline void
47rb_insert_augmented(struct rb_node *node, struct rb_root *root,
48 const struct rb_augment_callbacks *augment)
49{
9f973cb3 50 __rb_insert_augmented(node, root, augment->rotate);
cd9e61ed
DB
51}
52
53static inline void
54rb_insert_augmented_cached(struct rb_node *node,
55 struct rb_root_cached *root, bool newleft,
56 const struct rb_augment_callbacks *augment)
57{
9f973cb3
ML
58 if (newleft)
59 root->rb_leftmost = node;
60 rb_insert_augmented(node, &root->rb_root, augment);
9c079add
ML
61}
62
444b8a83 63/*
315cc066 64 * Template for declaring augmented rbtree callbacks (generic case)
444b8a83
ML
65 *
66 * RBSTATIC: 'static' or empty
67 * RBNAME: name of the rb_augment_callbacks structure
68 * RBSTRUCT: struct type of the tree nodes
69 * RBFIELD: name of struct rb_node field within RBSTRUCT
70 * RBTYPE: type of the RBAUGMENTED field
71 * RBAUGMENTED: name of RBTYPE field within RBSTRUCT holding data for subtree
72 * RBCOMPUTE: name of function that recomputes the RBAUGMENTED data
73 */
74
75#define RB_DECLARE_CALLBACKS(RBSTATIC, RBNAME, RBSTRUCT, RBFIELD, \
76 RBTYPE, RBAUGMENTED, RBCOMPUTE) \
9c079add 77static inline void \
444b8a83 78RBNAME ## _propagate(struct rb_node *rb, struct rb_node *stop) \
9c079add
ML
79{ \
80 while (rb != stop) { \
444b8a83
ML
81 RBSTRUCT *node = rb_entry(rb, RBSTRUCT, RBFIELD); \
82 RBTYPE augmented = RBCOMPUTE(node); \
83 if (node->RBAUGMENTED == augmented) \
9c079add 84 break; \
444b8a83
ML
85 node->RBAUGMENTED = augmented; \
86 rb = rb_parent(&node->RBFIELD); \
9c079add
ML
87 } \
88} \
89static inline void \
444b8a83 90RBNAME ## _copy(struct rb_node *rb_old, struct rb_node *rb_new) \
9c079add 91{ \
444b8a83
ML
92 RBSTRUCT *old = rb_entry(rb_old, RBSTRUCT, RBFIELD); \
93 RBSTRUCT *new = rb_entry(rb_new, RBSTRUCT, RBFIELD); \
94 new->RBAUGMENTED = old->RBAUGMENTED; \
9c079add
ML
95} \
96static void \
444b8a83 97RBNAME ## _rotate(struct rb_node *rb_old, struct rb_node *rb_new) \
9c079add 98{ \
444b8a83
ML
99 RBSTRUCT *old = rb_entry(rb_old, RBSTRUCT, RBFIELD); \
100 RBSTRUCT *new = rb_entry(rb_new, RBSTRUCT, RBFIELD); \
101 new->RBAUGMENTED = old->RBAUGMENTED; \
102 old->RBAUGMENTED = RBCOMPUTE(old); \
9c079add 103} \
444b8a83
ML
104RBSTATIC const struct rb_augment_callbacks RBNAME = { \
105 .propagate = RBNAME ## _propagate, \
106 .copy = RBNAME ## _copy, \
107 .rotate = RBNAME ## _rotate \
9c079add
ML
108};
109
315cc066
ML
110/*
111 * Template for declaring augmented rbtree callbacks,
112 * computing RBAUGMENTED scalar as max(RBCOMPUTE(node)) for all subtree nodes.
113 *
114 * RBSTATIC: 'static' or empty
115 * RBNAME: name of the rb_augment_callbacks structure
116 * RBSTRUCT: struct type of the tree nodes
117 * RBFIELD: name of struct rb_node field within RBSTRUCT
118 * RBTYPE: type of the RBAUGMENTED field
119 * RBAUGMENTED: name of RBTYPE field within RBSTRUCT holding data for subtree
120 * RBCOMPUTE: name of function that returns the per-node RBTYPE scalar
121 */
122
123#define RB_DECLARE_CALLBACKS_MAX(RBSTATIC, RBNAME, RBSTRUCT, RBFIELD, \
124 RBTYPE, RBAUGMENTED, RBCOMPUTE) \
125static inline RBTYPE RBNAME ## _compute_max(RBSTRUCT *node) \
126{ \
127 RBSTRUCT *child; \
128 RBTYPE max = RBCOMPUTE(node); \
129 if (node->RBFIELD.rb_left) { \
130 child = rb_entry(node->RBFIELD.rb_left, RBSTRUCT, RBFIELD); \
131 if (child->RBAUGMENTED > max) \
132 max = child->RBAUGMENTED; \
133 } \
134 if (node->RBFIELD.rb_right) { \
135 child = rb_entry(node->RBFIELD.rb_right, RBSTRUCT, RBFIELD); \
136 if (child->RBAUGMENTED > max) \
137 max = child->RBAUGMENTED; \
138 } \
139 return max; \
140} \
141RB_DECLARE_CALLBACKS(RBSTATIC, RBNAME, RBSTRUCT, RBFIELD, \
142 RBTYPE, RBAUGMENTED, RBNAME ## _compute_max)
143
9c079add
ML
144
145#define RB_RED 0
146#define RB_BLACK 1
147
148#define __rb_parent(pc) ((struct rb_node *)(pc & ~3))
149
150#define __rb_color(pc) ((pc) & 1)
151#define __rb_is_black(pc) __rb_color(pc)
152#define __rb_is_red(pc) (!__rb_color(pc))
153#define rb_color(rb) __rb_color((rb)->__rb_parent_color)
154#define rb_is_red(rb) __rb_is_red((rb)->__rb_parent_color)
155#define rb_is_black(rb) __rb_is_black((rb)->__rb_parent_color)
156
157static inline void rb_set_parent(struct rb_node *rb, struct rb_node *p)
158{
159 rb->__rb_parent_color = rb_color(rb) | (unsigned long)p;
160}
161
162static inline void rb_set_parent_color(struct rb_node *rb,
163 struct rb_node *p, int color)
164{
165 rb->__rb_parent_color = (unsigned long)p | color;
166}
167
168static inline void
169__rb_change_child(struct rb_node *old, struct rb_node *new,
170 struct rb_node *parent, struct rb_root *root)
171{
172 if (parent) {
173 if (parent->rb_left == old)
d72da4a4 174 WRITE_ONCE(parent->rb_left, new);
9c079add 175 else
d72da4a4 176 WRITE_ONCE(parent->rb_right, new);
9c079add 177 } else
d72da4a4 178 WRITE_ONCE(root->rb_node, new);
9c079add
ML
179}
180
c1adf200
DH
181static inline void
182__rb_change_child_rcu(struct rb_node *old, struct rb_node *new,
183 struct rb_node *parent, struct rb_root *root)
184{
185 if (parent) {
186 if (parent->rb_left == old)
187 rcu_assign_pointer(parent->rb_left, new);
188 else
189 rcu_assign_pointer(parent->rb_right, new);
190 } else
191 rcu_assign_pointer(root->rb_node, new);
192}
193
9c079add
ML
194extern void __rb_erase_color(struct rb_node *parent, struct rb_root *root,
195 void (*augment_rotate)(struct rb_node *old, struct rb_node *new));
196
3cb7a563
ML
197static __always_inline struct rb_node *
198__rb_erase_augmented(struct rb_node *node, struct rb_root *root,
199 const struct rb_augment_callbacks *augment)
9c079add 200{
d72da4a4
PZ
201 struct rb_node *child = node->rb_right;
202 struct rb_node *tmp = node->rb_left;
9c079add
ML
203 struct rb_node *parent, *rebalance;
204 unsigned long pc;
205
206 if (!tmp) {
207 /*
208 * Case 1: node to erase has no more than 1 child (easy!)
209 *
210 * Note that if there is one child it must be red due to 5)
211 * and node must be black due to 4). We adjust colors locally
212 * so as to bypass __rb_erase_color() later on.
213 */
214 pc = node->__rb_parent_color;
215 parent = __rb_parent(pc);
216 __rb_change_child(node, child, parent, root);
217 if (child) {
218 child->__rb_parent_color = pc;
219 rebalance = NULL;
220 } else
221 rebalance = __rb_is_black(pc) ? parent : NULL;
222 tmp = parent;
223 } else if (!child) {
224 /* Still case 1, but this time the child is node->rb_left */
225 tmp->__rb_parent_color = pc = node->__rb_parent_color;
226 parent = __rb_parent(pc);
227 __rb_change_child(node, tmp, parent, root);
228 rebalance = NULL;
229 tmp = parent;
230 } else {
231 struct rb_node *successor = child, *child2;
d72da4a4 232
9c079add
ML
233 tmp = child->rb_left;
234 if (!tmp) {
235 /*
236 * Case 2: node's successor is its right child
237 *
238 * (n) (s)
239 * / \ / \
240 * (x) (s) -> (x) (c)
241 * \
242 * (c)
243 */
244 parent = successor;
245 child2 = successor->rb_right;
d72da4a4 246
9c079add
ML
247 augment->copy(node, successor);
248 } else {
249 /*
250 * Case 3: node's successor is leftmost under
251 * node's right child subtree
252 *
253 * (n) (s)
254 * / \ / \
255 * (x) (y) -> (x) (y)
256 * / /
257 * (p) (p)
258 * / /
259 * (s) (c)
260 * \
261 * (c)
262 */
263 do {
264 parent = successor;
265 successor = tmp;
266 tmp = tmp->rb_left;
267 } while (tmp);
d72da4a4
PZ
268 child2 = successor->rb_right;
269 WRITE_ONCE(parent->rb_left, child2);
270 WRITE_ONCE(successor->rb_right, child);
9c079add 271 rb_set_parent(child, successor);
d72da4a4 272
9c079add
ML
273 augment->copy(node, successor);
274 augment->propagate(parent, successor);
275 }
276
d72da4a4
PZ
277 tmp = node->rb_left;
278 WRITE_ONCE(successor->rb_left, tmp);
9c079add
ML
279 rb_set_parent(tmp, successor);
280
281 pc = node->__rb_parent_color;
282 tmp = __rb_parent(pc);
283 __rb_change_child(node, successor, tmp, root);
d72da4a4 284
9c079add
ML
285 if (child2) {
286 successor->__rb_parent_color = pc;
287 rb_set_parent_color(child2, parent, RB_BLACK);
288 rebalance = NULL;
289 } else {
290 unsigned long pc2 = successor->__rb_parent_color;
291 successor->__rb_parent_color = pc;
292 rebalance = __rb_is_black(pc2) ? parent : NULL;
293 }
294 tmp = successor;
295 }
296
297 augment->propagate(tmp, NULL);
3cb7a563
ML
298 return rebalance;
299}
300
301static __always_inline void
302rb_erase_augmented(struct rb_node *node, struct rb_root *root,
303 const struct rb_augment_callbacks *augment)
304{
9f973cb3 305 struct rb_node *rebalance = __rb_erase_augmented(node, root, augment);
9c079add
ML
306 if (rebalance)
307 __rb_erase_color(rebalance, root, augment->rotate);
308}
309
cd9e61ed
DB
310static __always_inline void
311rb_erase_augmented_cached(struct rb_node *node, struct rb_root_cached *root,
312 const struct rb_augment_callbacks *augment)
313{
9f973cb3
ML
314 if (root->rb_leftmost == node)
315 root->rb_leftmost = rb_next(node);
316 rb_erase_augmented(node, &root->rb_root, augment);
cd9e61ed
DB
317}
318
9c079add 319#endif /* _LINUX_RBTREE_AUGMENTED_H */