1 /*- 2 * Copyright (c) 2010 Isilon Systems, Inc. 3 * Copyright (c) 2010 iX Systems, Inc. 4 * Copyright (c) 2010 Panasas, Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice unmodified, this list of conditions, and the following 12 * disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 #ifndef _LINUX_RBTREE_H_ 29 #define _LINUX_RBTREE_H_ 30 31 #include <sys/tree.h> 32 33 struct rb_node { 34 RB_ENTRY(rb_node) __entry; 35 }; 36 #define rb_left __entry.rbe_left 37 #define rb_right __entry.rbe_right 38 39 /* 40 * We provide a false structure that has the same bit pattern as tree.h 41 * presents so it matches the member names expected by linux. 42 */ 43 struct rb_root { 44 struct rb_node *rb_node; 45 }; 46 47 /* 48 * In linux all of the comparisons are done by the caller. 49 */ 50 int panic_cmp(struct rb_node *one, struct rb_node *two); 51 52 RB_HEAD(linux_root, rb_node); 53 RB_PROTOTYPE(linux_root, rb_node, __entry, panic_cmp); 54 55 #define rb_parent(r) RB_PARENT(r, __entry) 56 #define rb_color(r) RB_COLOR(r, __entry) 57 #define rb_is_red(r) (rb_color(r) == RB_RED) 58 #define rb_is_black(r) (rb_color(r) == RB_BLACK) 59 #define rb_set_parent(r, p) rb_parent((r)) = (p) 60 #define rb_set_color(r, c) rb_color((r)) = (c) 61 #define rb_entry(ptr, type, member) container_of(ptr, type, member) 62 63 #define RB_EMPTY_ROOT(root) RB_EMPTY((struct linux_root *)root) 64 #define RB_EMPTY_NODE(node) (rb_parent(node) == node) 65 #define RB_CLEAR_NODE(node) (rb_set_parent(node, node)) 66 67 #define rb_insert_color(node, root) \ 68 linux_root_RB_INSERT_COLOR((struct linux_root *)(root), (node)) 69 #define rb_erase(node, root) \ 70 linux_root_RB_REMOVE((struct linux_root *)(root), (node)) 71 #define rb_next(node) RB_NEXT(linux_root, NULL, (node)) 72 #define rb_prev(node) RB_PREV(linux_root, NULL, (node)) 73 #define rb_first(root) RB_MIN(linux_root, (struct linux_root *)(root)) 74 #define rb_last(root) RB_MAX(linux_root, (struct linux_root *)(root)) 75 76 static inline void 77 rb_link_node(struct rb_node *node, struct rb_node *parent, 78 struct rb_node **rb_link) 79 { 80 rb_set_parent(node, parent); 81 rb_set_color(node, RB_RED); 82 node->__entry.rbe_left = node->__entry.rbe_right = NULL; 83 *rb_link = node; 84 } 85 86 static inline void 87 rb_replace_node(struct rb_node *victim, struct rb_node *new, 88 struct rb_root *root) 89 { 90 struct rb_node *p; 91 92 p = rb_parent(victim); 93 if (p) { 94 if (p->rb_left == victim) 95 p->rb_left = new; 96 else 97 p->rb_right = new; 98 } else 99 root->rb_node = new; 100 if (victim->rb_left) 101 rb_set_parent(victim->rb_left, new); 102 if (victim->rb_right) 103 rb_set_parent(victim->rb_right, new); 104 *new = *victim; 105 } 106 107 #undef RB_ROOT 108 #define RB_ROOT (struct rb_root) { NULL } 109 110 #endif /* _LINUX_RBTREE_H_ */ 111