1219820Sjeff/*-
2219820Sjeff * Copyright (c) 2010 Isilon Systems, Inc.
3219820Sjeff * Copyright (c) 2010 iX Systems, Inc.
4219820Sjeff * Copyright (c) 2010 Panasas, Inc.
5271127Shselasky * Copyright (c) 2013, 2014 Mellanox Technologies, Ltd.
6219820Sjeff * All rights reserved.
7219820Sjeff *
8219820Sjeff * Redistribution and use in source and binary forms, with or without
9219820Sjeff * modification, are permitted provided that the following conditions
10219820Sjeff * are met:
11219820Sjeff * 1. Redistributions of source code must retain the above copyright
12219820Sjeff *    notice unmodified, this list of conditions, and the following
13219820Sjeff *    disclaimer.
14219820Sjeff * 2. Redistributions in binary form must reproduce the above copyright
15219820Sjeff *    notice, this list of conditions and the following disclaimer in the
16219820Sjeff *    documentation and/or other materials provided with the distribution.
17219820Sjeff *
18219820Sjeff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19219820Sjeff * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20219820Sjeff * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21219820Sjeff * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22219820Sjeff * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23219820Sjeff * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24219820Sjeff * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25219820Sjeff * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26219820Sjeff * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27219820Sjeff * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28219820Sjeff */
29219820Sjeff#ifndef	_LINUX_RBTREE_H_
30219820Sjeff#define	_LINUX_RBTREE_H_
31219820Sjeff
32219820Sjeff#include <sys/stddef.h>
33219820Sjeff#include <sys/tree.h>
34219820Sjeff
35219820Sjeffstruct rb_node {
36219820Sjeff	RB_ENTRY(rb_node)	__entry;
37219820Sjeff};
38219820Sjeff#define	rb_left		__entry.rbe_left
39219820Sjeff#define	rb_right	__entry.rbe_right
40219820Sjeff
41219820Sjeff/*
42219820Sjeff * We provide a false structure that has the same bit pattern as tree.h
43219820Sjeff * presents so it matches the member names expected by linux.
44219820Sjeff */
45219820Sjeffstruct rb_root {
46219820Sjeff	struct	rb_node	*rb_node;
47219820Sjeff};
48219820Sjeff
49219820Sjeff/*
50219820Sjeff * In linux all of the comparisons are done by the caller.
51219820Sjeff */
52219820Sjeffint panic_cmp(struct rb_node *one, struct rb_node *two);
53219820Sjeff
54219820SjeffRB_HEAD(linux_root, rb_node);
55219820SjeffRB_PROTOTYPE(linux_root, rb_node, __entry, panic_cmp);
56219820Sjeff
57219820Sjeff#define	rb_parent(r)	RB_PARENT(r, __entry)
58219820Sjeff#define	rb_color(r)	RB_COLOR(r, __entry)
59219820Sjeff#define	rb_is_red(r)	(rb_color(r) == RB_RED)
60219820Sjeff#define	rb_is_black(r)	(rb_color(r) == RB_BLACK)
61219820Sjeff#define	rb_set_parent(r, p)	rb_parent((r)) = (p)
62219820Sjeff#define	rb_set_color(r, c)	rb_color((r)) = (c)
63219820Sjeff#define	rb_entry(ptr, type, member)	container_of(ptr, type, member)
64219820Sjeff
65219820Sjeff#define RB_EMPTY_ROOT(root)     RB_EMPTY((struct linux_root *)root)
66219820Sjeff#define RB_EMPTY_NODE(node)     (rb_parent(node) == node)
67219820Sjeff#define RB_CLEAR_NODE(node)     (rb_set_parent(node, node))
68219820Sjeff
69219820Sjeff#define	rb_insert_color(node, root)					\
70219820Sjeff	linux_root_RB_INSERT_COLOR((struct linux_root *)(root), (node))
71219820Sjeff#define	rb_erase(node, root)						\
72219820Sjeff	linux_root_RB_REMOVE((struct linux_root *)(root), (node))
73219820Sjeff#define	rb_next(node)	RB_NEXT(linux_root, NULL, (node))
74219820Sjeff#define	rb_prev(node)	RB_PREV(linux_root, NULL, (node))
75219820Sjeff#define	rb_first(root)	RB_MIN(linux_root, (struct linux_root *)(root))
76219820Sjeff#define	rb_last(root)	RB_MAX(linux_root, (struct linux_root *)(root))
77219820Sjeff
78219820Sjeffstatic inline void
79219820Sjeffrb_link_node(struct rb_node *node, struct rb_node *parent,
80219820Sjeff    struct rb_node **rb_link)
81219820Sjeff{
82219820Sjeff	rb_set_parent(node, parent);
83219820Sjeff	rb_set_color(node, RB_RED);
84219820Sjeff	node->__entry.rbe_left = node->__entry.rbe_right = NULL;
85219820Sjeff	*rb_link = node;
86219820Sjeff}
87219820Sjeff
88219820Sjeffstatic inline void
89219820Sjeffrb_replace_node(struct rb_node *victim, struct rb_node *new,
90219820Sjeff    struct rb_root *root)
91219820Sjeff{
92219820Sjeff	struct rb_node *p;
93219820Sjeff
94219820Sjeff	p = rb_parent(victim);
95219820Sjeff	if (p) {
96219820Sjeff		if (p->rb_left == victim)
97219820Sjeff			p->rb_left = new;
98219820Sjeff		else
99219820Sjeff			p->rb_right = new;
100219820Sjeff	} else
101219820Sjeff		root->rb_node = new;
102219820Sjeff	if (victim->rb_left)
103219820Sjeff		rb_set_parent(victim->rb_left, new);
104219820Sjeff	if (victim->rb_right)
105219820Sjeff		rb_set_parent(victim->rb_right, new);
106219820Sjeff	*new = *victim;
107219820Sjeff}
108219820Sjeff
109219820Sjeff#undef RB_ROOT
110219820Sjeff#define RB_ROOT		(struct rb_root) { NULL }
111219820Sjeff
112219820Sjeff#endif	/* _LINUX_RBTREE_H_ */
113