1 #include <kvm/rbtree-interval.h>
4 #define rb_int(n) rb_entry(n, struct rb_int_node, node)
6 struct rb_int_node
*rb_int_search_single(struct rb_root
*root
, u64 point
)
8 struct rb_node
*node
= root
->rb_node
;
9 struct rb_node
*lowest
= NULL
;
12 struct rb_int_node
*cur
= rb_int(node
);
14 if (node
->rb_left
&& (rb_int(node
->rb_left
)->max_high
> point
)) {
16 } else if (cur
->low
<= point
&& cur
->high
> point
) {
19 } else if (point
> cur
->low
) {
20 node
= node
->rb_right
;
29 return rb_int(lowest
);
32 struct rb_int_node
*rb_int_search_range(struct rb_root
*root
, u64 low
, u64 high
)
34 struct rb_int_node
*range
;
36 range
= rb_int_search_single(root
, low
);
40 /* We simply verify that 'high' is smaller than the end of the range where 'low' is located */
41 if (range
->high
< high
)
47 static void update_node_max_high(struct rb_node
*node
, void *arg
)
49 struct rb_int_node
*i_node
= rb_int(node
);
51 i_node
->max_high
= i_node
->high
;
54 i_node
->max_high
= max(i_node
->max_high
, rb_int(node
->rb_left
)->max_high
);
56 i_node
->max_high
= max(i_node
->max_high
, rb_int(node
->rb_right
)->max_high
);
59 int rb_int_insert(struct rb_root
*root
, struct rb_int_node
*i_node
)
61 struct rb_node
**node
= &(root
->rb_node
), *parent
= NULL
;
64 int result
= i_node
->low
- rb_int(*node
)->low
;
68 node
= &((*node
)->rb_left
);
70 node
= &((*node
)->rb_right
);
75 rb_link_node(&i_node
->node
, parent
, node
);
76 rb_insert_color(&i_node
->node
, root
);
78 rb_augment_insert(&i_node
->node
, update_node_max_high
, NULL
);
82 void rb_int_erase(struct rb_root
*root
, struct rb_int_node
*node
)
84 struct rb_node
*deepest
;
86 deepest
= rb_augment_erase_begin(&node
->node
);
87 rb_erase(&node
->node
, root
);
88 rb_augment_erase_end(deepest
, update_node_max_high
, NULL
);