xref: /freebsd/contrib/xz/src/common/tuklib_physmem.c (revision 52c2bb75163559a6e2866ad374a7de67a4ea1273)
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       tuklib_physmem.c
4 /// \brief      Get the amount of physical memory
5 //
6 //  Author:     Lasse Collin
7 //
8 //  This file has been put into the public domain.
9 //  You can do whatever you want with this file.
10 //
11 ///////////////////////////////////////////////////////////////////////////////
12 
13 #include "tuklib_physmem.h"
14 
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__)
19 #	ifndef _WIN32_WINNT
20 #		define _WIN32_WINNT 0x0500
21 #	endif
22 #	include <windows.h>
23 
24 #elif defined(__OS2__)
25 #	define INCL_DOSMISC
26 #	include <os2.h>
27 
28 #elif defined(__DJGPP__)
29 #	include <dpmi.h>
30 
31 #elif defined(__VMS)
32 #	include <lib$routines.h>
33 #	include <syidef.h>
34 #	include <ssdef.h>
35 
36 #elif defined(AMIGA) || defined(__AROS__)
37 #	define __USE_INLINE__
38 #	include <proto/exec.h>
39 
40 #elif defined(__QNX__)
41 #	include <sys/syspage.h>
42 #	include <string.h>
43 
44 #elif defined(TUKLIB_PHYSMEM_AIX)
45 #	include <sys/systemcfg.h>
46 
47 #elif defined(TUKLIB_PHYSMEM_SYSCONF)
48 #	include <limits.h>
49 #	include <unistd.h>
50 
51 #elif defined(TUKLIB_PHYSMEM_SYSCTL)
52 #	ifdef HAVE_SYS_PARAM_H
53 #		include <sys/param.h>
54 #	endif
55 #	include <sys/sysctl.h>
56 
57 // Tru64
58 #elif defined(TUKLIB_PHYSMEM_GETSYSINFO)
59 #	include <sys/sysinfo.h>
60 #	include <machine/hal_sysinfo.h>
61 
62 // HP-UX
63 #elif defined(TUKLIB_PHYSMEM_PSTAT_GETSTATIC)
64 #	include <sys/param.h>
65 #	include <sys/pstat.h>
66 
67 // IRIX
68 #elif defined(TUKLIB_PHYSMEM_GETINVENT_R)
69 #	include <invent.h>
70 
71 // This sysinfo() is Linux-specific.
72 #elif defined(TUKLIB_PHYSMEM_SYSINFO)
73 #	include <sys/sysinfo.h>
74 #endif
75 
76 
77 extern uint64_t
78 tuklib_physmem(void)
79 {
80 	uint64_t ret = 0;
81 
82 #if defined(_WIN32) || defined(__CYGWIN__)
83 	if ((GetVersion() & 0xFF) >= 5) {
84 		// Windows 2000 and later have GlobalMemoryStatusEx() which
85 		// supports reporting values greater than 4 GiB. To keep the
86 		// code working also on older Windows versions, use
87 		// GlobalMemoryStatusEx() conditionally.
88 		HMODULE kernel32 = GetModuleHandle("kernel32.dll");
89 		if (kernel32 != NULL) {
90 			typedef BOOL (WINAPI *gmse_type)(LPMEMORYSTATUSEX);
91 			gmse_type gmse = (gmse_type)GetProcAddress(
92 					kernel32, "GlobalMemoryStatusEx");
93 			if (gmse != NULL) {
94 				MEMORYSTATUSEX meminfo;
95 				meminfo.dwLength = sizeof(meminfo);
96 				if (gmse(&meminfo))
97 					ret = meminfo.ullTotalPhys;
98 			}
99 		}
100 	}
101 
102 	if (ret == 0) {
103 		// GlobalMemoryStatus() is supported by Windows 95 and later,
104 		// so it is fine to link against it unconditionally. Note that
105 		// GlobalMemoryStatus() has no return value.
106 		MEMORYSTATUS meminfo;
107 		meminfo.dwLength = sizeof(meminfo);
108 		GlobalMemoryStatus(&meminfo);
109 		ret = meminfo.dwTotalPhys;
110 	}
111 
112 #elif defined(__OS2__)
113 	unsigned long mem;
114 	if (DosQuerySysInfo(QSV_TOTPHYSMEM, QSV_TOTPHYSMEM,
115 			&mem, sizeof(mem)) == 0)
116 		ret = mem;
117 
118 #elif defined(__DJGPP__)
119 	__dpmi_free_mem_info meminfo;
120 	if (__dpmi_get_free_memory_information(&meminfo) == 0
121 			&& meminfo.total_number_of_physical_pages
122 				!= (unsigned long)-1)
123 		ret = (uint64_t)meminfo.total_number_of_physical_pages * 4096;
124 
125 #elif defined(__VMS)
126 	int vms_mem;
127 	int val = SYI$_MEMSIZE;
128 	if (LIB$GETSYI(&val, &vms_mem, 0, 0, 0, 0) == SS$_NORMAL)
129 		ret = (uint64_t)vms_mem * 8192;
130 
131 #elif defined(AMIGA) || defined(__AROS__)
132 	ret = AvailMem(MEMF_TOTAL);
133 
134 #elif defined(__QNX__)
135 	const struct asinfo_entry *entries = SYSPAGE_ENTRY(asinfo);
136 	size_t count = SYSPAGE_ENTRY_SIZE(asinfo) / sizeof(struct asinfo_entry);
137 	const char *strings = SYSPAGE_ENTRY(strings)->data;
138 
139 	for (size_t i = 0; i < count; ++i)
140 		if (strcmp(strings + entries[i].name, "ram") == 0)
141 			ret += entries[i].end - entries[i].start + 1;
142 
143 #elif defined(TUKLIB_PHYSMEM_AIX)
144 	ret = _system_configuration.physmem;
145 
146 #elif defined(TUKLIB_PHYSMEM_SYSCONF)
147 	const long pagesize = sysconf(_SC_PAGESIZE);
148 	const long pages = sysconf(_SC_PHYS_PAGES);
149 	if (pagesize != -1 && pages != -1) {
150 		// According to docs, pagesize * pages can overflow.
151 		// Simple case is 32-bit box with 4 GiB or more RAM,
152 		// which may report exactly 4 GiB of RAM, and "long"
153 		// being 32-bit will overflow. Casting to uint64_t
154 		// hopefully avoids overflows in the near future.
155 		ret = (uint64_t)pagesize * (uint64_t)pages;
156 		if (ret > SIZE_T_MAX)
157 			ret = SIZE_T_MAX;
158 	}
159 
160 #elif defined(TUKLIB_PHYSMEM_SYSCTL)
161 	int name[2] = {
162 		CTL_HW,
163 #ifdef HW_PHYSMEM64
164 		HW_PHYSMEM64
165 #else
166 		HW_PHYSMEM
167 #endif
168 	};
169 	union {
170 		uint32_t u32;
171 		uint64_t u64;
172 	} mem;
173 	size_t mem_ptr_size = sizeof(mem.u64);
174 	if (sysctl(name, 2, &mem.u64, &mem_ptr_size, NULL, 0) != -1) {
175 		// IIRC, 64-bit "return value" is possible on some 64-bit
176 		// BSD systems even with HW_PHYSMEM (instead of HW_PHYSMEM64),
177 		// so support both.
178 		if (mem_ptr_size == sizeof(mem.u64))
179 			ret = mem.u64;
180 		else if (mem_ptr_size == sizeof(mem.u32))
181 			ret = mem.u32;
182 	}
183 
184 #elif defined(TUKLIB_PHYSMEM_GETSYSINFO)
185 	// Docs are unclear if "start" is needed, but it doesn't hurt
186 	// much to have it.
187 	int memkb;
188 	int start = 0;
189 	if (getsysinfo(GSI_PHYSMEM, (caddr_t)&memkb, sizeof(memkb), &start)
190 			!= -1)
191 		ret = (uint64_t)memkb * 1024;
192 
193 #elif defined(TUKLIB_PHYSMEM_PSTAT_GETSTATIC)
194 	struct pst_static pst;
195 	if (pstat_getstatic(&pst, sizeof(pst), 1, 0) != -1)
196 		ret = (uint64_t)pst.physical_memory * (uint64_t)pst.page_size;
197 
198 #elif defined(TUKLIB_PHYSMEM_GETINVENT_R)
199 	inv_state_t *st = NULL;
200 	if (setinvent_r(&st) != -1) {
201 		inventory_t *i;
202 		while ((i = getinvent_r(st)) != NULL) {
203 			if (i->inv_class == INV_MEMORY
204 					&& i->inv_type == INV_MAIN_MB) {
205 				ret = (uint64_t)i->inv_state << 20;
206 				break;
207 			}
208 		}
209 
210 		endinvent_r(st);
211 	}
212 
213 #elif defined(TUKLIB_PHYSMEM_SYSINFO)
214 	struct sysinfo si;
215 	if (sysinfo(&si) == 0)
216 		ret = (uint64_t)si.totalram * si.mem_unit;
217 #endif
218 
219 	return ret;
220 }
221