summaryrefslogtreecommitdiff
path: root/src/common/memory_detect.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/memory_detect.cpp')
-rw-r--r--src/common/memory_detect.cpp73
1 files changed, 73 insertions, 0 deletions
diff --git a/src/common/memory_detect.cpp b/src/common/memory_detect.cpp
new file mode 100644
index 000000000..8cff6ec37
--- /dev/null
+++ b/src/common/memory_detect.cpp
@@ -0,0 +1,73 @@
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#if defined(__APPLE__) || defined(__FreeBSD__)
13#include <sys/sysctl.h>
14#elif defined(__linux__)
15#include <sys/sysinfo.h>
16#else
17#include <unistd.h>
18#endif
19#endif
20
21#include "common/memory_detect.h"
22
23namespace Common {
24
25// Detects the RAM and Swapfile sizes
26static MemoryInfo Detect() {
27 MemoryInfo mem_info{};
28
29#ifdef _WIN32
30 MEMORYSTATUSEX memorystatus;
31 memorystatus.dwLength = sizeof(memorystatus);
32 GlobalMemoryStatusEx(&memorystatus);
33 mem_info.TotalPhysicalMemory = memorystatus.ullTotalPhys;
34 mem_info.TotalSwapMemory = memorystatus.ullTotalPageFile - mem_info.TotalPhysicalMemory;
35#elif defined(__APPLE__)
36 u64 ramsize;
37 struct xsw_usage vmusage;
38 std::size_t sizeof_ramsize = sizeof(ramsize);
39 std::size_t sizeof_vmusage = sizeof(vmusage);
40 // hw and vm are defined in sysctl.h
41 // https://github.com/apple/darwin-xnu/blob/master/bsd/sys/sysctl.h#L471
42 // sysctlbyname(const char *, void *, size_t *, void *, size_t);
43 sysctlbyname("hw.memsize", &ramsize, &sizeof_ramsize, nullptr, 0);
44 sysctlbyname("vm.swapusage", &vmusage, &sizeof_vmusage, nullptr, 0);
45 mem_info.TotalPhysicalMemory = ramsize;
46 mem_info.TotalSwapMemory = vmusage.xsu_total;
47#elif defined(__FreeBSD__)
48 u_long physmem, swap_total;
49 std::size_t sizeof_u_long = sizeof(u_long);
50 // sysctlbyname(const char *, void *, size_t *, const void *, size_t);
51 sysctlbyname("hw.physmem", &physmem, &sizeof_u_long, nullptr, 0);
52 sysctlbyname("vm.swap_total", &swap_total, &sizeof_u_long, nullptr, 0);
53 mem_info.TotalPhysicalMemory = physmem;
54 mem_info.TotalSwapMemory = swap_total;
55#elif defined(__linux__)
56 struct sysinfo meminfo;
57 sysinfo(&meminfo);
58 mem_info.TotalPhysicalMemory = meminfo.totalram;
59 mem_info.TotalSwapMemory = meminfo.totalswap;
60#else
61 mem_info.TotalPhysicalMemory = sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGE_SIZE);
62 mem_info.TotalSwapMemory = 0;
63#endif
64
65 return mem_info;
66}
67
68const MemoryInfo& GetMemInfo() {
69 static MemoryInfo mem_info = Detect();
70 return mem_info;
71}
72
73} // namespace Common \ No newline at end of file