diff options
Diffstat (limited to 'src/common/virtual_buffer.cpp')
| -rw-r--r-- | src/common/virtual_buffer.cpp | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/src/common/virtual_buffer.cpp b/src/common/virtual_buffer.cpp new file mode 100644 index 000000000..b426f4747 --- /dev/null +++ b/src/common/virtual_buffer.cpp | |||
| @@ -0,0 +1,52 @@ | |||
| 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 <stdio.h> | ||
| 9 | #include <sys/mman.h> | ||
| 10 | #include <sys/types.h> | ||
| 11 | #if defined __APPLE__ || defined __FreeBSD__ || defined __OpenBSD__ | ||
| 12 | #include <sys/sysctl.h> | ||
| 13 | #elif defined __HAIKU__ | ||
| 14 | #include <OS.h> | ||
| 15 | #else | ||
| 16 | #include <sys/sysinfo.h> | ||
| 17 | #endif | ||
| 18 | #endif | ||
| 19 | |||
| 20 | #include "common/assert.h" | ||
| 21 | #include "common/virtual_buffer.h" | ||
| 22 | |||
| 23 | namespace Common { | ||
| 24 | |||
| 25 | void* AllocateMemoryPages(std::size_t size) { | ||
| 26 | #ifdef _WIN32 | ||
| 27 | void* base{VirtualAlloc(nullptr, size, MEM_COMMIT, PAGE_READWRITE)}; | ||
| 28 | #else | ||
| 29 | void* base{mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0)}; | ||
| 30 | |||
| 31 | if (base == MAP_FAILED) { | ||
| 32 | base = nullptr; | ||
| 33 | } | ||
| 34 | #endif | ||
| 35 | |||
| 36 | ASSERT(base); | ||
| 37 | |||
| 38 | return base; | ||
| 39 | } | ||
| 40 | |||
| 41 | void FreeMemoryPages(void* base, std::size_t size) { | ||
| 42 | if (!base) { | ||
| 43 | return; | ||
| 44 | } | ||
| 45 | #ifdef _WIN32 | ||
| 46 | ASSERT(VirtualFree(base, 0, MEM_RELEASE)); | ||
| 47 | #else | ||
| 48 | ASSERT(munmap(base, size) == 0); | ||
| 49 | #endif | ||
| 50 | } | ||
| 51 | |||
| 52 | } // namespace Common | ||