Merge pull request #4208 from jbeich/freebsd

common: unbreak build on BSDs
This commit is contained in:
LC 2020-07-01 00:34:20 -04:00 committed by GitHub
commit 0b954a3305
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -9,10 +9,12 @@
// clang-format on // clang-format on
#else #else
#include <sys/types.h> #include <sys/types.h>
#ifdef __APPLE__ #if defined(__APPLE__) || defined(__FreeBSD__)
#include <sys/sysctl.h> #include <sys/sysctl.h>
#else #elif defined(__linux__)
#include <sys/sysinfo.h> #include <sys/sysinfo.h>
#else
#include <unistd.h>
#endif #endif
#endif #endif
@ -38,15 +40,26 @@ static MemoryInfo Detect() {
// hw and vm are defined in sysctl.h // hw and vm are defined in sysctl.h
// https://github.com/apple/darwin-xnu/blob/master/bsd/sys/sysctl.h#L471 // https://github.com/apple/darwin-xnu/blob/master/bsd/sys/sysctl.h#L471
// sysctlbyname(const char *, void *, size_t *, void *, size_t); // sysctlbyname(const char *, void *, size_t *, void *, size_t);
sysctlbyname("hw.memsize", &ramsize, &sizeof_ramsize, NULL, 0); sysctlbyname("hw.memsize", &ramsize, &sizeof_ramsize, nullptr, 0);
sysctlbyname("vm.swapusage", &vmusage, &sizeof_vmusage, NULL, 0); sysctlbyname("vm.swapusage", &vmusage, &sizeof_vmusage, nullptr, 0);
mem_info.TotalPhysicalMemory = ramsize; mem_info.TotalPhysicalMemory = ramsize;
mem_info.TotalSwapMemory = vmusage.xsu_total; mem_info.TotalSwapMemory = vmusage.xsu_total;
#else #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);
sysctlbyname("hw.physmem", &physmem, &sizeof_u_long, nullptr, 0);
sysctlbyname("vm.swap_total", &swap_total, &sizeof_u_long, nullptr, 0);
mem_info.TotalPhysicalMemory = physmem;
mem_info.TotalSwapMemory = swap_total;
#elif defined(__linux__)
struct sysinfo meminfo; struct sysinfo meminfo;
sysinfo(&meminfo); sysinfo(&meminfo);
mem_info.TotalPhysicalMemory = meminfo.totalram; mem_info.TotalPhysicalMemory = meminfo.totalram;
mem_info.TotalSwapMemory = meminfo.totalswap; mem_info.TotalSwapMemory = meminfo.totalswap;
#else
mem_info.TotalPhysicalMemory = sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGE_SIZE);
mem_info.TotalSwapMemory = 0;
#endif #endif
return mem_info; return mem_info;