1219820Sjeff/*-
2219820Sjeff * Copyright (c) 2010 Isilon Systems, Inc.
3219820Sjeff * Copyright (c) 2010 iX Systems, Inc.
4219820Sjeff * Copyright (c) 2010 Panasas, Inc.
5270710Shselasky * 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.
28289644Shselasky *
29289644Shselasky * $FreeBSD$
30219820Sjeff */
31219820Sjeff#ifndef	_LINUX_RBTREE_H_
32219820Sjeff#define	_LINUX_RBTREE_H_
33219820Sjeff
34219820Sjeff#include <sys/stddef.h>
35219820Sjeff#include <sys/tree.h>
36219820Sjeff
37219820Sjeffstruct rb_node {
38219820Sjeff	RB_ENTRY(rb_node)	__entry;
39219820Sjeff};
40219820Sjeff#define	rb_left		__entry.rbe_left
41219820Sjeff#define	rb_right	__entry.rbe_right
42219820Sjeff
43219820Sjeff/*
44219820Sjeff * We provide a false structure that has the same bit pattern as tree.h
45219820Sjeff * presents so it matches the member names expected by linux.
46219820Sjeff */
47219820Sjeffstruct rb_root {
48219820Sjeff	struct	rb_node	*rb_node;
49219820Sjeff};
50219820Sjeff
51219820Sjeff/*
52219820Sjeff * In linux all of the comparisons are done by the caller.
53219820Sjeff */
54219820Sjeffint panic_cmp(struct rb_node *one, struct rb_node *two);
55219820Sjeff
56219820SjeffRB_HEAD(linux_root, rb_node);
57219820SjeffRB_PROTOTYPE(linux_root, rb_node, __entry, panic_cmp);
58219820Sjeff
59219820Sjeff#define	rb_parent(r)	RB_PARENT(r, __entry)
60219820Sjeff#define	rb_color(r)	RB_COLOR(r, __entry)
61219820Sjeff#define	rb_is_red(r)	(rb_color(r) == RB_RED)
62219820Sjeff#define	rb_is_black(r)	(rb_color(r) == RB_BLACK)
63219820Sjeff#define	rb_set_parent(r, p)	rb_parent((r)) = (p)
64219820Sjeff#define	rb_set_color(r, c)	rb_color((r)) = (c)
65219820Sjeff#define	rb_entry(ptr, type, member)	container_of(ptr, type, member)
66219820Sjeff
67219820Sjeff#define RB_EMPTY_ROOT(root)     RB_EMPTY((struct linux_root *)root)
68219820Sjeff#define RB_EMPTY_NODE(node)     (rb_parent(node) == node)
69219820Sjeff#define RB_CLEAR_NODE(node)     (rb_set_parent(node, node))
70219820Sjeff
71219820Sjeff#define	rb_insert_color(node, root)					\
72219820Sjeff	linux_root_RB_INSERT_COLOR((struct linux_root *)(root), (node))
73219820Sjeff#define	rb_erase(node, root)						\
74219820Sjeff	linux_root_RB_REMOVE((struct linux_root *)(root), (node))
75219820Sjeff#define	rb_next(node)	RB_NEXT(linux_root, NULL, (node))
76219820Sjeff#define	rb_prev(node)	RB_PREV(linux_root, NULL, (node))
77219820Sjeff#define	rb_first(root)	RB_MIN(linux_root, (struct linux_root *)(root))
78219820Sjeff#define	rb_last(root)	RB_MAX(linux_root, (struct linux_root *)(root))
79219820Sjeff
80219820Sjeffstatic inline void
81219820Sjeffrb_link_node(struct rb_node *node, struct rb_node *parent,
82219820Sjeff    struct rb_node **rb_link)
83219820Sjeff{
84219820Sjeff	rb_set_parent(node, parent);
85219820Sjeff	rb_set_color(node, RB_RED);
86219820Sjeff	node->__entry.rbe_left = node->__entry.rbe_right = NULL;
87219820Sjeff	*rb_link = node;
88219820Sjeff}
89219820Sjeff
90219820Sjeffstatic inline void
91219820Sjeffrb_replace_node(struct rb_node *victim, struct rb_node *new,
92219820Sjeff    struct rb_root *root)
93219820Sjeff{
94219820Sjeff	struct rb_node *p;
95219820Sjeff
96219820Sjeff	p = rb_parent(victim);
97219820Sjeff	if (p) {
98219820Sjeff		if (p->rb_left == victim)
99219820Sjeff			p->rb_left = new;
100219820Sjeff		else
101219820Sjeff			p->rb_right = new;
102219820Sjeff	} else
103219820Sjeff		root->rb_node = new;
104219820Sjeff	if (victim->rb_left)
105219820Sjeff		rb_set_parent(victim->rb_left, new);
106219820Sjeff	if (victim->rb_right)
107219820Sjeff		rb_set_parent(victim->rb_right, new);
108219820Sjeff	*new = *victim;
109219820Sjeff}
110219820Sjeff
111219820Sjeff#undef RB_ROOT
112219820Sjeff#define RB_ROOT		(struct rb_root) { NULL }
113219820Sjeff
114219820Sjeff#endif	/* _LINUX_RBTREE_H_ */
115