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_node *rb_node; 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_insert_color_cached(node, root, leftmost) \ 81 linux_root_RB_INSERT_COLOR((struct linux_root *)(root), (node)) 82 #define rb_erase(node, root) \ 83 linux_root_RB_REMOVE((struct linux_root *)(root), (node)) 84 #define rb_erase_cached(node, root) \ 85 linux_root_RB_REMOVE((struct linux_root *)(root), (node)) 86 #define rb_next(node) RB_NEXT(linux_root, NULL, (node)) 87 #define rb_prev(node) RB_PREV(linux_root, NULL, (node)) 88 #define rb_first(root) RB_MIN(linux_root, (struct linux_root *)(root)) 89 #define rb_first_cached(root) RB_MIN(linux_root, (struct linux_root *)(root)) 90 #define rb_last(root) RB_MAX(linux_root, (struct linux_root *)(root)) 91 92 static inline struct rb_node * 93 __rb_deepest_left(struct rb_node *node) 94 { 95 struct rb_node *parent = NULL; 96 while (node) { 97 parent = node; 98 if (RB_LEFT(node, __entry)) 99 node = RB_LEFT(node, __entry); 100 else 101 node = RB_RIGHT(node, __entry); 102 } 103 return parent; 104 } 105 106 static inline struct rb_node * 107 rb_next_postorder(const struct rb_node *node) 108 { 109 struct rb_node *parent = RB_PARENT(node, __entry); 110 /* left -> right, right -> root */ 111 if (parent != NULL && 112 (node == RB_LEFT(parent, __entry)) && 113 (RB_RIGHT(parent, __entry))) 114 return __rb_deepest_left(RB_RIGHT(parent, __entry)); 115 else 116 return parent; 117 } 118 119 #define rbtree_postorder_for_each_entry_safe(x, y, head, member) \ 120 for ((x) = rb_entry_safe(__rb_deepest_left((head)->rb_node), \ 121 __typeof(*x), member); \ 122 ((x) != NULL) && ((y) = \ 123 rb_entry_safe(rb_next_postorder(&x->member), typeof(*x), member), 1); \ 124 (x) = (y)) 125 126 static inline void 127 rb_link_node(struct rb_node *node, struct rb_node *parent, 128 struct rb_node **rb_link) 129 { 130 rb_set_parent(node, parent); 131 rb_set_color(node, RB_RED); 132 node->__entry.rbe_left = node->__entry.rbe_right = NULL; 133 *rb_link = node; 134 } 135 136 static inline void 137 rb_replace_node(struct rb_node *victim, struct rb_node *new, 138 struct rb_root *root) 139 { 140 struct rb_node *p; 141 142 p = rb_parent(victim); 143 if (p) { 144 if (p->rb_left == victim) 145 p->rb_left = new; 146 else 147 p->rb_right = new; 148 } else 149 root->rb_node = new; 150 if (victim->rb_left) 151 rb_set_parent(victim->rb_left, new); 152 if (victim->rb_right) 153 rb_set_parent(victim->rb_right, new); 154 *new = *victim; 155 } 156 157 #undef RB_ROOT 158 #define RB_ROOT (struct rb_root) { NULL } 159 #define RB_ROOT_CACHED (struct rb_root_cached) { NULL } 160 161 struct interval_tree_node { 162 struct rb_node rb; 163 unsigned long start; 164 unsigned long last; 165 }; 166 167 static inline struct interval_tree_node * 168 interval_tree_iter_first(struct rb_root *root, 169 unsigned long start, unsigned long last) 170 { 171 #ifdef DRMDEBUG 172 printf("%s: stub start: 0x%lx last: 0x%lx\n", __func__, start, last); 173 #endif 174 return NULL; 175 } 176 177 static inline void 178 interval_tree_insert(struct interval_tree_node *node, struct rb_root *root) 179 { 180 #ifdef DRMDEBUG 181 printf("%s: stub start: 0x%lx last: 0x%lx\n", __func__, node->start, node->last); 182 #endif 183 } 184 185 static inline void 186 interval_tree_remove(struct interval_tree_node *node, struct rb_root *root) 187 { 188 #ifdef DRMDEBUG 189 printf("%s: stub start: 0x%lx last: 0x%lx\n", __func__, node->start, node->last); 190 #endif 191 } 192 193 #endif /* _LINUX_RBTREE_H_ */ 194