comparison system/System.cpp @ 221:07a96a1931c0

...
author Chris Cannam
date Thu, 25 Jan 2007 15:55:12 +0000
parents 91fdc752e540
children c7122e70f8ec
comparison
equal deleted inserted replaced
220:e08cb0158e81 221:07a96a1931c0
79 return UnknownProcessStatus; 79 return UnknownProcessStatus;
80 } 80 }
81 #endif 81 #endif
82 } 82 }
83 83
84 #ifdef _WIN32
85 /* MEMORYSTATUSEX is missing from older Windows headers, so define a
86 local replacement. This trick from MinGW source code. Ugh */
87 typedef struct
88 {
89 DWORD dwLength;
90 DWORD dwMemoryLoad;
91 DWORDLONG ullTotalPhys;
92 DWORDLONG ullAvailPhys;
93 DWORDLONG ullTotalPageFile;
94 DWORDLONG ullAvailPageFile;
95 DWORDLONG ullTotalVirtual;
96 DWORDLONG ullAvailVirtual;
97 DWORDLONG ullAvailExtendedVirtual;
98 } lMEMORYSTATUSEX;
99 typedef WINBOOL (WINAPI *PFN_MS_EX) (lMEMORYSTATUSEX*);
100 #endif
101
84 void 102 void
85 GetRealMemoryMBAvailable(int &available, int &total) 103 GetRealMemoryMBAvailable(int &available, int &total)
86 { 104 {
87 available = -1; 105 available = -1;
88 total = -1; 106 total = -1;
89 107
90 #ifdef _WIN32 108 #ifdef _WIN32
91 109
92 MEMORYSTATUSEX status; 110 static bool checked = false;
93 status.dwLength = sizeof(status); 111 static bool exFound = false;
94 if (!GlobalMemoryStatusEx(&status)) { 112 static PFN_MS_EX ex;
95 std::cerr << "WARNING: GetDiskFreeSpaceEx failed: error code " 113
96 << GetLastError() << std::endl; 114 if (!checked) {
97 return; 115
98 } 116 HMODULE h = GetModuleHandle("kernel32.dll");
99 DWORDLONG size = status.ullAvailPhys / 1048576; 117
118 if (h) {
119 if ((ex = (PFN_MS_EX)GetProcAddress(h, "GlobalMemoryStatusEx"))) {
120 exFound = true;
121 }
122 }
123
124 checked = true;
125 }
126
127 DWORDLONG avail = 0;
128 DWORDLONG total = 0;
129
130 if (exFound) {
131
132 lMEMORYSTATUSEX lms;
133 lms.dwLength = sizeof(lms);
134 if (!ex(&lms)) {
135 std::cerr << "WARNING: GlobalMemoryStatusEx failed: error code "
136 << GetLastError() << std::endl;
137 return;
138 }
139 avail = lms.ullAvailPhys;
140 total = lms.ullTotalPhys;
141
142 } else {
143
144 /* Fall back to GlobalMemoryStatus which is always available.
145 but returns wrong results for physical memory > 4GB */
146
147 MEMORYSTATUS ms;
148 GlobalMemoryStatus(&ms);
149 avail = ms.dwAvailPhys;
150 total = ms.dwTotalPhys;
151 }
152
153 DWORDLONG size = avail / 1048576;
100 if (size > INT_MAX) size = INT_MAX; 154 if (size > INT_MAX) size = INT_MAX;
101 available = int(size); 155 available = int(size);
102 156
103 size = status.ullTotalPhys / 1048576; 157 size = total / 1048576;
104 if (size > INT_MAX) size = INT_MAX; 158 if (size > INT_MAX) size = INT_MAX;
105 total = int(size); 159 total = int(size);
106 160
107 return; 161 return;
108 162