tuklib_physmem: Comment out support for Windows versions older than 2000.
[xz.git] / src / common / tuklib_physmem.c
blob779eb0a1ffd97a5c037ac64dc0d5b5ba1f49e46b
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.
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__)
19 # ifndef _WIN32_WINNT
20 # define _WIN32_WINNT 0x0500
21 # endif
22 # include <windows.h>
24 #elif defined(__OS2__)
25 # define INCL_DOSMISC
26 # include <os2.h>
28 #elif defined(__DJGPP__)
29 # include <dpmi.h>
31 #elif defined(__VMS)
32 # include <lib$routines.h>
33 # include <syidef.h>
34 # include <ssdef.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>
42 # include <string.h>
44 #elif defined(TUKLIB_PHYSMEM_AIX)
45 # include <sys/systemcfg.h>
47 #elif defined(TUKLIB_PHYSMEM_SYSCONF)
48 # include <unistd.h>
50 #elif defined(TUKLIB_PHYSMEM_SYSCTL)
51 # ifdef HAVE_SYS_PARAM_H
52 # include <sys/param.h>
53 # endif
54 # include <sys/sysctl.h>
56 // Tru64
57 #elif defined(TUKLIB_PHYSMEM_GETSYSINFO)
58 # include <sys/sysinfo.h>
59 # include <machine/hal_sysinfo.h>
61 // HP-UX
62 #elif defined(TUKLIB_PHYSMEM_PSTAT_GETSTATIC)
63 # include <sys/param.h>
64 # include <sys/pstat.h>
66 // IRIX
67 #elif defined(TUKLIB_PHYSMEM_GETINVENT_R)
68 # include <invent.h>
70 // This sysinfo() is Linux-specific.
71 #elif defined(TUKLIB_PHYSMEM_SYSINFO)
72 # include <sys/sysinfo.h>
73 #endif
76 extern uint64_t
77 tuklib_physmem(void)
79 uint64_t ret = 0;
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"
101 #endif
102 gmse_type gmse = (gmse_type)GetProcAddress(
103 kernel32, "GlobalMemoryStatusEx");
104 #ifdef CAN_DISABLE_WCAST_FUNCTION_TYPE
105 # pragma GCC diagnostic pop
106 #endif
107 if (gmse != NULL) {
108 MEMORYSTATUSEX meminfo;
109 meminfo.dwLength = sizeof(meminfo);
110 if (gmse(&meminfo))
111 ret = meminfo.ullTotalPhys;
116 if (ret == 0) {
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__)
128 unsigned long mem;
129 if (DosQuerySysInfo(QSV_TOTPHYSMEM, QSV_TOTPHYSMEM,
130 &mem, sizeof(mem)) == 0)
131 ret = mem;
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;
140 #elif defined(__VMS)
141 int vms_mem;
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)
173 int name[2] = {
174 CTL_HW,
175 #ifdef HW_PHYSMEM64
176 HW_PHYSMEM64
177 #else
178 HW_PHYSMEM
179 #endif
181 union {
182 uint32_t u32;
183 uint64_t u64;
184 } mem;
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),
189 // so support both.
190 if (mem_ptr_size == sizeof(mem.u64))
191 ret = mem.u64;
192 else if (mem_ptr_size == sizeof(mem.u32))
193 ret = mem.u32;
196 #elif defined(TUKLIB_PHYSMEM_GETSYSINFO)
197 // Docs are unclear if "start" is needed, but it doesn't hurt
198 // much to have it.
199 int memkb;
200 int start = 0;
201 if (getsysinfo(GSI_PHYSMEM, (caddr_t)&memkb, sizeof(memkb), &start)
202 != -1)
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) {
213 inventory_t *i;
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;
218 break;
222 endinvent_r(st);
225 #elif defined(TUKLIB_PHYSMEM_SYSINFO)
226 struct sysinfo si;
227 if (sysinfo(&si) == 0)
228 ret = (uint64_t)si.totalram * si.mem_unit;
229 #endif
231 return ret;