blob: 0f902ccb48b09c341dbc8160d7c967a03aba22fb [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
Michel Lespinasse9c079ad2012-10-08 16:31:33 -07002/*
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
Michel Lespinasse9c079ad2012-10-08 16:31:33 -07008
9 linux/include/linux/rbtree_augmented.h
10*/
11
12#ifndef _LINUX_RBTREE_AUGMENTED_H
13#define _LINUX_RBTREE_AUGMENTED_H
14
Will Deacon29fc7c52012-10-25 13:37:53 -070015#include <linux/compiler.h>
Michel Lespinasse9c079ad2012-10-08 16:31:33 -070016#include <linux/rbtree.h>
Sebastian Andrzej Siewior2075b162018-05-11 16:02:14 -070017#include <linux/rcupdate.h>
Michel Lespinasse9c079ad2012-10-08 16:31:33 -070018
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
Davidlohr Buesocd9e61e2017-09-08 16:14:36 -070033extern void __rb_insert_augmented(struct rb_node *node,
34 struct rb_root *root,
35 bool newleft, struct rb_node **leftmost,
Michel Lespinasse9c079ad2012-10-08 16:31:33 -070036 void (*augment_rotate)(struct rb_node *old, struct rb_node *new));
Lai Jiangshana841b652014-10-13 15:53:48 -070037/*
38 * Fixup the rbtree and update the augmented information when rebalancing.
39 *
40 * On insertion, the user must update the augmented information on the path
41 * leading to the inserted node, then call rb_link_node() as usual and
Wei Yang7e5ca362018-10-30 15:05:42 -070042 * rb_insert_augmented() instead of the usual rb_insert_color() call.
43 * If rb_insert_augmented() rebalances the rbtree, it will callback into
Lai Jiangshana841b652014-10-13 15:53:48 -070044 * a user provided function to update the augmented information on the
45 * affected subtrees.
46 */
Michel Lespinasse9c079ad2012-10-08 16:31:33 -070047static inline void
48rb_insert_augmented(struct rb_node *node, struct rb_root *root,
49 const struct rb_augment_callbacks *augment)
50{
Davidlohr Buesocd9e61e2017-09-08 16:14:36 -070051 __rb_insert_augmented(node, root, false, NULL, augment->rotate);
52}
53
54static inline void
55rb_insert_augmented_cached(struct rb_node *node,
56 struct rb_root_cached *root, bool newleft,
57 const struct rb_augment_callbacks *augment)
58{
59 __rb_insert_augmented(node, &root->rb_root,
60 newleft, &root->rb_leftmost, augment->rotate);
Michel Lespinasse9c079ad2012-10-08 16:31:33 -070061}
62
63#define RB_DECLARE_CALLBACKS(rbstatic, rbname, rbstruct, rbfield, \
64 rbtype, rbaugmented, rbcompute) \
65static inline void \
66rbname ## _propagate(struct rb_node *rb, struct rb_node *stop) \
67{ \
68 while (rb != stop) { \
69 rbstruct *node = rb_entry(rb, rbstruct, rbfield); \
70 rbtype augmented = rbcompute(node); \
71 if (node->rbaugmented == augmented) \
72 break; \
73 node->rbaugmented = augmented; \
74 rb = rb_parent(&node->rbfield); \
75 } \
76} \
77static inline void \
78rbname ## _copy(struct rb_node *rb_old, struct rb_node *rb_new) \
79{ \
80 rbstruct *old = rb_entry(rb_old, rbstruct, rbfield); \
81 rbstruct *new = rb_entry(rb_new, rbstruct, rbfield); \
82 new->rbaugmented = old->rbaugmented; \
83} \
84static void \
85rbname ## _rotate(struct rb_node *rb_old, struct rb_node *rb_new) \
86{ \
87 rbstruct *old = rb_entry(rb_old, rbstruct, rbfield); \
88 rbstruct *new = rb_entry(rb_new, rbstruct, rbfield); \
89 new->rbaugmented = old->rbaugmented; \
90 old->rbaugmented = rbcompute(old); \
91} \
92rbstatic const struct rb_augment_callbacks rbname = { \
Kees Cookf231aeb2017-02-24 15:01:04 -080093 .propagate = rbname ## _propagate, \
94 .copy = rbname ## _copy, \
95 .rotate = rbname ## _rotate \
Michel Lespinasse9c079ad2012-10-08 16:31:33 -070096};
97
98
99#define RB_RED 0
100#define RB_BLACK 1
101
102#define __rb_parent(pc) ((struct rb_node *)(pc & ~3))
103
104#define __rb_color(pc) ((pc) & 1)
105#define __rb_is_black(pc) __rb_color(pc)
106#define __rb_is_red(pc) (!__rb_color(pc))
107#define rb_color(rb) __rb_color((rb)->__rb_parent_color)
108#define rb_is_red(rb) __rb_is_red((rb)->__rb_parent_color)
109#define rb_is_black(rb) __rb_is_black((rb)->__rb_parent_color)
110
111static inline void rb_set_parent(struct rb_node *rb, struct rb_node *p)
112{
113 rb->__rb_parent_color = rb_color(rb) | (unsigned long)p;
114}
115
116static inline void rb_set_parent_color(struct rb_node *rb,
117 struct rb_node *p, int color)
118{
119 rb->__rb_parent_color = (unsigned long)p | color;
120}
121
122static inline void
123__rb_change_child(struct rb_node *old, struct rb_node *new,
124 struct rb_node *parent, struct rb_root *root)
125{
126 if (parent) {
127 if (parent->rb_left == old)
Peter Zijlstrad72da4a2015-05-27 11:09:36 +0930128 WRITE_ONCE(parent->rb_left, new);
Michel Lespinasse9c079ad2012-10-08 16:31:33 -0700129 else
Peter Zijlstrad72da4a2015-05-27 11:09:36 +0930130 WRITE_ONCE(parent->rb_right, new);
Michel Lespinasse9c079ad2012-10-08 16:31:33 -0700131 } else
Peter Zijlstrad72da4a2015-05-27 11:09:36 +0930132 WRITE_ONCE(root->rb_node, new);
Michel Lespinasse9c079ad2012-10-08 16:31:33 -0700133}
134
David Howellsc1adf202016-07-01 07:53:51 +0100135static inline void
136__rb_change_child_rcu(struct rb_node *old, struct rb_node *new,
137 struct rb_node *parent, struct rb_root *root)
138{
139 if (parent) {
140 if (parent->rb_left == old)
141 rcu_assign_pointer(parent->rb_left, new);
142 else
143 rcu_assign_pointer(parent->rb_right, new);
144 } else
145 rcu_assign_pointer(root->rb_node, new);
146}
147
Michel Lespinasse9c079ad2012-10-08 16:31:33 -0700148extern void __rb_erase_color(struct rb_node *parent, struct rb_root *root,
149 void (*augment_rotate)(struct rb_node *old, struct rb_node *new));
150
Michel Lespinasse3cb7a562013-01-11 14:32:20 -0800151static __always_inline struct rb_node *
152__rb_erase_augmented(struct rb_node *node, struct rb_root *root,
Davidlohr Buesocd9e61e2017-09-08 16:14:36 -0700153 struct rb_node **leftmost,
Michel Lespinasse3cb7a562013-01-11 14:32:20 -0800154 const struct rb_augment_callbacks *augment)
Michel Lespinasse9c079ad2012-10-08 16:31:33 -0700155{
Peter Zijlstrad72da4a2015-05-27 11:09:36 +0930156 struct rb_node *child = node->rb_right;
157 struct rb_node *tmp = node->rb_left;
Michel Lespinasse9c079ad2012-10-08 16:31:33 -0700158 struct rb_node *parent, *rebalance;
159 unsigned long pc;
160
Davidlohr Buesocd9e61e2017-09-08 16:14:36 -0700161 if (leftmost && node == *leftmost)
162 *leftmost = rb_next(node);
163
Michel Lespinasse9c079ad2012-10-08 16:31:33 -0700164 if (!tmp) {
165 /*
166 * Case 1: node to erase has no more than 1 child (easy!)
167 *
168 * Note that if there is one child it must be red due to 5)
169 * and node must be black due to 4). We adjust colors locally
170 * so as to bypass __rb_erase_color() later on.
171 */
172 pc = node->__rb_parent_color;
173 parent = __rb_parent(pc);
174 __rb_change_child(node, child, parent, root);
175 if (child) {
176 child->__rb_parent_color = pc;
177 rebalance = NULL;
178 } else
179 rebalance = __rb_is_black(pc) ? parent : NULL;
180 tmp = parent;
181 } else if (!child) {
182 /* Still case 1, but this time the child is node->rb_left */
183 tmp->__rb_parent_color = pc = node->__rb_parent_color;
184 parent = __rb_parent(pc);
185 __rb_change_child(node, tmp, parent, root);
186 rebalance = NULL;
187 tmp = parent;
188 } else {
189 struct rb_node *successor = child, *child2;
Peter Zijlstrad72da4a2015-05-27 11:09:36 +0930190
Michel Lespinasse9c079ad2012-10-08 16:31:33 -0700191 tmp = child->rb_left;
192 if (!tmp) {
193 /*
194 * Case 2: node's successor is its right child
195 *
196 * (n) (s)
197 * / \ / \
198 * (x) (s) -> (x) (c)
199 * \
200 * (c)
201 */
202 parent = successor;
203 child2 = successor->rb_right;
Peter Zijlstrad72da4a2015-05-27 11:09:36 +0930204
Michel Lespinasse9c079ad2012-10-08 16:31:33 -0700205 augment->copy(node, successor);
206 } else {
207 /*
208 * Case 3: node's successor is leftmost under
209 * node's right child subtree
210 *
211 * (n) (s)
212 * / \ / \
213 * (x) (y) -> (x) (y)
214 * / /
215 * (p) (p)
216 * / /
217 * (s) (c)
218 * \
219 * (c)
220 */
221 do {
222 parent = successor;
223 successor = tmp;
224 tmp = tmp->rb_left;
225 } while (tmp);
Peter Zijlstrad72da4a2015-05-27 11:09:36 +0930226 child2 = successor->rb_right;
227 WRITE_ONCE(parent->rb_left, child2);
228 WRITE_ONCE(successor->rb_right, child);
Michel Lespinasse9c079ad2012-10-08 16:31:33 -0700229 rb_set_parent(child, successor);
Peter Zijlstrad72da4a2015-05-27 11:09:36 +0930230
Michel Lespinasse9c079ad2012-10-08 16:31:33 -0700231 augment->copy(node, successor);
232 augment->propagate(parent, successor);
233 }
234
Peter Zijlstrad72da4a2015-05-27 11:09:36 +0930235 tmp = node->rb_left;
236 WRITE_ONCE(successor->rb_left, tmp);
Michel Lespinasse9c079ad2012-10-08 16:31:33 -0700237 rb_set_parent(tmp, successor);
238
239 pc = node->__rb_parent_color;
240 tmp = __rb_parent(pc);
241 __rb_change_child(node, successor, tmp, root);
Peter Zijlstrad72da4a2015-05-27 11:09:36 +0930242
Michel Lespinasse9c079ad2012-10-08 16:31:33 -0700243 if (child2) {
244 successor->__rb_parent_color = pc;
245 rb_set_parent_color(child2, parent, RB_BLACK);
246 rebalance = NULL;
247 } else {
248 unsigned long pc2 = successor->__rb_parent_color;
249 successor->__rb_parent_color = pc;
250 rebalance = __rb_is_black(pc2) ? parent : NULL;
251 }
252 tmp = successor;
253 }
254
255 augment->propagate(tmp, NULL);
Michel Lespinasse3cb7a562013-01-11 14:32:20 -0800256 return rebalance;
257}
258
259static __always_inline void
260rb_erase_augmented(struct rb_node *node, struct rb_root *root,
261 const struct rb_augment_callbacks *augment)
262{
Davidlohr Buesocd9e61e2017-09-08 16:14:36 -0700263 struct rb_node *rebalance = __rb_erase_augmented(node, root,
264 NULL, augment);
Michel Lespinasse9c079ad2012-10-08 16:31:33 -0700265 if (rebalance)
266 __rb_erase_color(rebalance, root, augment->rotate);
267}
268
Davidlohr Buesocd9e61e2017-09-08 16:14:36 -0700269static __always_inline void
270rb_erase_augmented_cached(struct rb_node *node, struct rb_root_cached *root,
271 const struct rb_augment_callbacks *augment)
272{
273 struct rb_node *rebalance = __rb_erase_augmented(node, &root->rb_root,
274 &root->rb_leftmost,
275 augment);
276 if (rebalance)
277 __rb_erase_color(rebalance, &root->rb_root, augment->rotate);
278}
279
Michel Lespinasse9c079ad2012-10-08 16:31:33 -0700280#endif /* _LINUX_RBTREE_AUGMENTED_H */