xref: /freebsd/sys/compat/linuxkpi/common/include/linux/rbtree.h (revision cfd6422a5217410fbd66f7a7a8a64d9d85e61229)
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	_LINUX_RBTREE_H_
32 #define	_LINUX_RBTREE_H_
33 
34 #ifndef _STANDALONE
35 #include <sys/stddef.h>
36 #endif
37 
38 #include <sys/tree.h>
39 
40 struct rb_node {
41 	RB_ENTRY(rb_node)	__entry;
42 };
43 #define	rb_left		__entry.rbe_left
44 #define	rb_right	__entry.rbe_right
45 
46 /*
47  * We provide a false structure that has the same bit pattern as tree.h
48  * presents so it matches the member names expected by linux.
49  */
50 struct rb_root {
51 	struct	rb_node	*rb_node;
52 };
53 
54 /*
55  * In linux all of the comparisons are done by the caller.
56  */
57 int panic_cmp(struct rb_node *one, struct rb_node *two);
58 
59 RB_HEAD(linux_root, rb_node);
60 RB_PROTOTYPE(linux_root, rb_node, __entry, panic_cmp);
61 
62 #define	rb_entry(ptr, type, member)	container_of(ptr, type, member)
63 
64 #define RB_EMPTY_ROOT(root)     RB_EMPTY((struct linux_root *)root)
65 #define RB_EMPTY_NODE(node)     (RB_PARENT(node, __entry) == node)
66 #define RB_CLEAR_NODE(node)     RB_SET_PARENT(node, node, __entry)
67 
68 #define	rb_insert_color(node, root)					\
69 	linux_root_RB_INSERT_COLOR((struct linux_root *)(root), (node))
70 #define	rb_erase(node, root)						\
71 	linux_root_RB_REMOVE((struct linux_root *)(root), (node))
72 #define	rb_next(node)	RB_NEXT(linux_root, NULL, (node))
73 #define	rb_prev(node)	RB_PREV(linux_root, NULL, (node))
74 #define	rb_first(root)	RB_MIN(linux_root, (struct linux_root *)(root))
75 #define	rb_last(root)	RB_MAX(linux_root, (struct linux_root *)(root))
76 
77 static inline void
78 rb_link_node(struct rb_node *node, struct rb_node *parent,
79     struct rb_node **rb_link)
80 {
81 	RB_SET(node, parent, __entry);
82 	*rb_link = node;
83 }
84 
85 static inline void
86 rb_replace_node(struct rb_node *victim, struct rb_node *new,
87     struct rb_root *root)
88 {
89 
90 	RB_SWAP_CHILD((struct linux_root *)root, victim, new, __entry);
91 	if (victim->rb_left)
92 		RB_SET_PARENT(victim->rb_left, new, __entry);
93 	if (victim->rb_right)
94 		RB_SET_PARENT(victim->rb_right, new, __entry);
95 	*new = *victim;
96 }
97 
98 #undef RB_ROOT
99 #define RB_ROOT		(struct rb_root) { NULL }
100 
101 #endif	/* _LINUX_RBTREE_H_ */
102