diff --git a/uppsrc/Core/Cpu.cpp b/uppsrc/Core/Cpu.cpp index 35a11b101..fadbc8a6f 100644 --- a/uppsrc/Core/Cpu.cpp +++ b/uppsrc/Core/Cpu.cpp @@ -129,24 +129,29 @@ int CPU_Cores() #endif -void GetSystemMemoryStatus(uint64& total, uint64& free) +#ifdef PLATFORM_MACOS +#include +#include +#endif + +void GetSystemMemoryStatus(uint64& total, uint64& available) { #ifdef PLATFORM_WIN32 MEMORYSTATUSEX m; m.dwLength = sizeof(m); if(GlobalMemoryStatusEx(&m)) { total = m.ullTotalPhys; - free = m.ullAvailPhys; + available = m.ullAvailPhys; return; } #endif #ifdef PLATFORM_LINUX int pgsz = getpagesize(); total = sysconf(_SC_PHYS_PAGES); - free = sysconf(_SC_AVPHYS_PAGES); - if(total >= 0 && free >= 0) { + available = sysconf(_SC_AVPHYS_PAGES); + if(total >= 0 && available >= 0) { total *= pgsz; - free *= pgsz; + available *= pgsz; return; } #endif @@ -160,13 +165,35 @@ void GetSystemMemoryStatus(uint64& total, uint64& free) if(sysctlbyname("vm.vmtotal", &vmt, &vmt_size, NULL, 0) >= 0 && sysctlbyname("vm.stats.vm.v_page_size", &page_size, &uint_size, NULL, 0) >= 0) { - free = vmt.t_free * page_size; + available = vmt.t_free * page_size; total = vmt.t_avm * page_size; return; } +#endif +#ifdef PLATFORM_MACOS + mach_msg_type_number_t count = HOST_VM_INFO_COUNT; + vm_statistics_data_t vmstat; + + int mib[2]; + int64 physical_memory; + size_t length; + + // Get the Physical memory size + mib[0] = CTL_HW; + mib[1] = HW_MEMSIZE; + length = sizeof(int64); + + if(host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&vmstat, &count) == KERN_SUCCESS && + sysctl(mib, 2, &physical_memory, &length, NULL, 0) >= 0) { + int pgsz = getpagesize(); + available = (vmstat.free_count + vmstat.inactive_count) * pgsz; +// available = physical_memory - (vmstat.wire_count + vmstat.inactive_count) * pgsz; + total = physical_memory; + return; + } #endif total = 256*1024*1024; - free = 16*1024*1024; + available = 16*1024*1024; } #define ENDIAN_SWAP { while(count--) { EndianSwap(*v++); } } diff --git a/uppsrc/Core/Defs.h b/uppsrc/Core/Defs.h index 1aabe1d76..f7f43032f 100644 --- a/uppsrc/Core/Defs.h +++ b/uppsrc/Core/Defs.h @@ -484,7 +484,7 @@ bool CpuAVX(); int CPU_Cores(); -void GetSystemMemoryStatus(uint64& total, uint64& free); +void GetSystemMemoryStatus(uint64& total, uint64& available); template inline void Swap(T& a, T& b) { T tmp = pick(a); a = pick(b); b = pick(tmp); }