memory_detect.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright 2020 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #ifdef _WIN32
  5. // clang-format off
  6. #include <windows.h>
  7. #include <sysinfoapi.h>
  8. // clang-format on
  9. #else
  10. #include <sys/types.h>
  11. #ifdef __APPLE__
  12. #include <sys/sysctl.h>
  13. #else
  14. #include <sys/sysinfo.h>
  15. #endif
  16. #endif
  17. #include "common/memory_detect.h"
  18. namespace Common {
  19. // Detects the RAM and Swapfile sizes
  20. static MemoryInfo Detect() {
  21. MemoryInfo mem_info{};
  22. #ifdef _WIN32
  23. MEMORYSTATUSEX memorystatus;
  24. memorystatus.dwLength = sizeof(memorystatus);
  25. GlobalMemoryStatusEx(&memorystatus);
  26. mem_info.TotalPhysicalMemory = memorystatus.ullTotalPhys;
  27. mem_info.TotalSwapMemory = memorystatus.ullTotalPageFile - mem_info.TotalPhysicalMemory;
  28. #elif defined(__APPLE__)
  29. u64 ramsize;
  30. struct xsw_usage vmusage;
  31. // hw and vm are defined in sysctl.h
  32. // https://github.com/apple/darwin-xnu/blob/master/bsd/sys/sysctl.h#L471
  33. sysctlbyname(hw.memsize, &ramsize, sizeof(ramsize), NULL, 0);
  34. sysctlbyname(vm.swapusage, &vmusage, sizeof(vmusage), NULL, 0);
  35. mem_info.TotalPhysicalMemory = ramsize;
  36. mem_info.TotalSwapMemory = vmusage.xsu_total;
  37. #else
  38. struct sysinfo meminfo;
  39. sysinfo(&meminfo);
  40. mem_info.TotalPhysicalMemory = meminfo.totalram;
  41. mem_info.TotalSwapMemory = meminfo.totalswap;
  42. #endif
  43. return mem_info;
  44. }
  45. const MemoryInfo& GetMemInfo() {
  46. static MemoryInfo mem_info = Detect();
  47. return mem_info;
  48. }
  49. } // namespace Common