xref: /freebsd/sys/compat/linuxkpi/common/include/linux/rbtree.h (revision b197d4b893974c9eb4d7b38704c6d5c486235d6f)
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_left
45 #define	rb_right	__entry.rbe_right
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)					\
78 	linux_root_RB_INSERT_COLOR((struct linux_root *)(root), (node))
79 #define	rb_erase(node, root)						\
80 	linux_root_RB_REMOVE((struct linux_root *)(root), (node))
81 #define	rb_next(node)	RB_NEXT(linux_root, NULL, (node))
82 #define	rb_prev(node)	RB_PREV(linux_root, NULL, (node))
83 #define	rb_first(root)	RB_MIN(linux_root, (struct linux_root *)(root))
84 #define	rb_last(root)	RB_MAX(linux_root, (struct linux_root *)(root))
85 #define	rb_first_cached(root)	(root)->rb_leftmost
86 
87 static inline struct rb_node *
88 __rb_deepest_left(struct rb_node *node)
89 {
90 	struct rb_node *parent = NULL;
91 	while (node != NULL) {
92 		parent = node;
93 		if (RB_LEFT(node, __entry))
94 			node = RB_LEFT(node, __entry);
95 		else
96 			node = RB_RIGHT(node, __entry);
97 	}
98 	return (parent);
99 }
100 
101 static inline struct rb_node *
102 rb_next_postorder(const struct rb_node *node)
103 {
104 	struct rb_node *parent =
105 	    RB_PARENT(__DECONST(struct rb_node *, node), __entry);
106 	/* left -> right, right -> root */
107 	if (parent != NULL &&
108 	    (node == RB_LEFT(parent, __entry)) &&
109 	    (RB_RIGHT(parent, __entry)))
110 		return (__rb_deepest_left(RB_RIGHT(parent, __entry)));
111 	else
112 		return (parent);
113 }
114 
115 #define	rbtree_postorder_for_each_entry_safe(x, y, head, member)	\
116 	for ((x) = rb_entry_safe(__rb_deepest_left((head)->rb_node),	\
117 	    __typeof(*x), member);					\
118 	    ((x) != NULL) && ((y) =					\
119 	    rb_entry_safe(rb_next_postorder(&x->member), typeof(*x), member), 1); \
120 	    (x) = (y))
121 
122 static inline void
123 rb_link_node(struct rb_node *node, struct rb_node *parent,
124     struct rb_node **rb_link)
125 {
126 	RB_SET(node, parent, __entry);
127 	*rb_link = node;
128 }
129 
130 static inline void
131 rb_replace_node(struct rb_node *victim, struct rb_node *new,
132     struct rb_root *root)
133 {
134 
135 	RB_SWAP_CHILD((struct linux_root *)root, rb_parent(victim),
136 	    victim, new, __entry);
137 	if (victim->rb_left)
138 		RB_SET_PARENT(victim->rb_left, new, __entry);
139 	if (victim->rb_right)
140 		RB_SET_PARENT(victim->rb_right, new, __entry);
141 	*new = *victim;
142 }
143 
144 static inline void
145 rb_insert_color_cached(struct rb_node *node, struct rb_root_cached *root,
146     bool leftmost)
147 {
148 	linux_root_RB_INSERT_COLOR((struct linux_root *)&root->rb_root, node);
149 	if (leftmost)
150 		root->rb_leftmost = node;
151 }
152 
153 static inline struct rb_node *
154 rb_erase_cached(struct rb_node *node, struct rb_root_cached *root)
155 {
156 	struct rb_node *retval;
157 
158 	if (node == root->rb_leftmost)
159 		retval = root->rb_leftmost = linux_root_RB_NEXT(node);
160 	else
161 		retval = NULL;
162 	linux_root_RB_REMOVE((struct linux_root *)&root->rb_root, node);
163 	return (retval);
164 }
165 
166 static inline void
167 rb_replace_node_cached(struct rb_node *old, struct rb_node *new,
168     struct rb_root_cached *root)
169 {
170 	rb_replace_node(old, new, &root->rb_root);
171 	if (root->rb_leftmost == old)
172 		root->rb_leftmost = new;
173 }
174 
175 #undef RB_ROOT
176 #define RB_ROOT		(struct rb_root) { NULL }
177 #define	RB_ROOT_CACHED	(struct rb_root_cached) { RB_ROOT, NULL }
178 
179 #endif	/* _LINUXKPI_LINUX_RBTREE_H_ */
180