make sponge use a temp file if the input is large
[moreutils.git] / physmem.c
blob0fcb5e96aae4266b30c5c738e28fd7db464e478b
1 /* Calculate the size of physical memory.
3 Copyright (C) 2000, 2001, 2003, 2005, 2006 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 <unistd.h>
23 #if HAVE_SYS_PSTAT_H
24 # include <sys/pstat.h>
25 #endif
27 #if HAVE_SYS_SYSMP_H
28 # include <sys/sysmp.h>
29 #endif
31 #if HAVE_SYS_SYSINFO_H && HAVE_MACHINE_HAL_SYSINFO_H
32 # include <sys/sysinfo.h>
33 # include <machine/hal_sysinfo.h>
34 #endif
36 #if HAVE_SYS_TABLE_H
37 # include <sys/table.h>
38 #endif
40 #include <sys/types.h>
42 #if HAVE_SYS_PARAM_H
43 # include <sys/param.h>
44 #endif
46 #if HAVE_SYS_SYSCTL_H
47 # include <sys/sysctl.h>
48 #endif
50 #if HAVE_SYS_SYSTEMCFG_H
51 # include <sys/systemcfg.h>
52 #endif
54 #ifdef _WIN32
55 # define WIN32_LEAN_AND_MEAN
56 # include <windows.h>
57 /* MEMORYSTATUSEX is missing from older windows headers, so define
58 a local replacement. */
59 typedef struct
61 DWORD dwLength;
62 DWORD dwMemoryLoad;
63 DWORDLONG ullTotalPhys;
64 DWORDLONG ullAvailPhys;
65 DWORDLONG ullTotalPageFile;
66 DWORDLONG ullAvailPageFile;
67 DWORDLONG ullTotalVirtual;
68 DWORDLONG ullAvailVirtual;
69 DWORDLONG ullAvailExtendedVirtual;
70 } lMEMORYSTATUSEX;
71 typedef WINBOOL (WINAPI *PFN_MS_EX) (lMEMORYSTATUSEX*);
72 #endif
74 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
76 /* Return the total amount of physical memory. */
77 double
78 physmem_total (void)
80 #if defined _SC_PHYS_PAGES && defined _SC_PAGESIZE
81 { /* This works on linux-gnu, solaris2 and cygwin. */
82 double pages = sysconf (_SC_PHYS_PAGES);
83 double pagesize = sysconf (_SC_PAGESIZE);
84 if (0 <= pages && 0 <= pagesize)
85 return pages * pagesize;
87 #endif
89 #if HAVE_PSTAT_GETSTATIC
90 { /* This works on hpux11. */
91 struct pst_static pss;
92 if (0 <= pstat_getstatic (&pss, sizeof pss, 1, 0))
94 double pages = pss.physical_memory;
95 double pagesize = pss.page_size;
96 if (0 <= pages && 0 <= pagesize)
97 return pages * pagesize;
100 #endif
102 #if HAVE_SYSMP && defined MP_SAGET && defined MPSA_RMINFO && defined _SC_PAGESIZE
103 { /* This works on irix6. */
104 struct rminfo realmem;
105 if (sysmp (MP_SAGET, MPSA_RMINFO, &realmem, sizeof realmem) == 0)
107 double pagesize = sysconf (_SC_PAGESIZE);
108 double pages = realmem.physmem;
109 if (0 <= pages && 0 <= pagesize)
110 return pages * pagesize;
113 #endif
115 #if HAVE_GETSYSINFO && defined GSI_PHYSMEM
116 { /* This works on Tru64 UNIX V4/5. */
117 int physmem;
119 if (getsysinfo (GSI_PHYSMEM, (caddr_t) &physmem, sizeof (physmem),
120 NULL, NULL, NULL) == 1)
122 double kbytes = physmem;
124 if (0 <= kbytes)
125 return kbytes * 1024.0;
128 #endif
130 #if HAVE_SYSCTL && defined HW_PHYSMEM
131 { /* This works on *bsd and darwin. */
132 unsigned int physmem;
133 size_t len = sizeof physmem;
134 static int mib[2] = { CTL_HW, HW_PHYSMEM };
136 if (sysctl (mib, ARRAY_SIZE (mib), &physmem, &len, NULL, 0) == 0
137 && len == sizeof (physmem))
138 return (double) physmem;
140 #endif
142 #if HAVE__SYSTEM_CONFIGURATION
143 /* This works on AIX. */
144 return _system_configuration.physmem;
145 #endif
147 #if defined _WIN32
148 { /* this works on windows */
149 PFN_MS_EX pfnex;
150 HMODULE h = GetModuleHandle ("kernel32.dll");
152 if (!h)
153 return 0.0;
155 /* Use GlobalMemoryStatusEx if available. */
156 if ((pfnex = (PFN_MS_EX) GetProcAddress (h, "GlobalMemoryStatusEx")))
158 lMEMORYSTATUSEX lms_ex;
159 lms_ex.dwLength = sizeof lms_ex;
160 if (!pfnex (&lms_ex))
161 return 0.0;
162 return (double) lms_ex.ullTotalPhys;
165 /* Fall back to GlobalMemoryStatus which is always available.
166 but returns wrong results for physical memory > 4GB. */
167 else
169 MEMORYSTATUS ms;
170 GlobalMemoryStatus (&ms);
171 return (double) ms.dwTotalPhys;
174 #endif
176 /* Guess 64 MB. It's probably an older host, so guess small. */
177 return 64 * 1024 * 1024;
180 /* Return the amount of physical memory available. */
181 double
182 physmem_available (void)
184 #if defined _SC_AVPHYS_PAGES && defined _SC_PAGESIZE
185 { /* This works on linux-gnu, solaris2 and cygwin. */
186 double pages = sysconf (_SC_AVPHYS_PAGES);
187 double pagesize = sysconf (_SC_PAGESIZE);
188 if (0 <= pages && 0 <= pagesize)
189 return pages * pagesize;
191 #endif
193 #if HAVE_PSTAT_GETSTATIC && HAVE_PSTAT_GETDYNAMIC
194 { /* This works on hpux11. */
195 struct pst_static pss;
196 struct pst_dynamic psd;
197 if (0 <= pstat_getstatic (&pss, sizeof pss, 1, 0)
198 && 0 <= pstat_getdynamic (&psd, sizeof psd, 1, 0))
200 double pages = psd.psd_free;
201 double pagesize = pss.page_size;
202 if (0 <= pages && 0 <= pagesize)
203 return pages * pagesize;
206 #endif
208 #if HAVE_SYSMP && defined MP_SAGET && defined MPSA_RMINFO && defined _SC_PAGESIZE
209 { /* This works on irix6. */
210 struct rminfo realmem;
211 if (sysmp (MP_SAGET, MPSA_RMINFO, &realmem, sizeof realmem) == 0)
213 double pagesize = sysconf (_SC_PAGESIZE);
214 double pages = realmem.availrmem;
215 if (0 <= pages && 0 <= pagesize)
216 return pages * pagesize;
219 #endif
221 #if HAVE_TABLE && defined TBL_VMSTATS
222 { /* This works on Tru64 UNIX V4/5. */
223 struct tbl_vmstats vmstats;
225 if (table (TBL_VMSTATS, 0, &vmstats, 1, sizeof (vmstats)) == 1)
227 double pages = vmstats.free_count;
228 double pagesize = vmstats.pagesize;
230 if (0 <= pages && 0 <= pagesize)
231 return pages * pagesize;
234 #endif
236 #if HAVE_SYSCTL && defined HW_USERMEM
237 { /* This works on *bsd and darwin. */
238 unsigned int usermem;
239 size_t len = sizeof usermem;
240 static int mib[2] = { CTL_HW, HW_USERMEM };
242 if (sysctl (mib, ARRAY_SIZE (mib), &usermem, &len, NULL, 0) == 0
243 && len == sizeof (usermem))
244 return (double) usermem;
246 #endif
248 #if defined _WIN32
249 { /* this works on windows */
250 PFN_MS_EX pfnex;
251 HMODULE h = GetModuleHandle ("kernel32.dll");
253 if (!h)
254 return 0.0;
256 /* Use GlobalMemoryStatusEx if available. */
257 if ((pfnex = (PFN_MS_EX) GetProcAddress (h, "GlobalMemoryStatusEx")))
259 lMEMORYSTATUSEX lms_ex;
260 lms_ex.dwLength = sizeof lms_ex;
261 if (!pfnex (&lms_ex))
262 return 0.0;
263 return (double) lms_ex.ullAvailPhys;
266 /* Fall back to GlobalMemoryStatus which is always available.
267 but returns wrong results for physical memory > 4GB */
268 else
270 MEMORYSTATUS ms;
271 GlobalMemoryStatus (&ms);
272 return (double) ms.dwAvailPhys;
275 #endif
277 /* Guess 25% of physical memory. */
278 return physmem_total () / 4;
282 #if DEBUG
284 # include <stdio.h>
285 # include <stdlib.h>
288 main (void)
290 printf ("%12.f %12.f\n", physmem_total (), physmem_available ());
291 exit (0);
294 #endif /* DEBUG */
297 Local Variables:
298 compile-command: "gcc -DDEBUG -g -O -Wall -W physmem.c"
299 End: