1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file tuklib_physmem.c
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 ///////////////////////////////////////////////////////////////////////////////
13 #include "tuklib_physmem.h"
15 // We want to use Windows-specific code on Cygwin, which also has memory
16 // information available via sysconf(), but on Cygwin 1.5 and older it
17 // gives wrong results (from our point of view).
18 #if defined(_WIN32) || defined(__CYGWIN__)
20 # define _WIN32_WINNT 0x0500
24 #elif defined(__OS2__)
28 #elif defined(__DJGPP__)
32 # include <lib$routines.h>
36 #elif defined(TUKLIB_PHYSMEM_SYSCONF)
39 #elif defined(TUKLIB_PHYSMEM_SYSCTL)
40 # ifdef HAVE_SYS_PARAM_H
41 # include <sys/param.h>
43 # include <sys/sysctl.h>
46 #elif defined(TUKLIB_PHYSMEM_GETINVENT_R)
49 // This sysinfo() is Linux-specific.
50 #elif defined(TUKLIB_PHYSMEM_SYSINFO)
51 # include <sys/sysinfo.h>
60 #if defined(_WIN32) || defined(__CYGWIN__)
61 if ((GetVersion() & 0xFF) >= 5) {
62 // Windows 2000 and later have GlobalMemoryStatusEx() which
63 // supports reporting values greater than 4 GiB. To keep the
64 // code working also on older Windows versions, use
65 // GlobalMemoryStatusEx() conditionally.
66 HMODULE kernel32 = GetModuleHandle("kernel32.dll");
67 if (kernel32 != NULL) {
68 BOOL (WINAPI *gmse)(LPMEMORYSTATUSEX) = GetProcAddress(
69 kernel32, "GlobalMemoryStatusEx");
71 MEMORYSTATUSEX meminfo;
72 meminfo.dwLength = sizeof(meminfo);
74 ret = meminfo.ullTotalPhys;
80 // GlobalMemoryStatus() is supported by Windows 95 and later,
81 // so it is fine to link against it unconditionally. Note that
82 // GlobalMemoryStatus() has no return value.
84 meminfo.dwLength = sizeof(meminfo);
85 GlobalMemoryStatus(&meminfo);
86 ret = meminfo.dwTotalPhys;
89 #elif defined(__OS2__)
91 if (DosQuerySysInfo(QSV_TOTPHYSMEM, QSV_TOTPHYSMEM,
92 &mem, sizeof(mem)) == 0)
95 #elif defined(__DJGPP__)
96 __dpmi_free_mem_info meminfo;
97 if (__dpmi_get_free_memory_information(&meminfo) == 0
98 && meminfo.total_number_of_physical_pages
100 ret = (uint64_t)meminfo.total_number_of_physical_pages * 4096;
104 int val = SYI$_MEMSIZE;
105 if (LIB$GETSYI(&val, &vms_mem, 0, 0, 0, 0) == SS$_NORMAL)
106 ret = (uint64_t)vms_mem * 8192;
108 #elif defined(TUKLIB_PHYSMEM_SYSCONF)
109 const long pagesize = sysconf(_SC_PAGESIZE);
110 const long pages = sysconf(_SC_PHYS_PAGES);
111 if (pagesize != -1 || pages != -1)
112 // According to docs, pagesize * pages can overflow.
113 // Simple case is 32-bit box with 4 GiB or more RAM,
114 // which may report exactly 4 GiB of RAM, and "long"
115 // being 32-bit will overflow. Casting to uint64_t
116 // hopefully avoids overflows in the near future.
117 ret = (uint64_t)pagesize * (uint64_t)pages;
119 #elif defined(TUKLIB_PHYSMEM_SYSCTL)
132 size_t mem_ptr_size = sizeof(mem.u64);
133 if (sysctl(name, 2, &mem.u64, &mem_ptr_size, NULL, 0) != -1) {
134 // IIRC, 64-bit "return value" is possible on some 64-bit
135 // BSD systems even with HW_PHYSMEM (instead of HW_PHYSMEM64),
137 if (mem_ptr_size == sizeof(mem.u64))
139 else if (mem_ptr_size == sizeof(mem.u32))
143 #elif defined(TUKLIB_PHYSMEM_GETINVENT_R)
144 inv_state_t *st = NULL;
145 if (setinvent_r(&st) != -1) {
147 while ((i = getinvent_r(st)) != NULL) {
148 if (i->inv_class == INV_MEMORY
149 && i->inv_type == INV_MAIN_MB) {
150 ret = (uint64_t)i->inv_state << 20;
158 #elif defined(TUKLIB_PHYSMEM_SYSINFO)
160 if (sysinfo(&si) == 0)
161 ret = (uint64_t)si.totalram * si.mem_unit;