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: releng/12.2/sys/compat/linuxkpi/common/include/linux/rbtree.h 290135 2015-10-29 08:28:39Z hselasky $ 30 */ 31 #ifndef _LINUX_RBTREE_H_ 32 #define _LINUX_RBTREE_H_ 33 34 #include <linux/list.h> 35 #include <linux/tree.h> 36 37 #ifdef __cplusplus 38 #if __cplusplus 39 extern "C" { 40 #endif /* __cplusplus */ 41 #endif /* __cplusplus */ 42 43 struct rb_node { 44 RB_ENTRY(rb_node) __entry; 45 }; 46 #define rb_left __entry.rbe_left 47 #define rb_right __entry.rbe_right 48 #define rb_parent_color __entry.rbe_color 49 /* 50 * We provide a false structure that has the same bit pattern as tree.h 51 * presents so it matches the member names expected by linux. 52 */ 53 struct rb_root { 54 struct rb_node *rb_node; 55 }; 56 57 /* 58 * In linux all of the comparisons are done by the caller. 59 */ 60 int panic_cmp(struct rb_node *one, struct rb_node *two); 61 62 RB_HEAD(linux_root, rb_node); 63 RB_PROTOTYPE(linux_root, rb_node, __entry, panic_cmp); 64 65 #define rb_parent(r) RB_PARENT(r, __entry) 66 #define rb_color(r) RB_COLOR(r, __entry) 67 #define rb_is_red(r) (rb_color(r) == RB_RED) 68 #define rb_is_black(r) (rb_color(r) == RB_BLACK) 69 #define rb_set_parent(r, p) rb_parent((r)) = (p) 70 #define rb_set_color(r, c) rb_color((r)) = (c) 71 #define rb_entry(ptr, type, member) LOS_DL_LIST_ENTRY(ptr, type, member) 72 73 #define RB_EMPTY_ROOT(root) RB_EMPTY((struct linux_root *)root) 74 #define RB_EMPTY_NODE(node) (rb_parent(node) == node) 75 #define RB_CLEAR_NODE(node) (rb_set_parent(node, node)) 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 (void)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 86 static inline void 87 rb_link_node(struct rb_node *node, struct rb_node *parent, 88 struct rb_node **rb_link) 89 { 90 rb_set_parent(node, parent); 91 rb_set_color(node, RB_RED); 92 node->__entry.rbe_left = node->__entry.rbe_right = NULL; 93 *rb_link = node; 94 } 95 96 static inline void 97 rb_replace_node(struct rb_node *victim, struct rb_node *newnode, 98 struct rb_root *root) 99 { 100 struct rb_node *p; 101 102 p = rb_parent(victim); 103 if (p) { 104 if (p->rb_left == victim) 105 p->rb_left = newnode; 106 else 107 p->rb_right = newnode; 108 } else 109 root->rb_node = newnode; 110 if (victim->rb_left) 111 rb_set_parent(victim->rb_left, newnode); 112 if (victim->rb_right) 113 rb_set_parent(victim->rb_right, newnode); 114 *newnode = *victim; 115 } 116 117 #undef RB_ROOT 118 #define RB_ROOT (struct rb_root) { NULL } 119 120 #ifdef __cplusplus 121 #if __cplusplus 122 } 123 #endif /* __cplusplus */ 124 #endif /* __cplusplus */ 125 126 #endif /* _LINUX_RBTREE_H_ */ 127