diff options
Diffstat (limited to 'src/common/memory_detect.cpp')
| -rw-r--r-- | src/common/memory_detect.cpp | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/src/common/memory_detect.cpp b/src/common/memory_detect.cpp new file mode 100644 index 000000000..b59a45d55 --- /dev/null +++ b/src/common/memory_detect.cpp | |||
| @@ -0,0 +1,57 @@ | |||
| 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 | // clang-format off | ||
| 7 | #include <windows.h> | ||
| 8 | #include <sysinfoapi.h> | ||
| 9 | // clang-format on | ||
| 10 | #else | ||
| 11 | #include <sys/types.h> | ||
| 12 | #ifdef __APPLE__ | ||
| 13 | #include <sys/sysctl.h> | ||
| 14 | #else | ||
| 15 | #include <sys/sysinfo.h> | ||
| 16 | #endif | ||
| 17 | #endif | ||
| 18 | |||
| 19 | #include "common/memory_detect.h" | ||
| 20 | |||
| 21 | namespace Common { | ||
| 22 | |||
| 23 | // Detects the RAM and Swapfile sizes | ||
| 24 | static MemoryInfo Detect() { | ||
| 25 | MemoryInfo mem_info{}; | ||
| 26 | |||
| 27 | #ifdef _WIN32 | ||
| 28 | MEMORYSTATUSEX memorystatus; | ||
| 29 | memorystatus.dwLength = sizeof(memorystatus); | ||
| 30 | GlobalMemoryStatusEx(&memorystatus); | ||
| 31 | mem_info.TotalPhysicalMemory = memorystatus.ullTotalPhys; | ||
| 32 | mem_info.TotalSwapMemory = memorystatus.ullTotalPageFile - mem_info.TotalPhysicalMemory; | ||
| 33 | #elif defined(__APPLE__) | ||
| 34 | u64 ramsize; | ||
| 35 | struct xsw_usage vmusage; | ||
| 36 | // hw and vm are defined in sysctl.h | ||
| 37 | // https://github.com/apple/darwin-xnu/blob/master/bsd/sys/sysctl.h#L471 | ||
| 38 | sysctlbyname(hw.memsize, &ramsize, sizeof(ramsize), NULL, 0); | ||
| 39 | sysctlbyname(vm.swapusage, &vmusage, sizeof(vmusage), NULL, 0); | ||
| 40 | mem_info.TotalPhysicalMemory = ramsize; | ||
| 41 | mem_info.TotalSwapMemory = vmusage.xsu_total; | ||
| 42 | #else | ||
| 43 | struct sysinfo meminfo; | ||
| 44 | sysinfo(&meminfo); | ||
| 45 | mem_info.TotalPhysicalMemory = meminfo.totalram; | ||
| 46 | mem_info.TotalSwapMemory = meminfo.totalswap; | ||
| 47 | #endif | ||
| 48 | |||
| 49 | return mem_info; | ||
| 50 | } | ||
| 51 | |||
| 52 | const MemoryInfo& GetMemInfo() { | ||
| 53 | static MemoryInfo mem_info = Detect(); | ||
| 54 | return mem_info; | ||
| 55 | } | ||
| 56 | |||
| 57 | } // namespace Common \ No newline at end of file | ||