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 /* for printf */ 33 #include <sys/types.h> 34 #include <sys/systm.h> 35 36 #include <sys/tree.h> 37 38 struct rb_node { 39 RB_ENTRY(rb_node) __entry; 40 }; 41 #define rb_left __entry.rbe_left 42 #define rb_right __entry.rbe_right 43 44 /* 45 * We provide a false structure that has the same bit pattern as tree.h 46 * presents so it matches the member names expected by linux. 47 */ 48 struct rb_root { 49 struct rb_node *rb_node; 50 }; 51 52 struct rb_root_cached { 53 struct rb_root rb_root; 54 }; 55 56 /* 57 * In linux all of the comparisons are done by the caller. 58 */ 59 int panic_cmp(struct rb_node *one, struct rb_node *two); 60 61 RB_HEAD(linux_root, rb_node); 62 RB_PROTOTYPE(linux_root, rb_node, __entry, panic_cmp); 63 64 #define rb_parent(r) RB_PARENT(r, __entry) 65 #define rb_color(r) RB_COLOR(r, __entry) 66 #define rb_is_red(r) (rb_color(r) == RB_RED) 67 #define rb_is_black(r) (rb_color(r) == RB_BLACK) 68 #define rb_set_parent(r, p) rb_parent((r)) = (p) 69 #define rb_set_color(r, c) rb_color((r)) = (c) 70 #define rb_entry(ptr, type, member) container_of(ptr, type, member) 71 #define rb_entry_safe(ptr, type, member) \ 72 (ptr ? rb_entry(ptr, type, member) : NULL) 73 74 #define RB_EMPTY_ROOT(root) ((root)->rb_node == NULL) 75 #define RB_EMPTY_NODE(node) (rb_parent(node) == node) 76 #define RB_CLEAR_NODE(node) (rb_set_parent(node, node)) 77 78 #define rb_insert_color(node, root) \ 79 linux_root_RB_INSERT_COLOR((struct linux_root *)(root), (node)) 80 #define rb_erase(node, root) \ 81 linux_root_RB_REMOVE((struct linux_root *)(root), (node)) 82 #define rb_next(node) RB_NEXT(linux_root, NULL, (node)) 83 #define rb_prev(node) RB_PREV(linux_root, NULL, (node)) 84 #define rb_first(root) RB_MIN(linux_root, (struct linux_root *)(root)) 85 #define rb_last(root) RB_MAX(linux_root, (struct linux_root *)(root)) 86 87 #define rb_insert_color_cached(node, root, leftmost) \ 88 linux_root_RB_INSERT_COLOR((struct linux_root *)(&(root)->rb_root), (node)) 89 #define rb_erase_cached(node, root) \ 90 linux_root_RB_REMOVE((struct linux_root *)(&(root)->rb_root), (node)) 91 #define rb_first_cached(root) RB_MIN(linux_root, (struct linux_root *)(&(root)->rb_root)) 92 93 static inline struct rb_node * 94 __rb_deepest_left(struct rb_node *node) 95 { 96 struct rb_node *parent = NULL; 97 while (node) { 98 parent = node; 99 if (RB_LEFT(node, __entry)) 100 node = RB_LEFT(node, __entry); 101 else 102 node = RB_RIGHT(node, __entry); 103 } 104 return parent; 105 } 106 107 static inline struct rb_node * 108 rb_next_postorder(const struct rb_node *node) 109 { 110 struct rb_node *parent = RB_PARENT(node, __entry); 111 /* left -> right, right -> root */ 112 if (parent != NULL && 113 (node == RB_LEFT(parent, __entry)) && 114 (RB_RIGHT(parent, __entry))) 115 return __rb_deepest_left(RB_RIGHT(parent, __entry)); 116 else 117 return parent; 118 } 119 120 #define rbtree_postorder_for_each_entry_safe(x, y, head, member) \ 121 for ((x) = rb_entry_safe(__rb_deepest_left((head)->rb_node), \ 122 __typeof(*x), member); \ 123 ((x) != NULL) && ((y) = \ 124 rb_entry_safe(rb_next_postorder(&x->member), typeof(*x), member), 1); \ 125 (x) = (y)) 126 127 static inline void 128 rb_link_node(struct rb_node *node, struct rb_node *parent, 129 struct rb_node **rb_link) 130 { 131 rb_set_parent(node, parent); 132 rb_set_color(node, RB_RED); 133 node->__entry.rbe_left = node->__entry.rbe_right = NULL; 134 *rb_link = node; 135 } 136 137 static inline void 138 rb_replace_node(struct rb_node *victim, struct rb_node *new, 139 struct rb_root *root) 140 { 141 struct rb_node *p; 142 143 p = rb_parent(victim); 144 if (p) { 145 if (p->rb_left == victim) 146 p->rb_left = new; 147 else 148 p->rb_right = new; 149 } else 150 root->rb_node = new; 151 if (victim->rb_left) 152 rb_set_parent(victim->rb_left, new); 153 if (victim->rb_right) 154 rb_set_parent(victim->rb_right, new); 155 *new = *victim; 156 } 157 158 #undef RB_ROOT 159 #define RB_ROOT (struct rb_root) { NULL } 160 #ifdef __clang__ 161 #define RB_ROOT_CACHED (struct rb_root_cached) { NULL } 162 #else 163 #define RB_ROOT_CACHED (struct rb_root_cached) { { NULL } } 164 #endif 165 166 struct interval_tree_node { 167 struct rb_node rb; 168 unsigned long start; 169 unsigned long last; 170 }; 171 172 static inline struct interval_tree_node * 173 interval_tree_iter_first(struct rb_root_cached *root, 174 unsigned long start, unsigned long last) 175 { 176 #ifdef DRMDEBUG 177 printf("%s: stub start: 0x%lx last: 0x%lx\n", __func__, start, last); 178 #endif 179 return NULL; 180 } 181 182 static inline void 183 interval_tree_insert(struct interval_tree_node *node, struct rb_root_cached *root) 184 { 185 #ifdef DRMDEBUG 186 printf("%s: stub start: 0x%lx last: 0x%lx\n", __func__, node->start, node->last); 187 #endif 188 } 189 190 static inline void 191 interval_tree_remove(struct interval_tree_node *node, struct rb_root_cached *root) 192 { 193 #ifdef DRMDEBUG 194 printf("%s: stub start: 0x%lx last: 0x%lx\n", __func__, node->start, node->last); 195 #endif 196 } 197 198 #endif /* _LINUX_RBTREE_H_ */ 199