Added few more stubs so that control reaches to DestroyDevice().
[mesa.git] / src / util / os_misc.c
index b6359ed195ca847ecd9eb14ac8e967bde0d84ed4..e184edc893da432bb187189ec903a6c7185ae512 100644 (file)
@@ -27,6 +27,8 @@
 
 
 #include "os_misc.h"
+#include "os_file.h"
+#include "macros.h"
 
 #include <stdarg.h>
 
 #endif
 #include <windows.h>
 #include <stdio.h>
+#include <stdlib.h>
 
 #else
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
+#include <inttypes.h>
 
 #endif
 
 
-#if defined(PIPE_OS_LINUX) || defined(PIPE_OS_CYGWIN) || defined(PIPE_OS_SOLARIS) || defined(PIPE_OS_HURD)
+#if DETECT_OS_ANDROID
+#  define LOG_TAG "MESA"
+#  include <unistd.h>
+#  include <log/log.h>
+#elif DETECT_OS_LINUX || DETECT_OS_CYGWIN || DETECT_OS_SOLARIS || DETECT_OS_HURD
 #  include <unistd.h>
-#elif defined(PIPE_OS_APPLE) || defined(PIPE_OS_BSD)
+#elif DETECT_OS_OPENBSD
+#  include <sys/resource.h>
+#  include <sys/sysctl.h>
+#elif DETECT_OS_APPLE || DETECT_OS_BSD
 #  include <sys/sysctl.h>
-#elif defined(PIPE_OS_HAIKU)
+#elif DETECT_OS_HAIKU
 #  include <kernel/OS.h>
-#elif defined(PIPE_OS_WINDOWS)
+#elif DETECT_OS_WINDOWS
 #  include <windows.h>
 #else
 #error unexpected platform in os_sysinfo.c
@@ -103,6 +115,9 @@ os_log_message(const char *message)
    fflush(stdout);
    fputs(message, fout);
    fflush(fout);
+#  if DETECT_OS_ANDROID
+   LOG_PRI(ANDROID_LOG_ERROR, LOG_TAG, "%s", message);
+#  endif
 #endif
 }
 
@@ -124,7 +139,7 @@ os_get_option(const char *name)
 bool
 os_get_total_physical_memory(uint64_t *size)
 {
-#if defined(PIPE_OS_LINUX) || defined(PIPE_OS_CYGWIN) || defined(PIPE_OS_SOLARIS) || defined(PIPE_OS_HURD)
+#if DETECT_OS_LINUX || DETECT_OS_CYGWIN || DETECT_OS_SOLARIS || DETECT_OS_HURD
    const long phys_pages = sysconf(_SC_PHYS_PAGES);
    const long page_size = sysconf(_SC_PAGE_SIZE);
 
@@ -133,25 +148,25 @@ os_get_total_physical_memory(uint64_t *size)
 
    *size = (uint64_t)phys_pages * (uint64_t)page_size;
    return true;
-#elif defined(PIPE_OS_APPLE) || defined(PIPE_OS_BSD)
+#elif DETECT_OS_APPLE || DETECT_OS_BSD
    size_t len = sizeof(*size);
    int mib[2];
 
    mib[0] = CTL_HW;
-#if defined(PIPE_OS_APPLE)
+#if DETECT_OS_APPLE
    mib[1] = HW_MEMSIZE;
-#elif defined(PIPE_OS_NETBSD) || defined(PIPE_OS_OPENBSD)
+#elif DETECT_OS_NETBSD || DETECT_OS_OPENBSD
    mib[1] = HW_PHYSMEM64;
-#elif defined(PIPE_OS_FREEBSD)
+#elif DETECT_OS_FREEBSD
    mib[1] = HW_REALMEM;
-#elif defined(PIPE_OS_DRAGONFLY)
+#elif DETECT_OS_DRAGONFLY
    mib[1] = HW_PHYSMEM;
 #else
 #error Unsupported *BSD
 #endif
 
    return (sysctl(mib, 2, size, &len, NULL, 0) == 0);
-#elif defined(PIPE_OS_HAIKU)
+#elif DETECT_OS_HAIKU
    system_info info;
    status_t ret;
 
@@ -161,7 +176,7 @@ os_get_total_physical_memory(uint64_t *size)
 
    *size = (uint64_t)info.max_pages * (uint64_t)B_PAGE_SIZE;
    return true;
-#elif defined(PIPE_OS_WINDOWS)
+#elif DETECT_OS_WINDOWS
    MEMORYSTATUSEX status;
    BOOL ret;
 
@@ -174,3 +189,47 @@ os_get_total_physical_memory(uint64_t *size)
    return false;
 #endif
 }
+
+bool
+os_get_available_system_memory(uint64_t *size)
+{
+#if DETECT_OS_LINUX
+   char *meminfo = os_read_file("/proc/meminfo", NULL);
+   if (!meminfo)
+      return false;
+
+   char *str = strstr(meminfo, "MemAvailable:");
+   if (!str) {
+      free(meminfo);
+      return false;
+   }
+
+   uint64_t kb_mem_available;
+   if (sscanf(str, "MemAvailable: %" PRIx64, &kb_mem_available) == 1) {
+      free(meminfo);
+      *size = kb_mem_available << 10;
+      return true;
+   }
+
+   free(meminfo);
+   return false;
+#elif DETECT_OS_OPENBSD
+   struct rlimit rl;
+   int mib[] = { CTL_HW, HW_USERMEM64 };
+   int64_t mem_available;
+   size_t len = sizeof(mem_available);
+
+   /* physmem - wired */
+   if (sysctl(mib, 2, &mem_available, &len, NULL, 0) == -1)
+      return false;
+
+   /* static login.conf limit */
+   if (getrlimit(RLIMIT_DATA, &rl) == -1)
+      return false;
+
+   *size = MIN2(mem_available, rl.rlim_cur);
+   return true;
+#else
+   return false;
+#endif
+}