compress_parallel.c: Determine physical memory on OS X
authorEric Biggers <ebiggers3@gmail.com>
Sat, 1 Feb 2014 01:43:24 +0000 (19:43 -0600)
committerEric Biggers <ebiggers3@gmail.com>
Sat, 1 Feb 2014 01:45:34 +0000 (19:45 -0600)
configure.ac
src/compress_parallel.c

index 198fa29..00cbfb3 100644 (file)
@@ -67,6 +67,7 @@ AC_CHECK_HEADERS([alloca.h            \
                  sys/endian.h          \
                  sys/file.h            \
                  sys/param.h           \
+                 sys/sysctl.h          \
                  sys/times.h           \
                  time.h                \
                  utime.h])
index 376e59d..eeb9f6f 100644 (file)
@@ -43,6 +43,9 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#ifdef HAVE_SYS_SYSCTL_H
+#  include <sys/sysctl.h>
+#endif
 
 struct message_queue {
        struct list_head list;
@@ -116,12 +119,19 @@ get_avail_memory(void)
        if (phys_bytes == 0)
                goto default_size;
        return phys_bytes;
-#else
+#elif defined(_SC_PAGESIZE) && defined(_SC_PHYS_PAGES)
        long page_size = sysconf(_SC_PAGESIZE);
        long num_pages = sysconf(_SC_PHYS_PAGES);
        if (page_size <= 0 || num_pages <= 0)
                goto default_size;
        return ((u64)page_size * (u64)num_pages);
+#else
+       int mib[2] = {CTL_HW, HW_MEMSIZE};
+       u64 memsize;
+       size_t len = sizeof(memsize);
+       if (sysctl(mib, ARRAY_LEN(mib), &memsize, &len, NULL, 0) < 0 || len != 8)
+               goto default_size;
+       return memsize;
 #endif
 
 default_size: