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 #include <linux/list.h>
35 #include <linux/tree.h>
36 #include <sys/types.h>
37
38 #ifdef __cplusplus
39 #if __cplusplus
40 extern "C" {
41 #endif /* __cplusplus */
42 #endif /* __cplusplus */
43
44 struct rb_node {
45 RB_ENTRY(rb_node) __entry;
46 };
47 #define rb_left __entry.rbe_left
48 #define rb_right __entry.rbe_right
49 #define rb_parent_color __entry.rbe_color
50 /*
51 * We provide a false structure that has the same bit pattern as tree.h
52 * presents so it matches the member names expected by linux.
53 */
54 struct rb_root {
55 struct rb_node *rb_node;
56 };
57
58 struct rb_root_cached {
59 struct rb_root rb_root;
60 struct rb_node *rb_leftmost;
61 };
62
63 /*
64 * In linux all of the comparisons are done by the caller.
65 */
66 int panic_cmp(struct rb_node *one, struct rb_node *two);
67
68 RB_HEAD(linux_root, rb_node);
69 RB_PROTOTYPE(linux_root, rb_node, __entry, panic_cmp);
70
71 #define rb_parent(r) RB_PARENT(r, __entry)
72 #define rb_color(r) RB_COLOR(r, __entry)
73 #define rb_is_red(r) (rb_color(r) == RB_RED)
74 #define rb_is_black(r) (rb_color(r) == RB_BLACK)
75 #define rb_set_parent(r, p) rb_parent((r)) = (p)
76 #define rb_set_color(r, c) rb_color((r)) = (c)
77 #define rb_entry(ptr, type, member) LOS_DL_LIST_ENTRY(ptr, type, member)
78
79 #define RB_EMPTY_ROOT(root) RB_EMPTY((struct linux_root *)root)
80 #define RB_EMPTY_NODE(node) (rb_parent(node) == node)
81 #define RB_CLEAR_NODE(node) (rb_set_parent(node, node))
82
83 #define rb_insert_color(node, root) \
84 linux_root_RB_INSERT_COLOR((struct linux_root *)(root), (node))
85 #define rb_erase(node, root) \
86 (void)linux_root_RB_REMOVE((struct linux_root *)(root), (node))
87 #define rb_next(node) RB_NEXT(linux_root, NULL, (node))
88 #define rb_prev(node) RB_PREV(linux_root, NULL, (node))
89 #define rb_first(root) RB_MIN(linux_root, (struct linux_root *)(root))
90 #define rb_last(root) RB_MAX(linux_root, (struct linux_root *)(root))
91 #define rb_first_cached(root) (root)->rb_leftmost
92
93 static inline void
rb_link_node(struct rb_node * node,struct rb_node * parent,struct rb_node ** rb_link)94 rb_link_node(struct rb_node *node, struct rb_node *parent,
95 struct rb_node **rb_link)
96 {
97 rb_set_parent(node, parent);
98 rb_set_color(node, RB_RED);
99 node->__entry.rbe_left = node->__entry.rbe_right = NULL;
100 *rb_link = node;
101 }
102
103 static inline void
rb_replace_node(struct rb_node * victim,struct rb_node * newnode,struct rb_root * root)104 rb_replace_node(struct rb_node *victim, struct rb_node *newnode,
105 struct rb_root *root)
106 {
107 struct rb_node *p;
108
109 p = rb_parent(victim);
110 if (p) {
111 if (p->rb_left == victim)
112 p->rb_left = newnode;
113 else
114 p->rb_right = newnode;
115 } else
116 root->rb_node = newnode;
117 if (victim->rb_left)
118 rb_set_parent(victim->rb_left, newnode);
119 if (victim->rb_right)
120 rb_set_parent(victim->rb_right, newnode);
121 *newnode = *victim;
122 }
123
124 static inline void
rb_insert_color_cached(struct rb_node * node,struct rb_root_cached * root,bool leftmost)125 rb_insert_color_cached(struct rb_node *node, struct rb_root_cached *root,
126 bool leftmost)
127 {
128 linux_root_RB_INSERT_COLOR((struct linux_root *)&root->rb_root, node);
129 if (leftmost)
130 root->rb_leftmost = node;
131 }
132
133 static inline struct rb_node *
rb_erase_cached(struct rb_node * node,struct rb_root_cached * root)134 rb_erase_cached(struct rb_node *node, struct rb_root_cached *root)
135 {
136 struct rb_node *retval;
137
138 if (node == root->rb_leftmost)
139 retval = root->rb_leftmost = linux_root_RB_NEXT(node);
140 else
141 retval = NULL;
142 linux_root_RB_REMOVE((struct linux_root *)&root->rb_root, node);
143 return (retval);
144 }
145
146 static inline void
rb_replace_node_cached(struct rb_node * old,struct rb_node * new,struct rb_root_cached * root)147 rb_replace_node_cached(struct rb_node *old, struct rb_node *new,
148 struct rb_root_cached *root)
149 {
150 rb_replace_node(old, new, &root->rb_root);
151 if (root->rb_leftmost == old)
152 root->rb_leftmost = new;
153 }
154
155 #undef RB_ROOT
156 #define RB_ROOT (struct rb_root) { NULL }
157 #define RB_ROOT_CACHED (struct rb_root_cached) { RB_ROOT, NULL }
158
159 #ifdef __cplusplus
160 #if __cplusplus
161 }
162 #endif /* __cplusplus */
163 #endif /* __cplusplus */
164
165 #endif /* _LINUXKPI_LINUX_RBTREE_H_ */
166