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