util: Prevent implicit declaration of function getenv.
[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 DETECT_OS_WINDOWS
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 #include <stdlib.h>
42
43 #else
44
45 #include <stdio.h>
46 #include <stdlib.h>
47
48 #endif
49
50
51 #if DETECT_OS_LINUX || DETECT_OS_CYGWIN || DETECT_OS_SOLARIS || DETECT_OS_HURD
52 # include <unistd.h>
53 #elif DETECT_OS_APPLE || DETECT_OS_BSD
54 # include <sys/sysctl.h>
55 #elif DETECT_OS_HAIKU
56 # include <kernel/OS.h>
57 #elif DETECT_OS_WINDOWS
58 # include <windows.h>
59 #else
60 #error unexpected platform in os_sysinfo.c
61 #endif
62
63
64 void
65 os_log_message(const char *message)
66 {
67 /* If the GALLIUM_LOG_FILE environment variable is set to a valid filename,
68 * write all messages to that file.
69 */
70 static FILE *fout = NULL;
71
72 if (!fout) {
73 #ifdef DEBUG
74 /* one-time init */
75 const char *filename = os_get_option("GALLIUM_LOG_FILE");
76 if (filename) {
77 const char *mode = "w";
78 if (filename[0] == '+') {
79 /* If the filename is prefixed with '+' then open the file for
80 * appending instead of normal writing.
81 */
82 mode = "a";
83 filename++; /* skip the '+' */
84 }
85 fout = fopen(filename, mode);
86 }
87 #endif
88 if (!fout)
89 fout = stderr;
90 }
91
92 #if DETECT_OS_WINDOWS
93 OutputDebugStringA(message);
94 if(GetConsoleWindow() && !IsDebuggerPresent()) {
95 fflush(stdout);
96 fputs(message, fout);
97 fflush(fout);
98 }
99 else if (fout != stderr) {
100 fputs(message, fout);
101 fflush(fout);
102 }
103 #else /* !DETECT_OS_WINDOWS */
104 fflush(stdout);
105 fputs(message, fout);
106 fflush(fout);
107 #endif
108 }
109
110
111 #if !defined(EMBEDDED_DEVICE)
112 const char *
113 os_get_option(const char *name)
114 {
115 return getenv(name);
116 }
117 #endif /* !EMBEDDED_DEVICE */
118
119
120 /**
121 * Return the size of the total physical memory.
122 * \param size returns the size of the total physical memory
123 * \return true for success, or false on failure
124 */
125 bool
126 os_get_total_physical_memory(uint64_t *size)
127 {
128 #if DETECT_OS_LINUX || DETECT_OS_CYGWIN || DETECT_OS_SOLARIS || DETECT_OS_HURD
129 const long phys_pages = sysconf(_SC_PHYS_PAGES);
130 const long page_size = sysconf(_SC_PAGE_SIZE);
131
132 if (phys_pages <= 0 || page_size <= 0)
133 return false;
134
135 *size = (uint64_t)phys_pages * (uint64_t)page_size;
136 return true;
137 #elif DETECT_OS_APPLE || DETECT_OS_BSD
138 size_t len = sizeof(*size);
139 int mib[2];
140
141 mib[0] = CTL_HW;
142 #if DETECT_OS_APPLE
143 mib[1] = HW_MEMSIZE;
144 #elif DETECT_OS_NETBSD || DETECT_OS_OPENBSD
145 mib[1] = HW_PHYSMEM64;
146 #elif DETECT_OS_FREEBSD
147 mib[1] = HW_REALMEM;
148 #elif DETECT_OS_DRAGONFLY
149 mib[1] = HW_PHYSMEM;
150 #else
151 #error Unsupported *BSD
152 #endif
153
154 return (sysctl(mib, 2, size, &len, NULL, 0) == 0);
155 #elif DETECT_OS_HAIKU
156 system_info info;
157 status_t ret;
158
159 ret = get_system_info(&info);
160 if (ret != B_OK || info.max_pages <= 0)
161 return false;
162
163 *size = (uint64_t)info.max_pages * (uint64_t)B_PAGE_SIZE;
164 return true;
165 #elif DETECT_OS_WINDOWS
166 MEMORYSTATUSEX status;
167 BOOL ret;
168
169 status.dwLength = sizeof(status);
170 ret = GlobalMemoryStatusEx(&status);
171 *size = status.ullTotalPhys;
172 return (ret == TRUE);
173 #else
174 #error unexpected platform in os_sysinfo.c
175 return false;
176 #endif
177 }