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>
37 #elif defined(TUKLIB_PHYSMEM_AIX)
38 # include <sys/systemcfg.h>
40 #elif defined(TUKLIB_PHYSMEM_SYSCONF)
43 #elif defined(TUKLIB_PHYSMEM_SYSCTL)
44 # ifdef HAVE_SYS_PARAM_H
45 # include <sys/param.h>
47 # include <sys/sysctl.h>
50 #elif defined(TUKLIB_PHYSMEM_GETSYSINFO)
51 # include <sys/sysinfo.h>
52 # include <machine/hal_sysinfo.h>
55 #elif defined(TUKLIB_PHYSMEM_PSTAT_GETSTATIC)
56 # include <sys/param.h>
57 # include <sys/pstat.h>
60 #elif defined(TUKLIB_PHYSMEM_GETINVENT_R)
63 // This sysinfo() is Linux-specific.
64 #elif defined(TUKLIB_PHYSMEM_SYSINFO)
65 # include <sys/sysinfo.h>
77 #if defined(_WIN32) || defined(__CYGWIN__)
78 if ((GetVersion() & 0xFF) >= 5) {
79 // Windows 2000 and later have GlobalMemoryStatusEx() which
80 // supports reporting values greater than 4 GiB. To keep the
81 // code working also on older Windows versions, use
82 // GlobalMemoryStatusEx() conditionally.
83 HMODULE kernel32
= GetModuleHandle("kernel32.dll");
84 if (kernel32
!= NULL
) {
85 BOOL (WINAPI
*gmse
)(LPMEMORYSTATUSEX
) = GetProcAddress(
86 kernel32
, "GlobalMemoryStatusEx");
88 MEMORYSTATUSEX meminfo
;
89 meminfo
.dwLength
= sizeof(meminfo
);
91 ret
= meminfo
.ullTotalPhys
;
97 // GlobalMemoryStatus() is supported by Windows 95 and later,
98 // so it is fine to link against it unconditionally. Note that
99 // GlobalMemoryStatus() has no return value.
100 MEMORYSTATUS meminfo
;
101 meminfo
.dwLength
= sizeof(meminfo
);
102 GlobalMemoryStatus(&meminfo
);
103 ret
= meminfo
.dwTotalPhys
;
106 #elif defined(__OS2__)
108 if (DosQuerySysInfo(QSV_TOTPHYSMEM
, QSV_TOTPHYSMEM
,
109 &mem
, sizeof(mem
)) == 0)
112 #elif defined(__DJGPP__)
113 __dpmi_free_mem_info meminfo
;
114 if (__dpmi_get_free_memory_information(&meminfo
) == 0
115 && meminfo
.total_number_of_physical_pages
116 != (unsigned long)-1)
117 ret
= (uint64_t)meminfo
.total_number_of_physical_pages
* 4096;
121 int val
= SYI$_MEMSIZE
;
122 if (LIB$
GETSYI(&val
, &vms_mem
, 0, 0, 0, 0) == SS$_NORMAL
)
123 ret
= (uint64_t)vms_mem
* 8192;
125 #elif defined(TUKLIB_PHYSMEM_AIX)
126 ret
= _system_configuration
.physmem
;
128 #elif defined(TUKLIB_PHYSMEM_SYSCONF)
129 const long pagesize
= sysconf(_SC_PAGESIZE
);
130 const long pages
= sysconf(_SC_PHYS_PAGES
);
131 if (pagesize
!= -1 && pages
!= -1)
132 // According to docs, pagesize * pages can overflow.
133 // Simple case is 32-bit box with 4 GiB or more RAM,
134 // which may report exactly 4 GiB of RAM, and "long"
135 // being 32-bit will overflow. Casting to uint64_t
136 // hopefully avoids overflows in the near future.
137 ret
= (uint64_t)pagesize
* (uint64_t)pages
;
139 #elif defined(TUKLIB_PHYSMEM_SYSCTL)
152 size_t mem_ptr_size
= sizeof(mem
.u64
);
153 if (sysctl(name
, 2, &mem
.u64
, &mem_ptr_size
, NULL
, 0) != -1) {
154 // IIRC, 64-bit "return value" is possible on some 64-bit
155 // BSD systems even with HW_PHYSMEM (instead of HW_PHYSMEM64),
157 if (mem_ptr_size
== sizeof(mem
.u64
))
159 else if (mem_ptr_size
== sizeof(mem
.u32
))
163 #elif defined(TUKLIB_PHYSMEM_GETSYSINFO)
164 // Docs are unclear if "start" is needed, but it doesn't hurt
168 if (getsysinfo(GSI_PHYSMEM
, (caddr_t
)&memkb
, sizeof(memkb
), &start
)
170 ret
= (uint64_t)memkb
* 1024;
172 #elif defined(TUKLIB_PHYSMEM_PSTAT_GETSTATIC)
173 struct pst_static pst
;
174 if (pstat_getstatic(&pst
, sizeof(pst
), 1, 0) != -1)
175 ret
= (uint64_t)pst
.physical_memory
* (uint64_t)pst
.page_size
;
177 #elif defined(TUKLIB_PHYSMEM_GETINVENT_R)
178 inv_state_t
*st
= NULL
;
179 if (setinvent_r(&st
) != -1) {
181 while ((i
= getinvent_r(st
)) != NULL
) {
182 if (i
->inv_class
== INV_MEMORY
183 && i
->inv_type
== INV_MAIN_MB
) {
184 ret
= (uint64_t)i
->inv_state
<< 20;
192 #elif defined(TUKLIB_PHYSMEM_SYSINFO)
194 if (sysinfo(&si
) == 0)
195 ret
= (uint64_t)si
.totalram
* si
.mem_unit
;