xref: /openbsd-src/sys/dev/pci/drm/include/linux/rbtree.h (revision 1a8dbaac879b9f3335ad7fb25429ce63ac1d6bac)
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	_LINUX_RBTREE_H_
30 #define	_LINUX_RBTREE_H_
31 
32 #include <sys/tree.h>
33 
34 struct rb_node {
35 	RB_ENTRY(rb_node)	__entry;
36 };
37 #define	rb_left		__entry.rbe_left
38 #define	rb_right	__entry.rbe_right
39 
40 /*
41  * We provide a false structure that has the same bit pattern as tree.h
42  * presents so it matches the member names expected by linux.
43  */
44 struct rb_root {
45 	struct	rb_node	*rb_node;
46 };
47 
48 struct rb_root_cached {
49 	struct rb_root rb_root;
50 };
51 
52 /*
53  * In linux all of the comparisons are done by the caller.
54  */
55 int panic_cmp(struct rb_node *one, struct rb_node *two);
56 
57 RB_HEAD(linux_root, rb_node);
58 RB_PROTOTYPE(linux_root, rb_node, __entry, panic_cmp);
59 
60 #define	rb_parent(r)	RB_PARENT(r, __entry)
61 #define	rb_color(r)	RB_COLOR(r, __entry)
62 #define	rb_is_red(r)	(rb_color(r) == RB_RED)
63 #define	rb_is_black(r)	(rb_color(r) == RB_BLACK)
64 #define	rb_set_parent(r, p)	rb_parent((r)) = (p)
65 #define	rb_set_color(r, c)	rb_color((r)) = (c)
66 #define	rb_entry(ptr, type, member)	container_of(ptr, type, member)
67 #define	rb_entry_safe(ptr, type, member) \
68 	(ptr ? rb_entry(ptr, type, member) : NULL)
69 
70 #define RB_EMPTY_ROOT(root)	((root)->rb_node == NULL)
71 #define RB_EMPTY_NODE(node)     (rb_parent(node) == node)
72 #define RB_CLEAR_NODE(node)     (rb_set_parent(node, node))
73 
74 #define	rb_insert_color(node, root)					\
75 	linux_root_RB_INSERT_COLOR((struct linux_root *)(root), (node))
76 #define	rb_erase(node, root)						\
77 	linux_root_RB_REMOVE((struct linux_root *)(root), (node))
78 #define	rb_next(node)	RB_NEXT(linux_root, NULL, (node))
79 #define	rb_prev(node)	RB_PREV(linux_root, NULL, (node))
80 #define	rb_first(root)	RB_MIN(linux_root, (struct linux_root *)(root))
81 #define	rb_last(root)	RB_MAX(linux_root, (struct linux_root *)(root))
82 
83 #define	rb_insert_color_cached(node, root, leftmost)			\
84 	linux_root_RB_INSERT_COLOR((struct linux_root *)(&(root)->rb_root), (node))
85 #define	rb_erase_cached(node, root)						\
86 	linux_root_RB_REMOVE((struct linux_root *)(&(root)->rb_root), (node))
87 #define	rb_first_cached(root)	RB_MIN(linux_root, (struct linux_root *)(&(root)->rb_root))
88 
89 static inline struct rb_node *
90 __rb_deepest_left(struct rb_node *node)
91 {
92 	struct rb_node *parent = NULL;
93 	while (node) {
94 		parent = node;
95 		if (RB_LEFT(node, __entry))
96 			node = RB_LEFT(node, __entry);
97 		else
98 			node = RB_RIGHT(node, __entry);
99 	}
100 	return parent;
101 }
102 
103 static inline struct rb_node *
104 rb_next_postorder(const struct rb_node *node)
105 {
106 	struct rb_node *parent = RB_PARENT(node, __entry);
107 	/* left -> right, right -> root */
108 	if (parent != NULL &&
109 	    (node == RB_LEFT(parent, __entry)) &&
110 	    (RB_RIGHT(parent, __entry)))
111 		return __rb_deepest_left(RB_RIGHT(parent, __entry));
112 	else
113 		return parent;
114 }
115 
116 #define	rbtree_postorder_for_each_entry_safe(x, y, head, member)	\
117 	for ((x) = rb_entry_safe(__rb_deepest_left((head)->rb_node),	\
118 	    __typeof(*x), member);					\
119 	    ((x) != NULL) && ((y) =					\
120 	    rb_entry_safe(rb_next_postorder(&x->member), typeof(*x), member), 1); \
121 	    (x) = (y))
122 
123 static inline void
124 rb_link_node(struct rb_node *node, struct rb_node *parent,
125     struct rb_node **rb_link)
126 {
127 	rb_set_parent(node, parent);
128 	rb_set_color(node, RB_RED);
129 	node->__entry.rbe_left = node->__entry.rbe_right = NULL;
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 	struct rb_node *p;
138 
139 	p = rb_parent(victim);
140 	if (p) {
141 		if (p->rb_left == victim)
142 			p->rb_left = new;
143 		else
144 			p->rb_right = new;
145 	} else
146 		root->rb_node = new;
147 	if (victim->rb_left)
148 		rb_set_parent(victim->rb_left, new);
149 	if (victim->rb_right)
150 		rb_set_parent(victim->rb_right, new);
151 	*new = *victim;
152 }
153 
154 #undef RB_ROOT
155 #define RB_ROOT		(struct rb_root) { NULL }
156 #ifdef __clang__
157 #define RB_ROOT_CACHED	(struct rb_root_cached) { NULL }
158 #else
159 #define RB_ROOT_CACHED	(struct rb_root_cached) { { NULL } }
160 #endif
161 
162 #endif	/* _LINUX_RBTREE_H_ */
163