OpenCoverage

physmem.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/coreutils/src/gnulib/lib/physmem.c
Source codeSwitch to Preprocessed file
LineSourceCount
1/* Calculate the size of physical memory.-
2-
3 Copyright (C) 2000-2001, 2003, 2005-2006, 2009-2018 Free Software-
4 Foundation, Inc.-
5-
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.-
10-
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.-
15-
16 You should have received a copy of the GNU General Public License-
17 along with this program. If not, see <https://www.gnu.org/licenses/>. */-
18-
19/* Written by Paul Eggert. */-
20-
21#include <config.h>-
22-
23#include "physmem.h"-
24-
25#include <unistd.h>-
26-
27#if HAVE_SYS_PSTAT_H-
28# include <sys/pstat.h>-
29#endif-
30-
31#if HAVE_SYS_SYSMP_H-
32# include <sys/sysmp.h>-
33#endif-
34-
35#if HAVE_SYS_SYSINFO_H-
36# include <sys/sysinfo.h>-
37#endif-
38-
39#if HAVE_MACHINE_HAL_SYSINFO_H-
40# include <machine/hal_sysinfo.h>-
41#endif-
42-
43#if HAVE_SYS_TABLE_H-
44# include <sys/table.h>-
45#endif-
46-
47#include <sys/types.h>-
48-
49#if HAVE_SYS_PARAM_H-
50# include <sys/param.h>-
51#endif-
52-
53#if HAVE_SYS_SYSCTL_H-
54# include <sys/sysctl.h>-
55#endif-
56-
57#if HAVE_SYS_SYSTEMCFG_H-
58# include <sys/systemcfg.h>-
59#endif-
60-
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. */-
66typedef struct-
67{-
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;-
78typedef WINBOOL (WINAPI *PFN_MS_EX) (lMEMORYSTATUSEX*);-
79#endif-
80-
81#define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))-
82-
83/* Return the total amount of physical memory. */-
84double-
85physmem_total (void)-
86{-
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)
0 <= pagesDescription
TRUEevaluated 685 times by 1 test
Evaluated by:
  • sort
FALSEnever evaluated
0 <= pagesizeDescription
TRUEevaluated 685 times by 1 test
Evaluated by:
  • sort
FALSEnever evaluated
0-685
92 return pages * pagesize;
executed 685 times by 1 test: return pages * pagesize;
Executed by:
  • sort
685
93 }-
94#endif-
95-
96#if HAVE_SYSINFO && HAVE_STRUCT_SYSINFO_MEM_UNIT-
97 { /* This works on linux. */-
98 struct sysinfo si;-
99 if (sysinfo(&si) == 0)
sysinfo(&si) == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
100 return (double) si.totalram * si.mem_unit;
never executed: return (double) si.totalram * si.mem_unit;
0
101 }-
102#endif-
103-
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))-
108 {-
109 double pages = pss.physical_memory;-
110 double pagesize = pss.page_size;-
111 if (0 <= pages && 0 <= pagesize)-
112 return pages * pagesize;-
113 }-
114 }-
115#endif-
116-
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)-
121 {-
122 double pagesize = sysconf (_SC_PAGESIZE);-
123 double pages = realmem.physmem;-
124 if (0 <= pages && 0 <= pagesize)-
125 return pages * pagesize;-
126 }-
127 }-
128#endif-
129-
130#if HAVE_GETSYSINFO && defined GSI_PHYSMEM-
131 { /* This works on Tru64 UNIX V4/5. */-
132 int physmem;-
133-
134 if (getsysinfo (GSI_PHYSMEM, (caddr_t) &physmem, sizeof (physmem),-
135 NULL, NULL, NULL) == 1)-
136 {-
137 double kbytes = physmem;-
138-
139 if (0 <= kbytes)-
140 return kbytes * 1024.0;-
141 }-
142 }-
143#endif-
144-
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 };-
150-
151 if (sysctl (mib, ARRAY_SIZE (mib), &physmem, &len, NULL, 0) == 0-
152 && len == sizeof (physmem))-
153 return (double) physmem;-
154 }-
155#endif-
156-
157#if HAVE__SYSTEM_CONFIGURATION-
158 /* This works on AIX. */-
159 return _system_configuration.physmem;-
160#endif-
161-
162#if defined _WIN32-
163 { /* this works on windows */-
164 PFN_MS_EX pfnex;-
165 HMODULE h = GetModuleHandle ("kernel32.dll");-
166-
167 if (!h)-
168 return 0.0;-
169-
170 /* Use GlobalMemoryStatusEx if available. */-
171 if ((pfnex = (PFN_MS_EX) GetProcAddress (h, "GlobalMemoryStatusEx")))-
172 {-
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;-
178 }-
179-
180 /* Fall back to GlobalMemoryStatus which is always available.-
181 but returns wrong results for physical memory > 4GB. */-
182 else-
183 {-
184 MEMORYSTATUS ms;-
185 GlobalMemoryStatus (&ms);-
186 return (double) ms.dwTotalPhys;-
187 }-
188 }-
189#endif-
190-
191 /* Guess 64 MB. It's probably an older host, so guess small. */-
192 return 64 * 1024 * 1024;
never executed: return 64 * 1024 * 1024;
0
193}-
194-
195/* Return the amount of physical memory available. */-
196double-
197physmem_available (void)-
198{-
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)
0 <= pagesDescription
TRUEevaluated 696 times by 2 tests
Evaluated by:
  • sort
  • wc
FALSEnever evaluated
0 <= pagesizeDescription
TRUEevaluated 696 times by 2 tests
Evaluated by:
  • sort
  • wc
FALSEnever evaluated
0-696
204 return pages * pagesize;
executed 696 times by 2 tests: return pages * pagesize;
Executed by:
  • sort
  • wc
696
205 }-
206#endif-
207-
208#if HAVE_SYSINFO && HAVE_STRUCT_SYSINFO_MEM_UNIT-
209 { /* This works on linux. */-
210 struct sysinfo si;-
211 if (sysinfo(&si) == 0)
sysinfo(&si) == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
212 return ((double) si.freeram + si.bufferram) * si.mem_unit;
never executed: return ((double) si.freeram + si.bufferram) * si.mem_unit;
0
213 }-
214#endif-
215-
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))-
222 {-
223 double pages = psd.psd_free;-
224 double pagesize = pss.page_size;-
225 if (0 <= pages && 0 <= pagesize)-
226 return pages * pagesize;-
227 }-
228 }-
229#endif-
230-
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)-
235 {-
236 double pagesize = sysconf (_SC_PAGESIZE);-
237 double pages = realmem.availrmem;-
238 if (0 <= pages && 0 <= pagesize)-
239 return pages * pagesize;-
240 }-
241 }-
242#endif-
243-
244#if HAVE_TABLE && defined TBL_VMSTATS-
245 { /* This works on Tru64 UNIX V4/5. */-
246 struct tbl_vmstats vmstats;-
247-
248 if (table (TBL_VMSTATS, 0, &vmstats, 1, sizeof (vmstats)) == 1)-
249 {-
250 double pages = vmstats.free_count;-
251 double pagesize = vmstats.pagesize;-
252-
253 if (0 <= pages && 0 <= pagesize)-
254 return pages * pagesize;-
255 }-
256 }-
257#endif-
258-
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 };-
264-
265 if (sysctl (mib, ARRAY_SIZE (mib), &usermem, &len, NULL, 0) == 0-
266 && len == sizeof (usermem))-
267 return (double) usermem;-
268 }-
269#endif-
270-
271#if defined _WIN32-
272 { /* this works on windows */-
273 PFN_MS_EX pfnex;-
274 HMODULE h = GetModuleHandle ("kernel32.dll");-
275-
276 if (!h)-
277 return 0.0;-
278-
279 /* Use GlobalMemoryStatusEx if available. */-
280 if ((pfnex = (PFN_MS_EX) GetProcAddress (h, "GlobalMemoryStatusEx")))-
281 {-
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;-
287 }-
288-
289 /* Fall back to GlobalMemoryStatus which is always available.-
290 but returns wrong results for physical memory > 4GB */-
291 else-
292 {-
293 MEMORYSTATUS ms;-
294 GlobalMemoryStatus (&ms);-
295 return (double) ms.dwAvailPhys;-
296 }-
297 }-
298#endif-
299-
300 /* Guess 25% of physical memory. */-
301 return physmem_total () / 4;
never executed: return physmem_total () / 4;
0
302}-
303-
304-
305#if DEBUG-
306-
307# include <stdio.h>-
308# include <stdlib.h>-
309-
310int-
311main (void)-
312{-
313 printf ("%12.f %12.f\n", physmem_total (), physmem_available ());-
314 exit (0);-
315}-
316-
317#endif /* DEBUG */-
318-
319/*-
320Local Variables:-
321compile-command: "gcc -DDEBUG -g -O -Wall -W physmem.c"-
322End:-
323*/-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.1.2