2022-04-23 03:59:50 -05:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2020-05-17 13:45:12 -05:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
// clang-format off
|
|
|
|
#include <windows.h>
|
|
|
|
#include <sysinfoapi.h>
|
|
|
|
// clang-format on
|
|
|
|
#else
|
|
|
|
#include <sys/types.h>
|
2020-06-29 17:39:31 -05:00
|
|
|
#if defined(__APPLE__) || defined(__FreeBSD__)
|
2020-05-17 13:45:12 -05:00
|
|
|
#include <sys/sysctl.h>
|
2020-06-29 17:39:31 -05:00
|
|
|
#elif defined(__linux__)
|
2020-05-17 13:45:12 -05:00
|
|
|
#include <sys/sysinfo.h>
|
2020-06-29 17:39:31 -05:00
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
2020-05-17 13:45:12 -05:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "common/memory_detect.h"
|
|
|
|
|
|
|
|
namespace Common {
|
|
|
|
|
|
|
|
// Detects the RAM and Swapfile sizes
|
|
|
|
static MemoryInfo Detect() {
|
|
|
|
MemoryInfo mem_info{};
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
MEMORYSTATUSEX memorystatus;
|
|
|
|
memorystatus.dwLength = sizeof(memorystatus);
|
|
|
|
GlobalMemoryStatusEx(&memorystatus);
|
|
|
|
mem_info.TotalPhysicalMemory = memorystatus.ullTotalPhys;
|
|
|
|
mem_info.TotalSwapMemory = memorystatus.ullTotalPageFile - mem_info.TotalPhysicalMemory;
|
|
|
|
#elif defined(__APPLE__)
|
|
|
|
u64 ramsize;
|
|
|
|
struct xsw_usage vmusage;
|
2020-05-27 10:21:59 -05:00
|
|
|
std::size_t sizeof_ramsize = sizeof(ramsize);
|
|
|
|
std::size_t sizeof_vmusage = sizeof(vmusage);
|
2020-05-17 13:45:12 -05:00
|
|
|
// hw and vm are defined in sysctl.h
|
|
|
|
// https://github.com/apple/darwin-xnu/blob/master/bsd/sys/sysctl.h#L471
|
2020-05-27 10:21:59 -05:00
|
|
|
// sysctlbyname(const char *, void *, size_t *, void *, size_t);
|
2020-06-30 17:55:47 -05:00
|
|
|
sysctlbyname("hw.memsize", &ramsize, &sizeof_ramsize, nullptr, 0);
|
|
|
|
sysctlbyname("vm.swapusage", &vmusage, &sizeof_vmusage, nullptr, 0);
|
2020-05-17 13:45:12 -05:00
|
|
|
mem_info.TotalPhysicalMemory = ramsize;
|
|
|
|
mem_info.TotalSwapMemory = vmusage.xsu_total;
|
2020-06-29 17:39:31 -05:00
|
|
|
#elif defined(__FreeBSD__)
|
|
|
|
u_long physmem, swap_total;
|
|
|
|
std::size_t sizeof_u_long = sizeof(u_long);
|
|
|
|
// sysctlbyname(const char *, void *, size_t *, const void *, size_t);
|
2020-06-30 17:55:47 -05:00
|
|
|
sysctlbyname("hw.physmem", &physmem, &sizeof_u_long, nullptr, 0);
|
|
|
|
sysctlbyname("vm.swap_total", &swap_total, &sizeof_u_long, nullptr, 0);
|
2020-06-29 17:39:31 -05:00
|
|
|
mem_info.TotalPhysicalMemory = physmem;
|
|
|
|
mem_info.TotalSwapMemory = swap_total;
|
|
|
|
#elif defined(__linux__)
|
2020-05-17 13:45:12 -05:00
|
|
|
struct sysinfo meminfo;
|
|
|
|
sysinfo(&meminfo);
|
|
|
|
mem_info.TotalPhysicalMemory = meminfo.totalram;
|
|
|
|
mem_info.TotalSwapMemory = meminfo.totalswap;
|
2020-06-29 17:39:31 -05:00
|
|
|
#else
|
|
|
|
mem_info.TotalPhysicalMemory = sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGE_SIZE);
|
|
|
|
mem_info.TotalSwapMemory = 0;
|
2020-05-17 13:45:12 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return mem_info;
|
|
|
|
}
|
|
|
|
|
|
|
|
const MemoryInfo& GetMemInfo() {
|
|
|
|
static MemoryInfo mem_info = Detect();
|
|
|
|
return mem_info;
|
|
|
|
}
|
|
|
|
|
2022-03-18 23:23:48 -05:00
|
|
|
} // namespace Common
|