OSDN Git Service

Merge remote-tracking branch 'mesa/18.3' into oreo-x86
[android-x86/external-mesa.git] / src / util / os_misc.c
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 #if defined(PIPE_OS_ANDROID)
50 #  define LOG_TAG "gallium"
51 #  include <log/log.h>
52 #elif defined(PIPE_OS_LINUX) || defined(PIPE_OS_CYGWIN) || defined(PIPE_OS_SOLARIS)
53 #  include <unistd.h>
54 #elif defined(PIPE_OS_APPLE) || defined(PIPE_OS_BSD)
55 #  include <sys/sysctl.h>
56 #elif defined(PIPE_OS_HAIKU)
57 #  include <kernel/OS.h>
58 #elif defined(PIPE_OS_WINDOWS)
59 #  include <windows.h>
60 #else
61 #error unexpected platform in os_sysinfo.c
62 #endif
63
64
65 void
66 os_log_message(const char *message)
67 {
68    /* If the GALLIUM_LOG_FILE environment variable is set to a valid filename,
69     * write all messages to that file.
70     */
71    static FILE *fout = NULL;
72
73    if (!fout) {
74 #ifdef DEBUG
75       /* one-time init */
76       const char *filename = os_get_option("GALLIUM_LOG_FILE");
77       if (filename) {
78          const char *mode = "w";
79          if (filename[0] == '+') {
80             /* If the filename is prefixed with '+' then open the file for
81              * appending instead of normal writing.
82              */
83             mode = "a";
84             filename++; /* skip the '+' */
85          }
86          fout = fopen(filename, mode);
87       }
88 #endif
89       if (!fout)
90          fout = stderr;
91    }
92
93 #if defined(PIPE_SUBSYSTEM_WINDOWS_USER)
94    OutputDebugStringA(message);
95    if(GetConsoleWindow() && !IsDebuggerPresent()) {
96       fflush(stdout);
97       fputs(message, fout);
98       fflush(fout);
99    }
100    else if (fout != stderr) {
101       fputs(message, fout);
102       fflush(fout);
103    }
104 #else /* !PIPE_SUBSYSTEM_WINDOWS */
105 #if defined(PIPE_OS_ANDROID)
106    if (fout == stderr) {
107       ALOGD("%s", message);
108       return;
109    }
110 #endif
111    fflush(stdout);
112    fputs(message, fout);
113    fflush(fout);
114 #endif
115 }
116
117
118 #if !defined(PIPE_SUBSYSTEM_EMBEDDED)
119 const char *
120 os_get_option(const char *name)
121 {
122    return getenv(name);
123 }
124 #endif /* !PIPE_SUBSYSTEM_EMBEDDED */
125
126
127 /**
128  * Return the size of the total physical memory.
129  * \param size returns the size of the total physical memory
130  * \return true for success, or false on failure
131  */
132 bool
133 os_get_total_physical_memory(uint64_t *size)
134 {
135 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_CYGWIN) || defined(PIPE_OS_SOLARIS)
136    const long phys_pages = sysconf(_SC_PHYS_PAGES);
137    const long page_size = sysconf(_SC_PAGE_SIZE);
138
139    if (phys_pages <= 0 || page_size <= 0)
140       return false;
141
142    *size = (uint64_t)phys_pages * (uint64_t)page_size;
143    return true;
144 #elif defined(PIPE_OS_APPLE) || defined(PIPE_OS_BSD)
145    size_t len = sizeof(*size);
146    int mib[2];
147
148    mib[0] = CTL_HW;
149 #if defined(PIPE_OS_APPLE)
150    mib[1] = HW_MEMSIZE;
151 #elif defined(PIPE_OS_NETBSD) || defined(PIPE_OS_OPENBSD)
152    mib[1] = HW_PHYSMEM64;
153 #elif defined(PIPE_OS_FREEBSD)
154    mib[1] = HW_REALMEM;
155 #elif defined(PIPE_OS_DRAGONFLY)
156    mib[1] = HW_PHYSMEM;
157 #else
158 #error Unsupported *BSD
159 #endif
160
161    return (sysctl(mib, 2, size, &len, NULL, 0) == 0);
162 #elif defined(PIPE_OS_HAIKU)
163    system_info info;
164    status_t ret;
165
166    ret = get_system_info(&info);
167    if (ret != B_OK || info.max_pages <= 0)
168       return false;
169
170    *size = (uint64_t)info.max_pages * (uint64_t)B_PAGE_SIZE;
171    return true;
172 #elif defined(PIPE_OS_WINDOWS)
173    MEMORYSTATUSEX status;
174    BOOL ret;
175
176    status.dwLength = sizeof(status);
177    ret = GlobalMemoryStatusEx(&status);
178    *size = status.ullTotalPhys;
179    return (ret == TRUE);
180 #else
181 #error unexpected platform in os_sysinfo.c
182    return false;
183 #endif
184 }