XenevaOS
Loading...
Searching...
No Matches
rbtree_augmented.h
Go to the documentation of this file.
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
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
33extern 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 */
46static inline void
47rb_insert_augmented(struct rb_node *node, struct rb_root *root,
48 const struct rb_augment_callbacks *augment)
49{
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{
58 if (newleft)
59 root->rb_leftmost = node;
60 rb_insert_augmented(node, &root->rb_root, augment);
61}
62
63static __always_inline struct rb_node *
64rb_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 case)
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 * RBAUGMENTED: name of field within RBSTRUCT holding data for subtree
97 * RBCOMPUTE: name of function that recomputes the RBAUGMENTED data
98 */
99
100#define RB_DECLARE_CALLBACKS(RBSTATIC, RBNAME, \
101 RBSTRUCT, RBFIELD, RBAUGMENTED, RBCOMPUTE) \
102static inline void \
103RBNAME ## _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} \
112static inline void \
113RBNAME ## _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 new->RBAUGMENTED = old->RBAUGMENTED; \
118} \
119static void \
120RBNAME ## _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 new->RBAUGMENTED = old->RBAUGMENTED; \
125 RBCOMPUTE(old, false); \
126} \
127RBSTATIC 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,
135 * computing RBAUGMENTED scalar as max(RBCOMPUTE(node)) for all subtree nodes.
136 *
137 * RBSTATIC: 'static' or empty
138 * RBNAME: name of the rb_augment_callbacks structure
139 * RBSTRUCT: struct type of the tree nodes
140 * RBFIELD: name of struct rb_node field within RBSTRUCT
141 * RBTYPE: type of the RBAUGMENTED field
142 * RBAUGMENTED: name of RBTYPE field within RBSTRUCT holding data for subtree
143 * RBCOMPUTE: name of function that returns the per-node RBTYPE scalar
144 */
145
146#define RB_DECLARE_CALLBACKS_MAX(RBSTATIC, RBNAME, RBSTRUCT, RBFIELD, \
147 RBTYPE, RBAUGMENTED, RBCOMPUTE) \
148static inline bool RBNAME ## _compute_max(RBSTRUCT *node, bool exit) \
149{ \
150 RBSTRUCT *child; \
151 RBTYPE max = RBCOMPUTE(node); \
152 if (node->RBFIELD.rb_left) { \
153 child = rb_entry(node->RBFIELD.rb_left, RBSTRUCT, RBFIELD); \
154 if (child->RBAUGMENTED > max) \
155 max = child->RBAUGMENTED; \
156 } \
157 if (node->RBFIELD.rb_right) { \
158 child = rb_entry(node->RBFIELD.rb_right, RBSTRUCT, RBFIELD); \
159 if (child->RBAUGMENTED > max) \
160 max = child->RBAUGMENTED; \
161 } \
162 if (exit && node->RBAUGMENTED == max) \
163 return true; \
164 node->RBAUGMENTED = max; \
165 return false; \
166} \
167RB_DECLARE_CALLBACKS(RBSTATIC, RBNAME, \
168 RBSTRUCT, RBFIELD, RBAUGMENTED, RBNAME ## _compute_max)
169
170
171#define RB_RED 0
172#define RB_BLACK 1
173
174#define __rb_parent(pc) ((struct rb_node *)(pc & ~3))
175
176#define __rb_color(pc) ((pc) & 1)
177#define __rb_is_black(pc) __rb_color(pc)
178#define __rb_is_red(pc) (!__rb_color(pc))
179#define rb_color(rb) __rb_color((rb)->__rb_parent_color)
180#define rb_is_red(rb) __rb_is_red((rb)->__rb_parent_color)
181#define rb_is_black(rb) __rb_is_black((rb)->__rb_parent_color)
182
183/* DCL delta -- `uintptr_t`; see rbtree.h. */
184static inline void rb_set_parent(struct rb_node *rb, struct rb_node *p)
185{
186 rb->__rb_parent_color = rb_color(rb) + (uintptr_t)p;
187}
188
189/* DCL delta -- `uintptr_t`; see rbtree.h. */
190static inline void rb_set_parent_color(struct rb_node *rb,
191 struct rb_node *p, int color)
192{
194}
195
196static inline void
197__rb_change_child(struct rb_node *old, struct rb_node *new,
198 struct rb_node *parent, struct rb_root *root)
199{
200 if (parent) {
201 if (parent->rb_left == old)
202 WRITE_ONCE(parent->rb_left, new);
203 else
204 WRITE_ONCE(parent->rb_right, new);
205 } else
206 WRITE_ONCE(root->rb_node, new);
207}
208
209static inline void
210__rb_change_child_rcu(struct rb_node *old, struct rb_node *new,
211 struct rb_node *parent, struct rb_root *root)
212{
213 if (parent) {
214 if (parent->rb_left == old)
215 rcu_assign_pointer(parent->rb_left, new);
216 else
217 rcu_assign_pointer(parent->rb_right, new);
218 } else
219 rcu_assign_pointer(root->rb_node, new);
220}
221
222extern void __rb_erase_color(struct rb_node *parent, struct rb_root *root,
223 void (*augment_rotate)(struct rb_node *old, struct rb_node *new));
224
225static __always_inline struct rb_node *
226__rb_erase_augmented(struct rb_node *node, struct rb_root *root,
227 const struct rb_augment_callbacks *augment)
228{
229 struct rb_node *child = node->rb_right;
230 struct rb_node *tmp = node->rb_left;
231 struct rb_node *parent, *rebalance;
232 /*
233 * DCL delta -- `uintptr_t`; see rbtree.h. `pc` holds a whole
234 * __rb_parent_color (parent pointer plus color bit) and is written
235 * back into nodes below, so it cannot be a 32-bit `unsigned long`
236 * here.
237 */
238 uintptr_t pc;
239
240 if (!tmp) {
241 /*
242 * Case 1: node to erase has no more than 1 child (easy!)
243 *
244 * Note that if there is one child it must be red due to 5)
245 * and node must be black due to 4). We adjust colors locally
246 * so as to bypass __rb_erase_color() later on.
247 */
248 pc = node->__rb_parent_color;
249 parent = __rb_parent(pc);
250 __rb_change_child(node, child, parent, root);
251 if (child) {
252 child->__rb_parent_color = pc;
253 rebalance = NULL;
254 } else
255 rebalance = __rb_is_black(pc) ? parent : NULL;
256 tmp = parent;
257 } else if (!child) {
258 /* Still case 1, but this time the child is node->rb_left */
259 tmp->__rb_parent_color = pc = node->__rb_parent_color;
260 parent = __rb_parent(pc);
261 __rb_change_child(node, tmp, parent, root);
262 rebalance = NULL;
263 tmp = parent;
264 } else {
265 struct rb_node *successor = child, *child2;
266
267 tmp = child->rb_left;
268 if (!tmp) {
269 /*
270 * Case 2: node's successor is its right child
271 *
272 * (n) (s)
273 * / \ / \
274 * (x) (s) -> (x) (c)
275 * \
276 * (c)
277 */
278 parent = successor;
279 child2 = successor->rb_right;
280
281 augment->copy(node, successor);
282 } else {
283 /*
284 * Case 3: node's successor is leftmost under
285 * node's right child subtree
286 *
287 * (n) (s)
288 * / \ / \
289 * (x) (y) -> (x) (y)
290 * / /
291 * (p) (p)
292 * / /
293 * (s) (c)
294 * \
295 * (c)
296 */
297 do {
298 parent = successor;
299 successor = tmp;
300 tmp = tmp->rb_left;
301 } while (tmp);
302 child2 = successor->rb_right;
303 WRITE_ONCE(parent->rb_left, child2);
304 WRITE_ONCE(successor->rb_right, child);
305 rb_set_parent(child, successor);
306
307 augment->copy(node, successor);
308 augment->propagate(parent, successor);
309 }
310
311 tmp = node->rb_left;
312 WRITE_ONCE(successor->rb_left, tmp);
313 rb_set_parent(tmp, successor);
314
315 pc = node->__rb_parent_color;
316 tmp = __rb_parent(pc);
317 __rb_change_child(node, successor, tmp, root);
318
319 if (child2) {
320 rb_set_parent_color(child2, parent, RB_BLACK);
321 rebalance = NULL;
322 } else {
323 rebalance = rb_is_black(successor) ? parent : NULL;
324 }
325 successor->__rb_parent_color = pc;
326 tmp = successor;
327 }
328
329 augment->propagate(tmp, NULL);
330 return rebalance;
331}
332
333static __always_inline void
334rb_erase_augmented(struct rb_node *node, struct rb_root *root,
335 const struct rb_augment_callbacks *augment)
336{
337 struct rb_node *rebalance = __rb_erase_augmented(node, root, augment);
338 if (rebalance)
339 __rb_erase_color(rebalance, root, augment->rotate);
340}
341
342static __always_inline void
343rb_erase_augmented_cached(struct rb_node *node, struct rb_root_cached *root,
344 const struct rb_augment_callbacks *augment)
345{
346 if (root->rb_leftmost == node)
347 root->rb_leftmost = rb_next(node);
348 rb_erase_augmented(node, &root->rb_root, augment);
349}
350
351#endif /* _LINUX_RBTREE_AUGMENTED_H */
unsigned int uintptr_t
Definition stdint.h:94
#define NULL
Definition actypes.h:561
#define __always_inline
Definition compiler_attributes.h:55
struct kobject * parent
Definition kobject.h:91
void int node
Definition kthread.h:31
#define WRITE_ONCE(x, val)
Definition kernel.h:608
struct rb_node * rb_next(const struct rb_node *)
#define rb_color(rb)
Definition rbtree_augmented.h:179
#define __rb_is_black(pc)
Definition rbtree_augmented.h:177
#define __rb_parent(pc)
Definition rbtree_augmented.h:174
void __rb_erase_color(struct rb_node *parent, struct rb_root *root, void(*augment_rotate)(struct rb_node *old, struct rb_node *new))
#define RB_BLACK
Definition rbtree_augmented.h:172
void __rb_insert_augmented(struct rb_node *node, struct rb_root *root, void(*augment_rotate)(struct rb_node *old, struct rb_node *new))
#define rb_is_black(rb)
Definition rbtree_augmented.h:181
#define rcu_assign_pointer(p, v)
Definition rcupdate.h:51
Definition i_video.h:141
Definition rbtree_augmented.h:27
void(* rotate)(struct rb_node *old, struct rb_node *new)
Definition rbtree_augmented.h:30
void(* propagate)(struct rb_node *node, struct rb_node *stop)
Definition rbtree_augmented.h:28
void(* copy)(struct rb_node *old, struct rb_node *new)
Definition rbtree_augmented.h:29
Definition rbtree_types.h:21
struct rb_node * rb_left
Definition rbtree_types.h:24
uintptr_t __rb_parent_color
Definition rbtree_types.h:22
struct rb_node * rb_right
Definition rbtree_types.h:23
Definition rbtree_types.h:48
struct rb_root rb_root
Definition rbtree_types.h:49
Definition rbtree_types.h:34
struct rb_node * rb_node
Definition rbtree_types.h:35
TTY * root
Definition tty.cpp:47