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, struct rb_root *root,
36 void (*augment_rotate)(struct rb_node *old, struct rb_node *new));
39 * Fixup the rbtree and update the augmented information when rebalancing.
41 * On insertion, the user must update the augmented information on the path
42 * leading to the inserted node, then call rb_link_node() as usual and
43 * rb_insert_augmented() instead of the usual rb_insert_color() call.
44 * If rb_insert_augmented() rebalances the rbtree, it will callback into
45 * a user provided function to update the augmented information on the
49 rb_insert_augmented(struct rb_node *node, struct rb_root *root,
50 const struct rb_augment_callbacks *augment)
52 __rb_insert_augmented(node, root, augment->rotate);
56 rb_insert_augmented_cached(struct rb_node *node,
57 struct rb_root_cached *root, bool newleft,
58 const struct rb_augment_callbacks *augment)
61 root->rb_leftmost = node;
62 rb_insert_augmented(node, &root->rb_root, augment);
66 * Template for declaring augmented rbtree callbacks
68 * RBSTATIC: 'static' or empty
69 * RBNAME: name of the rb_augment_callbacks structure
70 * RBSTRUCT: struct type of the tree nodes
71 * RBFIELD: name of struct rb_node field within RBSTRUCT
72 * RBTYPE: type of the RBAUGMENTED field
73 * RBAUGMENTED: name of RBTYPE field within RBSTRUCT holding data for subtree
74 * RBCOMPUTE: name of function that recomputes the RBAUGMENTED data
77 #define RB_DECLARE_CALLBACKS(RBSTATIC, RBNAME, RBSTRUCT, RBFIELD, \
78 RBTYPE, RBAUGMENTED, RBCOMPUTE) \
80 RBNAME ## _propagate(struct rb_node *rb, struct rb_node *stop) \
82 while (rb != stop) { \
83 RBSTRUCT *node = rb_entry(rb, RBSTRUCT, RBFIELD); \
84 RBTYPE augmented = RBCOMPUTE(node); \
85 if (node->RBAUGMENTED == augmented) \
87 node->RBAUGMENTED = augmented; \
88 rb = rb_parent(&node->RBFIELD); \
92 RBNAME ## _copy(struct rb_node *rb_old, struct rb_node *rb_new) \
94 RBSTRUCT *old = rb_entry(rb_old, RBSTRUCT, RBFIELD); \
95 RBSTRUCT *new = rb_entry(rb_new, RBSTRUCT, RBFIELD); \
96 new->RBAUGMENTED = old->RBAUGMENTED; \
99 RBNAME ## _rotate(struct rb_node *rb_old, struct rb_node *rb_new) \
101 RBSTRUCT *old = rb_entry(rb_old, RBSTRUCT, RBFIELD); \
102 RBSTRUCT *new = rb_entry(rb_new, RBSTRUCT, RBFIELD); \
103 new->RBAUGMENTED = old->RBAUGMENTED; \
104 old->RBAUGMENTED = RBCOMPUTE(old); \
106 RBSTATIC const struct rb_augment_callbacks RBNAME = { \
107 .propagate = RBNAME ## _propagate, \
108 .copy = RBNAME ## _copy, \
109 .rotate = RBNAME ## _rotate \
116 #define __rb_parent(pc) ((struct rb_node *)(pc & ~3))
118 #define __rb_color(pc) ((pc) & 1)
119 #define __rb_is_black(pc) __rb_color(pc)
120 #define __rb_is_red(pc) (!__rb_color(pc))
121 #define rb_color(rb) __rb_color((rb)->__rb_parent_color)
122 #define rb_is_red(rb) __rb_is_red((rb)->__rb_parent_color)
123 #define rb_is_black(rb) __rb_is_black((rb)->__rb_parent_color)
125 static inline void rb_set_parent(struct rb_node *rb, struct rb_node *p)
127 rb->__rb_parent_color = rb_color(rb) | (unsigned long)p;
130 static inline void rb_set_parent_color(struct rb_node *rb,
131 struct rb_node *p, int color)
133 rb->__rb_parent_color = (unsigned long)p | color;
137 __rb_change_child(struct rb_node *old, struct rb_node *new,
138 struct rb_node *parent, struct rb_root *root)
141 if (parent->rb_left == old)
142 WRITE_ONCE(parent->rb_left, new);
144 WRITE_ONCE(parent->rb_right, new);
146 WRITE_ONCE(root->rb_node, new);
149 extern void __rb_erase_color(struct rb_node *parent, struct rb_root *root,
150 void (*augment_rotate)(struct rb_node *old, struct rb_node *new));
152 static __always_inline struct rb_node *
153 __rb_erase_augmented(struct rb_node *node, struct rb_root *root,
154 const struct rb_augment_callbacks *augment)
156 struct rb_node *child = node->rb_right;
157 struct rb_node *tmp = node->rb_left;
158 struct rb_node *parent, *rebalance;
163 * Case 1: node to erase has no more than 1 child (easy!)
165 * Note that if there is one child it must be red due to 5)
166 * and node must be black due to 4). We adjust colors locally
167 * so as to bypass __rb_erase_color() later on.
169 pc = node->__rb_parent_color;
170 parent = __rb_parent(pc);
171 __rb_change_child(node, child, parent, root);
173 child->__rb_parent_color = pc;
176 rebalance = __rb_is_black(pc) ? parent : NULL;
179 /* Still case 1, but this time the child is node->rb_left */
180 tmp->__rb_parent_color = pc = node->__rb_parent_color;
181 parent = __rb_parent(pc);
182 __rb_change_child(node, tmp, parent, root);
186 struct rb_node *successor = child, *child2;
188 tmp = child->rb_left;
191 * Case 2: node's successor is its right child
200 child2 = successor->rb_right;
202 augment->copy(node, successor);
205 * Case 3: node's successor is leftmost under
206 * node's right child subtree
223 child2 = successor->rb_right;
224 WRITE_ONCE(parent->rb_left, child2);
225 WRITE_ONCE(successor->rb_right, child);
226 rb_set_parent(child, successor);
228 augment->copy(node, successor);
229 augment->propagate(parent, successor);
233 WRITE_ONCE(successor->rb_left, tmp);
234 rb_set_parent(tmp, successor);
236 pc = node->__rb_parent_color;
237 tmp = __rb_parent(pc);
238 __rb_change_child(node, successor, tmp, root);
241 successor->__rb_parent_color = pc;
242 rb_set_parent_color(child2, parent, RB_BLACK);
245 unsigned long pc2 = successor->__rb_parent_color;
246 successor->__rb_parent_color = pc;
247 rebalance = __rb_is_black(pc2) ? parent : NULL;
252 augment->propagate(tmp, NULL);
256 static __always_inline void
257 rb_erase_augmented(struct rb_node *node, struct rb_root *root,
258 const struct rb_augment_callbacks *augment)
260 struct rb_node *rebalance = __rb_erase_augmented(node, root, augment);
262 __rb_erase_color(rebalance, root, augment->rotate);
265 static __always_inline void
266 rb_erase_augmented_cached(struct rb_node *node, struct rb_root_cached *root,
267 const struct rb_augment_callbacks *augment)
269 if (root->rb_leftmost == node)
270 root->rb_leftmost = rb_next(node);
271 rb_erase_augmented(node, &root->rb_root, augment);
274 #endif /* _TOOLS_LINUX_RBTREE_AUGMENTED_H */