Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef _LINUX_INTERVAL_TREE_H
3#define _LINUX_INTERVAL_TREE_H
4
5#include <linux/rbtree.h>
6
7struct interval_tree_node {
8 struct rb_node rb;
9 unsigned long start; /* Start of interval */
10 unsigned long last; /* Last location _in_ interval */
11 unsigned long __subtree_last;
12};
13
14extern void
15interval_tree_insert(struct interval_tree_node *node,
16 struct rb_root_cached *root);
17
18extern void
19interval_tree_remove(struct interval_tree_node *node,
20 struct rb_root_cached *root);
21
22extern struct interval_tree_node *
23interval_tree_subtree_search(struct interval_tree_node *node,
24 unsigned long start, unsigned long last);
25
26extern struct interval_tree_node *
27interval_tree_iter_first(struct rb_root_cached *root,
28 unsigned long start, unsigned long last);
29
30extern struct interval_tree_node *
31interval_tree_iter_next(struct interval_tree_node *node,
32 unsigned long start, unsigned long last);
33
34/**
35 * struct interval_tree_span_iter - Find used and unused spans.
36 * @start_hole: Start of an interval for a hole when is_hole == 1
37 * @last_hole: Inclusive end of an interval for a hole when is_hole == 1
38 * @start_used: Start of a used interval when is_hole == 0
39 * @last_used: Inclusive end of a used interval when is_hole == 0
40 * @is_hole: 0 == used, 1 == is_hole, -1 == done iteration
41 *
42 * This iterator travels over spans in an interval tree. It does not return
43 * nodes but classifies each span as either a hole, where no nodes intersect, or
44 * a used, which is fully covered by nodes. Each iteration step toggles between
45 * hole and used until the entire range is covered. The returned spans always
46 * fully cover the requested range.
47 *
48 * The iterator is greedy, it always returns the largest hole or used possible,
49 * consolidating all consecutive nodes.
50 *
51 * Use interval_tree_span_iter_done() to detect end of iteration.
52 */
53struct interval_tree_span_iter {
54 /* private: not for use by the caller */
55 struct interval_tree_node *nodes[2];
56 unsigned long first_index;
57 unsigned long last_index;
58
59 /* public: */
60 union {
61 unsigned long start_hole;
62 unsigned long start_used;
63 };
64 union {
65 unsigned long last_hole;
66 unsigned long last_used;
67 };
68 int is_hole;
69};
70
71void interval_tree_span_iter_first(struct interval_tree_span_iter *state,
72 struct rb_root_cached *itree,
73 unsigned long first_index,
74 unsigned long last_index);
75void interval_tree_span_iter_advance(struct interval_tree_span_iter *iter,
76 struct rb_root_cached *itree,
77 unsigned long new_index);
78void interval_tree_span_iter_next(struct interval_tree_span_iter *state);
79
80static inline bool
81interval_tree_span_iter_done(struct interval_tree_span_iter *state)
82{
83 return state->is_hole == -1;
84}
85
86#define interval_tree_for_each_span(span, itree, first_index, last_index) \
87 for (interval_tree_span_iter_first(span, itree, \
88 first_index, last_index); \
89 !interval_tree_span_iter_done(span); \
90 interval_tree_span_iter_next(span))
91
92#endif /* _LINUX_INTERVAL_TREE_H */