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
34struct 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 */
44struct rb_root {
45	struct	rb_node	*rb_node;
46};
47
48struct 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 */
55int panic_cmp(struct rb_node *one, struct rb_node *two);
56
57RB_HEAD(linux_root, rb_node);
58RB_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#define	rb_replace_node_cached(old, new, root)				\
89	rb_replace_node(old, new, &(root)->rb_root)
90
91static inline struct rb_node *
92__rb_deepest_left(struct rb_node *node)
93{
94	struct rb_node *parent = NULL;
95	while (node) {
96		parent = node;
97		if (RB_LEFT(node, __entry))
98			node = RB_LEFT(node, __entry);
99		else
100			node = RB_RIGHT(node, __entry);
101	}
102	return parent;
103}
104
105static inline struct rb_node *
106rb_next_postorder(const struct rb_node *node)
107{
108	struct rb_node *parent = RB_PARENT(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
125static inline void
126rb_link_node(struct rb_node *node, struct rb_node *parent,
127    struct rb_node **rb_link)
128{
129	rb_set_parent(node, parent);
130	rb_set_color(node, RB_RED);
131	node->__entry.rbe_left = node->__entry.rbe_right = NULL;
132	*rb_link = node;
133}
134
135static inline void
136rb_replace_node(struct rb_node *victim, struct rb_node *new,
137    struct rb_root *root)
138{
139	struct rb_node *p;
140
141	p = rb_parent(victim);
142	if (p) {
143		if (p->rb_left == victim)
144			p->rb_left = new;
145		else
146			p->rb_right = new;
147	} else
148		root->rb_node = new;
149	if (victim->rb_left)
150		rb_set_parent(victim->rb_left, new);
151	if (victim->rb_right)
152		rb_set_parent(victim->rb_right, new);
153	*new = *victim;
154}
155
156static inline void
157rb_add_cached(struct rb_node *node, struct rb_root_cached *root,
158    bool (*less)(struct rb_node *, const struct rb_node *))
159{
160	struct rb_node **iter = &root->rb_root.rb_node;
161	struct rb_node *parent = NULL;
162
163	while (*iter) {
164		parent = *iter;
165
166		if (less(node, parent))
167			iter = &(*iter)->rb_left;
168		else
169			iter = &(*iter)->rb_right;
170	}
171
172	rb_link_node(node, parent, iter);
173	rb_insert_color_cached(node, root, false);
174}
175
176#undef RB_ROOT
177#define RB_ROOT		(struct rb_root) { NULL }
178#ifdef __clang__
179#define RB_ROOT_CACHED	(struct rb_root_cached) { NULL }
180#else
181#define RB_ROOT_CACHED	(struct rb_root_cached) { { NULL } }
182#endif
183
184#endif	/* _LINUX_RBTREE_H_ */
185