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