diff options
| author | 2024-02-04 14:44:17 +0100 | |
|---|---|---|
| committer | 2024-02-04 20:01:50 +0100 | |
| commit | 01ba6cf610641f1937092b469843b14ebc2a5962 (patch) | |
| tree | c7efb1bc196a7b8e8da9eb0924977e9176e8174b /src/common/range_sets.inc | |
| parent | VideoCore: Move Slot Vector to Common (diff) | |
| download | yuzu-01ba6cf610641f1937092b469843b14ebc2a5962.tar.gz yuzu-01ba6cf610641f1937092b469843b14ebc2a5962.tar.xz yuzu-01ba6cf610641f1937092b469843b14ebc2a5962.zip | |
Common: Introduce Range Sets
Diffstat (limited to '')
| -rw-r--r-- | src/common/range_sets.inc | 279 |
1 files changed, 279 insertions, 0 deletions
diff --git a/src/common/range_sets.inc b/src/common/range_sets.inc new file mode 100644 index 000000000..fa55a68fb --- /dev/null +++ b/src/common/range_sets.inc | |||
| @@ -0,0 +1,279 @@ | |||
| 1 | // SPDX-FileCopyrightText: 2024 yuzu Emulator Project | ||
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later | ||
| 3 | |||
| 4 | #pragma once | ||
| 5 | |||
| 6 | #include <limits> | ||
| 7 | #include <utility> | ||
| 8 | |||
| 9 | #define BOOST_NO_MT | ||
| 10 | #include <boost/pool/detail/mutex.hpp> | ||
| 11 | #undef BOOST_NO_MT | ||
| 12 | #include <boost/icl/interval.hpp> | ||
| 13 | #include <boost/icl/interval_base_set.hpp> | ||
| 14 | #include <boost/icl/interval_map.hpp> | ||
| 15 | #include <boost/icl/interval_set.hpp> | ||
| 16 | #include <boost/icl/split_interval_map.hpp> | ||
| 17 | #include <boost/pool/pool.hpp> | ||
| 18 | #include <boost/pool/pool_alloc.hpp> | ||
| 19 | #include <boost/pool/poolfwd.hpp> | ||
| 20 | |||
| 21 | #include "common/range_sets.h" | ||
| 22 | |||
| 23 | namespace boost { | ||
| 24 | template <typename T> | ||
| 25 | class fast_pool_allocator<T, default_user_allocator_new_delete, details::pool::null_mutex, 4096, 0>; | ||
| 26 | } | ||
| 27 | |||
| 28 | namespace Common { | ||
| 29 | |||
| 30 | template <typename AddressType> | ||
| 31 | struct RangeSet<AddressType>::RangeSetImpl { | ||
| 32 | using IntervalSet = boost::icl::interval_set< | ||
| 33 | AddressType, std::less, ICL_INTERVAL_INSTANCE(ICL_INTERVAL_DEFAULT, AddressType, std::less), | ||
| 34 | boost::fast_pool_allocator>; | ||
| 35 | using IntervalType = typename IntervalSet::interval_type; | ||
| 36 | |||
| 37 | RangeSetImpl() = default; | ||
| 38 | ~RangeSetImpl() = default; | ||
| 39 | |||
| 40 | void Add(AddressType base_address, size_t size) { | ||
| 41 | AddressType end_address = base_address + static_cast<AddressType>(size); | ||
| 42 | IntervalType interval{base_address, end_address}; | ||
| 43 | m_ranges_set.add(interval); | ||
| 44 | } | ||
| 45 | |||
| 46 | void Subtract(AddressType base_address, size_t size) { | ||
| 47 | AddressType end_address = base_address + static_cast<AddressType>(size); | ||
| 48 | IntervalType interval{base_address, end_address}; | ||
| 49 | m_ranges_set.subtract(interval); | ||
| 50 | } | ||
| 51 | |||
| 52 | IntervalSet m_ranges_set; | ||
| 53 | }; | ||
| 54 | |||
| 55 | template <typename AddressType> | ||
| 56 | struct SplitRangeSet<AddressType>::SplitRangeSetImpl { | ||
| 57 | |||
| 58 | using IntervalSet = | ||
| 59 | boost::icl::split_interval_map<AddressType, s32, boost::icl::partial_enricher, std::less, | ||
| 60 | boost::icl::inplace_plus, boost::icl::inter_section, | ||
| 61 | ICL_INTERVAL_INSTANCE(ICL_INTERVAL_DEFAULT, AddressType, | ||
| 62 | std::less), | ||
| 63 | boost::fast_pool_allocator>; | ||
| 64 | using IntervalType = typename IntervalSet::interval_type; | ||
| 65 | |||
| 66 | SplitRangeSetImpl() = default; | ||
| 67 | ~SplitRangeSetImpl() = default; | ||
| 68 | |||
| 69 | void Add(AddressType base_address, size_t size) { | ||
| 70 | AddressType end_address = base_address + static_cast<AddressType>(size); | ||
| 71 | IntervalType interval{base_address, end_address}; | ||
| 72 | m_split_ranges_set += std::make_pair(interval, 1); | ||
| 73 | } | ||
| 74 | |||
| 75 | template <bool has_on_delete, typename Func> | ||
| 76 | void Subtract(AddressType base_address, size_t size, s32 amount, | ||
| 77 | [[maybe_unused]] Func&& on_delete) { | ||
| 78 | AddressType end_address = base_address + static_cast<AddressType>(size); | ||
| 79 | IntervalType interval{base_address, end_address}; | ||
| 80 | bool any_removals = false; | ||
| 81 | m_split_ranges_set += std::make_pair(interval, -amount); | ||
| 82 | do { | ||
| 83 | any_removals = false; | ||
| 84 | auto it = m_split_ranges_set.lower_bound(interval); | ||
| 85 | if (it == m_split_ranges_set.end()) { | ||
| 86 | return; | ||
| 87 | } | ||
| 88 | auto end_it = m_split_ranges_set.upper_bound(interval); | ||
| 89 | for (; it != end_it; it++) { | ||
| 90 | if (it->second <= 0) { | ||
| 91 | if constexpr (has_on_delete) { | ||
| 92 | if (it->second == 0) { | ||
| 93 | on_delete(it->first.lower(), it->first.upper()); | ||
| 94 | } | ||
| 95 | } | ||
| 96 | any_removals = true; | ||
| 97 | m_split_ranges_set.erase(it); | ||
| 98 | break; | ||
| 99 | } | ||
| 100 | } | ||
| 101 | } while (any_removals); | ||
| 102 | } | ||
| 103 | |||
| 104 | IntervalSet m_split_ranges_set; | ||
| 105 | }; | ||
| 106 | |||
| 107 | template <typename AddressType> | ||
| 108 | RangeSet<AddressType>::RangeSet() { | ||
| 109 | m_impl = std::make_unique<RangeSet<AddressType>::RangeSetImpl>(); | ||
| 110 | } | ||
| 111 | |||
| 112 | template <typename AddressType> | ||
| 113 | RangeSet<AddressType>::~RangeSet() = default; | ||
| 114 | |||
| 115 | template <typename AddressType> | ||
| 116 | RangeSet<AddressType>::RangeSet(RangeSet&& other) { | ||
| 117 | m_impl = std::make_unique<RangeSet<AddressType>::RangeSetImpl>(); | ||
| 118 | m_impl->m_ranges_set = std::move(other.m_impl->m_ranges_set); | ||
| 119 | } | ||
| 120 | |||
| 121 | template <typename AddressType> | ||
| 122 | RangeSet<AddressType>& RangeSet<AddressType>::operator=(RangeSet&& other) { | ||
| 123 | m_impl->m_ranges_set = std::move(other.m_impl->m_ranges_set); | ||
| 124 | } | ||
| 125 | |||
| 126 | template <typename AddressType> | ||
| 127 | void RangeSet<AddressType>::Add(AddressType base_address, size_t size) { | ||
| 128 | m_impl->Add(base_address, size); | ||
| 129 | } | ||
| 130 | |||
| 131 | template <typename AddressType> | ||
| 132 | void RangeSet<AddressType>::Subtract(AddressType base_address, size_t size) { | ||
| 133 | m_impl->Subtract(base_address, size); | ||
| 134 | } | ||
| 135 | |||
| 136 | template <typename AddressType> | ||
| 137 | void RangeSet<AddressType>::Clear() { | ||
| 138 | m_impl->m_ranges_set.clear(); | ||
| 139 | } | ||
| 140 | |||
| 141 | template <typename AddressType> | ||
| 142 | bool RangeSet<AddressType>::Empty() const { | ||
| 143 | return m_impl->m_ranges_set.empty(); | ||
| 144 | } | ||
| 145 | |||
| 146 | template <typename AddressType> | ||
| 147 | template <typename Func> | ||
| 148 | void RangeSet<AddressType>::ForEach(Func&& func) const { | ||
| 149 | if (m_impl->m_ranges_set.empty()) { | ||
| 150 | return; | ||
| 151 | } | ||
| 152 | auto it = m_impl->m_ranges_set.begin(); | ||
| 153 | auto end_it = m_impl->m_ranges_set.end(); | ||
| 154 | for (; it != end_it; it++) { | ||
| 155 | const AddressType inter_addr_end = it->upper(); | ||
| 156 | const AddressType inter_addr = it->lower(); | ||
| 157 | func(inter_addr, inter_addr_end); | ||
| 158 | } | ||
| 159 | } | ||
| 160 | |||
| 161 | template <typename AddressType> | ||
| 162 | template <typename Func> | ||
| 163 | void RangeSet<AddressType>::ForEachInRange(AddressType base_addr, size_t size, Func&& func) const { | ||
| 164 | auto& range_set = m_impl->m_ranges_set; | ||
| 165 | const AddressType start_address = base_addr; | ||
| 166 | const AddressType end_address = start_address + size; | ||
| 167 | const RangeSetImpl::IntervalType search_interval{start_address, end_address}; | ||
| 168 | auto it = range_set.lower_bound(search_interval); | ||
| 169 | if (it == range_set.end()) { | ||
| 170 | return; | ||
| 171 | } | ||
| 172 | auto end_it = range_set.upper_bound(search_interval); | ||
| 173 | for (; it != end_it; it++) { | ||
| 174 | AddressType inter_addr_end = it->upper(); | ||
| 175 | AddressType inter_addr = it->lower(); | ||
| 176 | if (inter_addr_end > end_address) { | ||
| 177 | inter_addr_end = end_address; | ||
| 178 | } | ||
| 179 | if (inter_addr < start_address) { | ||
| 180 | inter_addr = start_address; | ||
| 181 | } | ||
| 182 | func(inter_addr, inter_addr_end); | ||
| 183 | } | ||
| 184 | } | ||
| 185 | |||
| 186 | template <typename AddressType> | ||
| 187 | SplitRangeSet<AddressType>::SplitRangeSet() { | ||
| 188 | m_impl = std::make_unique<SplitRangeSet<AddressType>::SplitRangeSetImpl>(); | ||
| 189 | } | ||
| 190 | |||
| 191 | template <typename AddressType> | ||
| 192 | SplitRangeSet<AddressType>::~SplitRangeSet() = default; | ||
| 193 | |||
| 194 | template <typename AddressType> | ||
| 195 | SplitRangeSet<AddressType>::SplitRangeSet(SplitRangeSet&& other) { | ||
| 196 | m_impl = std::make_unique<SplitRangeSet<AddressType>::SplitRangeSetImpl>(); | ||
| 197 | m_impl->m_split_ranges_set = std::move(other.m_impl->m_split_ranges_set); | ||
| 198 | } | ||
| 199 | |||
| 200 | template <typename AddressType> | ||
| 201 | SplitRangeSet<AddressType>& SplitRangeSet<AddressType>::operator=(SplitRangeSet&& other) { | ||
| 202 | m_impl->m_split_ranges_set = std::move(other.m_impl->m_split_ranges_set); | ||
| 203 | } | ||
| 204 | |||
| 205 | template <typename AddressType> | ||
| 206 | void SplitRangeSet<AddressType>::Add(AddressType base_address, size_t size) { | ||
| 207 | m_impl->Add(base_address, size); | ||
| 208 | } | ||
| 209 | |||
| 210 | template <typename AddressType> | ||
| 211 | void SplitRangeSet<AddressType>::Subtract(AddressType base_address, size_t size) { | ||
| 212 | m_impl->Subtract<false>(base_address, size, 1, [](AddressType, AddressType) {}); | ||
| 213 | } | ||
| 214 | |||
| 215 | template <typename AddressType> | ||
| 216 | template <typename Func> | ||
| 217 | void SplitRangeSet<AddressType>::Subtract(AddressType base_address, size_t size, Func&& on_delete) { | ||
| 218 | m_impl->Subtract<true>(base_address, size, 1, on_delete); | ||
| 219 | } | ||
| 220 | |||
| 221 | template <typename AddressType> | ||
| 222 | void SplitRangeSet<AddressType>::DeleteAll(AddressType base_address, size_t size) { | ||
| 223 | m_impl->Subtract<false>(base_address, size, std::numeric_limits<s32>::max(), | ||
| 224 | [](AddressType, AddressType) {}); | ||
| 225 | } | ||
| 226 | |||
| 227 | template <typename AddressType> | ||
| 228 | void SplitRangeSet<AddressType>::Clear() { | ||
| 229 | m_impl->m_split_ranges_set.clear(); | ||
| 230 | } | ||
| 231 | |||
| 232 | template <typename AddressType> | ||
| 233 | bool SplitRangeSet<AddressType>::Empty() const { | ||
| 234 | return m_impl->m_split_ranges_set.empty(); | ||
| 235 | } | ||
| 236 | |||
| 237 | template <typename AddressType> | ||
| 238 | template <typename Func> | ||
| 239 | void SplitRangeSet<AddressType>::ForEach(Func&& func) const { | ||
| 240 | if (m_impl->m_split_ranges_set.empty()) { | ||
| 241 | return; | ||
| 242 | } | ||
| 243 | auto it = m_impl->m_split_ranges_set.begin(); | ||
| 244 | auto end_it = m_impl->m_split_ranges_set.end(); | ||
| 245 | for (; it != end_it; it++) { | ||
| 246 | const AddressType inter_addr_end = it->first.upper(); | ||
| 247 | const AddressType inter_addr = it->first.lower(); | ||
| 248 | func(inter_addr, inter_addr_end, it->second); | ||
| 249 | } | ||
| 250 | } | ||
| 251 | |||
| 252 | template <typename AddressType> | ||
| 253 | template <typename Func> | ||
| 254 | void SplitRangeSet<AddressType>::ForEachInRange(AddressType base_address, size_t size, | ||
| 255 | Func&& func) const { | ||
| 256 | auto& range_set = m_impl->m_split_ranges_set; | ||
| 257 | const AddressType start_address = base_address; | ||
| 258 | const AddressType end_address = start_address + size; | ||
| 259 | const SplitRangeSetImpl::IntervalType search_interval{start_address, end_address}; | ||
| 260 | auto it = range_set.lower_bound(search_interval); | ||
| 261 | if (it == range_set.end()) { | ||
| 262 | return; | ||
| 263 | } | ||
| 264 | auto end_it = range_set.upper_bound(search_interval); | ||
| 265 | for (; it != end_it; it++) { | ||
| 266 | auto& inter = it->first; | ||
| 267 | AddressType inter_addr_end = inter.upper(); | ||
| 268 | AddressType inter_addr = inter.lower(); | ||
| 269 | if (inter_addr_end > end_address) { | ||
| 270 | inter_addr_end = end_address; | ||
| 271 | } | ||
| 272 | if (inter_addr < start_address) { | ||
| 273 | inter_addr = start_address; | ||
| 274 | } | ||
| 275 | func(inter_addr, inter_addr_end, it->second); | ||
| 276 | } | ||
| 277 | } | ||
| 278 | |||
| 279 | } // namespace Common \ No newline at end of file | ||