interval_tree.h revision 1.10 1 /* $NetBSD: interval_tree.h,v 1.10 2021/12/19 01:44:33 riastradh Exp $ */
2
3 /*-
4 * Copyright (c) 2018 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Taylor R. Campbell.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #ifndef _LINUX_INTERVAL_TREE_H_
33 #define _LINUX_INTERVAL_TREE_H_
34
35 #include <linux/rbtree.h>
36
37 struct interval_tree_node {
38 struct rb_node itn_node;
39 unsigned long start; /* inclusive */
40 unsigned long last; /* inclusive */
41 };
42
43 static inline int
44 interval_tree_compare_nodes(void *cookie, const void *va, const void *vb)
45 {
46 const struct interval_tree_node *na = va;
47 const struct interval_tree_node *nb = vb;
48
49 if (na->start < nb->start)
50 return -1;
51 if (na->start > nb->start)
52 return +1;
53 if (na->last < nb->last)
54 return -1;
55 if (na->last > nb->last)
56 return +1;
57 return 0;
58 }
59
60 static inline int
61 interval_tree_compare_key(void *cookie, const void *vn, const void *vk)
62 {
63 const struct interval_tree_node *n = vn;
64 const unsigned long *k = vk;
65
66 if (n->start < *k)
67 return -1;
68 if (*k < n->start)
69 return +1;
70 return 0;
71 }
72
73 static const rb_tree_ops_t interval_tree_ops = {
74 .rbto_compare_nodes = interval_tree_compare_nodes,
75 .rbto_compare_key = interval_tree_compare_key,
76 .rbto_node_offset = offsetof(struct interval_tree_node, itn_node),
77 };
78
79 static inline void
80 interval_tree_init(struct rb_root *root)
81 {
82
83 rb_tree_init(&root->rbr_tree, &interval_tree_ops);
84 }
85
86 static inline void
87 interval_tree_insert(struct interval_tree_node *node, struct rb_root *root)
88 {
89 struct interval_tree_node *collision __diagused;
90
91 collision = rb_tree_insert_node(&root->rbr_tree, node);
92 KASSERT(collision == node);
93 }
94
95 static inline void
96 interval_tree_remove(struct interval_tree_node *node, struct rb_root *root)
97 {
98
99 rb_tree_remove_node(&root->rbr_tree, node);
100 }
101
102 static inline struct interval_tree_node *
103 interval_tree_iter_first(struct rb_root *root, unsigned long start,
104 unsigned long last)
105 {
106 struct interval_tree_node *node;
107
108 node = rb_tree_find_node_geq(&root->rbr_tree, &start);
109 if (node == NULL)
110 return NULL;
111 if (last < node->start)
112 return NULL;
113 KASSERT(node->start <= last && node->last >= start);
114
115 return node;
116 }
117
118 /*
119 * XXX Linux's interval_tree_iter_next doesn't take the root as an
120 * argument, which makes this difficult. So we'll just patch those
121 * uses.
122 */
123 static inline struct interval_tree_node *
124 interval_tree_iter_next(struct rb_root *root, struct interval_tree_node *node,
125 unsigned long start, unsigned long last)
126 {
127 struct interval_tree_node *next;
128
129 KASSERT(node != NULL);
130 next = rb_tree_iterate(&root->rbr_tree, node, RB_DIR_RIGHT);
131 if (next == NULL)
132 return NULL;
133 if (last < next->start)
134 return NULL;
135 KASSERT(next->start <= last && next->last >= start);
136
137 return next;
138 }
139
140 #endif /* _LINUX_INTERVAL_TREE_H_ */
141