augmented rbtree: add comments for RB_DECLARE_CALLBACKS 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
ML
63/*
64 * Template for declaring augmented rbtree callbacks
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
110
111#define RB_RED 0
112#define RB_BLACK 1
113
114#define __rb_parent(pc) ((struct rb_node *)(pc & ~3))
115
116#define __rb_color(pc) ((pc) & 1)
117#define __rb_is_black(pc) __rb_color(pc)
118#define __rb_is_red(pc) (!__rb_color(pc))
119#define rb_color(rb) __rb_color((rb)->__rb_parent_color)
120#define rb_is_red(rb) __rb_is_red((rb)->__rb_parent_color)
121#define rb_is_black(rb) __rb_is_black((rb)->__rb_parent_color)
122
123static inline void rb_set_parent(struct rb_node *rb, struct rb_node *p)
124{
125 rb->__rb_parent_color = rb_color(rb) | (unsigned long)p;
126}
127
128static inline void rb_set_parent_color(struct rb_node *rb,
129 struct rb_node *p, int color)
130{
131 rb->__rb_parent_color = (unsigned long)p | color;
132}
133
134static inline void
135__rb_change_child(struct rb_node *old, struct rb_node *new,
136 struct rb_node *parent, struct rb_root *root)
137{
138 if (parent) {
139 if (parent->rb_left == old)
d72da4a4 140 WRITE_ONCE(parent->rb_left, new);
9c079add 141 else
d72da4a4 142 WRITE_ONCE(parent->rb_right, new);
9c079add 143 } else
d72da4a4 144 WRITE_ONCE(root->rb_node, new);
9c079add
ML
145}
146
c1adf200
DH
147static inline void
148__rb_change_child_rcu(struct rb_node *old, struct rb_node *new,
149 struct rb_node *parent, struct rb_root *root)
150{
151 if (parent) {
152 if (parent->rb_left == old)
153 rcu_assign_pointer(parent->rb_left, new);
154 else
155 rcu_assign_pointer(parent->rb_right, new);
156 } else
157 rcu_assign_pointer(root->rb_node, new);
158}
159
9c079add
ML
160extern void __rb_erase_color(struct rb_node *parent, struct rb_root *root,
161 void (*augment_rotate)(struct rb_node *old, struct rb_node *new));
162
3cb7a563
ML
163static __always_inline struct rb_node *
164__rb_erase_augmented(struct rb_node *node, struct rb_root *root,
165 const struct rb_augment_callbacks *augment)
9c079add 166{
d72da4a4
PZ
167 struct rb_node *child = node->rb_right;
168 struct rb_node *tmp = node->rb_left;
9c079add
ML
169 struct rb_node *parent, *rebalance;
170 unsigned long pc;
171
172 if (!tmp) {
173 /*
174 * Case 1: node to erase has no more than 1 child (easy!)
175 *
176 * Note that if there is one child it must be red due to 5)
177 * and node must be black due to 4). We adjust colors locally
178 * so as to bypass __rb_erase_color() later on.
179 */
180 pc = node->__rb_parent_color;
181 parent = __rb_parent(pc);
182 __rb_change_child(node, child, parent, root);
183 if (child) {
184 child->__rb_parent_color = pc;
185 rebalance = NULL;
186 } else
187 rebalance = __rb_is_black(pc) ? parent : NULL;
188 tmp = parent;
189 } else if (!child) {
190 /* Still case 1, but this time the child is node->rb_left */
191 tmp->__rb_parent_color = pc = node->__rb_parent_color;
192 parent = __rb_parent(pc);
193 __rb_change_child(node, tmp, parent, root);
194 rebalance = NULL;
195 tmp = parent;
196 } else {
197 struct rb_node *successor = child, *child2;
d72da4a4 198
9c079add
ML
199 tmp = child->rb_left;
200 if (!tmp) {
201 /*
202 * Case 2: node's successor is its right child
203 *
204 * (n) (s)
205 * / \ / \
206 * (x) (s) -> (x) (c)
207 * \
208 * (c)
209 */
210 parent = successor;
211 child2 = successor->rb_right;
d72da4a4 212
9c079add
ML
213 augment->copy(node, successor);
214 } else {
215 /*
216 * Case 3: node's successor is leftmost under
217 * node's right child subtree
218 *
219 * (n) (s)
220 * / \ / \
221 * (x) (y) -> (x) (y)
222 * / /
223 * (p) (p)
224 * / /
225 * (s) (c)
226 * \
227 * (c)
228 */
229 do {
230 parent = successor;
231 successor = tmp;
232 tmp = tmp->rb_left;
233 } while (tmp);
d72da4a4
PZ
234 child2 = successor->rb_right;
235 WRITE_ONCE(parent->rb_left, child2);
236 WRITE_ONCE(successor->rb_right, child);
9c079add 237 rb_set_parent(child, successor);
d72da4a4 238
9c079add
ML
239 augment->copy(node, successor);
240 augment->propagate(parent, successor);
241 }
242
d72da4a4
PZ
243 tmp = node->rb_left;
244 WRITE_ONCE(successor->rb_left, tmp);
9c079add
ML
245 rb_set_parent(tmp, successor);
246
247 pc = node->__rb_parent_color;
248 tmp = __rb_parent(pc);
249 __rb_change_child(node, successor, tmp, root);
d72da4a4 250
9c079add
ML
251 if (child2) {
252 successor->__rb_parent_color = pc;
253 rb_set_parent_color(child2, parent, RB_BLACK);
254 rebalance = NULL;
255 } else {
256 unsigned long pc2 = successor->__rb_parent_color;
257 successor->__rb_parent_color = pc;
258 rebalance = __rb_is_black(pc2) ? parent : NULL;
259 }
260 tmp = successor;
261 }
262
263 augment->propagate(tmp, NULL);
3cb7a563
ML
264 return rebalance;
265}
266
267static __always_inline void
268rb_erase_augmented(struct rb_node *node, struct rb_root *root,
269 const struct rb_augment_callbacks *augment)
270{
9f973cb3 271 struct rb_node *rebalance = __rb_erase_augmented(node, root, augment);
9c079add
ML
272 if (rebalance)
273 __rb_erase_color(rebalance, root, augment->rotate);
274}
275
cd9e61ed
DB
276static __always_inline void
277rb_erase_augmented_cached(struct rb_node *node, struct rb_root_cached *root,
278 const struct rb_augment_callbacks *augment)
279{
9f973cb3
ML
280 if (root->rb_leftmost == node)
281 root->rb_leftmost = rb_next(node);
282 rb_erase_augmented(node, &root->rb_root, augment);
cd9e61ed
DB
283}
284
9c079add 285#endif /* _LINUX_RBTREE_AUGMENTED_H */