Make the syscall error detector a kernel header (XCC)
[akaros.git] / kern / include / rbtree_augmented.h
1 /*
2   Red Black Trees
3   (C) 1999  Andrea Arcangeli <andrea@suse.de>
4   (C) 2002  David Woodhouse <dwmw2@infradead.org>
5   (C) 2012  Michel Lespinasse <walken@google.com>
6
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.
11
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.
16
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
20
21   linux/include/linux/rbtree_augmented.h
22 */
23
24 #pragma once
25
26 #include <compiler.h>
27 #include <rbtree.h>
28
29 /*
30  * Please note - only struct rb_augment_callbacks and the prototypes for
31  * rb_insert_augmented() and rb_erase_augmented() are intended to be public.
32  * The rest are implementation details you are not expected to depend on.
33  *
34  * See Documentation/rbtree.txt for documentation and samples.
35  */
36
37 struct rb_augment_callbacks {
38         void (*propagate)(struct rb_node *node, struct rb_node *stop);
39         void (*copy)(struct rb_node *old, struct rb_node *new);
40         void (*rotate)(struct rb_node *old, struct rb_node *new);
41 };
42
43 extern void __rb_insert_augmented(struct rb_node *node, struct rb_root *root,
44         void (*augment_rotate)(struct rb_node *old, struct rb_node *new));
45 /*
46  * Fixup the rbtree and update the augmented information when rebalancing.
47  *
48  * On insertion, the user must update the augmented information on the path
49  * leading to the inserted node, then call rb_link_node() as usual and
50  * rb_augment_inserted() instead of the usual rb_insert_color() call.
51  * If rb_augment_inserted() rebalances the rbtree, it will callback into
52  * a user provided function to update the augmented information on the
53  * affected subtrees.
54  */
55 static inline void
56 rb_insert_augmented(struct rb_node *node, struct rb_root *root,
57                     const struct rb_augment_callbacks *augment)
58 {
59         __rb_insert_augmented(node, root, augment->rotate);
60 }
61
62 #define RB_DECLARE_CALLBACKS(rbstatic, rbname, rbstruct, rbfield,       \
63                              rbtype, rbaugmented, rbcompute)            \
64 static inline void                                                      \
65 rbname ## _propagate(struct rb_node *rb, struct rb_node *stop)          \
66 {                                                                       \
67         while (rb != stop) {                                            \
68                 rbstruct *node = rb_entry(rb, rbstruct, rbfield);       \
69                 rbtype augmented = rbcompute(node);                     \
70                 if (node->rbaugmented == augmented)                     \
71                         break;                                          \
72                 node->rbaugmented = augmented;                          \
73                 rb = rb_parent(&node->rbfield);                         \
74         }                                                               \
75 }                                                                       \
76 static inline void                                                      \
77 rbname ## _copy(struct rb_node *rb_old, struct rb_node *rb_new)         \
78 {                                                                       \
79         rbstruct *old = rb_entry(rb_old, rbstruct, rbfield);            \
80         rbstruct *new = rb_entry(rb_new, rbstruct, rbfield);            \
81         new->rbaugmented = old->rbaugmented;                            \
82 }                                                                       \
83 static void                                                             \
84 rbname ## _rotate(struct rb_node *rb_old, struct rb_node *rb_new)       \
85 {                                                                       \
86         rbstruct *old = rb_entry(rb_old, rbstruct, rbfield);            \
87         rbstruct *new = rb_entry(rb_new, rbstruct, rbfield);            \
88         new->rbaugmented = old->rbaugmented;                            \
89         old->rbaugmented = rbcompute(old);                              \
90 }                                                                       \
91 rbstatic const struct rb_augment_callbacks rbname = {                   \
92         rbname ## _propagate, rbname ## _copy, rbname ## _rotate        \
93 };
94
95
96 #define RB_RED          0
97 #define RB_BLACK        1
98
99 #define __rb_parent(pc)    ((struct rb_node *)(pc & ~3))
100
101 #define __rb_color(pc)     ((pc) & 1)
102 #define __rb_is_black(pc)  __rb_color(pc)
103 #define __rb_is_red(pc)    (!__rb_color(pc))
104 #define rb_color(rb)       __rb_color((rb)->__rb_parent_color)
105 #define rb_is_red(rb)      __rb_is_red((rb)->__rb_parent_color)
106 #define rb_is_black(rb)    __rb_is_black((rb)->__rb_parent_color)
107
108 static inline void rb_set_parent(struct rb_node *rb, struct rb_node *p)
109 {
110         rb->__rb_parent_color = rb_color(rb) | (unsigned long)p;
111 }
112
113 static inline void rb_set_parent_color(struct rb_node *rb,
114                                        struct rb_node *p, int color)
115 {
116         rb->__rb_parent_color = (unsigned long)p | color;
117 }
118
119 static inline void
120 __rb_change_child(struct rb_node *old, struct rb_node *new,
121                   struct rb_node *parent, struct rb_root *root)
122 {
123         if (parent) {
124                 if (parent->rb_left == old)
125                         WRITE_ONCE(parent->rb_left, new);
126                 else
127                         WRITE_ONCE(parent->rb_right, new);
128         } else
129                 WRITE_ONCE(root->rb_node, new);
130 }
131
132 static inline void
133 __rb_change_child_rcu(struct rb_node *old, struct rb_node *new,
134                       struct rb_node *parent, struct rb_root *root)
135 {
136         if (parent) {
137                 if (parent->rb_left == old)
138                         rcu_assign_pointer(parent->rb_left, new);
139                 else
140                         rcu_assign_pointer(parent->rb_right, new);
141         } else
142                 rcu_assign_pointer(root->rb_node, new);
143 }
144
145 extern void __rb_erase_color(struct rb_node *parent, struct rb_root *root,
146         void (*augment_rotate)(struct rb_node *old, struct rb_node *new));
147
148 static __always_inline struct rb_node *
149 __rb_erase_augmented(struct rb_node *node, struct rb_root *root,
150                      const struct rb_augment_callbacks *augment)
151 {
152         struct rb_node *child = node->rb_right;
153         struct rb_node *tmp = node->rb_left;
154         struct rb_node *parent, *rebalance;
155         unsigned long pc;
156
157         if (!tmp) {
158                 /*
159                  * Case 1: node to erase has no more than 1 child (easy!)
160                  *
161                  * Note that if there is one child it must be red due to 5)
162                  * and node must be black due to 4). We adjust colors locally
163                  * so as to bypass __rb_erase_color() later on.
164                  */
165                 pc = node->__rb_parent_color;
166                 parent = __rb_parent(pc);
167                 __rb_change_child(node, child, parent, root);
168                 if (child) {
169                         child->__rb_parent_color = pc;
170                         rebalance = NULL;
171                 } else
172                         rebalance = __rb_is_black(pc) ? parent : NULL;
173                 tmp = parent;
174         } else if (!child) {
175                 /* Still case 1, but this time the child is node->rb_left */
176                 tmp->__rb_parent_color = pc = node->__rb_parent_color;
177                 parent = __rb_parent(pc);
178                 __rb_change_child(node, tmp, parent, root);
179                 rebalance = NULL;
180                 tmp = parent;
181         } else {
182                 struct rb_node *successor = child, *child2;
183
184                 tmp = child->rb_left;
185                 if (!tmp) {
186                         /*
187                          * Case 2: node's successor is its right child
188                          *
189                          *    (n)          (s)
190                          *    / \          / \
191                          *  (x) (s)  ->  (x) (c)
192                          *        \
193                          *        (c)
194                          */
195                         parent = successor;
196                         child2 = successor->rb_right;
197
198                         augment->copy(node, successor);
199                 } else {
200                         /*
201                          * Case 3: node's successor is leftmost under
202                          * node's right child subtree
203                          *
204                          *    (n)          (s)
205                          *    / \          / \
206                          *  (x) (y)  ->  (x) (y)
207                          *      /            /
208                          *    (p)          (p)
209                          *    /            /
210                          *  (s)          (c)
211                          *    \
212                          *    (c)
213                          */
214                         do {
215                                 parent = successor;
216                                 successor = tmp;
217                                 tmp = tmp->rb_left;
218                         } while (tmp);
219                         child2 = successor->rb_right;
220                         WRITE_ONCE(parent->rb_left, child2);
221                         WRITE_ONCE(successor->rb_right, child);
222                         rb_set_parent(child, successor);
223
224                         augment->copy(node, successor);
225                         augment->propagate(parent, successor);
226                 }
227
228                 tmp = node->rb_left;
229                 WRITE_ONCE(successor->rb_left, tmp);
230                 rb_set_parent(tmp, successor);
231
232                 pc = node->__rb_parent_color;
233                 tmp = __rb_parent(pc);
234                 __rb_change_child(node, successor, tmp, root);
235
236                 if (child2) {
237                         successor->__rb_parent_color = pc;
238                         rb_set_parent_color(child2, parent, RB_BLACK);
239                         rebalance = NULL;
240                 } else {
241                         unsigned long pc2 = successor->__rb_parent_color;
242                         successor->__rb_parent_color = pc;
243                         rebalance = __rb_is_black(pc2) ? parent : NULL;
244                 }
245                 tmp = successor;
246         }
247
248         augment->propagate(tmp, NULL);
249         return rebalance;
250 }
251
252 static __always_inline void
253 rb_erase_augmented(struct rb_node *node, struct rb_root *root,
254                    const struct rb_augment_callbacks *augment)
255 {
256         struct rb_node *rebalance = __rb_erase_augmented(node, root, augment);
257         if (rebalance)
258                 __rb_erase_color(rebalance, root, augment->rotate);
259 }