merge from gcc
[binutils.git] / libiberty / physmem.c
blob2011f0251bd1a3df3667909ac8854e8a03260722
1 /* Calculate the size of physical memory.
2 Copyright 2000, 2001, 2003 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Written by Paul Eggert and Jim Meyering. */
20 #if HAVE_CONFIG_H
21 # include <config.h>
22 #endif
24 #if HAVE_UNISTD_H
25 # include <unistd.h>
26 #endif
28 #if HAVE_SYS_PSTAT_H
29 # include <sys/pstat.h>
30 #endif
32 #if HAVE_SYS_SYSMP_H
33 #include <sys/sysmp.h>
34 #endif
36 #if HAVE_SYS_SYSINFO_H && HAVE_MACHINE_HAL_SYSINFO_H
37 # include <sys/sysinfo.h>
38 # include <machine/hal_sysinfo.h>
39 #endif
41 #if HAVE_SYS_TABLE_H
42 # include <sys/table.h>
43 #endif
45 #include "libiberty.h"
47 /* Return the total amount of physical memory. */
48 double
49 physmem_total ()
51 #if defined _SC_PHYS_PAGES && defined _SC_PAGESIZE
53 double pages = sysconf (_SC_PHYS_PAGES);
54 double pagesize = sysconf (_SC_PAGESIZE);
55 if (0 <= pages && 0 <= pagesize)
56 return pages * pagesize;
58 #endif
60 #if HAVE_PSTAT_GETSTATIC
61 { /* This works on hpux11. */
62 struct pst_static pss;
63 if (0 <= pstat_getstatic (&pss, sizeof pss, 1, 0))
65 double pages = pss.physical_memory;
66 double pagesize = pss.page_size;
67 if (0 <= pages && 0 <= pagesize)
68 return pages * pagesize;
71 #endif
73 #if HAVE_SYSMP && defined MP_SAGET && defined MPSA_RMINFO && defined _SC_PAGESIZE
74 { /* This works on irix6. */
75 struct rminfo realmem;
76 if (sysmp(MP_SAGET, MPSA_RMINFO, &realmem, sizeof(realmem)) == 0)
78 double pagesize = sysconf (_SC_PAGESIZE);
79 double pages = realmem.physmem;
80 if (0 <= pages && 0 <= pagesize)
81 return pages * pagesize;
84 #endif
86 #if HAVE_GETSYSINFO
87 { /* This works on Tru64 UNIX V4/5. */
88 int physmem;
90 if (getsysinfo (GSI_PHYSMEM, (caddr_t) &physmem, sizeof (physmem),
91 NULL, NULL, NULL) == 1)
93 double kbytes = physmem;
95 if (0 <= kbytes)
96 return kbytes * 1024.0;
99 #endif
101 /* Return 0 if we can't determine the value. */
102 return 0;
105 /* Return the amount of physical memory available. */
106 double
107 physmem_available ()
109 #if defined _SC_AVPHYS_PAGES && defined _SC_PAGESIZE
111 double pages = sysconf (_SC_AVPHYS_PAGES);
112 double pagesize = sysconf (_SC_PAGESIZE);
113 if (0 <= pages && 0 <= pagesize)
114 return pages * pagesize;
116 #endif
118 #if HAVE_PSTAT_GETSTATIC && HAVE_PSTAT_GETDYNAMIC
119 { /* This works on hpux11. */
120 struct pst_static pss;
121 struct pst_dynamic psd;
122 if (0 <= pstat_getstatic (&pss, sizeof pss, 1, 0)
123 && 0 <= pstat_getdynamic (&psd, sizeof psd, 1, 0))
125 double pages = psd.psd_free;
126 double pagesize = pss.page_size;
127 if (0 <= pages && 0 <= pagesize)
128 return pages * pagesize;
131 #endif
133 #if HAVE_SYSMP && defined MP_SAGET && defined MPSA_RMINFO && defined _SC_PAGESIZE
134 { /* This works on irix6. */
135 struct rminfo realmem;
136 if (sysmp(MP_SAGET, MPSA_RMINFO, &realmem, sizeof(realmem)) == 0)
138 double pagesize = sysconf (_SC_PAGESIZE);
139 double pages = realmem.availrmem;
140 if (0 <= pages && 0 <= pagesize)
141 return pages * pagesize;
144 #endif
146 #if HAVE_TABLE && HAVE_SYS_TABLE_H
147 { /* This works on Tru64 UNIX V4/5. */
148 struct tbl_vmstats vmstats;
150 if (table (TBL_VMSTATS, 0, &vmstats, 1, sizeof (vmstats)) == 1)
152 double pages = vmstats.free_count;
153 double pagesize = vmstats.pagesize;
155 if (0 <= pages && 0 <= pagesize)
156 return pages * pagesize;
159 #endif
161 /* Guess 25% of physical memory. */
162 return physmem_total () / 4;
166 #if DEBUG
168 # include <stdio.h>
169 # include <stdlib.h>
172 main ()
174 printf ("%12.f %12.f\n", physmem_total (), physmem_available ());
175 exit (0);
178 #endif /* DEBUG */
181 Local Variables:
182 compile-command: "gcc -DDEBUG -DHAVE_CONFIG_H -I.. -g -O -Wall -W physmem.c"
183 End: