diff options
Diffstat (limited to 'src/common')
| -rw-r--r-- | src/common/CMakeLists.txt | 3 | ||||
| -rw-r--r-- | src/common/range_sets.h | 73 | ||||
| -rw-r--r-- | src/common/range_sets.inc | 304 | ||||
| -rw-r--r-- | src/common/slot_vector.h | 227 | ||||
| -rw-r--r-- | src/common/typed_address.h | 82 |
5 files changed, 648 insertions, 41 deletions
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 8ff1326f2..779be211e 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt | |||
| @@ -107,6 +107,8 @@ add_library(common STATIC | |||
| 107 | quaternion.h | 107 | quaternion.h |
| 108 | range_map.h | 108 | range_map.h |
| 109 | range_mutex.h | 109 | range_mutex.h |
| 110 | range_sets.h | ||
| 111 | range_sets.inc | ||
| 110 | reader_writer_queue.h | 112 | reader_writer_queue.h |
| 111 | ring_buffer.h | 113 | ring_buffer.h |
| 112 | ${CMAKE_CURRENT_BINARY_DIR}/scm_rev.cpp | 114 | ${CMAKE_CURRENT_BINARY_DIR}/scm_rev.cpp |
| @@ -121,6 +123,7 @@ add_library(common STATIC | |||
| 121 | settings_input.cpp | 123 | settings_input.cpp |
| 122 | settings_input.h | 124 | settings_input.h |
| 123 | settings_setting.h | 125 | settings_setting.h |
| 126 | slot_vector.h | ||
| 124 | socket_types.h | 127 | socket_types.h |
| 125 | spin_lock.cpp | 128 | spin_lock.cpp |
| 126 | spin_lock.h | 129 | spin_lock.h |
diff --git a/src/common/range_sets.h b/src/common/range_sets.h new file mode 100644 index 000000000..f8fcee483 --- /dev/null +++ b/src/common/range_sets.h | |||
| @@ -0,0 +1,73 @@ | |||
| 1 | // SPDX-FileCopyrightText: 2024 yuzu Emulator Project | ||
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later | ||
| 3 | |||
| 4 | #pragma once | ||
| 5 | |||
| 6 | #include <memory> | ||
| 7 | |||
| 8 | #include "common/common_types.h" | ||
| 9 | |||
| 10 | namespace Common { | ||
| 11 | |||
| 12 | template <typename AddressType> | ||
| 13 | class RangeSet { | ||
| 14 | public: | ||
| 15 | RangeSet(); | ||
| 16 | ~RangeSet(); | ||
| 17 | |||
| 18 | RangeSet(RangeSet const&) = delete; | ||
| 19 | RangeSet& operator=(RangeSet const&) = delete; | ||
| 20 | |||
| 21 | RangeSet(RangeSet&& other); | ||
| 22 | RangeSet& operator=(RangeSet&& other); | ||
| 23 | |||
| 24 | void Add(AddressType base_address, size_t size); | ||
| 25 | void Subtract(AddressType base_address, size_t size); | ||
| 26 | void Clear(); | ||
| 27 | bool Empty() const; | ||
| 28 | |||
| 29 | template <typename Func> | ||
| 30 | void ForEach(Func&& func) const; | ||
| 31 | |||
| 32 | template <typename Func> | ||
| 33 | void ForEachInRange(AddressType device_addr, size_t size, Func&& func) const; | ||
| 34 | |||
| 35 | private: | ||
| 36 | struct RangeSetImpl; | ||
| 37 | std::unique_ptr<RangeSetImpl> m_impl; | ||
| 38 | }; | ||
| 39 | |||
| 40 | template <typename AddressType> | ||
| 41 | class OverlapRangeSet { | ||
| 42 | public: | ||
| 43 | OverlapRangeSet(); | ||
| 44 | ~OverlapRangeSet(); | ||
| 45 | |||
| 46 | OverlapRangeSet(OverlapRangeSet const&) = delete; | ||
| 47 | OverlapRangeSet& operator=(OverlapRangeSet const&) = delete; | ||
| 48 | |||
| 49 | OverlapRangeSet(OverlapRangeSet&& other); | ||
| 50 | OverlapRangeSet& operator=(OverlapRangeSet&& other); | ||
| 51 | |||
| 52 | void Add(AddressType base_address, size_t size); | ||
| 53 | void Subtract(AddressType base_address, size_t size); | ||
| 54 | |||
| 55 | template <typename Func> | ||
| 56 | void Subtract(AddressType base_address, size_t size, Func&& on_delete); | ||
| 57 | |||
| 58 | void DeleteAll(AddressType base_address, size_t size); | ||
| 59 | void Clear(); | ||
| 60 | bool Empty() const; | ||
| 61 | |||
| 62 | template <typename Func> | ||
| 63 | void ForEach(Func&& func) const; | ||
| 64 | |||
| 65 | template <typename Func> | ||
| 66 | void ForEachInRange(AddressType device_addr, size_t size, Func&& func) const; | ||
| 67 | |||
| 68 | private: | ||
| 69 | struct OverlapRangeSetImpl; | ||
| 70 | std::unique_ptr<OverlapRangeSetImpl> m_impl; | ||
| 71 | }; | ||
| 72 | |||
| 73 | } // namespace Common | ||
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 | |||
| 20 | namespace Common { | ||
| 21 | |||
| 22 | namespace { | ||
| 23 | template <class T> | ||
| 24 | using RangeSetsAllocator = | ||
| 25 | boost::fast_pool_allocator<T, boost::default_user_allocator_new_delete, | ||
| 26 | boost::details::pool::default_mutex, 1024, 2048>; | ||
| 27 | } | ||
| 28 | |||
| 29 | template <typename AddressType> | ||
| 30 | struct 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 | |||
| 94 | template <typename AddressType> | ||
| 95 | struct 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 | |||
| 187 | template <typename AddressType> | ||
| 188 | RangeSet<AddressType>::RangeSet() { | ||
| 189 | m_impl = std::make_unique<RangeSet<AddressType>::RangeSetImpl>(); | ||
| 190 | } | ||
| 191 | |||
| 192 | template <typename AddressType> | ||
| 193 | RangeSet<AddressType>::~RangeSet() = default; | ||
| 194 | |||
| 195 | template <typename AddressType> | ||
| 196 | RangeSet<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 | |||
| 201 | template <typename AddressType> | ||
| 202 | RangeSet<AddressType>& RangeSet<AddressType>::operator=(RangeSet&& other) { | ||
| 203 | m_impl->m_ranges_set = std::move(other.m_impl->m_ranges_set); | ||
| 204 | } | ||
| 205 | |||
| 206 | template <typename AddressType> | ||
| 207 | void RangeSet<AddressType>::Add(AddressType base_address, size_t size) { | ||
| 208 | m_impl->Add(base_address, size); | ||
| 209 | } | ||
| 210 | |||
| 211 | template <typename AddressType> | ||
| 212 | void RangeSet<AddressType>::Subtract(AddressType base_address, size_t size) { | ||
| 213 | m_impl->Subtract(base_address, size); | ||
| 214 | } | ||
| 215 | |||
| 216 | template <typename AddressType> | ||
| 217 | void RangeSet<AddressType>::Clear() { | ||
| 218 | m_impl->m_ranges_set.clear(); | ||
| 219 | } | ||
| 220 | |||
| 221 | template <typename AddressType> | ||
| 222 | bool RangeSet<AddressType>::Empty() const { | ||
| 223 | return m_impl->m_ranges_set.empty(); | ||
| 224 | } | ||
| 225 | |||
| 226 | template <typename AddressType> | ||
| 227 | template <typename Func> | ||
| 228 | void RangeSet<AddressType>::ForEach(Func&& func) const { | ||
| 229 | m_impl->ForEach(std::move(func)); | ||
| 230 | } | ||
| 231 | |||
| 232 | template <typename AddressType> | ||
| 233 | template <typename Func> | ||
| 234 | void 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 | |||
| 239 | template <typename AddressType> | ||
| 240 | OverlapRangeSet<AddressType>::OverlapRangeSet() { | ||
| 241 | m_impl = std::make_unique<OverlapRangeSet<AddressType>::OverlapRangeSetImpl>(); | ||
| 242 | } | ||
| 243 | |||
| 244 | template <typename AddressType> | ||
| 245 | OverlapRangeSet<AddressType>::~OverlapRangeSet() = default; | ||
| 246 | |||
| 247 | template <typename AddressType> | ||
| 248 | OverlapRangeSet<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 | |||
| 253 | template <typename AddressType> | ||
| 254 | OverlapRangeSet<AddressType>& OverlapRangeSet<AddressType>::operator=(OverlapRangeSet&& other) { | ||
| 255 | m_impl->m_split_ranges_set = std::move(other.m_impl->m_split_ranges_set); | ||
| 256 | } | ||
| 257 | |||
| 258 | template <typename AddressType> | ||
| 259 | void OverlapRangeSet<AddressType>::Add(AddressType base_address, size_t size) { | ||
| 260 | m_impl->Add(base_address, size); | ||
| 261 | } | ||
| 262 | |||
| 263 | template <typename AddressType> | ||
| 264 | void OverlapRangeSet<AddressType>::Subtract(AddressType base_address, size_t size) { | ||
| 265 | m_impl->template Subtract<false>(base_address, size, 1, [](AddressType, AddressType) {}); | ||
| 266 | } | ||
| 267 | |||
| 268 | template <typename AddressType> | ||
| 269 | template <typename Func> | ||
| 270 | void 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 | |||
| 275 | template <typename AddressType> | ||
| 276 | void 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 | |||
| 281 | template <typename AddressType> | ||
| 282 | void OverlapRangeSet<AddressType>::Clear() { | ||
| 283 | m_impl->m_split_ranges_set.clear(); | ||
| 284 | } | ||
| 285 | |||
| 286 | template <typename AddressType> | ||
| 287 | bool OverlapRangeSet<AddressType>::Empty() const { | ||
| 288 | return m_impl->m_split_ranges_set.empty(); | ||
| 289 | } | ||
| 290 | |||
| 291 | template <typename AddressType> | ||
| 292 | template <typename Func> | ||
| 293 | void OverlapRangeSet<AddressType>::ForEach(Func&& func) const { | ||
| 294 | m_impl->ForEach(func); | ||
| 295 | } | ||
| 296 | |||
| 297 | template <typename AddressType> | ||
| 298 | template <typename Func> | ||
| 299 | void 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 | ||
diff --git a/src/common/slot_vector.h b/src/common/slot_vector.h new file mode 100644 index 000000000..34ff7de94 --- /dev/null +++ b/src/common/slot_vector.h | |||
| @@ -0,0 +1,227 @@ | |||
| 1 | // SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project | ||
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later | ||
| 3 | |||
| 4 | #pragma once | ||
| 5 | |||
| 6 | #include <algorithm> | ||
| 7 | #include <bit> | ||
| 8 | #include <numeric> | ||
| 9 | #include <type_traits> | ||
| 10 | #include <utility> | ||
| 11 | #include <vector> | ||
| 12 | |||
| 13 | #include "common/assert.h" | ||
| 14 | #include "common/common_types.h" | ||
| 15 | #include "common/polyfill_ranges.h" | ||
| 16 | |||
| 17 | namespace Common { | ||
| 18 | |||
| 19 | struct SlotId { | ||
| 20 | static constexpr u32 INVALID_INDEX = std::numeric_limits<u32>::max(); | ||
| 21 | |||
| 22 | constexpr auto operator<=>(const SlotId&) const noexcept = default; | ||
| 23 | |||
| 24 | constexpr explicit operator bool() const noexcept { | ||
| 25 | return index != INVALID_INDEX; | ||
| 26 | } | ||
| 27 | |||
| 28 | u32 index = INVALID_INDEX; | ||
| 29 | }; | ||
| 30 | |||
| 31 | template <class T> | ||
| 32 | requires std::is_nothrow_move_assignable_v<T> && std::is_nothrow_move_constructible_v<T> | ||
| 33 | class SlotVector { | ||
| 34 | public: | ||
| 35 | class Iterator { | ||
| 36 | friend SlotVector<T>; | ||
| 37 | |||
| 38 | public: | ||
| 39 | constexpr Iterator() = default; | ||
| 40 | |||
| 41 | Iterator& operator++() noexcept { | ||
| 42 | const u64* const bitset = slot_vector->stored_bitset.data(); | ||
| 43 | const u32 size = static_cast<u32>(slot_vector->stored_bitset.size()) * 64; | ||
| 44 | if (id.index < size) { | ||
| 45 | do { | ||
| 46 | ++id.index; | ||
| 47 | } while (id.index < size && !IsValid(bitset)); | ||
| 48 | if (id.index == size) { | ||
| 49 | id.index = SlotId::INVALID_INDEX; | ||
| 50 | } | ||
| 51 | } | ||
| 52 | return *this; | ||
| 53 | } | ||
| 54 | |||
| 55 | Iterator operator++(int) noexcept { | ||
| 56 | const Iterator copy{*this}; | ||
| 57 | ++*this; | ||
| 58 | return copy; | ||
| 59 | } | ||
| 60 | |||
| 61 | bool operator==(const Iterator& other) const noexcept { | ||
| 62 | return id.index == other.id.index; | ||
| 63 | } | ||
| 64 | |||
| 65 | bool operator!=(const Iterator& other) const noexcept { | ||
| 66 | return id.index != other.id.index; | ||
| 67 | } | ||
| 68 | |||
| 69 | std::pair<SlotId, T*> operator*() const noexcept { | ||
| 70 | return {id, std::addressof((*slot_vector)[id])}; | ||
| 71 | } | ||
| 72 | |||
| 73 | T* operator->() const noexcept { | ||
| 74 | return std::addressof((*slot_vector)[id]); | ||
| 75 | } | ||
| 76 | |||
| 77 | private: | ||
| 78 | Iterator(SlotVector<T>* slot_vector_, SlotId id_) noexcept | ||
| 79 | : slot_vector{slot_vector_}, id{id_} {} | ||
| 80 | |||
| 81 | bool IsValid(const u64* bitset) const noexcept { | ||
| 82 | return ((bitset[id.index / 64] >> (id.index % 64)) & 1) != 0; | ||
| 83 | } | ||
| 84 | |||
| 85 | SlotVector<T>* slot_vector; | ||
| 86 | SlotId id; | ||
| 87 | }; | ||
| 88 | |||
| 89 | ~SlotVector() noexcept { | ||
| 90 | size_t index = 0; | ||
| 91 | for (u64 bits : stored_bitset) { | ||
| 92 | for (size_t bit = 0; bits; ++bit, bits >>= 1) { | ||
| 93 | if ((bits & 1) != 0) { | ||
| 94 | values[index + bit].object.~T(); | ||
| 95 | } | ||
| 96 | } | ||
| 97 | index += 64; | ||
| 98 | } | ||
| 99 | delete[] values; | ||
| 100 | } | ||
| 101 | |||
| 102 | [[nodiscard]] T& operator[](SlotId id) noexcept { | ||
| 103 | ValidateIndex(id); | ||
| 104 | return values[id.index].object; | ||
| 105 | } | ||
| 106 | |||
| 107 | [[nodiscard]] const T& operator[](SlotId id) const noexcept { | ||
| 108 | ValidateIndex(id); | ||
| 109 | return values[id.index].object; | ||
| 110 | } | ||
| 111 | |||
| 112 | template <typename... Args> | ||
| 113 | [[nodiscard]] SlotId insert(Args&&... args) noexcept { | ||
| 114 | const u32 index = FreeValueIndex(); | ||
| 115 | new (&values[index].object) T(std::forward<Args>(args)...); | ||
| 116 | SetStorageBit(index); | ||
| 117 | |||
| 118 | return SlotId{index}; | ||
| 119 | } | ||
| 120 | |||
| 121 | void erase(SlotId id) noexcept { | ||
| 122 | values[id.index].object.~T(); | ||
| 123 | free_list.push_back(id.index); | ||
| 124 | ResetStorageBit(id.index); | ||
| 125 | } | ||
| 126 | |||
| 127 | [[nodiscard]] Iterator begin() noexcept { | ||
| 128 | const auto it = std::ranges::find_if(stored_bitset, [](u64 value) { return value != 0; }); | ||
| 129 | if (it == stored_bitset.end()) { | ||
| 130 | return end(); | ||
| 131 | } | ||
| 132 | const u32 word_index = static_cast<u32>(std::distance(it, stored_bitset.begin())); | ||
| 133 | const SlotId first_id{word_index * 64 + static_cast<u32>(std::countr_zero(*it))}; | ||
| 134 | return Iterator(this, first_id); | ||
| 135 | } | ||
| 136 | |||
| 137 | [[nodiscard]] Iterator end() noexcept { | ||
| 138 | return Iterator(this, SlotId{SlotId::INVALID_INDEX}); | ||
| 139 | } | ||
| 140 | |||
| 141 | [[nodiscard]] size_t size() const noexcept { | ||
| 142 | return values_capacity - free_list.size(); | ||
| 143 | } | ||
| 144 | |||
| 145 | private: | ||
| 146 | struct NonTrivialDummy { | ||
| 147 | NonTrivialDummy() noexcept {} | ||
| 148 | }; | ||
| 149 | |||
| 150 | union Entry { | ||
| 151 | Entry() noexcept : dummy{} {} | ||
| 152 | ~Entry() noexcept {} | ||
| 153 | |||
| 154 | NonTrivialDummy dummy; | ||
| 155 | T object; | ||
| 156 | }; | ||
| 157 | |||
| 158 | void SetStorageBit(u32 index) noexcept { | ||
| 159 | stored_bitset[index / 64] |= u64(1) << (index % 64); | ||
| 160 | } | ||
| 161 | |||
| 162 | void ResetStorageBit(u32 index) noexcept { | ||
| 163 | stored_bitset[index / 64] &= ~(u64(1) << (index % 64)); | ||
| 164 | } | ||
| 165 | |||
| 166 | bool ReadStorageBit(u32 index) noexcept { | ||
| 167 | return ((stored_bitset[index / 64] >> (index % 64)) & 1) != 0; | ||
| 168 | } | ||
| 169 | |||
| 170 | void ValidateIndex(SlotId id) const noexcept { | ||
| 171 | DEBUG_ASSERT(id); | ||
| 172 | DEBUG_ASSERT(id.index / 64 < stored_bitset.size()); | ||
| 173 | DEBUG_ASSERT(((stored_bitset[id.index / 64] >> (id.index % 64)) & 1) != 0); | ||
| 174 | } | ||
| 175 | |||
| 176 | [[nodiscard]] u32 FreeValueIndex() noexcept { | ||
| 177 | if (free_list.empty()) { | ||
| 178 | Reserve(values_capacity ? (values_capacity << 1) : 1); | ||
| 179 | } | ||
| 180 | const u32 free_index = free_list.back(); | ||
| 181 | free_list.pop_back(); | ||
| 182 | return free_index; | ||
| 183 | } | ||
| 184 | |||
| 185 | void Reserve(size_t new_capacity) noexcept { | ||
| 186 | Entry* const new_values = new Entry[new_capacity]; | ||
| 187 | size_t index = 0; | ||
| 188 | for (u64 bits : stored_bitset) { | ||
| 189 | for (size_t bit = 0; bits; ++bit, bits >>= 1) { | ||
| 190 | const size_t i = index + bit; | ||
| 191 | if ((bits & 1) == 0) { | ||
| 192 | continue; | ||
| 193 | } | ||
| 194 | T& old_value = values[i].object; | ||
| 195 | new (&new_values[i].object) T(std::move(old_value)); | ||
| 196 | old_value.~T(); | ||
| 197 | } | ||
| 198 | index += 64; | ||
| 199 | } | ||
| 200 | |||
| 201 | stored_bitset.resize((new_capacity + 63) / 64); | ||
| 202 | |||
| 203 | const size_t old_free_size = free_list.size(); | ||
| 204 | free_list.resize(old_free_size + (new_capacity - values_capacity)); | ||
| 205 | std::iota(free_list.begin() + old_free_size, free_list.end(), | ||
| 206 | static_cast<u32>(values_capacity)); | ||
| 207 | |||
| 208 | delete[] values; | ||
| 209 | values = new_values; | ||
| 210 | values_capacity = new_capacity; | ||
| 211 | } | ||
| 212 | |||
| 213 | Entry* values = nullptr; | ||
| 214 | size_t values_capacity = 0; | ||
| 215 | |||
| 216 | std::vector<u64> stored_bitset; | ||
| 217 | std::vector<u32> free_list; | ||
| 218 | }; | ||
| 219 | |||
| 220 | } // namespace Common | ||
| 221 | |||
| 222 | template <> | ||
| 223 | struct std::hash<Common::SlotId> { | ||
| 224 | size_t operator()(const Common::SlotId& id) const noexcept { | ||
| 225 | return std::hash<u32>{}(id.index); | ||
| 226 | } | ||
| 227 | }; | ||
diff --git a/src/common/typed_address.h b/src/common/typed_address.h index 64f4a07c2..d5e743583 100644 --- a/src/common/typed_address.h +++ b/src/common/typed_address.h | |||
| @@ -186,68 +186,68 @@ static_assert(std::is_trivially_destructible_v<PhysicalAddress>); | |||
| 186 | static_assert(std::is_trivially_destructible_v<VirtualAddress>); | 186 | static_assert(std::is_trivially_destructible_v<VirtualAddress>); |
| 187 | static_assert(std::is_trivially_destructible_v<ProcessAddress>); | 187 | static_assert(std::is_trivially_destructible_v<ProcessAddress>); |
| 188 | 188 | ||
| 189 | static_assert(Null<uint64_t> == 0); | 189 | static_assert(Null<uint64_t> == 0U); |
| 190 | static_assert(Null<PhysicalAddress> == Null<uint64_t>); | 190 | static_assert(Null<PhysicalAddress> == Null<uint64_t>); |
| 191 | static_assert(Null<VirtualAddress> == Null<uint64_t>); | 191 | static_assert(Null<VirtualAddress> == Null<uint64_t>); |
| 192 | static_assert(Null<ProcessAddress> == Null<uint64_t>); | 192 | static_assert(Null<ProcessAddress> == Null<uint64_t>); |
| 193 | 193 | ||
| 194 | // Constructor/assignment validations. | 194 | // Constructor/assignment validations. |
| 195 | static_assert([] { | 195 | static_assert([] { |
| 196 | const PhysicalAddress a(5); | 196 | const PhysicalAddress a(5U); |
| 197 | PhysicalAddress b(a); | 197 | PhysicalAddress b(a); |
| 198 | return b; | 198 | return b; |
| 199 | }() == PhysicalAddress(5)); | 199 | }() == PhysicalAddress(5U)); |
| 200 | static_assert([] { | 200 | static_assert([] { |
| 201 | const PhysicalAddress a(5); | 201 | const PhysicalAddress a(5U); |
| 202 | PhysicalAddress b(10); | 202 | PhysicalAddress b(10U); |
| 203 | b = a; | 203 | b = a; |
| 204 | return b; | 204 | return b; |
| 205 | }() == PhysicalAddress(5)); | 205 | }() == PhysicalAddress(5U)); |
| 206 | 206 | ||
| 207 | // Arithmetic validations. | 207 | // Arithmetic validations. |
| 208 | static_assert(PhysicalAddress(10) + 5 == PhysicalAddress(15)); | 208 | static_assert(PhysicalAddress(10U) + 5U == PhysicalAddress(15U)); |
| 209 | static_assert(PhysicalAddress(10) - 5 == PhysicalAddress(5)); | 209 | static_assert(PhysicalAddress(10U) - 5U == PhysicalAddress(5U)); |
| 210 | static_assert([] { | 210 | static_assert([] { |
| 211 | PhysicalAddress v(10); | 211 | PhysicalAddress v(10U); |
| 212 | v += 5; | 212 | v += 5U; |
| 213 | return v; | 213 | return v; |
| 214 | }() == PhysicalAddress(15)); | 214 | }() == PhysicalAddress(15U)); |
| 215 | static_assert([] { | 215 | static_assert([] { |
| 216 | PhysicalAddress v(10); | 216 | PhysicalAddress v(10U); |
| 217 | v -= 5; | 217 | v -= 5U; |
| 218 | return v; | 218 | return v; |
| 219 | }() == PhysicalAddress(5)); | 219 | }() == PhysicalAddress(5U)); |
| 220 | static_assert(PhysicalAddress(10)++ == PhysicalAddress(10)); | 220 | static_assert(PhysicalAddress(10U)++ == PhysicalAddress(10U)); |
| 221 | static_assert(++PhysicalAddress(10) == PhysicalAddress(11)); | 221 | static_assert(++PhysicalAddress(10U) == PhysicalAddress(11U)); |
| 222 | static_assert(PhysicalAddress(10)-- == PhysicalAddress(10)); | 222 | static_assert(PhysicalAddress(10U)-- == PhysicalAddress(10U)); |
| 223 | static_assert(--PhysicalAddress(10) == PhysicalAddress(9)); | 223 | static_assert(--PhysicalAddress(10U) == PhysicalAddress(9U)); |
| 224 | 224 | ||
| 225 | // Logical validations. | 225 | // Logical validations. |
| 226 | static_assert((PhysicalAddress(0b11111111) >> 1) == 0b01111111); | 226 | static_assert((PhysicalAddress(0b11111111U) >> 1) == 0b01111111U); |
| 227 | static_assert((PhysicalAddress(0b10101010) >> 1) == 0b01010101); | 227 | static_assert((PhysicalAddress(0b10101010U) >> 1) == 0b01010101U); |
| 228 | static_assert((PhysicalAddress(0b11111111) << 1) == 0b111111110); | 228 | static_assert((PhysicalAddress(0b11111111U) << 1) == 0b111111110U); |
| 229 | static_assert((PhysicalAddress(0b01010101) << 1) == 0b10101010); | 229 | static_assert((PhysicalAddress(0b01010101U) << 1) == 0b10101010U); |
| 230 | static_assert((PhysicalAddress(0b11111111) & 0b01010101) == 0b01010101); | 230 | static_assert((PhysicalAddress(0b11111111U) & 0b01010101U) == 0b01010101U); |
| 231 | static_assert((PhysicalAddress(0b11111111) & 0b10101010) == 0b10101010); | 231 | static_assert((PhysicalAddress(0b11111111U) & 0b10101010U) == 0b10101010U); |
| 232 | static_assert((PhysicalAddress(0b01010101) & 0b10101010) == 0b00000000); | 232 | static_assert((PhysicalAddress(0b01010101U) & 0b10101010U) == 0b00000000U); |
| 233 | static_assert((PhysicalAddress(0b00000000) | 0b01010101) == 0b01010101); | 233 | static_assert((PhysicalAddress(0b00000000U) | 0b01010101U) == 0b01010101U); |
| 234 | static_assert((PhysicalAddress(0b11111111) | 0b01010101) == 0b11111111); | 234 | static_assert((PhysicalAddress(0b11111111U) | 0b01010101U) == 0b11111111U); |
| 235 | static_assert((PhysicalAddress(0b10101010) | 0b01010101) == 0b11111111); | 235 | static_assert((PhysicalAddress(0b10101010U) | 0b01010101U) == 0b11111111U); |
| 236 | 236 | ||
| 237 | // Comparisons. | 237 | // Comparisons. |
| 238 | static_assert(PhysicalAddress(0) == PhysicalAddress(0)); | 238 | static_assert(PhysicalAddress(0U) == PhysicalAddress(0U)); |
| 239 | static_assert(PhysicalAddress(0) != PhysicalAddress(1)); | 239 | static_assert(PhysicalAddress(0U) != PhysicalAddress(1U)); |
| 240 | static_assert(PhysicalAddress(0) < PhysicalAddress(1)); | 240 | static_assert(PhysicalAddress(0U) < PhysicalAddress(1U)); |
| 241 | static_assert(PhysicalAddress(0) <= PhysicalAddress(1)); | 241 | static_assert(PhysicalAddress(0U) <= PhysicalAddress(1U)); |
| 242 | static_assert(PhysicalAddress(1) > PhysicalAddress(0)); | 242 | static_assert(PhysicalAddress(1U) > PhysicalAddress(0U)); |
| 243 | static_assert(PhysicalAddress(1) >= PhysicalAddress(0)); | 243 | static_assert(PhysicalAddress(1U) >= PhysicalAddress(0U)); |
| 244 | 244 | ||
| 245 | static_assert(!(PhysicalAddress(0) == PhysicalAddress(1))); | 245 | static_assert(!(PhysicalAddress(0U) == PhysicalAddress(1U))); |
| 246 | static_assert(!(PhysicalAddress(0) != PhysicalAddress(0))); | 246 | static_assert(!(PhysicalAddress(0U) != PhysicalAddress(0U))); |
| 247 | static_assert(!(PhysicalAddress(1) < PhysicalAddress(0))); | 247 | static_assert(!(PhysicalAddress(1U) < PhysicalAddress(0U))); |
| 248 | static_assert(!(PhysicalAddress(1) <= PhysicalAddress(0))); | 248 | static_assert(!(PhysicalAddress(1U) <= PhysicalAddress(0U))); |
| 249 | static_assert(!(PhysicalAddress(0) > PhysicalAddress(1))); | 249 | static_assert(!(PhysicalAddress(0U) > PhysicalAddress(1U))); |
| 250 | static_assert(!(PhysicalAddress(0) >= PhysicalAddress(1))); | 250 | static_assert(!(PhysicalAddress(0U) >= PhysicalAddress(1U))); |
| 251 | 251 | ||
| 252 | } // namespace Common | 252 | } // namespace Common |
| 253 | 253 | ||