aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLasse Collin <lasse.collin@tukaani.org>2009-02-15 15:48:45 +0200
committerLasse Collin <lasse.collin@tukaani.org>2009-02-15 15:48:45 +0200
commit7494816ab08d82f4d6409788825930c4e43cfd0d (patch)
treea902c63c0475ba90951d411c577bddd52174e5a4 /src
parentFix microsecond vs. nanosecond confusion in my_time(). (diff)
downloadxz-7494816ab08d82f4d6409788825930c4e43cfd0d.tar.xz
Make physmem.h work on old Windows versions.
Thanks to Hongbo Ni for the original patch.
Diffstat (limited to 'src')
-rw-r--r--src/common/physmem.h31
1 files changed, 27 insertions, 4 deletions
diff --git a/src/common/physmem.h b/src/common/physmem.h
index 7075bc56..acf03c7a 100644
--- a/src/common/physmem.h
+++ b/src/common/physmem.h
@@ -81,10 +81,33 @@ physmem(void)
ret = (uint64_t)(si.totalram) * si.mem_unit;
#elif defined(_WIN32)
- MEMORYSTATUSEX meminfo;
- meminfo.dwLength = sizeof(meminfo);
- if (GlobalMemoryStatusEx(&meminfo))
- ret = meminfo.ullTotalPhys;
+ if ((GetVersion() & 0xFF) >= 5) {
+ // Windows 2000 and later have GlobalMemoryStatusEx() which
+ // supports reporting values greater than 4 GiB. To keep the
+ // code working also on older Windows versions, use
+ // GlobalMemoryStatusEx() conditionally.
+ HMODULE kernel32 = GetModuleHandle("kernel32.dll");
+ if (kernel32 != NULL) {
+ BOOL (WINAPI *gmse)(LPMEMORYSTATUSEX) = GetProcAddress(
+ kernel32, "GlobalMemoryStatusEx");
+ if (gmse != NULL) {
+ MEMORYSTATUSEX meminfo;
+ meminfo.dwLength = sizeof(meminfo);
+ if (gmse(&meminfo))
+ ret = meminfo.ullTotalPhys;
+ }
+ }
+ }
+
+ if (ret == 0) {
+ // GlobalMemoryStatus() is supported by Windows 95 and later,
+ // so it is fine to link against it unconditionally. Note that
+ // GlobalMemoryStatus() has no return value.
+ MEMORYSTATUS meminfo;
+ meminfo.dwLength = sizeof(meminfo);
+ GlobalMemoryStatus(&meminfo);
+ ret = meminfo.dwTotalPhys;
+ }
#elif defined(__DJGPP__)
__dpmi_free_mem_info meminfo;