kvm tools, setup: Create private directory
[linux-2.6/next.git] / tools / kvm / util / rbtree-interval.c
blobd02fbf0c469c08d2cff4fdcc521b566d436b1498
1 #include <kvm/rbtree-interval.h>
2 #include <stddef.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;
11 while (node) {
12 struct rb_int_node *cur = rb_int(node);
14 if (node->rb_left && (rb_int(node->rb_left)->max_high > point)) {
15 node = node->rb_left;
16 } else if (cur->low <= point && cur->high > point) {
17 lowest = node;
18 break;
19 } else if (point > cur->low) {
20 node = node->rb_right;
21 } else {
22 break;
26 if (lowest == NULL)
27 return NULL;
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);
37 if (range == NULL)
38 return NULL;
40 /* We simply verify that 'high' is smaller than the end of the range where 'low' is located */
41 if (range->high < high)
42 return NULL;
44 return range;
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;
53 if (node->rb_left)
54 i_node->max_high = max(i_node->max_high, rb_int(node->rb_left)->max_high);
55 if (node->rb_right)
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;
63 while (*node) {
64 int result = i_node->low - rb_int(*node)->low;
66 parent = *node;
67 if (result < 0)
68 node = &((*node)->rb_left);
69 else if (result > 0)
70 node = &((*node)->rb_right);
71 else
72 return 0;
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);
79 return 1;
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);