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(AMIGA) || defined(__AROS__)
37 # define __USE_INLINE__
38 # include <proto/exec.h>
40 #elif defined(__QNX__)
41 # include <sys/syspage.h>
44 #elif defined(TUKLIB_PHYSMEM_AIX)
45 # include <sys/systemcfg.h>
47 #elif defined(TUKLIB_PHYSMEM_SYSCONF)
50 #elif defined(TUKLIB_PHYSMEM_SYSCTL)
51 # ifdef HAVE_SYS_PARAM_H
52 # include <sys/param.h>
54 # include <sys/sysctl.h>
57 #elif defined(TUKLIB_PHYSMEM_GETSYSINFO)
58 # include <sys/sysinfo.h>
59 # include <machine/hal_sysinfo.h>
62 #elif defined(TUKLIB_PHYSMEM_PSTAT_GETSTATIC)
63 # include <sys/param.h>
64 # include <sys/pstat.h>
67 #elif defined(TUKLIB_PHYSMEM_GETINVENT_R)
70 // This sysinfo() is Linux-specific.
71 #elif defined(TUKLIB_PHYSMEM_SYSINFO)
72 # include <sys/sysinfo.h>
81 #if defined(_WIN32) || defined(__CYGWIN__)
82 // This requires Windows 2000 or later.
83 MEMORYSTATUSEX meminfo
;
84 meminfo
.dwLength
= sizeof(meminfo
);
85 if (GlobalMemoryStatusEx(&meminfo
))
86 ret
= meminfo
.ullTotalPhys
;
89 // Old version that is compatible with even Win95:
90 if ((GetVersion() & 0xFF) >= 5) {
91 // Windows 2000 and later have GlobalMemoryStatusEx() which
92 // supports reporting values greater than 4 GiB. To keep the
93 // code working also on older Windows versions, use
94 // GlobalMemoryStatusEx() conditionally.
95 HMODULE kernel32 = GetModuleHandle(TEXT("kernel32.dll"));
96 if (kernel32 != NULL) {
97 typedef BOOL (WINAPI *gmse_type)(LPMEMORYSTATUSEX);
98 #ifdef CAN_DISABLE_WCAST_FUNCTION_TYPE
99 # pragma GCC diagnostic push
100 # pragma GCC diagnostic ignored "-Wcast-function-type"
102 gmse_type gmse = (gmse_type)GetProcAddress(
103 kernel32, "GlobalMemoryStatusEx");
104 #ifdef CAN_DISABLE_WCAST_FUNCTION_TYPE
105 # pragma GCC diagnostic pop
108 MEMORYSTATUSEX meminfo;
109 meminfo.dwLength = sizeof(meminfo);
111 ret = meminfo.ullTotalPhys;
117 // GlobalMemoryStatus() is supported by Windows 95 and later,
118 // so it is fine to link against it unconditionally. Note that
119 // GlobalMemoryStatus() has no return value.
120 MEMORYSTATUS meminfo;
121 meminfo.dwLength = sizeof(meminfo);
122 GlobalMemoryStatus(&meminfo);
123 ret = meminfo.dwTotalPhys;
127 #elif defined(__OS2__)
129 if (DosQuerySysInfo(QSV_TOTPHYSMEM
, QSV_TOTPHYSMEM
,
130 &mem
, sizeof(mem
)) == 0)
133 #elif defined(__DJGPP__)
134 __dpmi_free_mem_info meminfo
;
135 if (__dpmi_get_free_memory_information(&meminfo
) == 0
136 && meminfo
.total_number_of_physical_pages
137 != (unsigned long)-1)
138 ret
= (uint64_t)meminfo
.total_number_of_physical_pages
* 4096;
142 int val
= SYI$_MEMSIZE
;
143 if (LIB$
GETSYI(&val
, &vms_mem
, 0, 0, 0, 0) == SS$_NORMAL
)
144 ret
= (uint64_t)vms_mem
* 8192;
146 #elif defined(AMIGA) || defined(__AROS__)
147 ret
= AvailMem(MEMF_TOTAL
);
149 #elif defined(__QNX__)
150 const struct asinfo_entry
*entries
= SYSPAGE_ENTRY(asinfo
);
151 size_t count
= SYSPAGE_ENTRY_SIZE(asinfo
) / sizeof(struct asinfo_entry
);
152 const char *strings
= SYSPAGE_ENTRY(strings
)->data
;
154 for (size_t i
= 0; i
< count
; ++i
)
155 if (strcmp(strings
+ entries
[i
].name
, "ram") == 0)
156 ret
+= entries
[i
].end
- entries
[i
].start
+ 1;
158 #elif defined(TUKLIB_PHYSMEM_AIX)
159 ret
= _system_configuration
.physmem
;
161 #elif defined(TUKLIB_PHYSMEM_SYSCONF)
162 const long pagesize
= sysconf(_SC_PAGESIZE
);
163 const long pages
= sysconf(_SC_PHYS_PAGES
);
164 if (pagesize
!= -1 && pages
!= -1)
165 // According to docs, pagesize * pages can overflow.
166 // Simple case is 32-bit box with 4 GiB or more RAM,
167 // which may report exactly 4 GiB of RAM, and "long"
168 // being 32-bit will overflow. Casting to uint64_t
169 // hopefully avoids overflows in the near future.
170 ret
= (uint64_t)pagesize
* (uint64_t)pages
;
172 #elif defined(TUKLIB_PHYSMEM_SYSCTL)
185 size_t mem_ptr_size
= sizeof(mem
.u64
);
186 if (sysctl(name
, 2, &mem
.u64
, &mem_ptr_size
, NULL
, 0) != -1) {
187 // IIRC, 64-bit "return value" is possible on some 64-bit
188 // BSD systems even with HW_PHYSMEM (instead of HW_PHYSMEM64),
190 if (mem_ptr_size
== sizeof(mem
.u64
))
192 else if (mem_ptr_size
== sizeof(mem
.u32
))
196 #elif defined(TUKLIB_PHYSMEM_GETSYSINFO)
197 // Docs are unclear if "start" is needed, but it doesn't hurt
201 if (getsysinfo(GSI_PHYSMEM
, (caddr_t
)&memkb
, sizeof(memkb
), &start
)
203 ret
= (uint64_t)memkb
* 1024;
205 #elif defined(TUKLIB_PHYSMEM_PSTAT_GETSTATIC)
206 struct pst_static pst
;
207 if (pstat_getstatic(&pst
, sizeof(pst
), 1, 0) != -1)
208 ret
= (uint64_t)pst
.physical_memory
* (uint64_t)pst
.page_size
;
210 #elif defined(TUKLIB_PHYSMEM_GETINVENT_R)
211 inv_state_t
*st
= NULL
;
212 if (setinvent_r(&st
) != -1) {
214 while ((i
= getinvent_r(st
)) != NULL
) {
215 if (i
->inv_class
== INV_MEMORY
216 && i
->inv_type
== INV_MAIN_MB
) {
217 ret
= (uint64_t)i
->inv_state
<< 20;
225 #elif defined(TUKLIB_PHYSMEM_SYSINFO)
227 if (sysinfo(&si
) == 0)
228 ret
= (uint64_t)si
.totalram
* si
.mem_unit
;