diff options
Diffstat (limited to 'src/common/virtual_buffer.h')
| -rw-r--r-- | src/common/virtual_buffer.h | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/src/common/virtual_buffer.h b/src/common/virtual_buffer.h new file mode 100644 index 000000000..da064e59e --- /dev/null +++ b/src/common/virtual_buffer.h | |||
| @@ -0,0 +1,58 @@ | |||
| 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 "common/common_funcs.h" | ||
| 8 | |||
| 9 | namespace Common { | ||
| 10 | |||
| 11 | void* AllocateMemoryPages(std::size_t size); | ||
| 12 | void FreeMemoryPages(void* base, std::size_t size); | ||
| 13 | |||
| 14 | template <typename T> | ||
| 15 | class VirtualBuffer final : NonCopyable { | ||
| 16 | public: | ||
| 17 | constexpr VirtualBuffer() = default; | ||
| 18 | explicit VirtualBuffer(std::size_t count) : alloc_size{count * sizeof(T)} { | ||
| 19 | base_ptr = reinterpret_cast<T*>(AllocateMemoryPages(alloc_size)); | ||
| 20 | } | ||
| 21 | |||
| 22 | ~VirtualBuffer() { | ||
| 23 | FreeMemoryPages(base_ptr, alloc_size); | ||
| 24 | } | ||
| 25 | |||
| 26 | void resize(std::size_t count) { | ||
| 27 | FreeMemoryPages(base_ptr, alloc_size); | ||
| 28 | |||
| 29 | alloc_size = count * sizeof(T); | ||
| 30 | base_ptr = reinterpret_cast<T*>(AllocateMemoryPages(alloc_size)); | ||
| 31 | } | ||
| 32 | |||
| 33 | constexpr const T& operator[](std::size_t index) const { | ||
| 34 | return base_ptr[index]; | ||
| 35 | } | ||
| 36 | |||
| 37 | constexpr T& operator[](std::size_t index) { | ||
| 38 | return base_ptr[index]; | ||
| 39 | } | ||
| 40 | |||
| 41 | constexpr T* data() { | ||
| 42 | return base_ptr; | ||
| 43 | } | ||
| 44 | |||
| 45 | constexpr const T* data() const { | ||
| 46 | return base_ptr; | ||
| 47 | } | ||
| 48 | |||
| 49 | constexpr std::size_t size() const { | ||
| 50 | return alloc_size / sizeof(T); | ||
| 51 | } | ||
| 52 | |||
| 53 | private: | ||
| 54 | std::size_t alloc_size{}; | ||
| 55 | T* base_ptr{}; | ||
| 56 | }; | ||
| 57 | |||
| 58 | } // namespace Common | ||