diff options
Diffstat (limited to 'src/common/virtual_buffer.h')
| -rw-r--r-- | src/common/virtual_buffer.h | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/src/common/virtual_buffer.h b/src/common/virtual_buffer.h new file mode 100644 index 000000000..91d430036 --- /dev/null +++ b/src/common/virtual_buffer.h | |||
| @@ -0,0 +1,82 @@ | |||
| 1 | // Copyright 2020 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <type_traits> | ||
| 8 | #include <utility> | ||
| 9 | |||
| 10 | namespace Common { | ||
| 11 | |||
| 12 | void* AllocateMemoryPages(std::size_t size) noexcept; | ||
| 13 | void FreeMemoryPages(void* base, std::size_t size) noexcept; | ||
| 14 | |||
| 15 | template <typename T> | ||
| 16 | class VirtualBuffer final { | ||
| 17 | public: | ||
| 18 | static_assert( | ||
| 19 | std::is_trivially_constructible_v<T>, | ||
| 20 | "T must be trivially constructible, as non-trivial constructors will not be executed " | ||
| 21 | "with the current allocator"); | ||
| 22 | |||
| 23 | constexpr VirtualBuffer() = default; | ||
| 24 | explicit VirtualBuffer(std::size_t count) : alloc_size{count * sizeof(T)} { | ||
| 25 | base_ptr = reinterpret_cast<T*>(AllocateMemoryPages(alloc_size)); | ||
| 26 | } | ||
| 27 | |||
| 28 | ~VirtualBuffer() noexcept { | ||
| 29 | FreeMemoryPages(base_ptr, alloc_size); | ||
| 30 | } | ||
| 31 | |||
| 32 | VirtualBuffer(const VirtualBuffer&) = delete; | ||
| 33 | VirtualBuffer& operator=(const VirtualBuffer&) = delete; | ||
| 34 | |||
| 35 | VirtualBuffer(VirtualBuffer&& other) noexcept | ||
| 36 | : alloc_size{std::exchange(other.alloc_size, 0)}, base_ptr{std::exchange(other.base_ptr), | ||
| 37 | nullptr} {} | ||
| 38 | |||
| 39 | VirtualBuffer& operator=(VirtualBuffer&& other) noexcept { | ||
| 40 | alloc_size = std::exchange(other.alloc_size, 0); | ||
| 41 | base_ptr = std::exchange(other.base_ptr, nullptr); | ||
| 42 | return *this; | ||
| 43 | } | ||
| 44 | |||
| 45 | void resize(std::size_t count) { | ||
| 46 | const auto new_size = count * sizeof(T); | ||
| 47 | if (new_size == alloc_size) { | ||
| 48 | return; | ||
| 49 | } | ||
| 50 | |||
| 51 | FreeMemoryPages(base_ptr, alloc_size); | ||
| 52 | |||
| 53 | alloc_size = new_size; | ||
| 54 | base_ptr = reinterpret_cast<T*>(AllocateMemoryPages(alloc_size)); | ||
| 55 | } | ||
| 56 | |||
| 57 | [[nodiscard]] constexpr const T& operator[](std::size_t index) const { | ||
| 58 | return base_ptr[index]; | ||
| 59 | } | ||
| 60 | |||
| 61 | [[nodiscard]] constexpr T& operator[](std::size_t index) { | ||
| 62 | return base_ptr[index]; | ||
| 63 | } | ||
| 64 | |||
| 65 | [[nodiscard]] constexpr T* data() { | ||
| 66 | return base_ptr; | ||
| 67 | } | ||
| 68 | |||
| 69 | [[nodiscard]] constexpr const T* data() const { | ||
| 70 | return base_ptr; | ||
| 71 | } | ||
| 72 | |||
| 73 | [[nodiscard]] constexpr std::size_t size() const { | ||
| 74 | return alloc_size / sizeof(T); | ||
| 75 | } | ||
| 76 | |||
| 77 | private: | ||
| 78 | std::size_t alloc_size{}; | ||
| 79 | T* base_ptr{}; | ||
| 80 | }; | ||
| 81 | |||
| 82 | } // namespace Common | ||