1 /*-
2 * Copyright (c) 2010 Isilon Systems, Inc.
3 * Copyright (c) 2010 iX Systems, Inc.
4 * Copyright (c) 2010 Panasas, Inc.
5 * Copyright (c) 2013, 2014 Mellanox Technologies, Ltd.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice unmodified, this list of conditions, and the following
13 * disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29 #ifndef _LINUXKPI_LINUX_RBTREE_H_
30 #define _LINUXKPI_LINUX_RBTREE_H_
31
32 #ifndef _STANDALONE
33 #include <sys/stddef.h>
34 #endif
35
36 #include <sys/types.h>
37 #include <sys/tree.h>
38
39 struct rb_node {
40 RB_ENTRY(rb_node) __entry;
41 };
42 #define rb_left __entry.rbe_link[_RB_L-1]
43 #define rb_right __entry.rbe_link[_RB_R-1]
44
45 /*
46 * We provide a false structure that has the same bit pattern as tree.h
47 * presents so it matches the member names expected by linux.
48 */
49 struct rb_root {
50 struct rb_node *rb_node;
51 };
52
53 struct rb_root_cached {
54 struct rb_root rb_root;
55 struct rb_node *rb_leftmost;
56 };
57
58 /*
59 * In linux all of the comparisons are done by the caller.
60 */
61 int panic_cmp(struct rb_node *one, struct rb_node *two);
62
63 RB_HEAD(linux_root, rb_node);
64 RB_PROTOTYPE(linux_root, rb_node, __entry, panic_cmp);
65
66 #define rb_parent(r) RB_PARENT(r, __entry)
67 #define rb_entry(ptr, type, member) container_of(ptr, type, member)
68 #define rb_entry_safe(ptr, type, member) \
69 ((ptr) != NULL ? rb_entry(ptr, type, member) : NULL)
70
71 #define RB_EMPTY_ROOT(root) ((root)->rb_node == NULL)
72 #define RB_EMPTY_NODE(node) (RB_PARENT(node, __entry) == node)
73 #define RB_CLEAR_NODE(node) RB_SET_PARENT(node, node, __entry)
74
75 #define rb_insert_color(node, root) \
76 linux_root_RB_INSERT_COLOR((struct linux_root *)(root), (node))
77 #define rb_erase(node, root) \
78 linux_root_RB_REMOVE((struct linux_root *)(root), (node))
79 #define rb_next(node) RB_NEXT(linux_root, NULL, (node))
80 #define rb_prev(node) RB_PREV(linux_root, NULL, (node))
81 #define rb_first(root) RB_MIN(linux_root, (struct linux_root *)(root))
82 #define rb_last(root) RB_MAX(linux_root, (struct linux_root *)(root))
83 #define rb_first_cached(root) (root)->rb_leftmost
84
85 static inline struct rb_node *
__rb_deepest_left(struct rb_node * node)86 __rb_deepest_left(struct rb_node *node)
87 {
88 struct rb_node *parent = NULL;
89 while (node != NULL) {
90 parent = node;
91 if (RB_LEFT(node, __entry))
92 node = RB_LEFT(node, __entry);
93 else
94 node = RB_RIGHT(node, __entry);
95 }
96 return (parent);
97 }
98
99 static inline struct rb_node *
rb_next_postorder(const struct rb_node * node)100 rb_next_postorder(const struct rb_node *node)
101 {
102 struct rb_node *parent =
103 RB_PARENT(__DECONST(struct rb_node *, node), __entry);
104 /* left -> right, right -> root */
105 if (parent != NULL &&
106 (node == RB_LEFT(parent, __entry)) &&
107 (RB_RIGHT(parent, __entry)))
108 return (__rb_deepest_left(RB_RIGHT(parent, __entry)));
109 else
110 return (parent);
111 }
112
113 #define rbtree_postorder_for_each_entry_safe(x, y, head, member) \
114 for ((x) = rb_entry_safe(__rb_deepest_left((head)->rb_node), \
115 __typeof(*x), member); \
116 ((x) != NULL) && ((y) = \
117 rb_entry_safe(rb_next_postorder(&x->member), typeof(*x), member), 1); \
118 (x) = (y))
119
120 static inline void
rb_link_node(struct rb_node * node,struct rb_node * parent,struct rb_node ** rb_link)121 rb_link_node(struct rb_node *node, struct rb_node *parent,
122 struct rb_node **rb_link)
123 {
124 RB_SET(node, parent, __entry);
125 *rb_link = node;
126 }
127
128 static inline void
rb_replace_node(struct rb_node * victim,struct rb_node * new,struct rb_root * root)129 rb_replace_node(struct rb_node *victim, struct rb_node *new,
130 struct rb_root *root)
131 {
132
133 RB_SWAP_CHILD((struct linux_root *)root, rb_parent(victim),
134 victim, new, __entry);
135 if (RB_LEFT(victim, __entry))
136 RB_SET_PARENT(RB_LEFT(victim, __entry), new, __entry);
137 if (RB_RIGHT(victim, __entry))
138 RB_SET_PARENT(RB_RIGHT(victim, __entry), new, __entry);
139 *new = *victim;
140 }
141
142 static inline void
rb_insert_color_cached(struct rb_node * node,struct rb_root_cached * root,bool leftmost)143 rb_insert_color_cached(struct rb_node *node, struct rb_root_cached *root,
144 bool leftmost)
145 {
146 linux_root_RB_INSERT_COLOR((struct linux_root *)&root->rb_root, node);
147 if (leftmost)
148 root->rb_leftmost = node;
149 }
150
151 static inline struct rb_node *
rb_erase_cached(struct rb_node * node,struct rb_root_cached * root)152 rb_erase_cached(struct rb_node *node, struct rb_root_cached *root)
153 {
154 struct rb_node *retval;
155
156 if (node == root->rb_leftmost)
157 retval = root->rb_leftmost = linux_root_RB_NEXT(node);
158 else
159 retval = NULL;
160 linux_root_RB_REMOVE((struct linux_root *)&root->rb_root, node);
161 return (retval);
162 }
163
164 static inline void
rb_replace_node_cached(struct rb_node * old,struct rb_node * new,struct rb_root_cached * root)165 rb_replace_node_cached(struct rb_node *old, struct rb_node *new,
166 struct rb_root_cached *root)
167 {
168 rb_replace_node(old, new, &root->rb_root);
169 if (root->rb_leftmost == old)
170 root->rb_leftmost = new;
171 }
172
173 #undef RB_ROOT
174 #define RB_ROOT (struct rb_root) { NULL }
175 #define RB_ROOT_CACHED (struct rb_root_cached) { RB_ROOT, NULL }
176
177 #endif /* _LINUXKPI_LINUX_RBTREE_H_ */
178