1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94#ifndef _LINUX_RBTREE_H
95#define _LINUX_RBTREE_H
96
97#include <linux/kernel.h>
98#include <linux/stddef.h>
99
100struct rb_node
101{
102 unsigned long rb_parent_color;
103#define RB_RED 0
104#define RB_BLACK 1
105 struct rb_node *rb_right;
106 struct rb_node *rb_left;
107} __attribute__((aligned(sizeof(long))));
108
109
110struct rb_root
111{
112 struct rb_node *rb_node;
113};
114
115
116#define rb_parent(r) ((struct rb_node *)((r)->rb_parent_color & ~3))
117#define rb_color(r) ((r)->rb_parent_color & 1)
118#define rb_is_red(r) (!rb_color(r))
119#define rb_is_black(r) rb_color(r)
120#define rb_set_red(r) do { (r)->rb_parent_color &= ~1; } while (0)
121#define rb_set_black(r) do { (r)->rb_parent_color |= 1; } while (0)
122
123static inline void rb_set_parent(struct rb_node *rb, struct rb_node *p)
124{
125 rb->rb_parent_color = (rb->rb_parent_color & 3) | (unsigned long)p;
126}
127static inline void rb_set_color(struct rb_node *rb, int color)
128{
129 rb->rb_parent_color = (rb->rb_parent_color & ~1) | color;
130}
131
132#define RB_ROOT (struct rb_root) { NULL, }
133#define rb_entry(ptr, type, member) container_of(ptr, type, member)
134
135#define RB_EMPTY_ROOT(root) ((root)->rb_node == NULL)
136#define RB_EMPTY_NODE(node) (rb_parent(node) == node)
137#define RB_CLEAR_NODE(node) (rb_set_parent(node, node))
138
139static inline void rb_init_node(struct rb_node *rb)
140{
141 rb->rb_parent_color = 0;
142 rb->rb_right = NULL;
143 rb->rb_left = NULL;
144 RB_CLEAR_NODE(rb);
145}
146
147extern void rb_insert_color(struct rb_node *, struct rb_root *);
148extern void rb_erase(struct rb_node *, struct rb_root *);
149
150typedef void (*rb_augment_f)(struct rb_node *node, void *data);
151
152extern void rb_augment_insert(struct rb_node *node,
153 rb_augment_f func, void *data);
154extern struct rb_node *rb_augment_erase_begin(struct rb_node *node);
155extern void rb_augment_erase_end(struct rb_node *node,
156 rb_augment_f func, void *data);
157
158
159extern struct rb_node *rb_next(const struct rb_node *);
160extern struct rb_node *rb_prev(const struct rb_node *);
161extern struct rb_node *rb_first(const struct rb_root *);
162extern struct rb_node *rb_last(const struct rb_root *);
163
164
165extern void rb_replace_node(struct rb_node *victim, struct rb_node *new,
166 struct rb_root *root);
167
168static inline void rb_link_node(struct rb_node * node, struct rb_node * parent,
169 struct rb_node ** rb_link)
170{
171 node->rb_parent_color = (unsigned long )parent;
172 node->rb_left = node->rb_right = NULL;
173
174 *rb_link = node;
175}
176
177#endif
178