comparison system/System.cpp @ 222:c7122e70f8ec

Fixes for Windows build
author Chris Cannam
date Thu, 25 Jan 2007 17:41:00 +0000
parents 07a96a1931c0
children bf753a9abf0c
comparison
equal deleted inserted replaced
221:07a96a1931c0 222:c7122e70f8ec
21 #include <stdint.h> 21 #include <stdint.h>
22 22
23 #ifndef _WIN32 23 #ifndef _WIN32
24 #include <signal.h> 24 #include <signal.h>
25 #include <sys/statvfs.h> 25 #include <sys/statvfs.h>
26 #include <windows.h>
26 #endif 27 #endif
27 28
28 #ifdef __APPLE__ 29 #ifdef __APPLE__
29 #include <sys/param.h> 30 #include <sys/param.h>
30 #include <sys/sysctl.h> 31 #include <sys/sysctl.h>
111 static bool exFound = false; 112 static bool exFound = false;
112 static PFN_MS_EX ex; 113 static PFN_MS_EX ex;
113 114
114 if (!checked) { 115 if (!checked) {
115 116
116 HMODULE h = GetModuleHandle("kernel32.dll"); 117 HMODULE h = GetModuleHandleA("kernel32.dll");
117 118
118 if (h) { 119 if (h) {
119 if ((ex = (PFN_MS_EX)GetProcAddress(h, "GlobalMemoryStatusEx"))) { 120 if ((ex = (PFN_MS_EX)GetProcAddress(h, "GlobalMemoryStatusEx"))) {
120 exFound = true; 121 exFound = true;
121 } 122 }
122 } 123 }
123 124
124 checked = true; 125 checked = true;
125 } 126 }
126 127
127 DWORDLONG avail = 0; 128 DWORDLONG wavail = 0;
128 DWORDLONG total = 0; 129 DWORDLONG wtotal = 0;
129 130
130 if (exFound) { 131 if (exFound) {
131 132
132 lMEMORYSTATUSEX lms; 133 lMEMORYSTATUSEX lms;
133 lms.dwLength = sizeof(lms); 134 lms.dwLength = sizeof(lms);
134 if (!ex(&lms)) { 135 if (!ex(&lms)) {
135 std::cerr << "WARNING: GlobalMemoryStatusEx failed: error code " 136 std::cerr << "WARNING: GlobalMemoryStatusEx failed: error code "
136 << GetLastError() << std::endl; 137 << GetLastError() << std::endl;
137 return; 138 return;
138 } 139 }
139 avail = lms.ullAvailPhys; 140 wavail = lms.ullAvailPhys;
140 total = lms.ullTotalPhys; 141 wtotal = lms.ullTotalPhys;
141 142
142 } else { 143 } else {
143 144
144 /* Fall back to GlobalMemoryStatus which is always available. 145 /* Fall back to GlobalMemoryStatus which is always available.
145 but returns wrong results for physical memory > 4GB */ 146 but returns wrong results for physical memory > 4GB */
146 147
147 MEMORYSTATUS ms; 148 MEMORYSTATUS ms;
148 GlobalMemoryStatus(&ms); 149 GlobalMemoryStatus(&ms);
149 avail = ms.dwAvailPhys; 150 wavail = ms.dwAvailPhys;
150 total = ms.dwTotalPhys; 151 wtotal = ms.dwTotalPhys;
151 } 152 }
152 153
153 DWORDLONG size = avail / 1048576; 154 DWORDLONG size = wavail / 1048576;
154 if (size > INT_MAX) size = INT_MAX; 155 if (size > INT_MAX) size = INT_MAX;
155 available = int(size); 156 available = int(size);
156 157
157 size = total / 1048576; 158 size = wtotal / 1048576;
158 if (size > INT_MAX) size = INT_MAX; 159 if (size > INT_MAX) size = INT_MAX;
159 total = int(size); 160 total = int(size);
160 161
161 return; 162 return;
162 163
222 223
223 int 224 int
224 GetDiscSpaceMBAvailable(const char *path) 225 GetDiscSpaceMBAvailable(const char *path)
225 { 226 {
226 #ifdef _WIN32 227 #ifdef _WIN32
227 __int64 available, total, totalFree; 228 ULARGE_INTEGER available, total, totalFree;
228 if (GetDiskFreeSpaceEx(path, &available, &total, &totalFree)) { 229 if (GetDiskFreeSpaceExA(path, &available, &total, &totalFree)) {
229 available /= 1048576; 230 __int64 a = available.QuadPart;
230 if (available > INT_MAX) available = INT_MAX; 231 a /= 1048576;
231 return int(available); 232 if (a > INT_MAX) a = INT_MAX;
233 return int(a);
232 } else { 234 } else {
233 std::cerr << "WARNING: GetDiskFreeSpaceEx failed: error code " 235 std::cerr << "WARNING: GetDiskFreeSpaceEx failed: error code "
234 << GetLastError() << std::endl; 236 << GetLastError() << std::endl;
235 return -1; 237 return -1;
236 } 238 }