1f1a8151dSFrançois Tigeot /*-
2f1a8151dSFrançois Tigeot * Copyright (c) 2010 Isilon Systems, Inc.
3f1a8151dSFrançois Tigeot * Copyright (c) 2010 iX Systems, Inc.
4f1a8151dSFrançois Tigeot * Copyright (c) 2010 Panasas, Inc.
5f1a8151dSFrançois Tigeot * Copyright (c) 2013, 2014 Mellanox Technologies, Ltd.
6f1a8151dSFrançois Tigeot * All rights reserved.
7f1a8151dSFrançois Tigeot *
8f1a8151dSFrançois Tigeot * Redistribution and use in source and binary forms, with or without
9f1a8151dSFrançois Tigeot * modification, are permitted provided that the following conditions
10f1a8151dSFrançois Tigeot * are met:
11f1a8151dSFrançois Tigeot * 1. Redistributions of source code must retain the above copyright
12f1a8151dSFrançois Tigeot * notice unmodified, this list of conditions, and the following
13f1a8151dSFrançois Tigeot * disclaimer.
14f1a8151dSFrançois Tigeot * 2. Redistributions in binary form must reproduce the above copyright
15f1a8151dSFrançois Tigeot * notice, this list of conditions and the following disclaimer in the
16f1a8151dSFrançois Tigeot * documentation and/or other materials provided with the distribution.
17f1a8151dSFrançois Tigeot *
18f1a8151dSFrançois Tigeot * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19f1a8151dSFrançois Tigeot * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20f1a8151dSFrançois Tigeot * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21f1a8151dSFrançois Tigeot * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22f1a8151dSFrançois Tigeot * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23f1a8151dSFrançois Tigeot * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24f1a8151dSFrançois Tigeot * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25f1a8151dSFrançois Tigeot * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26f1a8151dSFrançois Tigeot * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27f1a8151dSFrançois Tigeot * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28f1a8151dSFrançois Tigeot */
29f1a8151dSFrançois Tigeot #ifndef _LINUX_RBTREE_H_
30f1a8151dSFrançois Tigeot #define _LINUX_RBTREE_H_
31f1a8151dSFrançois Tigeot
32a8601baeSFrançois Tigeot #include <linux/kernel.h>
33a8601baeSFrançois Tigeot #include <linux/stddef.h>
34a8601baeSFrançois Tigeot #include <linux/rcupdate.h>
35a8601baeSFrançois Tigeot
36f1a8151dSFrançois Tigeot #include <sys/tree.h>
37a34b4168SMatthew Dillon #include <sys/spinlock.h>
38f1a8151dSFrançois Tigeot
39f1a8151dSFrançois Tigeot struct rb_node {
40f1a8151dSFrançois Tigeot RB_ENTRY(rb_node) __entry;
41f1a8151dSFrançois Tigeot };
42f1a8151dSFrançois Tigeot #define rb_left __entry.rbe_left
43f1a8151dSFrançois Tigeot #define rb_right __entry.rbe_right
44f1a8151dSFrançois Tigeot
45f1a8151dSFrançois Tigeot /*
46a34b4168SMatthew Dillon * This must match enough of sys/tree.h so the macros still work.
47f1a8151dSFrançois Tigeot */
48f1a8151dSFrançois Tigeot struct rb_root {
49a34b4168SMatthew Dillon struct rb_node *rb_node; /* only member under linux */
50a34b4168SMatthew Dillon void *rbh_inprog; /* so we can use sys/tree macros */
51a34b4168SMatthew Dillon struct spinlock rbh_spin; /* so we can use sys/tree macros */
52f1a8151dSFrançois Tigeot };
53f1a8151dSFrançois Tigeot
54f1a8151dSFrançois Tigeot /*
55*78973132SSergey Zigachev * Leftmost-cached rbtrees.
56*78973132SSergey Zigachev *
57*78973132SSergey Zigachev * We do not cache the rightmost node based on footprint
58*78973132SSergey Zigachev * size vs number of potential users that could benefit
59*78973132SSergey Zigachev * from O(1) rb_last(). Just not worth it, users that want
60*78973132SSergey Zigachev * this feature can always implement the logic explicitly.
61*78973132SSergey Zigachev * Furthermore, users that want to cache both pointers may
62*78973132SSergey Zigachev * find it a bit asymmetric, but that's ok.
63*78973132SSergey Zigachev */
64*78973132SSergey Zigachev struct rb_root_cached {
65*78973132SSergey Zigachev struct rb_root rb_root;
66*78973132SSergey Zigachev struct rb_node *rb_leftmost;
67*78973132SSergey Zigachev };
68*78973132SSergey Zigachev
69*78973132SSergey Zigachev /*
70f1a8151dSFrançois Tigeot * In linux all of the comparisons are done by the caller.
71f1a8151dSFrançois Tigeot */
72f1a8151dSFrançois Tigeot int panic_cmp(struct rb_node *one, struct rb_node *two);
73f1a8151dSFrançois Tigeot
74f1a8151dSFrançois Tigeot RB_HEAD(linux_root, rb_node);
75f1a8151dSFrançois Tigeot RB_PROTOTYPE(linux_root, rb_node, __entry, panic_cmp);
76f1a8151dSFrançois Tigeot
77f1a8151dSFrançois Tigeot #define rb_parent(r) RB_PARENT(r, __entry)
78f1a8151dSFrançois Tigeot #define rb_color(r) RB_COLOR(r, __entry)
79f1a8151dSFrançois Tigeot #define rb_is_red(r) (rb_color(r) == RB_RED)
80f1a8151dSFrançois Tigeot #define rb_is_black(r) (rb_color(r) == RB_BLACK)
81f1a8151dSFrançois Tigeot #define rb_set_parent(r, p) rb_parent((r)) = (p)
82f1a8151dSFrançois Tigeot #define rb_set_color(r, c) rb_color((r)) = (c)
83f1a8151dSFrançois Tigeot #define rb_entry(ptr, type, member) container_of(ptr, type, member)
84c2bbe163SFrançois Tigeot #define rb_entry_safe(ptr, type, member) \
85c2bbe163SFrançois Tigeot (ptr ? rb_entry(ptr, type, member) : NULL)
86f1a8151dSFrançois Tigeot
87f1a8151dSFrançois Tigeot #define RB_EMPTY_ROOT(root) RB_EMPTY((struct linux_root *)root)
88f1a8151dSFrançois Tigeot #define RB_EMPTY_NODE(node) (rb_parent(node) == node)
89f1a8151dSFrançois Tigeot #define RB_CLEAR_NODE(node) (rb_set_parent(node, node))
90f1a8151dSFrançois Tigeot
91f1a8151dSFrançois Tigeot #define rb_insert_color(node, root) \
92f1a8151dSFrançois Tigeot linux_root_RB_INSERT_COLOR((struct linux_root *)(root), (node))
93f1a8151dSFrançois Tigeot #define rb_erase(node, root) \
94f1a8151dSFrançois Tigeot linux_root_RB_REMOVE((struct linux_root *)(root), (node))
95f1a8151dSFrançois Tigeot #define rb_next(node) RB_NEXT(linux_root, NULL, (node))
96f1a8151dSFrançois Tigeot #define rb_prev(node) RB_PREV(linux_root, NULL, (node))
97f1a8151dSFrançois Tigeot #define rb_first(root) RB_MIN(linux_root, (struct linux_root *)(root))
98f1a8151dSFrançois Tigeot #define rb_last(root) RB_MAX(linux_root, (struct linux_root *)(root))
99f1a8151dSFrançois Tigeot
100*78973132SSergey Zigachev #define rb_insert_color_cached(node, root, leftmost) \
101*78973132SSergey Zigachev linux_root_RB_INSERT_COLOR((struct linux_root *)(&(root)->rb_root), (node))
102*78973132SSergey Zigachev #define rb_erase_cached(node, root) \
103*78973132SSergey Zigachev linux_root_RB_REMOVE((struct linux_root *)(&(root)->rb_root), (node))
104*78973132SSergey Zigachev #define rb_first_cached(root) RB_MIN(linux_root, (struct linux_root *)(&(root)->rb_root))
105*78973132SSergey Zigachev
106c2bbe163SFrançois Tigeot static inline struct rb_node *
__rb_deepest_left(struct rb_node * node)107c2bbe163SFrançois Tigeot __rb_deepest_left(struct rb_node *node)
108c2bbe163SFrançois Tigeot {
109c2bbe163SFrançois Tigeot struct rb_node *parent = NULL;
110c2bbe163SFrançois Tigeot while (node) {
111c2bbe163SFrançois Tigeot parent = node;
112c2bbe163SFrançois Tigeot if (RB_LEFT(node, __entry))
113c2bbe163SFrançois Tigeot node = RB_LEFT(node, __entry);
114c2bbe163SFrançois Tigeot else
115c2bbe163SFrançois Tigeot node = RB_RIGHT(node, __entry);
116c2bbe163SFrançois Tigeot }
117c2bbe163SFrançois Tigeot return parent;
118c2bbe163SFrançois Tigeot }
119c2bbe163SFrançois Tigeot
120c2bbe163SFrançois Tigeot static inline struct rb_node *
rb_next_postorder(const struct rb_node * node)121c2bbe163SFrançois Tigeot rb_next_postorder(const struct rb_node *node)
122c2bbe163SFrançois Tigeot {
123c2bbe163SFrançois Tigeot struct rb_node *parent = RB_PARENT(node, __entry);
124c2bbe163SFrançois Tigeot /* left -> right, right -> root */
125c2bbe163SFrançois Tigeot if (parent != NULL &&
126c2bbe163SFrançois Tigeot (node == RB_LEFT(parent, __entry)) &&
127c2bbe163SFrançois Tigeot (RB_RIGHT(parent, __entry)))
128c2bbe163SFrançois Tigeot return __rb_deepest_left(RB_RIGHT(parent, __entry));
129c2bbe163SFrançois Tigeot else
130c2bbe163SFrançois Tigeot return parent;
131c2bbe163SFrançois Tigeot }
132c2bbe163SFrançois Tigeot
133c2bbe163SFrançois Tigeot #define rbtree_postorder_for_each_entry_safe(x, y, head, member) \
134c2bbe163SFrançois Tigeot for ((x) = rb_entry_safe(__rb_deepest_left((head)->rb_node), \
135c2bbe163SFrançois Tigeot __typeof(*x), member); \
136c2bbe163SFrançois Tigeot ((x) != NULL) && ((y) = \
137c2bbe163SFrançois Tigeot rb_entry_safe(rb_next_postorder(&x->member), typeof(*x), member), 1); \
138c2bbe163SFrançois Tigeot (x) = (y))
139c2bbe163SFrançois Tigeot
140f1a8151dSFrançois Tigeot static inline void
rb_link_node(struct rb_node * node,struct rb_node * parent,struct rb_node ** rb_link)141f1a8151dSFrançois Tigeot rb_link_node(struct rb_node *node, struct rb_node *parent,
142f1a8151dSFrançois Tigeot struct rb_node **rb_link)
143f1a8151dSFrançois Tigeot {
144f1a8151dSFrançois Tigeot rb_set_parent(node, parent);
145f1a8151dSFrançois Tigeot rb_set_color(node, RB_RED);
146f1a8151dSFrançois Tigeot node->__entry.rbe_left = node->__entry.rbe_right = NULL;
147f1a8151dSFrançois Tigeot *rb_link = node;
148f1a8151dSFrançois Tigeot }
149f1a8151dSFrançois Tigeot
150f1a8151dSFrançois Tigeot static inline void
rb_replace_node(struct rb_node * victim,struct rb_node * new,struct rb_root * root)151f1a8151dSFrançois Tigeot rb_replace_node(struct rb_node *victim, struct rb_node *new,
152f1a8151dSFrançois Tigeot struct rb_root *root)
153f1a8151dSFrançois Tigeot {
154f1a8151dSFrançois Tigeot struct rb_node *p;
155f1a8151dSFrançois Tigeot
156f1a8151dSFrançois Tigeot p = rb_parent(victim);
157f1a8151dSFrançois Tigeot if (p) {
158f1a8151dSFrançois Tigeot if (p->rb_left == victim)
159f1a8151dSFrançois Tigeot p->rb_left = new;
160f1a8151dSFrançois Tigeot else
161f1a8151dSFrançois Tigeot p->rb_right = new;
162f1a8151dSFrançois Tigeot } else
163f1a8151dSFrançois Tigeot root->rb_node = new;
164f1a8151dSFrançois Tigeot if (victim->rb_left)
165f1a8151dSFrançois Tigeot rb_set_parent(victim->rb_left, new);
166f1a8151dSFrançois Tigeot if (victim->rb_right)
167f1a8151dSFrançois Tigeot rb_set_parent(victim->rb_right, new);
168f1a8151dSFrançois Tigeot *new = *victim;
169f1a8151dSFrançois Tigeot }
170f1a8151dSFrançois Tigeot
171a8601baeSFrançois Tigeot #define LINUX_RB_ROOT (struct rb_root) { NULL }
172f1a8151dSFrançois Tigeot
173*78973132SSergey Zigachev #define LINUX_RB_ROOT_CACHED (struct rb_root_cached) { {NULL, }, NULL }
174*78973132SSergey Zigachev
175f1a8151dSFrançois Tigeot #endif /* _LINUX_RBTREE_H_ */
176