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 * $FreeBSD$
30 */
31 #ifndef _LINUXKPI_LINUX_RBTREE_H_
32 #define _LINUXKPI_LINUX_RBTREE_H_
33
34 #ifndef _STANDALONE
35 #include <sys/stddef.h>
36 #endif
37
38 #include <sys/types.h>
39 #include <sys/tree.h>
40
41 struct rb_node {
42 RB_ENTRY(rb_node) __entry;
43 };
44 #define rb_left __entry.rbe_link[_RB_L]
45 #define rb_right __entry.rbe_link[_RB_R]
46
47 /*
48 * We provide a false structure that has the same bit pattern as tree.h
49 * presents so it matches the member names expected by linux.
50 */
51 struct rb_root {
52 struct rb_node *rb_node;
53 };
54
55 struct rb_root_cached {
56 struct rb_root rb_root;
57 struct rb_node *rb_leftmost;
58 };
59
60 /*
61 * In linux all of the comparisons are done by the caller.
62 */
63 int panic_cmp(struct rb_node *one, struct rb_node *two);
64
65 RB_HEAD(linux_root, rb_node);
66 RB_PROTOTYPE(linux_root, rb_node, __entry, panic_cmp);
67
68 #define rb_parent(r) RB_PARENT(r, __entry)
69 #define rb_entry(ptr, type, member) container_of(ptr, type, member)
70 #define rb_entry_safe(ptr, type, member) \
71 ((ptr) != NULL ? rb_entry(ptr, type, member) : NULL)
72
73 #define RB_EMPTY_ROOT(root) ((root)->rb_node == NULL)
74 #define RB_EMPTY_NODE(node) (RB_PARENT(node, __entry) == node)
75 #define RB_CLEAR_NODE(node) RB_SET_PARENT(node, node, __entry)
76
77 #define rb_insert_color(node, root) do { \
78 if (rb_parent(node)) \
79 linux_root_RB_INSERT_COLOR((struct linux_root *)(root), \
80 rb_parent(node), (node)); \
81 } while (0)
82 #define rb_erase(node, root) \
83 linux_root_RB_REMOVE((struct linux_root *)(root), (node))
84 #define rb_next(node) RB_NEXT(linux_root, NULL, (node))
85 #define rb_prev(node) RB_PREV(linux_root, NULL, (node))
86 #define rb_first(root) RB_MIN(linux_root, (struct linux_root *)(root))
87 #define rb_last(root) RB_MAX(linux_root, (struct linux_root *)(root))
88 #define rb_first_cached(root) (root)->rb_leftmost
89
90 static inline struct rb_node *
91 __rb_deepest_left(struct rb_node *node)
92 {
93 struct rb_node *parent = NULL;
94 while (node != NULL) {
95 parent = node;
96 if (RB_LEFT(node, __entry))
97 node = RB_LEFT(node, __entry);
98 else
99 node = RB_RIGHT(node, __entry);
100 }
101 return (parent);
102 }
103
104 static inline struct rb_node *
105 rb_next_postorder(const struct rb_node *node)
106 {
107 struct rb_node *parent =
108 RB_PARENT(__DECONST(struct rb_node *, node), __entry);
109 /* left -> right, right -> root */
110 if (parent != NULL &&
111 (node == RB_LEFT(parent, __entry)) &&
112 (RB_RIGHT(parent, __entry)))
113 return (__rb_deepest_left(RB_RIGHT(parent, __entry)));
114 else
115 return (parent);
116 }
117
118 #define rbtree_postorder_for_each_entry_safe(x, y, head, member) \
119 for ((x) = rb_entry_safe(__rb_deepest_left((head)->rb_node), \
120 __typeof(*x), member); \
121 ((x) != NULL) && ((y) = \
122 rb_entry_safe(rb_next_postorder(&x->member), typeof(*x), member), 1); \
123 (x) = (y))
124
125 static inline void
126 rb_link_node(struct rb_node *node, struct rb_node *parent,
127 struct rb_node **rb_link)
128 {
129 RB_SET(node, parent, __entry);
130 *rb_link = node;
131 }
132
133 static inline void
134 rb_replace_node(struct rb_node *victim, struct rb_node *new,
135 struct rb_root *root)
136 {
137
138 RB_SWAP_CHILD((struct linux_root *)root, rb_parent(victim),
139 victim, new, __entry);
140 if (RB_LEFT(victim, __entry))
141 RB_SET_PARENT(RB_LEFT(victim, __entry), new, __entry);
142 if (RB_RIGHT(victim, __entry))
143 RB_SET_PARENT(RB_RIGHT(victim, __entry), new, __entry);
144 *new = *victim;
145 }
146
147 static inline void
148 rb_insert_color_cached(struct rb_node *node, struct rb_root_cached *root,
149 bool leftmost)
150 {
151 if (rb_parent(node))
152 linux_root_RB_INSERT_COLOR((struct linux_root *)&root->rb_root,
153 rb_parent(node), node);
154 if (leftmost)
155 root->rb_leftmost = node;
156 }
157
158 static inline struct rb_node *
159 rb_erase_cached(struct rb_node *node, struct rb_root_cached *root)
160 {
161 struct rb_node *retval;
162
163 if (node == root->rb_leftmost)
164 retval = root->rb_leftmost = linux_root_RB_NEXT(node);
165 else
166 retval = NULL;
167 linux_root_RB_REMOVE((struct linux_root *)&root->rb_root, node);
168 return (retval);
169 }
170
171 static inline void
172 rb_replace_node_cached(struct rb_node *old, struct rb_node *new,
173 struct rb_root_cached *root)
174 {
175 rb_replace_node(old, new, &root->rb_root);
176 if (root->rb_leftmost == old)
177 root->rb_leftmost = new;
178 }
179
180 #undef RB_ROOT
181 #define RB_ROOT (struct rb_root) { NULL }
182 #define RB_ROOT_CACHED (struct rb_root_cached) { RB_ROOT, NULL }
183
184 #endif /* _LINUXKPI_LINUX_RBTREE_H_ */
Cache object: 7d5ccdc4db73150ca09fac5217a4c03c
|