range_sets.inc 10 KB

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