iso9660fs: initialize buffer cache
[minix.git] / external / public-domain / xz / dist / src / common / tuklib_physmem.c
blobb20c771bfc8efd81a2a7b887e450b11985d874da
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 // AIX
37 #elif defined(TUKLIB_PHYSMEM_AIX)
38 # include <sys/systemcfg.h>
40 #elif defined(TUKLIB_PHYSMEM_SYSCONF)
41 # include <unistd.h>
43 #elif defined(TUKLIB_PHYSMEM_SYSCTL)
44 # ifdef HAVE_SYS_PARAM_H
45 # include <sys/param.h>
46 # endif
47 # include <sys/sysctl.h>
49 // Tru64
50 #elif defined(TUKLIB_PHYSMEM_GETSYSINFO)
51 # include <sys/sysinfo.h>
52 # include <machine/hal_sysinfo.h>
54 // HP-UX
55 #elif defined(TUKLIB_PHYSMEM_PSTAT_GETSTATIC)
56 # include <sys/param.h>
57 # include <sys/pstat.h>
59 // IRIX
60 #elif defined(TUKLIB_PHYSMEM_GETINVENT_R)
61 # include <invent.h>
63 // This sysinfo() is Linux-specific.
64 #elif defined(TUKLIB_PHYSMEM_SYSINFO)
65 # include <sys/sysinfo.h>
66 #endif
69 extern uint64_t
70 tuklib_physmem(void)
72 #ifdef __minix
73 return 0;
74 #else
75 uint64_t ret = 0;
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");
87 if (gmse != NULL) {
88 MEMORYSTATUSEX meminfo;
89 meminfo.dwLength = sizeof(meminfo);
90 if (gmse(&meminfo))
91 ret = meminfo.ullTotalPhys;
96 if (ret == 0) {
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__)
107 unsigned long mem;
108 if (DosQuerySysInfo(QSV_TOTPHYSMEM, QSV_TOTPHYSMEM,
109 &mem, sizeof(mem)) == 0)
110 ret = mem;
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;
119 #elif defined(__VMS)
120 int vms_mem;
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)
140 int name[2] = {
141 CTL_HW,
142 #ifdef HW_PHYSMEM64
143 HW_PHYSMEM64
144 #else
145 HW_PHYSMEM
146 #endif
148 union {
149 uint32_t u32;
150 uint64_t u64;
151 } mem;
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),
156 // so support both.
157 if (mem_ptr_size == sizeof(mem.u64))
158 ret = mem.u64;
159 else if (mem_ptr_size == sizeof(mem.u32))
160 ret = mem.u32;
163 #elif defined(TUKLIB_PHYSMEM_GETSYSINFO)
164 // Docs are unclear if "start" is needed, but it doesn't hurt
165 // much to have it.
166 int memkb;
167 int start = 0;
168 if (getsysinfo(GSI_PHYSMEM, (caddr_t)&memkb, sizeof(memkb), &start)
169 != -1)
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) {
180 inventory_t *i;
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;
185 break;
189 endinvent_r(st);
192 #elif defined(TUKLIB_PHYSMEM_SYSINFO)
193 struct sysinfo si;
194 if (sysinfo(&si) == 0)
195 ret = (uint64_t)si.totalram * si.mem_unit;
196 #endif
198 return ret;
199 #endif