source: GPL/branches/uniaud32-next/lib32/rbtree.c@ 715

Last change on this file since 715 was 651, checked in by Paul Smedley, 5 years ago

Update rbtree to linux 5.10.10 code

File size: 17.1 KB
Line 
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 linux/lib/rbtree.c
10*/
11/* from 5.10.10 */
12
13#include <linux/rbtree_augmented.h>
14#include <linux/export.h>
15#include <linux/module.h>
16#include <linux/printk.h>
17
18/*
19 * red-black trees properties: https://en.wikipedia.org/wiki/Rbtree
20 *
21 * 1) A node is either red or black
22 * 2) The root is black
23 * 3) All leaves (NULL) are black
24 * 4) Both children of every red node are black
25 * 5) Every simple path from root to leaves contains the same number
26 * of black nodes.
27 *
28 * 4 and 5 give the O(log n) guarantee, since 4 implies you cannot have two
29 * consecutive red nodes in a path and every red node is therefore followed by
30 * a black. So if B is the number of black nodes on every simple path (as per
31 * 5), then the longest possible path due to 4 is 2B.
32 *
33 * We shall indicate color with case, where black nodes are uppercase and red
34 * nodes will be lowercase. Unknown color nodes shall be drawn as red within
35 * parentheses and have some accompanying text comment.
36 */
37
38/*
39 * Notes on lockless lookups:
40 *
41 * All stores to the tree structure (rb_left and rb_right) must be done using
42 * WRITE_ONCE(). And we must not inadvertently cause (temporary) loops in the
43 * tree structure as seen in program order.
44 *
45 * These two requirements will allow lockless iteration of the tree -- not
46 * correct iteration mind you, tree rotations are not atomic so a lookup might
47 * miss entire subtrees.
48 *
49 * But they do guarantee that any such traversal will only see valid elements
50 * and that it will indeed complete -- does not get stuck in a loop.
51 *
52 * It also guarantees that if the lookup returns an element it is the 'correct'
53 * one. But not returning an element does _NOT_ mean it's not present.
54 *
55 * NOTE:
56 *
57 * Stores to __rb_parent_color are not important for simple lookups so those
58 * are left undone as of now. Nor did I check for loops involving parent
59 * pointers.
60 */
61
62static inline void rb_set_black(struct rb_node *rb)
63{
64 rb->__rb_parent_color |= RB_BLACK;
65}
66
67static inline struct rb_node *rb_red_parent(struct rb_node *red)
68{
69 return (struct rb_node *)red->__rb_parent_color;
70}
71
72/*
73 * Helper function for rotations:
74 * - old's parent and color get assigned to new
75 * - old gets assigned new as a parent and 'color' as a color.
76 */
77static inline void
78__rb_rotate_set_parents(struct rb_node *old, struct rb_node *new,
79 struct rb_root *root, int color)
80{
81 struct rb_node *parent = rb_parent(old);
82 new->__rb_parent_color = old->__rb_parent_color;
83 rb_set_parent_color(old, new, color);
84 __rb_change_child(old, new, parent, root);
85}
86
87static inline void
88__rb_insert(struct rb_node *node, struct rb_root *root,
89 void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
90{
91 struct rb_node *parent = rb_red_parent(node), *gparent, *tmp;
92
93 while (true) {
94 /*
95 * Loop invariant: node is red.
96 */
97 if (unlikely(!parent)) {
98 /*
99 * The inserted node is root. Either this is the
100 * first node, or we recursed at Case 1 below and
101 * are no longer violating 4).
102 */
103 rb_set_parent_color(node, NULL, RB_BLACK);
104 break;
105 }
106
107 /*
108 * If there is a black parent, we are done.
109 * Otherwise, take some corrective action as,
110 * per 4), we don't want a red root or two
111 * consecutive red nodes.
112 */
113 if(rb_is_black(parent))
114 break;
115
116 gparent = rb_red_parent(parent);
117
118 tmp = gparent->rb_right;
119 if (parent != tmp) { /* parent == gparent->rb_left */
120 if (tmp && rb_is_red(tmp)) {
121 /*
122 * Case 1 - node's uncle is red (color flips).
123 *
124 * G g
125 * / \ / \
126 * p u --> P U
127 * / /
128 * n n
129 *
130 * However, since g's parent might be red, and
131 * 4) does not allow this, we need to recurse
132 * at g.
133 */
134 rb_set_parent_color(tmp, gparent, RB_BLACK);
135 rb_set_parent_color(parent, gparent, RB_BLACK);
136 node = gparent;
137 parent = rb_parent(node);
138 rb_set_parent_color(node, parent, RB_RED);
139 continue;
140 }
141
142 tmp = parent->rb_right;
143 if (node == tmp) {
144 /*
145 * Case 2 - node's uncle is black and node is
146 * the parent's right child (left rotate at parent).
147 *
148 * G G
149 * / \ / \
150 * p U --> n U
151 * \ /
152 * n p
153 *
154 * This still leaves us in violation of 4), the
155 * continuation into Case 3 will fix that.
156 */
157 tmp = node->rb_left;
158 WRITE_ONCE(parent->rb_right, tmp);
159 WRITE_ONCE(node->rb_left, parent);
160 if (tmp)
161 rb_set_parent_color(tmp, parent,
162 RB_BLACK);
163 rb_set_parent_color(parent, node, RB_RED);
164 augment_rotate(parent, node);
165 parent = node;
166 tmp = node->rb_right;
167 }
168
169 /*
170 * Case 3 - node's uncle is black and node is
171 * the parent's left child (right rotate at gparent).
172 *
173 * G P
174 * / \ / \
175 * p U --> n g
176 * / \
177 * n U
178 */
179 WRITE_ONCE(gparent->rb_left, tmp); /* == parent->rb_right */
180 WRITE_ONCE(parent->rb_right, gparent);
181 if (tmp)
182 rb_set_parent_color(tmp, gparent, RB_BLACK);
183 __rb_rotate_set_parents(gparent, parent, root, RB_RED);
184 augment_rotate(gparent, parent);
185 break;
186 } else {
187 tmp = gparent->rb_left;
188 if (tmp && rb_is_red(tmp)) {
189 /* Case 1 - color flips */
190 rb_set_parent_color(tmp, gparent, RB_BLACK);
191 rb_set_parent_color(parent, gparent, RB_BLACK);
192 node = gparent;
193 parent = rb_parent(node);
194 rb_set_parent_color(node, parent, RB_RED);
195 continue;
196 }
197
198 tmp = parent->rb_left;
199 if (node == tmp) {
200 /* Case 2 - right rotate at parent */
201 tmp = node->rb_right;
202 WRITE_ONCE(parent->rb_left, tmp);
203 WRITE_ONCE(node->rb_right, parent);
204 if (tmp)
205 rb_set_parent_color(tmp, parent,
206 RB_BLACK);
207 rb_set_parent_color(parent, node, RB_RED);
208 augment_rotate(parent, node);
209 parent = node;
210 tmp = node->rb_left;
211 }
212
213 /* Case 3 - left rotate at gparent */
214 WRITE_ONCE(gparent->rb_right, tmp); /* == parent->rb_left */
215 WRITE_ONCE(parent->rb_left, gparent);
216 if (tmp)
217 rb_set_parent_color(tmp, gparent, RB_BLACK);
218 __rb_rotate_set_parents(gparent, parent, root, RB_RED);
219 augment_rotate(gparent, parent);
220 break;
221 }
222 }
223}
224
225/*
226 * Inline version for rb_erase() use - we want to be able to inline
227 * and eliminate the dummy_rotate callback there
228 */
229static inline void
230____rb_erase_color(struct rb_node *parent, struct rb_root *root,
231 void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
232{
233 struct rb_node *node = NULL, *sibling, *tmp1, *tmp2;
234
235 while (true) {
236 /*
237 * Loop invariants:
238 * - node is black (or NULL on first iteration)
239 * - node is not the root (parent is not NULL)
240 * - All leaf paths going through parent and node have a
241 * black node count that is 1 lower than other leaf paths.
242 */
243 sibling = parent->rb_right;
244 if (node != sibling) { /* node == parent->rb_left */
245 if (rb_is_red(sibling)) {
246 /*
247 * Case 1 - left rotate at parent
248 *
249 * P S
250 * / \ / \
251 * N s --> p Sr
252 * / \ / \
253 * Sl Sr N Sl
254 */
255 tmp1 = sibling->rb_left;
256 WRITE_ONCE(parent->rb_right, tmp1);
257 WRITE_ONCE(sibling->rb_left, parent);
258 rb_set_parent_color(tmp1, parent, RB_BLACK);
259 __rb_rotate_set_parents(parent, sibling, root,
260 RB_RED);
261 augment_rotate(parent, sibling);
262 sibling = tmp1;
263 }
264 tmp1 = sibling->rb_right;
265 if (!tmp1 || rb_is_black(tmp1)) {
266 tmp2 = sibling->rb_left;
267 if (!tmp2 || rb_is_black(tmp2)) {
268 /*
269 * Case 2 - sibling color flip
270 * (p could be either color here)
271 *
272 * (p) (p)
273 * / \ / \
274 * N S --> N s
275 * / \ / \
276 * Sl Sr Sl Sr
277 *
278 * This leaves us violating 5) which
279 * can be fixed by flipping p to black
280 * if it was red, or by recursing at p.
281 * p is red when coming from Case 1.
282 */
283 rb_set_parent_color(sibling, parent,
284 RB_RED);
285 if (rb_is_red(parent))
286 rb_set_black(parent);
287 else {
288 node = parent;
289 parent = rb_parent(node);
290 if (parent)
291 continue;
292 }
293 break;
294 }
295 /*
296 * Case 3 - right rotate at sibling
297 * (p could be either color here)
298 *
299 * (p) (p)
300 * / \ / \
301 * N S --> N sl
302 * / \ \
303 * sl Sr S
304 * \
305 * Sr
306 *
307 * Note: p might be red, and then both
308 * p and sl are red after rotation(which
309 * breaks property 4). This is fixed in
310 * Case 4 (in __rb_rotate_set_parents()
311 * which set sl the color of p
312 * and set p RB_BLACK)
313 *
314 * (p) (sl)
315 * / \ / \
316 * N sl --> P S
317 * \ / \
318 * S N Sr
319 * \
320 * Sr
321 */
322 tmp1 = tmp2->rb_right;
323 WRITE_ONCE(sibling->rb_left, tmp1);
324 WRITE_ONCE(tmp2->rb_right, sibling);
325 WRITE_ONCE(parent->rb_right, tmp2);
326 if (tmp1)
327 rb_set_parent_color(tmp1, sibling,
328 RB_BLACK);
329 augment_rotate(sibling, tmp2);
330 tmp1 = sibling;
331 sibling = tmp2;
332 }
333 /*
334 * Case 4 - left rotate at parent + color flips
335 * (p and sl could be either color here.
336 * After rotation, p becomes black, s acquires
337 * p's color, and sl keeps its color)
338 *
339 * (p) (s)
340 * / \ / \
341 * N S --> P Sr
342 * / \ / \
343 * (sl) sr N (sl)
344 */
345 tmp2 = sibling->rb_left;
346 WRITE_ONCE(parent->rb_right, tmp2);
347 WRITE_ONCE(sibling->rb_left, parent);
348 rb_set_parent_color(tmp1, sibling, RB_BLACK);
349 if (tmp2)
350 rb_set_parent(tmp2, parent);
351 __rb_rotate_set_parents(parent, sibling, root,
352 RB_BLACK);
353 augment_rotate(parent, sibling);
354 break;
355 } else {
356 sibling = parent->rb_left;
357 if (rb_is_red(sibling)) {
358 /* Case 1 - right rotate at parent */
359 tmp1 = sibling->rb_right;
360 WRITE_ONCE(parent->rb_left, tmp1);
361 WRITE_ONCE(sibling->rb_right, parent);
362 rb_set_parent_color(tmp1, parent, RB_BLACK);
363 __rb_rotate_set_parents(parent, sibling, root,
364 RB_RED);
365 augment_rotate(parent, sibling);
366 sibling = tmp1;
367 }
368 tmp1 = sibling->rb_left;
369 if (!tmp1 || rb_is_black(tmp1)) {
370 tmp2 = sibling->rb_right;
371 if (!tmp2 || rb_is_black(tmp2)) {
372 /* Case 2 - sibling color flip */
373 rb_set_parent_color(sibling, parent,
374 RB_RED);
375 if (rb_is_red(parent))
376 rb_set_black(parent);
377 else {
378 node = parent;
379 parent = rb_parent(node);
380 if (parent)
381 continue;
382 }
383 break;
384 }
385 /* Case 3 - left rotate at sibling */
386 tmp1 = tmp2->rb_left;
387 WRITE_ONCE(sibling->rb_right, tmp1);
388 WRITE_ONCE(tmp2->rb_left, sibling);
389 WRITE_ONCE(parent->rb_left, tmp2);
390 if (tmp1)
391 rb_set_parent_color(tmp1, sibling,
392 RB_BLACK);
393 augment_rotate(sibling, tmp2);
394 tmp1 = sibling;
395 sibling = tmp2;
396 }
397 /* Case 4 - right rotate at parent + color flips */
398 tmp2 = sibling->rb_right;
399 WRITE_ONCE(parent->rb_left, tmp2);
400 WRITE_ONCE(sibling->rb_right, parent);
401 rb_set_parent_color(tmp1, sibling, RB_BLACK);
402 if (tmp2)
403 rb_set_parent(tmp2, parent);
404 __rb_rotate_set_parents(parent, sibling, root,
405 RB_BLACK);
406 augment_rotate(parent, sibling);
407 break;
408 }
409 }
410}
411
412/* Non-inline version for rb_erase_augmented() use */
413void __rb_erase_color(struct rb_node *parent, struct rb_root *root,
414 void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
415{
416 ____rb_erase_color(parent, root, augment_rotate);
417}
418EXPORT_SYMBOL(__rb_erase_color);
419
420/*
421 * Non-augmented rbtree manipulation functions.
422 *
423 * We use dummy augmented callbacks here, and have the compiler optimize them
424 * out of the rb_insert_color() and rb_erase() function definitions.
425 */
426
427static inline void dummy_propagate(struct rb_node *node, struct rb_node *stop) {}
428static inline void dummy_copy(struct rb_node *old, struct rb_node *new) {}
429static inline void dummy_rotate(struct rb_node *old, struct rb_node *new) {}
430
431static const struct rb_augment_callbacks dummy_callbacks = {
432 .propagate = dummy_propagate,
433 .copy = dummy_copy,
434 .rotate = dummy_rotate
435};
436
437void rb_insert_color(struct rb_node *node, struct rb_root *root)
438{
439 __rb_insert(node, root, dummy_rotate);
440}
441EXPORT_SYMBOL(rb_insert_color);
442
443void rb_erase(struct rb_node *node, struct rb_root *root)
444{
445 struct rb_node *rebalance;
446 rebalance = __rb_erase_augmented(node, root, &dummy_callbacks);
447 if (rebalance)
448 ____rb_erase_color(rebalance, root, dummy_rotate);
449}
450EXPORT_SYMBOL(rb_erase);
451
452/*
453 * Augmented rbtree manipulation functions.
454 *
455 * This instantiates the same inline functions as in the non-augmented
456 * case, but this time with user-defined callbacks.
457 */
458
459void __rb_insert_augmented(struct rb_node *node, struct rb_root *root,
460 void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
461{
462 __rb_insert(node, root, augment_rotate);
463}
464EXPORT_SYMBOL(__rb_insert_augmented);
465
466/*
467 * This function returns the first node (in sort order) of the tree.
468 */
469struct rb_node *rb_first(const struct rb_root *root)
470{
471 struct rb_node *n;
472
473 n = root->rb_node;
474 if (!n)
475 return NULL;
476 while (n->rb_left)
477 n = n->rb_left;
478 return n;
479}
480EXPORT_SYMBOL(rb_first);
481
482struct rb_node *rb_last(const struct rb_root *root)
483{
484 struct rb_node *n;
485
486 n = root->rb_node;
487 if (!n)
488 return NULL;
489 while (n->rb_right)
490 n = n->rb_right;
491 return n;
492}
493EXPORT_SYMBOL(rb_last);
494
495struct rb_node *rb_next(const struct rb_node *node)
496{
497 struct rb_node *parent;
498
499 if (RB_EMPTY_NODE(node))
500 return NULL;
501
502 /*
503 * If we have a right-hand child, go down and then left as far
504 * as we can.
505 */
506 if (node->rb_right) {
507 node = node->rb_right;
508 while (node->rb_left)
509 node = node->rb_left;
510 return (struct rb_node *)node;
511 }
512
513 /*
514 * No right-hand children. Everything down and left is smaller than us,
515 * so any 'next' node must be in the general direction of our parent.
516 * Go up the tree; any time the ancestor is a right-hand child of its
517 * parent, keep going up. First time it's a left-hand child of its
518 * parent, said parent is our 'next' node.
519 */
520 while ((parent = rb_parent(node)) && node == parent->rb_right)
521 node = parent;
522
523 return parent;
524}
525EXPORT_SYMBOL(rb_next);
526
527struct rb_node *rb_prev(const struct rb_node *node)
528{
529 struct rb_node *parent;
530
531 if (RB_EMPTY_NODE(node))
532 return NULL;
533
534 /*
535 * If we have a left-hand child, go down and then right as far
536 * as we can.
537 */
538 if (node->rb_left) {
539 node = node->rb_left;
540 while (node->rb_right)
541 node = node->rb_right;
542 return (struct rb_node *)node;
543 }
544
545 /*
546 * No left-hand children. Go up till we find an ancestor which
547 * is a right-hand child of its parent.
548 */
549 while ((parent = rb_parent(node)) && node == parent->rb_left)
550 node = parent;
551
552 return parent;
553}
554EXPORT_SYMBOL(rb_prev);
555
556void rb_replace_node(struct rb_node *victim, struct rb_node *new,
557 struct rb_root *root)
558{
559 struct rb_node *parent = rb_parent(victim);
560
561 /* Copy the pointers/colour from the victim to the replacement */
562 *new = *victim;
563
564 /* Set the surrounding nodes to point to the replacement */
565 if (victim->rb_left)
566 rb_set_parent(victim->rb_left, new);
567 if (victim->rb_right)
568 rb_set_parent(victim->rb_right, new);
569 __rb_change_child(victim, new, parent, root);
570}
571EXPORT_SYMBOL(rb_replace_node);
572
573#ifndef TARGET_OS2
574void rb_replace_node_rcu(struct rb_node *victim, struct rb_node *new,
575 struct rb_root *root)
576{
577 struct rb_node *parent = rb_parent(victim);
578
579 /* Copy the pointers/colour from the victim to the replacement */
580 *new = *victim;
581
582 /* Set the surrounding nodes to point to the replacement */
583 if (victim->rb_left)
584 rb_set_parent(victim->rb_left, new);
585 if (victim->rb_right)
586 rb_set_parent(victim->rb_right, new);
587
588 /* Set the parent's pointer to the new node last after an RCU barrier
589 * so that the pointers onwards are seen to be set correctly when doing
590 * an RCU walk over the tree.
591 */
592 __rb_change_child_rcu(victim, new, parent, root);
593}
594EXPORT_SYMBOL(rb_replace_node_rcu);
595#endif
596
597static struct rb_node *rb_left_deepest_node(const struct rb_node *node)
598{
599 for (;;) {
600 if (node->rb_left)
601 node = node->rb_left;
602 else if (node->rb_right)
603 node = node->rb_right;
604 else
605 return (struct rb_node *)node;
606 }
607}
608
609struct rb_node *rb_next_postorder(const struct rb_node *node)
610{
611 const struct rb_node *parent;
612 if (!node)
613 return NULL;
614 parent = rb_parent(node);
615
616 /* If we're sitting on node, we've already seen our children */
617 if (parent && node == parent->rb_left && parent->rb_right) {
618 /* If we are the parent's left node, go to the parent's right
619 * node then all the way down to the left */
620 return rb_left_deepest_node(parent->rb_right);
621 } else
622 /* Otherwise we are the parent's right node, and the parent
623 * should be next */
624 return (struct rb_node *)parent;
625}
626EXPORT_SYMBOL(rb_next_postorder);
627
628struct rb_node *rb_first_postorder(const struct rb_root *root)
629{
630 if (!root->rb_node)
631 return NULL;
632
633 return rb_left_deepest_node(root->rb_node);
634}
635EXPORT_SYMBOL(rb_first_postorder);
Note: See TracBrowser for help on using the repository browser.