1 // SPDX-License-Identifier: 0BSD
3 ///////////////////////////////////////////////////////////////////////////////
5 /// \file tuklib_physmem.c
6 /// \brief Get the amount of physical memory
8 // Author: Lasse Collin
10 ///////////////////////////////////////////////////////////////////////////////
12 #include "tuklib_physmem.h"
14 // We want to use Windows-specific code on Cygwin, which also has memory
15 // information available via sysconf(), but on Cygwin 1.5 and older it
16 // gives wrong results (from our point of view).
17 #if defined(_WIN32) || defined(__CYGWIN__)
19 # define _WIN32_WINNT 0x0500
23 #elif defined(__OS2__)
27 #elif defined(__DJGPP__)
31 # include <lib$routines.h>
35 #elif defined(AMIGA) || defined(__AROS__)
36 # define __USE_INLINE__
37 # include <proto/exec.h>
39 #elif defined(__QNX__)
40 # include <sys/syspage.h>
43 #elif defined(TUKLIB_PHYSMEM_AIX)
44 # include <sys/systemcfg.h>
46 #elif defined(TUKLIB_PHYSMEM_SYSCONF)
49 #elif defined(TUKLIB_PHYSMEM_SYSCTL)
50 # ifdef HAVE_SYS_PARAM_H
51 # include <sys/param.h>
53 # include <sys/sysctl.h>
56 #elif defined(TUKLIB_PHYSMEM_GETSYSINFO)
57 # include <sys/sysinfo.h>
58 # include <machine/hal_sysinfo.h>
61 #elif defined(TUKLIB_PHYSMEM_PSTAT_GETSTATIC)
62 # include <sys/param.h>
63 # include <sys/pstat.h>
66 #elif defined(TUKLIB_PHYSMEM_GETINVENT_R)
69 // This sysinfo() is Linux-specific.
70 #elif defined(TUKLIB_PHYSMEM_SYSINFO)
71 # include <sys/sysinfo.h>
80 #if defined(_WIN32) || defined(__CYGWIN__)
81 // This requires Windows 2000 or later.
82 MEMORYSTATUSEX meminfo
;
83 meminfo
.dwLength
= sizeof(meminfo
);
84 if (GlobalMemoryStatusEx(&meminfo
))
85 ret
= meminfo
.ullTotalPhys
;
88 // Old version that is compatible with even Win95:
89 if ((GetVersion() & 0xFF) >= 5) {
90 // Windows 2000 and later have GlobalMemoryStatusEx() which
91 // supports reporting values greater than 4 GiB. To keep the
92 // code working also on older Windows versions, use
93 // GlobalMemoryStatusEx() conditionally.
94 HMODULE kernel32 = GetModuleHandle(TEXT("kernel32.dll"));
95 if (kernel32 != NULL) {
96 typedef BOOL (WINAPI *gmse_type)(LPMEMORYSTATUSEX);
97 #ifdef CAN_DISABLE_WCAST_FUNCTION_TYPE
98 # pragma GCC diagnostic push
99 # pragma GCC diagnostic ignored "-Wcast-function-type"
101 gmse_type gmse = (gmse_type)GetProcAddress(
102 kernel32, "GlobalMemoryStatusEx");
103 #ifdef CAN_DISABLE_WCAST_FUNCTION_TYPE
104 # pragma GCC diagnostic pop
107 MEMORYSTATUSEX meminfo;
108 meminfo.dwLength = sizeof(meminfo);
110 ret = meminfo.ullTotalPhys;
116 // GlobalMemoryStatus() is supported by Windows 95 and later,
117 // so it is fine to link against it unconditionally. Note that
118 // GlobalMemoryStatus() has no return value.
119 MEMORYSTATUS meminfo;
120 meminfo.dwLength = sizeof(meminfo);
121 GlobalMemoryStatus(&meminfo);
122 ret = meminfo.dwTotalPhys;
126 #elif defined(__OS2__)
128 if (DosQuerySysInfo(QSV_TOTPHYSMEM
, QSV_TOTPHYSMEM
,
129 &mem
, sizeof(mem
)) == 0)
132 #elif defined(__DJGPP__)
133 __dpmi_free_mem_info meminfo
;
134 if (__dpmi_get_free_memory_information(&meminfo
) == 0
135 && meminfo
.total_number_of_physical_pages
136 != (unsigned long)-1)
137 ret
= (uint64_t)meminfo
.total_number_of_physical_pages
* 4096;
141 int val
= SYI$_MEMSIZE
;
142 if (LIB$
GETSYI(&val
, &vms_mem
, 0, 0, 0, 0) == SS$_NORMAL
)
143 ret
= (uint64_t)vms_mem
* 8192;
145 #elif defined(AMIGA) || defined(__AROS__)
146 ret
= AvailMem(MEMF_TOTAL
);
148 #elif defined(__QNX__)
149 const struct asinfo_entry
*entries
= SYSPAGE_ENTRY(asinfo
);
150 size_t count
= SYSPAGE_ENTRY_SIZE(asinfo
) / sizeof(struct asinfo_entry
);
151 const char *strings
= SYSPAGE_ENTRY(strings
)->data
;
153 for (size_t i
= 0; i
< count
; ++i
)
154 if (strcmp(strings
+ entries
[i
].name
, "ram") == 0)
155 ret
+= entries
[i
].end
- entries
[i
].start
+ 1;
157 #elif defined(TUKLIB_PHYSMEM_AIX)
158 ret
= _system_configuration
.physmem
;
160 #elif defined(TUKLIB_PHYSMEM_SYSCONF)
161 const long pagesize
= sysconf(_SC_PAGESIZE
);
162 const long pages
= sysconf(_SC_PHYS_PAGES
);
163 if (pagesize
!= -1 && pages
!= -1)
164 // According to docs, pagesize * pages can overflow.
165 // Simple case is 32-bit box with 4 GiB or more RAM,
166 // which may report exactly 4 GiB of RAM, and "long"
167 // being 32-bit will overflow. Casting to uint64_t
168 // hopefully avoids overflows in the near future.
169 ret
= (uint64_t)pagesize
* (uint64_t)pages
;
171 #elif defined(TUKLIB_PHYSMEM_SYSCTL)
184 size_t mem_ptr_size
= sizeof(mem
.u64
);
185 if (sysctl(name
, 2, &mem
.u64
, &mem_ptr_size
, NULL
, 0) != -1) {
186 // IIRC, 64-bit "return value" is possible on some 64-bit
187 // BSD systems even with HW_PHYSMEM (instead of HW_PHYSMEM64),
189 if (mem_ptr_size
== sizeof(mem
.u64
))
191 else if (mem_ptr_size
== sizeof(mem
.u32
))
195 #elif defined(TUKLIB_PHYSMEM_GETSYSINFO)
196 // Docs are unclear if "start" is needed, but it doesn't hurt
200 if (getsysinfo(GSI_PHYSMEM
, (caddr_t
)&memkb
, sizeof(memkb
), &start
)
202 ret
= (uint64_t)memkb
* 1024;
204 #elif defined(TUKLIB_PHYSMEM_PSTAT_GETSTATIC)
205 struct pst_static pst
;
206 if (pstat_getstatic(&pst
, sizeof(pst
), 1, 0) != -1)
207 ret
= (uint64_t)pst
.physical_memory
* (uint64_t)pst
.page_size
;
209 #elif defined(TUKLIB_PHYSMEM_GETINVENT_R)
210 inv_state_t
*st
= NULL
;
211 if (setinvent_r(&st
) != -1) {
213 while ((i
= getinvent_r(st
)) != NULL
) {
214 if (i
->inv_class
== INV_MEMORY
215 && i
->inv_type
== INV_MAIN_MB
) {
216 ret
= (uint64_t)i
->inv_state
<< 20;
224 #elif defined(TUKLIB_PHYSMEM_SYSINFO)
226 if (sysinfo(&si
) == 0)
227 ret
= (uint64_t)si
.totalram
* si
.mem_unit
;