1
2
3
4
5
6
7
8
9
10 #include <linux/rbtree_augmented.h>
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 #define INTERVAL_TREE_DEFINE(ITSTRUCT, ITRB, ITTYPE, ITSUBTREE, \
29 ITSTART, ITLAST, ITSTATIC, ITPREFIX) \
30 \
31 \
32 \
33 RB_DECLARE_CALLBACKS_MAX(static, ITPREFIX ## _augment, \
34 ITSTRUCT, ITRB, ITTYPE, ITSUBTREE, ITLAST) \
35 \
36 \
37 \
38 ITSTATIC void ITPREFIX ## _insert(ITSTRUCT *node, \
39 struct rb_root_cached *root) \
40 { \
41 struct rb_node **link = &root->rb_root.rb_node, *rb_parent = NULL; \
42 ITTYPE start = ITSTART(node), last = ITLAST(node); \
43 ITSTRUCT *parent; \
44 bool leftmost = true; \
45 \
46 while (*link) { \
47 rb_parent = *link; \
48 parent = rb_entry(rb_parent, ITSTRUCT, ITRB); \
49 if (parent->ITSUBTREE < last) \
50 parent->ITSUBTREE = last; \
51 if (start < ITSTART(parent)) \
52 link = &parent->ITRB.rb_left; \
53 else { \
54 link = &parent->ITRB.rb_right; \
55 leftmost = false; \
56 } \
57 } \
58 \
59 node->ITSUBTREE = last; \
60 rb_link_node(&node->ITRB, rb_parent, link); \
61 rb_insert_augmented_cached(&node->ITRB, root, \
62 leftmost, &ITPREFIX ## _augment); \
63 } \
64 \
65 ITSTATIC void ITPREFIX ## _remove(ITSTRUCT *node, \
66 struct rb_root_cached *root) \
67 { \
68 rb_erase_augmented_cached(&node->ITRB, root, &ITPREFIX ## _augment); \
69 } \
70 \
71
72
73
74
75
76
77
78 \
79 \
80 static ITSTRUCT * \
81 ITPREFIX ## _subtree_search(ITSTRUCT *node, ITTYPE start, ITTYPE last) \
82 { \
83 while (true) { \
84
85
86
87 \
88 if (node->ITRB.rb_left) { \
89 ITSTRUCT *left = rb_entry(node->ITRB.rb_left, \
90 ITSTRUCT, ITRB); \
91 if (start <= left->ITSUBTREE) { \
92
93
94
95
96
97
98
99 \
100 node = left; \
101 continue; \
102 } \
103 } \
104 if (ITSTART(node) <= last) { \
105 if (start <= ITLAST(node)) \
106 return node; \
107 if (node->ITRB.rb_right) { \
108 node = rb_entry(node->ITRB.rb_right, \
109 ITSTRUCT, ITRB); \
110 if (start <= node->ITSUBTREE) \
111 continue; \
112 } \
113 } \
114 return NULL; \
115 } \
116 } \
117 \
118 ITSTATIC ITSTRUCT * \
119 ITPREFIX ## _iter_first(struct rb_root_cached *root, \
120 ITTYPE start, ITTYPE last) \
121 { \
122 ITSTRUCT *node, *leftmost; \
123 \
124 if (!root->rb_root.rb_node) \
125 return NULL; \
126 \
127
128
129
130
131
132
133
134
135
136
137
138
139 \
140 node = rb_entry(root->rb_root.rb_node, ITSTRUCT, ITRB); \
141 if (node->ITSUBTREE < start) \
142 return NULL; \
143 \
144 leftmost = rb_entry(root->rb_leftmost, ITSTRUCT, ITRB); \
145 if (ITSTART(leftmost) > last) \
146 return NULL; \
147 \
148 return ITPREFIX ## _subtree_search(node, start, last); \
149 } \
150 \
151 ITSTATIC ITSTRUCT * \
152 ITPREFIX ## _iter_next(ITSTRUCT *node, ITTYPE start, ITTYPE last) \
153 { \
154 struct rb_node *rb = node->ITRB.rb_right, *prev; \
155 \
156 while (true) { \
157
158
159
160
161
162
163 \
164 if (rb) { \
165 ITSTRUCT *right = rb_entry(rb, ITSTRUCT, ITRB); \
166 if (start <= right->ITSUBTREE) \
167 return ITPREFIX ## _subtree_search(right, \
168 start, last); \
169 } \
170 \
171 \
172 do { \
173 rb = rb_parent(&node->ITRB); \
174 if (!rb) \
175 return NULL; \
176 prev = &node->ITRB; \
177 node = rb_entry(rb, ITSTRUCT, ITRB); \
178 rb = node->ITRB.rb_right; \
179 } while (prev == rb); \
180 \
181 \
182 if (last < ITSTART(node)) \
183 return NULL; \
184 else if (start <= ITLAST(node)) \
185 return node; \
186 } \
187 }