1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_INTERVAL_TREE_H
3 #define _LINUX_INTERVAL_TREE_H
5 #include <linux/rbtree.h>
7 struct interval_tree_node
{
9 unsigned long start
; /* Start of interval */
10 unsigned long last
; /* Last location _in_ interval */
11 unsigned long __subtree_last
;
15 interval_tree_insert(struct interval_tree_node
*node
,
16 struct rb_root_cached
*root
);
19 interval_tree_remove(struct interval_tree_node
*node
,
20 struct rb_root_cached
*root
);
22 extern struct interval_tree_node
*
23 interval_tree_iter_first(struct rb_root_cached
*root
,
24 unsigned long start
, unsigned long last
);
26 extern struct interval_tree_node
*
27 interval_tree_iter_next(struct interval_tree_node
*node
,
28 unsigned long start
, unsigned long last
);
31 * struct interval_tree_span_iter - Find used and unused spans.
32 * @start_hole: Start of an interval for a hole when is_hole == 1
33 * @last_hole: Inclusive end of an interval for a hole when is_hole == 1
34 * @start_used: Start of a used interval when is_hole == 0
35 * @last_used: Inclusive end of a used interval when is_hole == 0
36 * @is_hole: 0 == used, 1 == is_hole, -1 == done iteration
38 * This iterator travels over spans in an interval tree. It does not return
39 * nodes but classifies each span as either a hole, where no nodes intersect, or
40 * a used, which is fully covered by nodes. Each iteration step toggles between
41 * hole and used until the entire range is covered. The returned spans always
42 * fully cover the requested range.
44 * The iterator is greedy, it always returns the largest hole or used possible,
45 * consolidating all consecutive nodes.
47 * Use interval_tree_span_iter_done() to detect end of iteration.
49 struct interval_tree_span_iter
{
50 /* private: not for use by the caller */
51 struct interval_tree_node
*nodes
[2];
52 unsigned long first_index
;
53 unsigned long last_index
;
57 unsigned long start_hole
;
58 unsigned long start_used
;
61 unsigned long last_hole
;
62 unsigned long last_used
;
67 void interval_tree_span_iter_first(struct interval_tree_span_iter
*state
,
68 struct rb_root_cached
*itree
,
69 unsigned long first_index
,
70 unsigned long last_index
);
71 void interval_tree_span_iter_advance(struct interval_tree_span_iter
*iter
,
72 struct rb_root_cached
*itree
,
73 unsigned long new_index
);
74 void interval_tree_span_iter_next(struct interval_tree_span_iter
*state
);
77 interval_tree_span_iter_done(struct interval_tree_span_iter
*state
)
79 return state
->is_hole
== -1;
82 #define interval_tree_for_each_span(span, itree, first_index, last_index) \
83 for (interval_tree_span_iter_first(span, itree, \
84 first_index, last_index); \
85 !interval_tree_span_iter_done(span); \
86 interval_tree_span_iter_next(span))
88 #endif /* _LINUX_INTERVAL_TREE_H */