augmented rbtree: add comments for RB_DECLARE_CALLBACKS macro
[linux-2.6-block.git] / tools / include / linux / rbtree_augmented.h
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   tools/linux/include/linux/rbtree_augmented.h
10
11   Copied from:
12   linux/include/linux/rbtree_augmented.h
13 */
14
15 #ifndef _TOOLS_LINUX_RBTREE_AUGMENTED_H
16 #define _TOOLS_LINUX_RBTREE_AUGMENTED_H
17
18 #include <linux/compiler.h>
19 #include <linux/rbtree.h>
20
21 /*
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.
25  *
26  * See Documentation/rbtree.txt for documentation and samples.
27  */
28
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);
33 };
34
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));
37
38 /*
39  * Fixup the rbtree and update the augmented information when rebalancing.
40  *
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
46  * affected subtrees.
47  */
48 static inline void
49 rb_insert_augmented(struct rb_node *node, struct rb_root *root,
50                     const struct rb_augment_callbacks *augment)
51 {
52         __rb_insert_augmented(node, root, augment->rotate);
53 }
54
55 static inline void
56 rb_insert_augmented_cached(struct rb_node *node,
57                            struct rb_root_cached *root, bool newleft,
58                            const struct rb_augment_callbacks *augment)
59 {
60         if (newleft)
61                 root->rb_leftmost = node;
62         rb_insert_augmented(node, &root->rb_root, augment);
63 }
64
65 /*
66  * Template for declaring augmented rbtree callbacks
67  *
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
75  */
76
77 #define RB_DECLARE_CALLBACKS(RBSTATIC, RBNAME, RBSTRUCT, RBFIELD,       \
78                              RBTYPE, RBAUGMENTED, RBCOMPUTE)            \
79 static inline void                                                      \
80 RBNAME ## _propagate(struct rb_node *rb, struct rb_node *stop)          \
81 {                                                                       \
82         while (rb != stop) {                                            \
83                 RBSTRUCT *node = rb_entry(rb, RBSTRUCT, RBFIELD);       \
84                 RBTYPE augmented = RBCOMPUTE(node);                     \
85                 if (node->RBAUGMENTED == augmented)                     \
86                         break;                                          \
87                 node->RBAUGMENTED = augmented;                          \
88                 rb = rb_parent(&node->RBFIELD);                         \
89         }                                                               \
90 }                                                                       \
91 static inline void                                                      \
92 RBNAME ## _copy(struct rb_node *rb_old, struct rb_node *rb_new)         \
93 {                                                                       \
94         RBSTRUCT *old = rb_entry(rb_old, RBSTRUCT, RBFIELD);            \
95         RBSTRUCT *new = rb_entry(rb_new, RBSTRUCT, RBFIELD);            \
96         new->RBAUGMENTED = old->RBAUGMENTED;                            \
97 }                                                                       \
98 static void                                                             \
99 RBNAME ## _rotate(struct rb_node *rb_old, struct rb_node *rb_new)       \
100 {                                                                       \
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);                              \
105 }                                                                       \
106 RBSTATIC const struct rb_augment_callbacks RBNAME = {                   \
107         .propagate = RBNAME ## _propagate,                              \
108         .copy = RBNAME ## _copy,                                        \
109         .rotate = RBNAME ## _rotate                                     \
110 };
111
112
113 #define RB_RED          0
114 #define RB_BLACK        1
115
116 #define __rb_parent(pc)    ((struct rb_node *)(pc & ~3))
117
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)
124
125 static inline void rb_set_parent(struct rb_node *rb, struct rb_node *p)
126 {
127         rb->__rb_parent_color = rb_color(rb) | (unsigned long)p;
128 }
129
130 static inline void rb_set_parent_color(struct rb_node *rb,
131                                        struct rb_node *p, int color)
132 {
133         rb->__rb_parent_color = (unsigned long)p | color;
134 }
135
136 static inline void
137 __rb_change_child(struct rb_node *old, struct rb_node *new,
138                   struct rb_node *parent, struct rb_root *root)
139 {
140         if (parent) {
141                 if (parent->rb_left == old)
142                         WRITE_ONCE(parent->rb_left, new);
143                 else
144                         WRITE_ONCE(parent->rb_right, new);
145         } else
146                 WRITE_ONCE(root->rb_node, new);
147 }
148
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));
151
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)
155 {
156         struct rb_node *child = node->rb_right;
157         struct rb_node *tmp = node->rb_left;
158         struct rb_node *parent, *rebalance;
159         unsigned long pc;
160
161         if (!tmp) {
162                 /*
163                  * Case 1: node to erase has no more than 1 child (easy!)
164                  *
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.
168                  */
169                 pc = node->__rb_parent_color;
170                 parent = __rb_parent(pc);
171                 __rb_change_child(node, child, parent, root);
172                 if (child) {
173                         child->__rb_parent_color = pc;
174                         rebalance = NULL;
175                 } else
176                         rebalance = __rb_is_black(pc) ? parent : NULL;
177                 tmp = parent;
178         } else if (!child) {
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);
183                 rebalance = NULL;
184                 tmp = parent;
185         } else {
186                 struct rb_node *successor = child, *child2;
187
188                 tmp = child->rb_left;
189                 if (!tmp) {
190                         /*
191                          * Case 2: node's successor is its right child
192                          *
193                          *    (n)          (s)
194                          *    / \          / \
195                          *  (x) (s)  ->  (x) (c)
196                          *        \
197                          *        (c)
198                          */
199                         parent = successor;
200                         child2 = successor->rb_right;
201
202                         augment->copy(node, successor);
203                 } else {
204                         /*
205                          * Case 3: node's successor is leftmost under
206                          * node's right child subtree
207                          *
208                          *    (n)          (s)
209                          *    / \          / \
210                          *  (x) (y)  ->  (x) (y)
211                          *      /            /
212                          *    (p)          (p)
213                          *    /            /
214                          *  (s)          (c)
215                          *    \
216                          *    (c)
217                          */
218                         do {
219                                 parent = successor;
220                                 successor = tmp;
221                                 tmp = tmp->rb_left;
222                         } while (tmp);
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);
227
228                         augment->copy(node, successor);
229                         augment->propagate(parent, successor);
230                 }
231
232                 tmp = node->rb_left;
233                 WRITE_ONCE(successor->rb_left, tmp);
234                 rb_set_parent(tmp, successor);
235
236                 pc = node->__rb_parent_color;
237                 tmp = __rb_parent(pc);
238                 __rb_change_child(node, successor, tmp, root);
239
240                 if (child2) {
241                         successor->__rb_parent_color = pc;
242                         rb_set_parent_color(child2, parent, RB_BLACK);
243                         rebalance = NULL;
244                 } else {
245                         unsigned long pc2 = successor->__rb_parent_color;
246                         successor->__rb_parent_color = pc;
247                         rebalance = __rb_is_black(pc2) ? parent : NULL;
248                 }
249                 tmp = successor;
250         }
251
252         augment->propagate(tmp, NULL);
253         return rebalance;
254 }
255
256 static __always_inline void
257 rb_erase_augmented(struct rb_node *node, struct rb_root *root,
258                    const struct rb_augment_callbacks *augment)
259 {
260         struct rb_node *rebalance = __rb_erase_augmented(node, root, augment);
261         if (rebalance)
262                 __rb_erase_color(rebalance, root, augment->rotate);
263 }
264
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)
268 {
269         if (root->rb_leftmost == node)
270                 root->rb_leftmost = rb_next(node);
271         rb_erase_augmented(node, &root->rb_root, augment);
272 }
273
274 #endif /* _TOOLS_LINUX_RBTREE_AUGMENTED_H */