From e19065aa429439fc8c829687cdb5109355bca159 Mon Sep 17 00:00:00 2001 From: Chenxi Zhou Date: Wed, 4 May 2022 13:31:03 +0100 Subject: [PATCH] OSX compatibility --- asset.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 57 insertions(+), 2 deletions(-) diff --git a/asset.c b/asset.c index abf1c45..579cc3d 100644 --- a/asset.c +++ b/asset.c @@ -33,9 +33,12 @@ #include #include #include +#include #include "asset.h" +#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) + double cputime(void) { struct rusage r; @@ -61,6 +64,56 @@ double realtime(void) return tp.tv_sec + tp.tv_usec * 1e-6; } +long physmem_total(void) +{ + #if defined _SC_PHYS_PAGES && defined _SC_PAGESIZE + { + /* This works on linux-gnu, solaris2 and cygwin */ + long pages = sysconf(_SC_PHYS_PAGES); + long pagesize = sysconf(_SC_PAGESIZE); + if (0 <= pages && 0 <= pagesize) + return pages * pagesize; + } + #elif defined CTL_HW && defined HW_PHYSMEM + { + /* This works on *bsd and darwin */ + unsigned int physmem; + size_t len = sizeof physmem; + static int mib[2] = { CTL_HW, HW_PHYSMEM }; + if (sysctl (mib, ARRAY_SIZE(mib), &physmem, &len, NULL, 0) == 0 + && len == sizeof (physmem)) + return physmem; + } + #endif + + return 0; +} + +long physmem_avail(void) +{ + #if defined _SC_AVPHYS_PAGES && defined _SC_PAGESIZE + { + /* This works on linux-gnu, solaris2 and cygwin */ + long pages = sysconf(_SC_AVPHYS_PAGES); + long pagesize = sysconf(_SC_PAGESIZE); + if (0 <= pages && 0 <= pagesize) + return pages * pagesize; + } + #elif defined CTL_HW && defined HW_USERMEM + { + /* This works on *bsd and darwin */ + unsigned int usermem; + size_t len = sizeof usermem; + static int mib[2] = { CTL_HW, HW_USERMEM }; + if (sysctl (mib, ARRAY_SIZE(mib), &usermem, &len, NULL, 0) == 0 + && len == sizeof (usermem)) + return usermem; + } + #endif + + return 0; +} + void ram_limit(long *total, long *avail) { long t, a, a1; @@ -69,9 +122,11 @@ void ram_limit(long *total, long *avail) a = lim.rlim_cur; getrlimit(RLIMIT_AS, &lim); a = MAX(a, (long) lim.rlim_cur); - a1 = (long) (sysconf(_SC_AVPHYS_PAGES) * sysconf(_SC_PAGESIZE)); + // a1 = (long) (sysconf(_SC_AVPHYS_PAGES) * sysconf(_SC_PAGESIZE)); + a1 = physmem_avail(); a = a > 0? MIN(a, a1) : a1; - t = sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE); + // t = sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE); + t = physmem_total(); *avail = a; *total = t; }