Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /**************************************************************************
  2.  *
  3.  * Copyright 2008-2010 Vmware, Inc.
  4.  * All Rights Reserved.
  5.  *
  6.  * Permission is hereby granted, free of charge, to any person obtaining a
  7.  * copy of this software and associated documentation files (the
  8.  * "Software"), to deal in the Software without restriction, including
  9.  * without limitation the rights to use, copy, modify, merge, publish,
  10.  * distribute, sub license, and/or sell copies of the Software, and to
  11.  * permit persons to whom the Software is furnished to do so, subject to
  12.  * the following conditions:
  13.  *
  14.  * The above copyright notice and this permission notice (including the
  15.  * next paragraph) shall be included in all copies or substantial portions
  16.  * of the Software.
  17.  *
  18.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  19.  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20.  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
  21.  * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
  22.  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  23.  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  24.  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25.  *
  26.  **************************************************************************/
  27.  
  28.  
  29. #include "os_misc.h"
  30.  
  31. #include <stdarg.h>
  32.  
  33.  
  34. #if defined(PIPE_SUBSYSTEM_WINDOWS_USER)
  35.  
  36. #ifndef WIN32_LEAN_AND_MEAN
  37. #define WIN32_LEAN_AND_MEAN      // Exclude rarely-used stuff from Windows headers
  38. #endif
  39. #include <windows.h>
  40. #include <stdio.h>
  41.  
  42. #else
  43.  
  44. #include <stdio.h>
  45. #include <stdlib.h>
  46.  
  47. #endif
  48.  
  49.  
  50. #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_CYGWIN) || defined(PIPE_OS_SOLARIS)
  51. #  include <unistd.h>
  52. #elif defined(PIPE_OS_APPLE) || defined(PIPE_OS_BSD)
  53. #  include <sys/sysctl.h>
  54. #elif defined(PIPE_OS_HAIKU)
  55. #  include <kernel/OS.h>
  56. #elif defined(PIPE_OS_WINDOWS)
  57. #  include <windows.h>
  58. #else
  59. #error unexpected platform in os_sysinfo.c
  60. #endif
  61.  
  62.  
  63. void
  64. os_log_message(const char *message)
  65. {
  66.    /* If the GALLIUM_LOG_FILE environment variable is set to a valid filename,
  67.     * write all messages to that file.
  68.     */
  69.    static FILE *fout = NULL;
  70.  
  71.    if (!fout) {
  72.       /* one-time init */
  73.       const char *filename = os_get_option("GALLIUM_LOG_FILE");
  74.       if (filename)
  75.          fout = fopen(filename, "w");
  76.       if (!fout)
  77.          fout = stderr;
  78.    }
  79.  
  80. #if defined(PIPE_SUBSYSTEM_WINDOWS_USER)
  81.    OutputDebugStringA(message);
  82.    if(GetConsoleWindow() && !IsDebuggerPresent()) {
  83.       fflush(stdout);
  84.       fputs(message, fout);
  85.       fflush(fout);
  86.    }
  87.    else if (fout != stderr) {
  88.       fputs(message, fout);
  89.       fflush(fout);
  90.    }
  91. #else /* !PIPE_SUBSYSTEM_WINDOWS */
  92.    fflush(stdout);
  93.    fputs(message, fout);
  94.    fflush(fout);
  95. #endif
  96. }
  97.  
  98.  
  99. const char *
  100. os_get_option(const char *name)
  101. {
  102.    return getenv(name);
  103. }
  104.  
  105.  
  106. /**
  107.  * Return the size of the total physical memory.
  108.  * \param size returns the size of the total physical memory
  109.  * \return true for success, or false on failure
  110.  */
  111. bool
  112. os_get_total_physical_memory(uint64_t *size)
  113. {
  114. #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_CYGWIN) || defined(PIPE_OS_SOLARIS)
  115.    const long phys_pages = sysconf(_SC_PHYS_PAGES);
  116.    const long page_size = sysconf(_SC_PAGE_SIZE);
  117.  
  118.    *size = phys_pages * page_size;
  119.    return (phys_pages > 0 && page_size > 0);
  120. #elif defined(PIPE_OS_APPLE) || defined(PIPE_OS_BSD)
  121.    size_t len = sizeof(*size);
  122.    int mib[2];
  123.  
  124.    mib[0] = CTL_HW;
  125. #if defined(PIPE_OS_APPLE)
  126.    mib[1] = HW_MEMSIZE;
  127. #elif defined(PIPE_OS_NETBSD) || defined(PIPE_OS_OPENBSD)
  128.    mib[1] = HW_PHYSMEM64;
  129. #elif defined(PIPE_OS_FREEBSD)
  130.    mib[1] = HW_REALMEM;
  131. #elif defined(PIPE_OS_DRAGONFLY)
  132.    mib[1] = HW_PHYSMEM;
  133. #else
  134. #error Unsupported *BSD
  135. #endif
  136.  
  137.    return (sysctl(mib, 2, size, &len, NULL, 0) == 0);
  138. #elif defined(PIPE_OS_HAIKU)
  139.    system_info info;
  140.    status_t ret;
  141.  
  142.    ret = get_system_info(&info);
  143.    *size = info.max_pages * B_PAGE_SIZE;
  144.    return (ret == B_OK);
  145. #elif defined(PIPE_OS_WINDOWS)
  146.    MEMORYSTATUSEX status;
  147.    BOOL ret;
  148.  
  149.    status.dwLength = sizeof(status);
  150.    ret = GlobalMemoryStatusEx(&status);
  151.    *size = status.ullTotalPhys;
  152.    return (ret == TRUE);
  153. #else
  154. #error unexpected platform in os_sysinfo.c
  155.    return false;
  156. #endif
  157. }
  158.