1 1.5 riastrad /* $NetBSD: interval_tree_generic.h,v 1.5 2021/12/19 12:33:11 riastradh Exp $ */ 2 1.1 riastrad 3 1.1 riastrad /*- 4 1.1 riastrad * Copyright (c) 2018 The NetBSD Foundation, Inc. 5 1.1 riastrad * All rights reserved. 6 1.1 riastrad * 7 1.1 riastrad * This code is derived from software contributed to The NetBSD Foundation 8 1.1 riastrad * by Taylor R. Campbell. 9 1.1 riastrad * 10 1.1 riastrad * Redistribution and use in source and binary forms, with or without 11 1.1 riastrad * modification, are permitted provided that the following conditions 12 1.1 riastrad * are met: 13 1.1 riastrad * 1. Redistributions of source code must retain the above copyright 14 1.1 riastrad * notice, this list of conditions and the following disclaimer. 15 1.1 riastrad * 2. Redistributions in binary form must reproduce the above copyright 16 1.1 riastrad * notice, this list of conditions and the following disclaimer in the 17 1.1 riastrad * documentation and/or other materials provided with the distribution. 18 1.1 riastrad * 19 1.1 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 1.1 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 1.1 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 1.1 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 1.1 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 1.1 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 1.1 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 1.1 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 1.1 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 1.1 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 1.1 riastrad * POSSIBILITY OF SUCH DAMAGE. 30 1.1 riastrad */ 31 1.1 riastrad 32 1.1 riastrad #ifndef _LINUX_INTERVAL_TREE_GENERIC_H_ 33 1.1 riastrad #define _LINUX_INTERVAL_TREE_GENERIC_H_ 34 1.1 riastrad 35 1.5 riastrad /* XXX See interval_tree.h for warnings. */ 36 1.5 riastrad 37 1.5 riastrad #include <sys/rbtree.h> 38 1.5 riastrad 39 1.2 riastrad #define INTERVAL_TREE_DEFINE(T, F, KT, KLAST, NSTART, NLAST, QUAL, PREFIX) \ 40 1.2 riastrad \ 41 1.2 riastrad static inline int \ 42 1.2 riastrad PREFIX##__compare_nodes(void *__cookie, const void *__va, const void *__vb) \ 43 1.2 riastrad { \ 44 1.2 riastrad const T *__na = __va; \ 45 1.2 riastrad const T *__nb = __vb; \ 46 1.2 riastrad const KT __astart = START(__na), __alast = LAST(__na); \ 47 1.2 riastrad const KT __bstart = START(__nb), __blast = LAST(__nb); \ 48 1.2 riastrad \ 49 1.2 riastrad if (__astart < __bstart) \ 50 1.2 riastrad return -1; \ 51 1.2 riastrad if (__astart > __bstart) \ 52 1.2 riastrad return +1; \ 53 1.2 riastrad if (__alast < __blast) \ 54 1.2 riastrad return -1; \ 55 1.2 riastrad if (__alast > __blast) \ 56 1.5 riastrad return +1; \ 57 1.2 riastrad return 0; \ 58 1.2 riastrad } \ 59 1.2 riastrad \ 60 1.2 riastrad static inline int \ 61 1.2 riastrad PREFIX##__compare_key(void *__cookie, const void *__vn, const void *__vk) \ 62 1.2 riastrad { \ 63 1.2 riastrad const T *__n = __vn; \ 64 1.3 riastrad const KT *__k = __vk; \ 65 1.2 riastrad const KT __nstart = START(__n), __nlast = LAST(__n); \ 66 1.2 riastrad \ 67 1.2 riastrad if (__nlast < *__k) \ 68 1.2 riastrad return -1; \ 69 1.2 riastrad if (*__k < __nstart) \ 70 1.2 riastrad return +1; \ 71 1.2 riastrad return 0; \ 72 1.2 riastrad } \ 73 1.2 riastrad \ 74 1.2 riastrad static const rb_tree_ops_t PREFIX##__rbtree_ops = { \ 75 1.2 riastrad .rbto_compare_nodes = PREFIX##__compare_nodes, \ 76 1.2 riastrad .rbto_compare_key = PREFIX##__compare_key, \ 77 1.2 riastrad .rbto_node_offset = offsetof(T, F), \ 78 1.2 riastrad }; \ 79 1.2 riastrad \ 80 1.2 riastrad /* Not in Linux API, needed for us. */ \ 81 1.2 riastrad QUAL void \ 82 1.2 riastrad PREFIX##_init(struct rb_root_cached *__root) \ 83 1.2 riastrad { \ 84 1.3 riastrad rb_tree_init(&__root->rb_root.rbr_tree, &PREFIX##__rbtree_ops); \ 85 1.2 riastrad } \ 86 1.2 riastrad \ 87 1.2 riastrad QUAL void \ 88 1.2 riastrad PREFIX##_insert(T *__node, struct rb_root_cached *__root) \ 89 1.2 riastrad { \ 90 1.2 riastrad T *__collision __diagused; \ 91 1.2 riastrad \ 92 1.3 riastrad __collision = rb_tree_insert_node(&__root->rb_root.rbr_tree, __node); \ 93 1.2 riastrad KASSERT(__collision == __node); \ 94 1.2 riastrad } \ 95 1.2 riastrad \ 96 1.2 riastrad QUAL void \ 97 1.2 riastrad PREFIX##_remove(T *__node, struct rb_root_cached *__root) \ 98 1.2 riastrad { \ 99 1.3 riastrad rb_tree_remove_node(&__root->rb_root.rbr_tree, __node); \ 100 1.2 riastrad } \ 101 1.2 riastrad \ 102 1.2 riastrad QUAL T * \ 103 1.2 riastrad PREFIX##_iter_first(struct rb_root_cached *__root, KT __start, KT __last) \ 104 1.2 riastrad { \ 105 1.2 riastrad T *__node; \ 106 1.2 riastrad \ 107 1.3 riastrad __node = rb_tree_find_node_geq(&__root->rb_root.rbr_tree, &__start); \ 108 1.2 riastrad if (__node == NULL) \ 109 1.2 riastrad return NULL; \ 110 1.5 riastrad KASSERT(__start <= START(__node)); \ 111 1.2 riastrad if (__last < START(__node)) \ 112 1.2 riastrad return NULL; \ 113 1.2 riastrad \ 114 1.2 riastrad return __node; \ 115 1.4 riastrad } \ 116 1.4 riastrad \ 117 1.4 riastrad QUAL T * \ 118 1.4 riastrad PREFIX##_iter_next(struct rb_root_cached *__root, T *__node, \ 119 1.4 riastrad KT __start, KT __last) \ 120 1.4 riastrad { \ 121 1.4 riastrad T *__next; \ 122 1.4 riastrad \ 123 1.4 riastrad KASSERT(__node != NULL); \ 124 1.4 riastrad __next = rb_tree_iterate(&__root->rb_root.rbr_tree, __node, \ 125 1.4 riastrad RB_DIR_RIGHT); \ 126 1.4 riastrad if (__next == NULL) \ 127 1.4 riastrad return NULL; \ 128 1.4 riastrad if (__last < START(__next)) \ 129 1.4 riastrad return NULL; \ 130 1.4 riastrad KASSERT(LAST(__next) >= __start); \ 131 1.4 riastrad \ 132 1.4 riastrad return __next; \ 133 1.2 riastrad } 134 1.2 riastrad 135 1.1 riastrad #endif /* _LINUX_INTERVAL_TREE_GENERIC_H_ */ 136