3 (C) 1999 Andrea Arcangeli <andrea@suse.de>
4 (C) 2002 David Woodhouse <dwmw2@infradead.org>
5 (C) 2012 Michel Lespinasse <walken@google.com>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 linux/include/linux/rbtree_augmented.h
24 #ifndef _LINUX_RBTREE_AUGMENTED_H
25 #define _LINUX_RBTREE_AUGMENTED_H
27 #include <linux/compiler.h>
28 #include <linux/rbtree.h>
31 * Please note - only struct rb_augment_callbacks and the prototypes for
32 * rb_insert_augmented() and rb_erase_augmented() are intended to be public.
33 * The rest are implementation details you are not expected to depend on.
35 * See Documentation/rbtree.txt for documentation and samples.
38 struct rb_augment_callbacks {
39 void (*propagate)(struct rb_node *node, struct rb_node *stop);
40 void (*copy)(struct rb_node *old, struct rb_node *new);
41 void (*rotate)(struct rb_node *old, struct rb_node *new);
44 extern void __rb_insert_augmented(struct rb_node *node,
46 bool newleft, struct rb_node **leftmost,
47 void (*augment_rotate)(struct rb_node *old, struct rb_node *new));
49 * Fixup the rbtree and update the augmented information when rebalancing.
51 * On insertion, the user must update the augmented information on the path
52 * leading to the inserted node, then call rb_link_node() as usual and
53 * rb_augment_inserted() instead of the usual rb_insert_color() call.
54 * If rb_augment_inserted() rebalances the rbtree, it will callback into
55 * a user provided function to update the augmented information on the
59 rb_insert_augmented(struct rb_node *node, struct rb_root *root,
60 const struct rb_augment_callbacks *augment)
62 __rb_insert_augmented(node, root, false, NULL, augment->rotate);
66 rb_insert_augmented_cached(struct rb_node *node,
67 struct rb_root_cached *root, bool newleft,
68 const struct rb_augment_callbacks *augment)
70 __rb_insert_augmented(node, &root->rb_root,
71 newleft, &root->rb_leftmost, augment->rotate);
74 #define RB_DECLARE_CALLBACKS(rbstatic, rbname, rbstruct, rbfield, \
75 rbtype, rbaugmented, rbcompute) \
77 rbname ## _propagate(struct rb_node *rb, struct rb_node *stop) \
79 while (rb != stop) { \
80 rbstruct *node = rb_entry(rb, rbstruct, rbfield); \
81 rbtype augmented = rbcompute(node); \
82 if (node->rbaugmented == augmented) \
84 node->rbaugmented = augmented; \
85 rb = rb_parent(&node->rbfield); \
89 rbname ## _copy(struct rb_node *rb_old, struct rb_node *rb_new) \
91 rbstruct *old = rb_entry(rb_old, rbstruct, rbfield); \
92 rbstruct *new = rb_entry(rb_new, rbstruct, rbfield); \
93 new->rbaugmented = old->rbaugmented; \
96 rbname ## _rotate(struct rb_node *rb_old, struct rb_node *rb_new) \
98 rbstruct *old = rb_entry(rb_old, rbstruct, rbfield); \
99 rbstruct *new = rb_entry(rb_new, rbstruct, rbfield); \
100 new->rbaugmented = old->rbaugmented; \
101 old->rbaugmented = rbcompute(old); \
103 rbstatic const struct rb_augment_callbacks rbname = { \
104 rbname ## _propagate, rbname ## _copy, rbname ## _rotate \
111 #define __rb_parent(pc) ((struct rb_node *)(pc & ~3))
113 #define __rb_color(pc) ((pc) & 1)
114 #define __rb_is_black(pc) __rb_color(pc)
115 #define __rb_is_red(pc) (!__rb_color(pc))
116 #define rb_color(rb) __rb_color((rb)->__rb_parent_color)
117 #define rb_is_red(rb) __rb_is_red((rb)->__rb_parent_color)
118 #define rb_is_black(rb) __rb_is_black((rb)->__rb_parent_color)
120 static inline void rb_set_parent(struct rb_node *rb, struct rb_node *p)
122 rb->__rb_parent_color = rb_color(rb) | (unsigned long)p;
125 static inline void rb_set_parent_color(struct rb_node *rb,
126 struct rb_node *p, int color)
128 rb->__rb_parent_color = (unsigned long)p | color;
132 __rb_change_child(struct rb_node *old, struct rb_node *new,
133 struct rb_node *parent, struct rb_root *root)
136 if (parent->rb_left == old)
137 WRITE_ONCE(parent->rb_left, new);
139 WRITE_ONCE(parent->rb_right, new);
141 WRITE_ONCE(root->rb_node, new);
145 __rb_change_child_rcu(struct rb_node *old, struct rb_node *new,
146 struct rb_node *parent, struct rb_root *root)
149 if (parent->rb_left == old)
150 rcu_assign_pointer(parent->rb_left, new);
152 rcu_assign_pointer(parent->rb_right, new);
154 rcu_assign_pointer(root->rb_node, new);
157 extern void __rb_erase_color(struct rb_node *parent, struct rb_root *root,
158 void (*augment_rotate)(struct rb_node *old, struct rb_node *new));
160 static __always_inline struct rb_node *
161 __rb_erase_augmented(struct rb_node *node, struct rb_root *root,
162 struct rb_node **leftmost,
163 const struct rb_augment_callbacks *augment)
165 struct rb_node *child = node->rb_right;
166 struct rb_node *tmp = node->rb_left;
167 struct rb_node *parent, *rebalance;
170 if (leftmost && node == *leftmost)
171 *leftmost = rb_next(node);
175 * Case 1: node to erase has no more than 1 child (easy!)
177 * Note that if there is one child it must be red due to 5)
178 * and node must be black due to 4). We adjust colors locally
179 * so as to bypass __rb_erase_color() later on.
181 pc = node->__rb_parent_color;
182 parent = __rb_parent(pc);
183 __rb_change_child(node, child, parent, root);
185 child->__rb_parent_color = pc;
188 rebalance = __rb_is_black(pc) ? parent : NULL;
191 /* Still case 1, but this time the child is node->rb_left */
192 tmp->__rb_parent_color = pc = node->__rb_parent_color;
193 parent = __rb_parent(pc);
194 __rb_change_child(node, tmp, parent, root);
198 struct rb_node *successor = child, *child2;
200 tmp = child->rb_left;
203 * Case 2: node's successor is its right child
212 child2 = successor->rb_right;
214 augment->copy(node, successor);
217 * Case 3: node's successor is leftmost under
218 * node's right child subtree
235 child2 = successor->rb_right;
236 WRITE_ONCE(parent->rb_left, child2);
237 WRITE_ONCE(successor->rb_right, child);
238 rb_set_parent(child, successor);
240 augment->copy(node, successor);
241 augment->propagate(parent, successor);
245 WRITE_ONCE(successor->rb_left, tmp);
246 rb_set_parent(tmp, successor);
248 pc = node->__rb_parent_color;
249 tmp = __rb_parent(pc);
250 __rb_change_child(node, successor, tmp, root);
253 successor->__rb_parent_color = pc;
254 rb_set_parent_color(child2, parent, RB_BLACK);
257 unsigned long pc2 = successor->__rb_parent_color;
258 successor->__rb_parent_color = pc;
259 rebalance = __rb_is_black(pc2) ? parent : NULL;
264 augment->propagate(tmp, NULL);
268 static __always_inline void
269 rb_erase_augmented(struct rb_node *node, struct rb_root *root,
270 const struct rb_augment_callbacks *augment)
272 struct rb_node *rebalance = __rb_erase_augmented(node, root,
275 __rb_erase_color(rebalance, root, augment->rotate);
278 static __always_inline void
279 rb_erase_augmented_cached(struct rb_node *node, struct rb_root_cached *root,
280 const struct rb_augment_callbacks *augment)
282 struct rb_node *rebalance = __rb_erase_augmented(node, &root->rb_root,
286 __rb_erase_color(rebalance, &root->rb_root, augment->rotate);
289 #endif /* _LINUX_RBTREE_AUGMENTED_H */