1 ///////////////////////////////////////////////////////////////////////////////
4 /// \brief Get the amount of physical memory
6 // Author: Lasse Collin
8 // This file has been put into the public domain.
9 // You can do whatever you want with this file.
11 ///////////////////////////////////////////////////////////////////////////////
16 // Test for Windows first, because we want to use Windows-specific code
17 // on Cygwin, which also has memory information available via sysconf(), but
18 // on Cygwin 1.5 and older it gives wrong results (from our point of view).
19 #if defined(_WIN32) || defined(__CYGWIN__)
21 # define _WIN32_WINNT 0x0500
25 #elif defined(HAVE_PHYSMEM_SYSCONF)
28 #elif defined(HAVE_PHYSMEM_SYSCTL)
29 # ifdef HAVE_SYS_PARAM_H
30 # include <sys/param.h>
32 # ifdef HAVE_SYS_SYSCTL_H
33 # include <sys/sysctl.h>
36 #elif defined(HAVE_PHYSMEM_SYSINFO)
37 # include <sys/sysinfo.h>
39 #elif defined(__DJGPP__)
44 /// \brief Get the amount of physical memory in bytes
46 /// \return Amount of physical memory in bytes. On error, zero is
48 static inline uint64_t
53 #if defined(_WIN32) || defined(__CYGWIN__)
54 if ((GetVersion() & 0xFF) >= 5) {
55 // Windows 2000 and later have GlobalMemoryStatusEx() which
56 // supports reporting values greater than 4 GiB. To keep the
57 // code working also on older Windows versions, use
58 // GlobalMemoryStatusEx() conditionally.
59 HMODULE kernel32 = GetModuleHandle("kernel32.dll");
60 if (kernel32 != NULL) {
61 BOOL (WINAPI *gmse)(LPMEMORYSTATUSEX) = GetProcAddress(
62 kernel32, "GlobalMemoryStatusEx");
64 MEMORYSTATUSEX meminfo;
65 meminfo.dwLength = sizeof(meminfo);
67 ret = meminfo.ullTotalPhys;
73 // GlobalMemoryStatus() is supported by Windows 95 and later,
74 // so it is fine to link against it unconditionally. Note that
75 // GlobalMemoryStatus() has no return value.
77 meminfo.dwLength = sizeof(meminfo);
78 GlobalMemoryStatus(&meminfo);
79 ret = meminfo.dwTotalPhys;
82 #elif defined(HAVE_PHYSMEM_SYSCONF)
83 const long pagesize = sysconf(_SC_PAGESIZE);
84 const long pages = sysconf(_SC_PHYS_PAGES);
85 if (pagesize != -1 || pages != -1)
86 // According to docs, pagesize * pages can overflow.
87 // Simple case is 32-bit box with 4 GiB or more RAM,
88 // which may report exactly 4 GiB of RAM, and "long"
89 // being 32-bit will overflow. Casting to uint64_t
90 // hopefully avoids overflows in the near future.
91 ret = (uint64_t)(pagesize) * (uint64_t)(pages);
93 #elif defined(HAVE_PHYSMEM_SYSCTL)
106 size_t mem_ptr_size = sizeof(mem.u64);
107 if (!sysctl(name, 2, &mem.u64, &mem_ptr_size, NULL, 0)) {
108 // IIRC, 64-bit "return value" is possible on some 64-bit
109 // BSD systems even with HW_PHYSMEM (instead of HW_PHYSMEM64),
111 if (mem_ptr_size == sizeof(mem.u64))
113 else if (mem_ptr_size == sizeof(mem.u32))
117 #elif defined(HAVE_PHYSMEM_SYSINFO)
119 if (sysinfo(&si) == 0)
120 ret = (uint64_t)(si.totalram) * si.mem_unit;
122 #elif defined(__DJGPP__)
123 __dpmi_free_mem_info meminfo;
124 if (__dpmi_get_free_memory_information(&meminfo) == 0
125 && meminfo.total_number_of_physical_pages
126 != (unsigned long)(-1))
127 ret = (uint64_t)(meminfo.total_number_of_physical_pages)