aboutsummaryrefslogtreecommitdiff
path: root/CPP/Windows/System.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'CPP/Windows/System.cpp')
-rw-r--r--CPP/Windows/System.cpp41
1 files changed, 27 insertions, 14 deletions
diff --git a/CPP/Windows/System.cpp b/CPP/Windows/System.cpp
index 03c8988..5fa87f3 100644
--- a/CPP/Windows/System.cpp
+++ b/CPP/Windows/System.cpp
@@ -142,9 +142,9 @@ typedef BOOL (WINAPI *Func_GlobalMemoryStatusEx)(MY_LPMEMORYSTATUSEX lpBuffer);
142#endif // !UNDER_CE 142#endif // !UNDER_CE
143 143
144 144
145bool GetRamSize(UInt64 &size) 145bool GetRamSize(size_t &size)
146{ 146{
147 size = (UInt64)(sizeof(size_t)) << 29; 147 size = (size_t)sizeof(size_t) << 29;
148 148
149 #ifndef UNDER_CE 149 #ifndef UNDER_CE
150 MY_MEMORYSTATUSEX stat; 150 MY_MEMORYSTATUSEX stat;
@@ -167,11 +167,23 @@ bool GetRamSize(UInt64 &size)
167 "GlobalMemoryStatusEx"); 167 "GlobalMemoryStatusEx");
168 if (fn && fn(&stat)) 168 if (fn && fn(&stat))
169 { 169 {
170 size = MyMin(stat.ullTotalVirtual, stat.ullTotalPhys); 170 // (MY_MEMORYSTATUSEX::ullTotalVirtual) < 4 GiB in 32-bit mode
171 size_t size2 = (size_t)0 - 1;
172 if (size2 > stat.ullTotalPhys)
173 size2 = (size_t)stat.ullTotalPhys;
174 if (size2 > stat.ullTotalVirtual)
175 size2 = (size_t)stat.ullTotalVirtual;
176 size = size2;
171 return true; 177 return true;
172 } 178 }
173 #endif 179 #endif
174 180
181 // On computers with more than 4 GB of memory:
182 // new docs : GlobalMemoryStatus can report (-1) value to indicate an overflow.
183 // some old docs : GlobalMemoryStatus can report (modulo 4 GiB) value.
184 // (for example, if 5 GB total memory, it could report 1 GB).
185 // We don't want to get (modulo 4 GiB) value.
186 // So we use GlobalMemoryStatusEx() instead.
175 { 187 {
176 MEMORYSTATUS stat2; 188 MEMORYSTATUS stat2;
177 stat2.dwLength = sizeof(stat2); 189 stat2.dwLength = sizeof(stat2);
@@ -187,9 +199,11 @@ bool GetRamSize(UInt64 &size)
187// POSIX 199// POSIX
188// #include <stdio.h> 200// #include <stdio.h>
189 201
190bool GetRamSize(UInt64 &size) 202bool GetRamSize(size_t &size)
191{ 203{
192 size = (UInt64)(sizeof(size_t)) << 29; 204 UInt64 size64;
205 size = (size_t)sizeof(size_t) << 29;
206 size64 = size;
193 207
194#if defined(__APPLE__) || defined(__DragonFly__) || \ 208#if defined(__APPLE__) || defined(__DragonFly__) || \
195 defined(BSD) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) 209 defined(BSD) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
@@ -215,7 +229,7 @@ bool GetRamSize(UInt64 &size)
215 // we use strict check (size_sys == sizeof(val)) for returned value 229 // we use strict check (size_sys == sizeof(val)) for returned value
216 // because big-endian encoding is possible: 230 // because big-endian encoding is possible:
217 if (res == 0 && size_sys == sizeof(val) && val) 231 if (res == 0 && size_sys == sizeof(val) && val)
218 size = val; 232 size64 = val;
219 else 233 else
220 { 234 {
221 uint32_t val32 = 0; 235 uint32_t val32 = 0;
@@ -223,12 +237,12 @@ bool GetRamSize(UInt64 &size)
223 res = sysctl(mib, 2, &val32, &size_sys, NULL, 0); 237 res = sysctl(mib, 2, &val32, &size_sys, NULL, 0);
224 // printf("\n sysctl res=%d val=%llx size_sys = %d, %d\n", res, (long long int)val32, (int)size_sys, errno); 238 // printf("\n sysctl res=%d val=%llx size_sys = %d, %d\n", res, (long long int)val32, (int)size_sys, errno);
225 if (res == 0 && size_sys == sizeof(val32) && val32) 239 if (res == 0 && size_sys == sizeof(val32) && val32)
226 size = val32; 240 size64 = val32;
227 } 241 }
228 242
229 #elif defined(_AIX) 243 #elif defined(_AIX)
230 #if defined(_SC_AIX_REALMEM) // AIX 244 #if defined(_SC_AIX_REALMEM) // AIX
231 size = (UInt64)sysconf(_SC_AIX_REALMEM) * 1024; 245 size64 = (UInt64)sysconf(_SC_AIX_REALMEM) * 1024;
232 #endif 246 #endif
233 #elif 0 || defined(__sun) 247 #elif 0 || defined(__sun)
234 #if defined(_SC_PHYS_PAGES) && defined(_SC_PAGESIZE) 248 #if defined(_SC_PHYS_PAGES) && defined(_SC_PAGESIZE)
@@ -240,7 +254,7 @@ bool GetRamSize(UInt64 &size)
240 // printf("\n_SC_PHYS_PAGES (hex) = %lx", (unsigned long)phys_pages); 254 // printf("\n_SC_PHYS_PAGES (hex) = %lx", (unsigned long)phys_pages);
241 // printf("\n_SC_PAGESIZE = %lu\n", (unsigned long)page_size); 255 // printf("\n_SC_PAGESIZE = %lu\n", (unsigned long)page_size);
242 if (phys_pages != -1 && page_size != -1) 256 if (phys_pages != -1 && page_size != -1)
243 size = (UInt64)(Int64)phys_pages * (UInt64)(Int64)page_size; 257 size64 = (UInt64)(Int64)phys_pages * (UInt64)(Int64)page_size;
244 } 258 }
245 #endif 259 #endif
246 #elif defined(__gnu_hurd__) 260 #elif defined(__gnu_hurd__)
@@ -253,7 +267,7 @@ bool GetRamSize(UInt64 &size)
253 struct sysinfo info; 267 struct sysinfo info;
254 if (::sysinfo(&info) != 0) 268 if (::sysinfo(&info) != 0)
255 return false; 269 return false;
256 size = (UInt64)info.mem_unit * info.totalram; 270 size64 = (UInt64)info.mem_unit * info.totalram;
257 /* 271 /*
258 printf("\n mem_unit = %lld", (UInt64)info.mem_unit); 272 printf("\n mem_unit = %lld", (UInt64)info.mem_unit);
259 printf("\n totalram = %lld", (UInt64)info.totalram); 273 printf("\n totalram = %lld", (UInt64)info.totalram);
@@ -262,10 +276,9 @@ bool GetRamSize(UInt64 &size)
262 276
263 #endif 277 #endif
264 278
265 const UInt64 kLimit = (UInt64)1 << (sizeof(size_t) * 8 - 1); 279 size = (size_t)1 << (sizeof(size_t) * 8 - 1);
266 if (size > kLimit) 280 if (size > size64)
267 size = kLimit; 281 size = (size_t)size64;
268
269 return true; 282 return true;
270} 283}
271 284