merge from gcc
[external/binutils.git] / libiberty / physmem.c
1 /* Calculate the size of physical memory.
2    Copyright 2000, 2001 Free Software Foundation, Inc.
3
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.
8
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.
13
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.  */
17
18 /* Written by Paul Eggert.  */
19
20 #if HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #if HAVE_UNISTD_H
25 # include <unistd.h>
26 #endif
27
28 #if HAVE_SYS_PSTAT_H
29 # include <sys/pstat.h>
30 #endif
31
32 /* Return the total amount of physical memory.  */
33 double
34 physmem_total ()
35 {
36 #if defined _SC_PHYS_PAGES && defined _SC_PAGESIZE
37   {
38     double pages = sysconf (_SC_PHYS_PAGES);
39     double pagesize = sysconf (_SC_PAGESIZE);
40     if (0 <= pages && 0 <= pagesize)
41       return pages * pagesize;
42   }
43 #endif
44
45 #if HAVE_PSTAT_GETSTATIC
46   {
47     struct pst_static pss;
48     if (0 <= pstat_getstatic (&pss, sizeof pss, 1, 0))
49       {
50         double pages = pss.physical_memory;
51         double pagesize = pss.page_size;
52         if (0 <= pages && 0 <= pagesize)
53           return pages * pagesize;
54       }
55   }
56 #endif
57
58   /* Return 0 if we can't determine the value.  */
59   return 0;
60 }
61
62 /* Return the amount of physical memory available.  */
63 double
64 physmem_available ()
65 {
66 #if defined _SC_AVPHYS_PAGES && defined _SC_PAGESIZE
67   {
68     double pages = sysconf (_SC_AVPHYS_PAGES);
69     double pagesize = sysconf (_SC_PAGESIZE);
70     if (0 <= pages && 0 <= pagesize)
71       return pages * pagesize;
72   }
73 #endif
74
75 #if HAVE_PSTAT_GETSTATIC && HAVE_PSTAT_GETDYNAMIC
76   {
77     struct pst_static pss;
78     struct pst_dynamic psd;
79     if (0 <= pstat_getstatic (&pss, sizeof pss, 1, 0)
80         && 0 <= pstat_getdynamic (&psd, sizeof psd, 1, 0))
81       {
82         double pages = psd.psd_free;
83         double pagesize = pss.page_size;
84         if (0 <= pages && 0 <= pagesize)
85           return pages * pagesize;
86       }
87   }
88 #endif
89
90   /* Guess 25% of physical memory.  */
91   return physmem_total () / 4;
92 }