diff options
Diffstat (limited to 'src/common/free_region_manager.h')
| -rw-r--r-- | src/common/free_region_manager.h | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/src/common/free_region_manager.h b/src/common/free_region_manager.h new file mode 100644 index 000000000..2e590d609 --- /dev/null +++ b/src/common/free_region_manager.h | |||
| @@ -0,0 +1,55 @@ | |||
| 1 | // SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project | ||
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later | ||
| 3 | |||
| 4 | #pragma once | ||
| 5 | |||
| 6 | #include <mutex> | ||
| 7 | #include <boost/icl/interval_set.hpp> | ||
| 8 | |||
| 9 | namespace Common { | ||
| 10 | |||
| 11 | class FreeRegionManager { | ||
| 12 | public: | ||
| 13 | explicit FreeRegionManager() = default; | ||
| 14 | ~FreeRegionManager() = default; | ||
| 15 | |||
| 16 | void SetAddressSpace(void* start, size_t size) { | ||
| 17 | this->FreeBlock(start, size); | ||
| 18 | } | ||
| 19 | |||
| 20 | std::pair<void*, size_t> FreeBlock(void* block_ptr, size_t size) { | ||
| 21 | std::scoped_lock lk(m_mutex); | ||
| 22 | |||
| 23 | // Check to see if we are adjacent to any regions. | ||
| 24 | auto start_address = reinterpret_cast<uintptr_t>(block_ptr); | ||
| 25 | auto end_address = start_address + size; | ||
| 26 | auto it = m_free_regions.find({start_address - 1, end_address + 1}); | ||
| 27 | |||
| 28 | // If we are, join with them, ensuring we stay in bounds. | ||
| 29 | if (it != m_free_regions.end()) { | ||
| 30 | start_address = std::min(start_address, it->lower()); | ||
| 31 | end_address = std::max(end_address, it->upper()); | ||
| 32 | } | ||
| 33 | |||
| 34 | // Free the relevant region. | ||
| 35 | m_free_regions.insert({start_address, end_address}); | ||
| 36 | |||
| 37 | // Return the adjusted pointers. | ||
| 38 | block_ptr = reinterpret_cast<void*>(start_address); | ||
| 39 | size = end_address - start_address; | ||
| 40 | return {block_ptr, size}; | ||
| 41 | } | ||
| 42 | |||
| 43 | void AllocateBlock(void* block_ptr, size_t size) { | ||
| 44 | std::scoped_lock lk(m_mutex); | ||
| 45 | |||
| 46 | auto address = reinterpret_cast<uintptr_t>(block_ptr); | ||
| 47 | m_free_regions.subtract({address, address + size}); | ||
| 48 | } | ||
| 49 | |||
| 50 | private: | ||
| 51 | std::mutex m_mutex; | ||
| 52 | boost::icl::interval_set<uintptr_t> m_free_regions; | ||
| 53 | }; | ||
| 54 | |||
| 55 | } // namespace Common | ||