tgsi: break gigantic tgsi_scan_shader() function into pieces
[mesa.git] / src / gallium / auxiliary / os / 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
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 #if !defined(PIPE_SUBSYSTEM_EMBEDDED)
100 const char *
101 os_get_option(const char *name)
102 {
103 return getenv(name);
104 }
105 #endif /* !PIPE_SUBSYSTEM_EMBEDDED */
106
107
108 /**
109 * Return the size of the total physical memory.
110 * \param size returns the size of the total physical memory
111 * \return true for success, or false on failure
112 */
113 bool
114 os_get_total_physical_memory(uint64_t *size)
115 {
116 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_CYGWIN) || defined(PIPE_OS_SOLARIS)
117 const long phys_pages = sysconf(_SC_PHYS_PAGES);
118 const long page_size = sysconf(_SC_PAGE_SIZE);
119
120 *size = phys_pages * page_size;
121 return (phys_pages > 0 && page_size > 0);
122 #elif defined(PIPE_OS_APPLE) || defined(PIPE_OS_BSD)
123 size_t len = sizeof(*size);
124 int mib[2];
125
126 mib[0] = CTL_HW;
127 #if defined(PIPE_OS_APPLE)
128 mib[1] = HW_MEMSIZE;
129 #elif defined(PIPE_OS_NETBSD) || defined(PIPE_OS_OPENBSD)
130 mib[1] = HW_PHYSMEM64;
131 #elif defined(PIPE_OS_FREEBSD)
132 mib[1] = HW_REALMEM;
133 #elif defined(PIPE_OS_DRAGONFLY)
134 mib[1] = HW_PHYSMEM;
135 #else
136 #error Unsupported *BSD
137 #endif
138
139 return (sysctl(mib, 2, size, &len, NULL, 0) == 0);
140 #elif defined(PIPE_OS_HAIKU)
141 system_info info;
142 status_t ret;
143
144 ret = get_system_info(&info);
145 *size = info.max_pages * B_PAGE_SIZE;
146 return (ret == B_OK);
147 #elif defined(PIPE_OS_WINDOWS)
148 MEMORYSTATUSEX status;
149 BOOL ret;
150
151 status.dwLength = sizeof(status);
152 ret = GlobalMemoryStatusEx(&status);
153 *size = status.ullTotalPhys;
154 return (ret == TRUE);
155 #else
156 #error unexpected platform in os_sysinfo.c
157 return false;
158 #endif
159 }