1 /* SPDX-License-Identifier: GPL-2.0-or-later */
4 (C) 1999 Andrea Arcangeli <andrea@suse.de>
5 (C) 2002 David Woodhouse <dwmw2@infradead.org>
6 (C) 2012 Michel Lespinasse <walken@google.com>
9 tools/linux/include/linux/rbtree_augmented.h
12 linux/include/linux/rbtree_augmented.h
15 #ifndef _TOOLS_LINUX_RBTREE_AUGMENTED_H
16 #define _TOOLS_LINUX_RBTREE_AUGMENTED_H
18 #include <linux/compiler.h>
19 #include <linux/rbtree.h>
22 * Please note - only struct rb_augment_callbacks and the prototypes for
23 * rb_insert_augmented() and rb_erase_augmented() are intended to be public.
24 * The rest are implementation details you are not expected to depend on.
26 * See Documentation/rbtree.txt for documentation and samples.
29 struct rb_augment_callbacks {
30 void (*propagate)(struct rb_node *node, struct rb_node *stop);
31 void (*copy)(struct rb_node *old, struct rb_node *new);
32 void (*rotate)(struct rb_node *old, struct rb_node *new);
35 extern void __rb_insert_augmented(struct rb_node *node,
37 bool newleft, struct rb_node **leftmost,
38 void (*augment_rotate)(struct rb_node *old, struct rb_node *new));
40 * Fixup the rbtree and update the augmented information when rebalancing.
42 * On insertion, the user must update the augmented information on the path
43 * leading to the inserted node, then call rb_link_node() as usual and
44 * rb_augment_inserted() instead of the usual rb_insert_color() call.
45 * If rb_augment_inserted() rebalances the rbtree, it will callback into
46 * a user provided function to update the augmented information on the
50 rb_insert_augmented(struct rb_node *node, struct rb_root *root,
51 const struct rb_augment_callbacks *augment)
53 __rb_insert_augmented(node, root, false, NULL, augment->rotate);
57 rb_insert_augmented_cached(struct rb_node *node,
58 struct rb_root_cached *root, bool newleft,
59 const struct rb_augment_callbacks *augment)
61 __rb_insert_augmented(node, &root->rb_root,
62 newleft, &root->rb_leftmost, augment->rotate);
65 #define RB_DECLARE_CALLBACKS(rbstatic, rbname, rbstruct, rbfield, \
66 rbtype, rbaugmented, rbcompute) \
68 rbname ## _propagate(struct rb_node *rb, struct rb_node *stop) \
70 while (rb != stop) { \
71 rbstruct *node = rb_entry(rb, rbstruct, rbfield); \
72 rbtype augmented = rbcompute(node); \
73 if (node->rbaugmented == augmented) \
75 node->rbaugmented = augmented; \
76 rb = rb_parent(&node->rbfield); \
80 rbname ## _copy(struct rb_node *rb_old, struct rb_node *rb_new) \
82 rbstruct *old = rb_entry(rb_old, rbstruct, rbfield); \
83 rbstruct *new = rb_entry(rb_new, rbstruct, rbfield); \
84 new->rbaugmented = old->rbaugmented; \
87 rbname ## _rotate(struct rb_node *rb_old, struct rb_node *rb_new) \
89 rbstruct *old = rb_entry(rb_old, rbstruct, rbfield); \
90 rbstruct *new = rb_entry(rb_new, rbstruct, rbfield); \
91 new->rbaugmented = old->rbaugmented; \
92 old->rbaugmented = rbcompute(old); \
94 rbstatic const struct rb_augment_callbacks rbname = { \
95 .propagate = rbname ## _propagate, \
96 .copy = rbname ## _copy, \
97 .rotate = rbname ## _rotate \
104 #define __rb_parent(pc) ((struct rb_node *)(pc & ~3))
106 #define __rb_color(pc) ((pc) & 1)
107 #define __rb_is_black(pc) __rb_color(pc)
108 #define __rb_is_red(pc) (!__rb_color(pc))
109 #define rb_color(rb) __rb_color((rb)->__rb_parent_color)
110 #define rb_is_red(rb) __rb_is_red((rb)->__rb_parent_color)
111 #define rb_is_black(rb) __rb_is_black((rb)->__rb_parent_color)
113 static inline void rb_set_parent(struct rb_node *rb, struct rb_node *p)
115 rb->__rb_parent_color = rb_color(rb) | (unsigned long)p;
118 static inline void rb_set_parent_color(struct rb_node *rb,
119 struct rb_node *p, int color)
121 rb->__rb_parent_color = (unsigned long)p | color;
125 __rb_change_child(struct rb_node *old, struct rb_node *new,
126 struct rb_node *parent, struct rb_root *root)
129 if (parent->rb_left == old)
130 WRITE_ONCE(parent->rb_left, new);
132 WRITE_ONCE(parent->rb_right, new);
134 WRITE_ONCE(root->rb_node, new);
137 extern void __rb_erase_color(struct rb_node *parent, struct rb_root *root,
138 void (*augment_rotate)(struct rb_node *old, struct rb_node *new));
140 static __always_inline struct rb_node *
141 __rb_erase_augmented(struct rb_node *node, struct rb_root *root,
142 struct rb_node **leftmost,
143 const struct rb_augment_callbacks *augment)
145 struct rb_node *child = node->rb_right;
146 struct rb_node *tmp = node->rb_left;
147 struct rb_node *parent, *rebalance;
150 if (leftmost && node == *leftmost)
151 *leftmost = rb_next(node);
155 * Case 1: node to erase has no more than 1 child (easy!)
157 * Note that if there is one child it must be red due to 5)
158 * and node must be black due to 4). We adjust colors locally
159 * so as to bypass __rb_erase_color() later on.
161 pc = node->__rb_parent_color;
162 parent = __rb_parent(pc);
163 __rb_change_child(node, child, parent, root);
165 child->__rb_parent_color = pc;
168 rebalance = __rb_is_black(pc) ? parent : NULL;
171 /* Still case 1, but this time the child is node->rb_left */
172 tmp->__rb_parent_color = pc = node->__rb_parent_color;
173 parent = __rb_parent(pc);
174 __rb_change_child(node, tmp, parent, root);
178 struct rb_node *successor = child, *child2;
180 tmp = child->rb_left;
183 * Case 2: node's successor is its right child
192 child2 = successor->rb_right;
194 augment->copy(node, successor);
197 * Case 3: node's successor is leftmost under
198 * node's right child subtree
215 child2 = successor->rb_right;
216 WRITE_ONCE(parent->rb_left, child2);
217 WRITE_ONCE(successor->rb_right, child);
218 rb_set_parent(child, successor);
220 augment->copy(node, successor);
221 augment->propagate(parent, successor);
225 WRITE_ONCE(successor->rb_left, tmp);
226 rb_set_parent(tmp, successor);
228 pc = node->__rb_parent_color;
229 tmp = __rb_parent(pc);
230 __rb_change_child(node, successor, tmp, root);
233 successor->__rb_parent_color = pc;
234 rb_set_parent_color(child2, parent, RB_BLACK);
237 unsigned long pc2 = successor->__rb_parent_color;
238 successor->__rb_parent_color = pc;
239 rebalance = __rb_is_black(pc2) ? parent : NULL;
244 augment->propagate(tmp, NULL);
248 static __always_inline void
249 rb_erase_augmented(struct rb_node *node, struct rb_root *root,
250 const struct rb_augment_callbacks *augment)
252 struct rb_node *rebalance = __rb_erase_augmented(node, root,
255 __rb_erase_color(rebalance, root, augment->rotate);
258 static __always_inline void
259 rb_erase_augmented_cached(struct rb_node *node, struct rb_root_cached *root,
260 const struct rb_augment_callbacks *augment)
262 struct rb_node *rebalance = __rb_erase_augmented(node, &root->rb_root,
266 __rb_erase_color(rebalance, &root->rb_root, augment->rotate);
269 #endif /* _TOOLS_LINUX_RBTREE_AUGMENTED_H */