warnings: fix compilation with old autoconf
[gnulib/ericb.git] / lib / physmem.c
blobc38c8a475bd6f82357561c86f737dc2eb1d73445
1 /* Calculate the size of physical memory.
3 Copyright (C) 2000-2001, 2003, 2005-2006, 2009-2017 Free Software
4 Foundation, Inc.
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 /* Written by Paul Eggert. */
21 #include <config.h>
23 #include "physmem.h"
25 #include <unistd.h>
27 #if HAVE_SYS_PSTAT_H
28 # include <sys/pstat.h>
29 #endif
31 #if HAVE_SYS_SYSMP_H
32 # include <sys/sysmp.h>
33 #endif
35 #if HAVE_SYS_SYSINFO_H
36 # include <sys/sysinfo.h>
37 #endif
39 #if HAVE_MACHINE_HAL_SYSINFO_H
40 # include <machine/hal_sysinfo.h>
41 #endif
43 #if HAVE_SYS_TABLE_H
44 # include <sys/table.h>
45 #endif
47 #include <sys/types.h>
49 #if HAVE_SYS_PARAM_H
50 # include <sys/param.h>
51 #endif
53 #if HAVE_SYS_SYSCTL_H
54 # include <sys/sysctl.h>
55 #endif
57 #if HAVE_SYS_SYSTEMCFG_H
58 # include <sys/systemcfg.h>
59 #endif
61 #ifdef _WIN32
62 # define WIN32_LEAN_AND_MEAN
63 # include <windows.h>
64 /* MEMORYSTATUSEX is missing from older windows headers, so define
65 a local replacement. */
66 typedef struct
68 DWORD dwLength;
69 DWORD dwMemoryLoad;
70 DWORDLONG ullTotalPhys;
71 DWORDLONG ullAvailPhys;
72 DWORDLONG ullTotalPageFile;
73 DWORDLONG ullAvailPageFile;
74 DWORDLONG ullTotalVirtual;
75 DWORDLONG ullAvailVirtual;
76 DWORDLONG ullAvailExtendedVirtual;
77 } lMEMORYSTATUSEX;
78 typedef WINBOOL (WINAPI *PFN_MS_EX) (lMEMORYSTATUSEX*);
79 #endif
81 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
83 /* Return the total amount of physical memory. */
84 double
85 physmem_total (void)
87 #if defined _SC_PHYS_PAGES && defined _SC_PAGESIZE
88 { /* This works on linux-gnu, solaris2 and cygwin. */
89 double pages = sysconf (_SC_PHYS_PAGES);
90 double pagesize = sysconf (_SC_PAGESIZE);
91 if (0 <= pages && 0 <= pagesize)
92 return pages * pagesize;
94 #endif
96 #if HAVE_SYSINFO && HAVE_STRUCT_SYSINFO_MEM_UNIT
97 { /* This works on linux. */
98 struct sysinfo si;
99 if (sysinfo(&si) == 0)
100 return (double) si.totalram * si.mem_unit;
102 #endif
104 #if HAVE_PSTAT_GETSTATIC
105 { /* This works on hpux11. */
106 struct pst_static pss;
107 if (0 <= pstat_getstatic (&pss, sizeof pss, 1, 0))
109 double pages = pss.physical_memory;
110 double pagesize = pss.page_size;
111 if (0 <= pages && 0 <= pagesize)
112 return pages * pagesize;
115 #endif
117 #if HAVE_SYSMP && defined MP_SAGET && defined MPSA_RMINFO && defined _SC_PAGESIZE
118 { /* This works on irix6. */
119 struct rminfo realmem;
120 if (sysmp (MP_SAGET, MPSA_RMINFO, &realmem, sizeof realmem) == 0)
122 double pagesize = sysconf (_SC_PAGESIZE);
123 double pages = realmem.physmem;
124 if (0 <= pages && 0 <= pagesize)
125 return pages * pagesize;
128 #endif
130 #if HAVE_GETSYSINFO && defined GSI_PHYSMEM
131 { /* This works on Tru64 UNIX V4/5. */
132 int physmem;
134 if (getsysinfo (GSI_PHYSMEM, (caddr_t) &physmem, sizeof (physmem),
135 NULL, NULL, NULL) == 1)
137 double kbytes = physmem;
139 if (0 <= kbytes)
140 return kbytes * 1024.0;
143 #endif
145 #if HAVE_SYSCTL && defined HW_PHYSMEM
146 { /* This works on *bsd and darwin. */
147 unsigned int physmem;
148 size_t len = sizeof physmem;
149 static int mib[2] = { CTL_HW, HW_PHYSMEM };
151 if (sysctl (mib, ARRAY_SIZE (mib), &physmem, &len, NULL, 0) == 0
152 && len == sizeof (physmem))
153 return (double) physmem;
155 #endif
157 #if HAVE__SYSTEM_CONFIGURATION
158 /* This works on AIX. */
159 return _system_configuration.physmem;
160 #endif
162 #if defined _WIN32
163 { /* this works on windows */
164 PFN_MS_EX pfnex;
165 HMODULE h = GetModuleHandle ("kernel32.dll");
167 if (!h)
168 return 0.0;
170 /* Use GlobalMemoryStatusEx if available. */
171 if ((pfnex = (PFN_MS_EX) GetProcAddress (h, "GlobalMemoryStatusEx")))
173 lMEMORYSTATUSEX lms_ex;
174 lms_ex.dwLength = sizeof lms_ex;
175 if (!pfnex (&lms_ex))
176 return 0.0;
177 return (double) lms_ex.ullTotalPhys;
180 /* Fall back to GlobalMemoryStatus which is always available.
181 but returns wrong results for physical memory > 4GB. */
182 else
184 MEMORYSTATUS ms;
185 GlobalMemoryStatus (&ms);
186 return (double) ms.dwTotalPhys;
189 #endif
191 /* Guess 64 MB. It's probably an older host, so guess small. */
192 return 64 * 1024 * 1024;
195 /* Return the amount of physical memory available. */
196 double
197 physmem_available (void)
199 #if defined _SC_AVPHYS_PAGES && defined _SC_PAGESIZE
200 { /* This works on linux-gnu, solaris2 and cygwin. */
201 double pages = sysconf (_SC_AVPHYS_PAGES);
202 double pagesize = sysconf (_SC_PAGESIZE);
203 if (0 <= pages && 0 <= pagesize)
204 return pages * pagesize;
206 #endif
208 #if HAVE_SYSINFO && HAVE_STRUCT_SYSINFO_MEM_UNIT
209 { /* This works on linux. */
210 struct sysinfo si;
211 if (sysinfo(&si) == 0)
212 return ((double) si.freeram + si.bufferram) * si.mem_unit;
214 #endif
216 #if HAVE_PSTAT_GETSTATIC && HAVE_PSTAT_GETDYNAMIC
217 { /* This works on hpux11. */
218 struct pst_static pss;
219 struct pst_dynamic psd;
220 if (0 <= pstat_getstatic (&pss, sizeof pss, 1, 0)
221 && 0 <= pstat_getdynamic (&psd, sizeof psd, 1, 0))
223 double pages = psd.psd_free;
224 double pagesize = pss.page_size;
225 if (0 <= pages && 0 <= pagesize)
226 return pages * pagesize;
229 #endif
231 #if HAVE_SYSMP && defined MP_SAGET && defined MPSA_RMINFO && defined _SC_PAGESIZE
232 { /* This works on irix6. */
233 struct rminfo realmem;
234 if (sysmp (MP_SAGET, MPSA_RMINFO, &realmem, sizeof realmem) == 0)
236 double pagesize = sysconf (_SC_PAGESIZE);
237 double pages = realmem.availrmem;
238 if (0 <= pages && 0 <= pagesize)
239 return pages * pagesize;
242 #endif
244 #if HAVE_TABLE && defined TBL_VMSTATS
245 { /* This works on Tru64 UNIX V4/5. */
246 struct tbl_vmstats vmstats;
248 if (table (TBL_VMSTATS, 0, &vmstats, 1, sizeof (vmstats)) == 1)
250 double pages = vmstats.free_count;
251 double pagesize = vmstats.pagesize;
253 if (0 <= pages && 0 <= pagesize)
254 return pages * pagesize;
257 #endif
259 #if HAVE_SYSCTL && defined HW_USERMEM
260 { /* This works on *bsd and darwin. */
261 unsigned int usermem;
262 size_t len = sizeof usermem;
263 static int mib[2] = { CTL_HW, HW_USERMEM };
265 if (sysctl (mib, ARRAY_SIZE (mib), &usermem, &len, NULL, 0) == 0
266 && len == sizeof (usermem))
267 return (double) usermem;
269 #endif
271 #if defined _WIN32
272 { /* this works on windows */
273 PFN_MS_EX pfnex;
274 HMODULE h = GetModuleHandle ("kernel32.dll");
276 if (!h)
277 return 0.0;
279 /* Use GlobalMemoryStatusEx if available. */
280 if ((pfnex = (PFN_MS_EX) GetProcAddress (h, "GlobalMemoryStatusEx")))
282 lMEMORYSTATUSEX lms_ex;
283 lms_ex.dwLength = sizeof lms_ex;
284 if (!pfnex (&lms_ex))
285 return 0.0;
286 return (double) lms_ex.ullAvailPhys;
289 /* Fall back to GlobalMemoryStatus which is always available.
290 but returns wrong results for physical memory > 4GB */
291 else
293 MEMORYSTATUS ms;
294 GlobalMemoryStatus (&ms);
295 return (double) ms.dwAvailPhys;
298 #endif
300 /* Guess 25% of physical memory. */
301 return physmem_total () / 4;
305 #if DEBUG
307 # include <stdio.h>
308 # include <stdlib.h>
311 main (void)
313 printf ("%12.f %12.f\n", physmem_total (), physmem_available ());
314 exit (0);
317 #endif /* DEBUG */
320 Local Variables:
321 compile-command: "gcc -DDEBUG -g -O -Wall -W physmem.c"
322 End: