memory_detect.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. std::size_t sizeof_ramsize = sizeof(ramsize);
  32. std::size_t sizeof_vmusage = sizeof(vmusage);
  33. // hw and vm are defined in sysctl.h
  34. // https://github.com/apple/darwin-xnu/blob/master/bsd/sys/sysctl.h#L471
  35. // sysctlbyname(const char *, void *, size_t *, void *, size_t);
  36. sysctlbyname("hw.memsize", &ramsize, &sizeof_ramsize, NULL, 0);
  37. sysctlbyname("vm.swapusage", &vmusage, &sizeof_vmusage, NULL, 0);
  38. mem_info.TotalPhysicalMemory = ramsize;
  39. mem_info.TotalSwapMemory = vmusage.xsu_total;
  40. #else
  41. struct sysinfo meminfo;
  42. sysinfo(&meminfo);
  43. mem_info.TotalPhysicalMemory = meminfo.totalram;
  44. mem_info.TotalSwapMemory = meminfo.totalswap;
  45. #endif
  46. return mem_info;
  47. }
  48. const MemoryInfo& GetMemInfo() {
  49. static MemoryInfo mem_info = Detect();
  50. return mem_info;
  51. }
  52. } // namespace Common