rbtree.h revision 270710
12061Sjkh/*- 250479Speter * Copyright (c) 2010 Isilon Systems, Inc. 32061Sjkh * Copyright (c) 2010 iX Systems, Inc. 438666Sjb * Copyright (c) 2010 Panasas, Inc. 532427Sjb * Copyright (c) 2013, 2014 Mellanox Technologies, Ltd. 638666Sjb * All rights reserved. 738666Sjb * 838666Sjb * Redistribution and use in source and binary forms, with or without 938666Sjb * modification, are permitted provided that the following conditions 1064049Salex * are met: 1164049Salex * 1. Redistributions of source code must retain the above copyright 1266071Smarkm * notice unmodified, this list of conditions, and the following 1338666Sjb * disclaimer. 1444918Sjkh * 2. Redistributions in binary form must reproduce the above copyright 1538666Sjb * notice, this list of conditions and the following disclaimer in the 1638666Sjb * documentation and/or other materials provided with the distribution. 1738666Sjb * 1838666Sjb * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1938666Sjb * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 2038666Sjb * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2138666Sjb * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 2238978Sjb * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2338978Sjb * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2432427Sjb * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2538666Sjb * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2638666Sjb * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2738666Sjb * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2838666Sjb */ 2938666Sjb#ifndef _LINUX_RBTREE_H_ 3038666Sjb#define _LINUX_RBTREE_H_ 3117308Speter 3238666Sjb#include <sys/stddef.h> 3338666Sjb#include <sys/tree.h> 3438666Sjb 3519175Sbdestruct rb_node { 3638666Sjb RB_ENTRY(rb_node) __entry; 3738666Sjb}; 3838042Sbde#define rb_left __entry.rbe_left 3939726Sjb#define rb_right __entry.rbe_right 4038666Sjb 4138666Sjb/* 4238042Sbde * We provide a false structure that has the same bit pattern as tree.h 4338666Sjb * presents so it matches the member names expected by linux. 4449315Shoek */ 4517308Speterstruct rb_root { 4638666Sjb struct rb_node *rb_node; 4738666Sjb}; 4838666Sjb 4938666Sjb/* 5017308Speter * In linux all of the comparisons are done by the caller. 5145108Sobrien */ 5242128Speterint panic_cmp(struct rb_node *one, struct rb_node *two); 5342128Speter 5438666SjbRB_HEAD(linux_root, rb_node); 5551361SjbRB_PROTOTYPE(linux_root, rb_node, __entry, panic_cmp); 5638666Sjb 5717308Speter#define rb_parent(r) RB_PARENT(r, __entry) 5838666Sjb#define rb_color(r) RB_COLOR(r, __entry) 5917308Speter#define rb_is_red(r) (rb_color(r) == RB_RED) 6038666Sjb#define rb_is_black(r) (rb_color(r) == RB_BLACK) 6117308Speter#define rb_set_parent(r, p) rb_parent((r)) = (p) 6227910Sasami#define rb_set_color(r, c) rb_color((r)) = (c) 6343226Sjkh#define rb_entry(ptr, type, member) container_of(ptr, type, member) 6443226Sjkh 6543226Sjkh#define RB_EMPTY_ROOT(root) RB_EMPTY((struct linux_root *)root) 6638666Sjb#define RB_EMPTY_NODE(node) (rb_parent(node) == node) 6727910Sasami#define RB_CLEAR_NODE(node) (rb_set_parent(node, node)) 6838666Sjb 6938666Sjb#define rb_insert_color(node, root) \ 7038666Sjb linux_root_RB_INSERT_COLOR((struct linux_root *)(root), (node)) 7127910Sasami#define rb_erase(node, root) \ 7238666Sjb linux_root_RB_REMOVE((struct linux_root *)(root), (node)) 7338666Sjb#define rb_next(node) RB_NEXT(linux_root, NULL, (node)) 7443226Sjkh#define rb_prev(node) RB_PREV(linux_root, NULL, (node)) 7543226Sjkh#define rb_first(root) RB_MIN(linux_root, (struct linux_root *)(root)) 7627910Sasami#define rb_last(root) RB_MAX(linux_root, (struct linux_root *)(root)) 7738666Sjb 7838666Sjbstatic inline void 7927910Sasamirb_link_node(struct rb_node *node, struct rb_node *parent, 8038666Sjb struct rb_node **rb_link) 8127910Sasami{ 8217308Speter rb_set_parent(node, parent); 8338666Sjb rb_set_color(node, RB_RED); 8438666Sjb node->__entry.rbe_left = node->__entry.rbe_right = NULL; 8517308Speter *rb_link = node; 8655678Smarcel} 8755678Smarcel 8866071Smarkmstatic inline void 8966071Smarkmrb_replace_node(struct rb_node *victim, struct rb_node *new, 9066071Smarkm struct rb_root *root) 912061Sjkh{ 9255026Smarcel struct rb_node *p; 9355026Smarcel 9454324Smarcel p = rb_parent(victim); 9517308Speter if (p) { 9638666Sjb if (p->rb_left == victim) 9717308Speter p->rb_left = new; 9855678Smarcel else 9938666Sjb p->rb_right = new; 10054324Smarcel } else 1012302Spaul root->rb_node = new; 10239206Sjkh if (victim->rb_left) 10339206Sjkh rb_set_parent(victim->rb_left, new); 10439206Sjkh if (victim->rb_right) 10554324Smarcel rb_set_parent(victim->rb_right, new); 10617308Speter *new = *victim; 10754324Smarcel} 10854324Smarcel 10954324Smarcel#undef RB_ROOT 11054324Smarcel#define RB_ROOT (struct rb_root) { NULL } 11154324Smarcel 11254324Smarcel#endif /* _LINUX_RBTREE_H_ */ 11354324Smarcel