diff options
Diffstat (limited to 'src/common/virtual_buffer.cpp')
| -rw-r--r-- | src/common/virtual_buffer.cpp | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/src/common/virtual_buffer.cpp b/src/common/virtual_buffer.cpp new file mode 100644 index 000000000..e3ca29258 --- /dev/null +++ b/src/common/virtual_buffer.cpp | |||
| @@ -0,0 +1,43 @@ | |||
| 1 | // Copyright 2020 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #ifdef _WIN32 | ||
| 6 | #include <windows.h> | ||
| 7 | #else | ||
| 8 | #include <sys/mman.h> | ||
| 9 | #endif | ||
| 10 | |||
| 11 | #include "common/assert.h" | ||
| 12 | #include "common/virtual_buffer.h" | ||
| 13 | |||
| 14 | namespace Common { | ||
| 15 | |||
| 16 | void* AllocateMemoryPages(std::size_t size) noexcept { | ||
| 17 | #ifdef _WIN32 | ||
| 18 | void* base{VirtualAlloc(nullptr, size, MEM_COMMIT, PAGE_READWRITE)}; | ||
| 19 | #else | ||
| 20 | void* base{mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0)}; | ||
| 21 | |||
| 22 | if (base == MAP_FAILED) { | ||
| 23 | base = nullptr; | ||
| 24 | } | ||
| 25 | #endif | ||
| 26 | |||
| 27 | ASSERT(base); | ||
| 28 | |||
| 29 | return base; | ||
| 30 | } | ||
| 31 | |||
| 32 | void FreeMemoryPages(void* base, [[maybe_unused]] std::size_t size) noexcept { | ||
| 33 | if (!base) { | ||
| 34 | return; | ||
| 35 | } | ||
| 36 | #ifdef _WIN32 | ||
| 37 | ASSERT(VirtualFree(base, 0, MEM_RELEASE)); | ||
| 38 | #else | ||
| 39 | ASSERT(munmap(base, size) == 0); | ||
| 40 | #endif | ||
| 41 | } | ||
| 42 | |||
| 43 | } // namespace Common | ||