Home | History | Annotate | Line # | Download | only in linux
interval_tree.h revision 1.11
      1 /*	$NetBSD: interval_tree.h,v 1.11 2021/12/19 01:48:30 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_cached *root)
     81 {
     82 
     83 	rb_tree_init(&root->rb_root.rbr_tree, &interval_tree_ops);
     84 }
     85 
     86 static inline void
     87 interval_tree_insert(struct interval_tree_node *node,
     88     struct rb_root_cached *root)
     89 {
     90 	struct interval_tree_node *collision __diagused;
     91 
     92 	collision = rb_tree_insert_node(&root->rb_root.rbr_tree, node);
     93 	KASSERT(collision == node);
     94 }
     95 
     96 static inline void
     97 interval_tree_remove(struct interval_tree_node *node,
     98     struct rb_root_cached *root)
     99 {
    100 
    101 	rb_tree_remove_node(&root->rb_root.rbr_tree, node);
    102 }
    103 
    104 static inline struct interval_tree_node *
    105 interval_tree_iter_first(struct rb_root_cached *root, unsigned long start,
    106     unsigned long last)
    107 {
    108 	struct interval_tree_node *node;
    109 
    110 	node = rb_tree_find_node_geq(&root->rb_root.rbr_tree, &start);
    111 	if (node == NULL)
    112 		return NULL;
    113 	if (last < node->start)
    114 		return NULL;
    115 	KASSERT(node->start <= last && node->last >= start);
    116 
    117 	return node;
    118 }
    119 
    120 /*
    121  * XXX Linux's interval_tree_iter_next doesn't take the root as an
    122  * argument, which makes this difficult.  So we'll just patch those
    123  * uses.
    124  */
    125 static inline struct interval_tree_node *
    126 interval_tree_iter_next(struct rb_root_cached *root,
    127     struct interval_tree_node *node, unsigned long start, unsigned long last)
    128 {
    129 	struct interval_tree_node *next;
    130 
    131 	KASSERT(node != NULL);
    132 	next = rb_tree_iterate(&root->rb_root.rbr_tree, node, RB_DIR_RIGHT);
    133 	if (next == NULL)
    134 		return NULL;
    135 	if (last < next->start)
    136 		return NULL;
    137 	KASSERT(next->start <= last && next->last >= start);
    138 
    139 	return next;
    140 }
    141 
    142 #endif	/* _LINUX_INTERVAL_TREE_H_ */
    143