| 1 |  | /* Calculate the size of physical memory. | 
| 2 | – | Copyright 2000, 2001, 2003 Free Software Foundation, Inc. | 
| 2 |  |  | 
| 3 | < | This program is free software; you can redistribute it and/or modify | 
| 3 | > | Copyright (C) 2000, 2001, 2003, 2005, 2006 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 2, or (at your option) | 
| 9 | < | any later version. | 
| 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 | 
| 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, write to the Free Software Foundation, | 
| 16 | < | Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */ | 
| 17 | > | along with this program.  If not, see <http://www.gnu.org/licenses/>.  */ | 
| 18 |  |  | 
| 19 |  | /* Written by Paul Eggert.  */ | 
| 20 |  |  | 
| 21 |  | #include "config.h" | 
| 22 |  |  | 
| 23 | < | #if HAVE_UNISTD_H | 
| 23 | < | # include <unistd.h> | 
| 24 | < | #endif | 
| 23 | > | #include "physmem.h" | 
| 24 |  |  | 
| 25 | + | #include <unistd.h> | 
| 26 | + |  | 
| 27 |  | #if HAVE_SYS_PSTAT_H | 
| 28 |  | # include <sys/pstat.h> | 
| 29 |  | #endif | 
| 75 |  | typedef WINBOOL (WINAPI *PFN_MS_EX) (lMEMORYSTATUSEX*); | 
| 76 |  | #endif | 
| 77 |  |  | 
| 78 | + | #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0])) | 
| 79 | + |  | 
| 80 |  | /* Return the total amount of physical memory.  */ | 
| 81 | < | RealType | 
| 82 | < | physmem_total () | 
| 81 | > | double | 
| 82 | > | physmem_total (void) | 
| 83 |  | { | 
| 84 |  | #if defined _SC_PHYS_PAGES && defined _SC_PAGESIZE | 
| 85 |  | { /* This works on linux-gnu, solaris2 and cygwin.  */ | 
| 86 | < | RealType pages = sysconf (_SC_PHYS_PAGES); | 
| 87 | < | RealType pagesize = sysconf (_SC_PAGESIZE); | 
| 86 | > | double pages = sysconf (_SC_PHYS_PAGES); | 
| 87 | > | double pagesize = sysconf (_SC_PAGESIZE); | 
| 88 |  | if (0 <= pages && 0 <= pagesize) | 
| 89 |  | return pages * pagesize; | 
| 90 |  | } | 
| 95 |  | struct pst_static pss; | 
| 96 |  | if (0 <= pstat_getstatic (&pss, sizeof pss, 1, 0)) | 
| 97 |  | { | 
| 98 | < | RealType pages = pss.physical_memory; | 
| 99 | < | RealType pagesize = pss.page_size; | 
| 98 | > | double pages = pss.physical_memory; | 
| 99 | > | double pagesize = pss.page_size; | 
| 100 |  | if (0 <= pages && 0 <= pagesize) | 
| 101 |  | return pages * pagesize; | 
| 102 |  | } | 
| 108 |  | struct rminfo realmem; | 
| 109 |  | if (sysmp (MP_SAGET, MPSA_RMINFO, &realmem, sizeof realmem) == 0) | 
| 110 |  | { | 
| 111 | < | RealType pagesize = sysconf (_SC_PAGESIZE); | 
| 112 | < | RealType pages = realmem.physmem; | 
| 111 | > | double pagesize = sysconf (_SC_PAGESIZE); | 
| 112 | > | double pages = realmem.physmem; | 
| 113 |  | if (0 <= pages && 0 <= pagesize) | 
| 114 |  | return pages * pagesize; | 
| 115 |  | } | 
| 123 |  | if (getsysinfo (GSI_PHYSMEM, (caddr_t) &physmem, sizeof (physmem), | 
| 124 |  | NULL, NULL, NULL) == 1) | 
| 125 |  | { | 
| 126 | < | RealType kbytes = physmem; | 
| 126 | > | double kbytes = physmem; | 
| 127 |  |  | 
| 128 |  | if (0 <= kbytes) | 
| 129 |  | return kbytes * 1024.0; | 
| 137 |  | size_t len = sizeof physmem; | 
| 138 |  | static int mib[2] = { CTL_HW, HW_PHYSMEM }; | 
| 139 |  |  | 
| 140 | < | if (sysctl (mib, 2, &physmem, &len, NULL, 0) == 0 | 
| 140 | > | if (sysctl (mib, ARRAY_SIZE (mib), &physmem, &len, NULL, 0) == 0 | 
| 141 |  | && len == sizeof (physmem)) | 
| 142 | < | return (RealType) physmem; | 
| 142 | > | return (double) physmem; | 
| 143 |  | } | 
| 144 |  | #endif | 
| 145 |  |  | 
| 163 |  | lms_ex.dwLength = sizeof lms_ex; | 
| 164 |  | if (!pfnex (&lms_ex)) | 
| 165 |  | return 0.0; | 
| 166 | < | return (RealType) lms_ex.ullTotalPhys; | 
| 166 | > | return (double) lms_ex.ullTotalPhys; | 
| 167 |  | } | 
| 168 |  |  | 
| 169 |  | /*  Fall back to GlobalMemoryStatus which is always available. | 
| 172 |  | { | 
| 173 |  | MEMORYSTATUS ms; | 
| 174 |  | GlobalMemoryStatus (&ms); | 
| 175 | < | return (RealType) ms.dwTotalPhys; | 
| 175 | > | return (double) ms.dwTotalPhys; | 
| 176 |  | } | 
| 177 |  | } | 
| 178 |  | #endif | 
| 179 |  |  | 
| 180 | < | /* Return 0 if we can't determine the value.  */ | 
| 181 | < | return 0; | 
| 180 | > | /* Guess 64 MB.  It's probably an older host, so guess small.  */ | 
| 181 | > | return 64 * 1024 * 1024; | 
| 182 |  | } | 
| 183 |  |  | 
| 184 |  | /* Return the amount of physical memory available.  */ | 
| 185 | < | RealType | 
| 186 | < | physmem_available () | 
| 185 | > | double | 
| 186 | > | physmem_available (void) | 
| 187 |  | { | 
| 188 |  | #if defined _SC_AVPHYS_PAGES && defined _SC_PAGESIZE | 
| 189 |  | { /* This works on linux-gnu, solaris2 and cygwin.  */ | 
| 190 | < | RealType pages = sysconf (_SC_AVPHYS_PAGES); | 
| 191 | < | RealType pagesize = sysconf (_SC_PAGESIZE); | 
| 190 | > | double pages = sysconf (_SC_AVPHYS_PAGES); | 
| 191 | > | double pagesize = sysconf (_SC_PAGESIZE); | 
| 192 |  | if (0 <= pages && 0 <= pagesize) | 
| 193 |  | return pages * pagesize; | 
| 194 |  | } | 
| 201 |  | if (0 <= pstat_getstatic (&pss, sizeof pss, 1, 0) | 
| 202 |  | && 0 <= pstat_getdynamic (&psd, sizeof psd, 1, 0)) | 
| 203 |  | { | 
| 204 | < | RealType pages = psd.psd_free; | 
| 205 | < | RealType pagesize = pss.page_size; | 
| 204 | > | double pages = psd.psd_free; | 
| 205 | > | double pagesize = pss.page_size; | 
| 206 |  | if (0 <= pages && 0 <= pagesize) | 
| 207 |  | return pages * pagesize; | 
| 208 |  | } | 
| 214 |  | struct rminfo realmem; | 
| 215 |  | if (sysmp (MP_SAGET, MPSA_RMINFO, &realmem, sizeof realmem) == 0) | 
| 216 |  | { | 
| 217 | < | RealType pagesize = sysconf (_SC_PAGESIZE); | 
| 218 | < | RealType pages = realmem.availrmem; | 
| 217 | > | double pagesize = sysconf (_SC_PAGESIZE); | 
| 218 | > | double pages = realmem.availrmem; | 
| 219 |  | if (0 <= pages && 0 <= pagesize) | 
| 220 |  | return pages * pagesize; | 
| 221 |  | } | 
| 228 |  |  | 
| 229 |  | if (table (TBL_VMSTATS, 0, &vmstats, 1, sizeof (vmstats)) == 1) | 
| 230 |  | { | 
| 231 | < | RealType pages = vmstats.free_count; | 
| 232 | < | RealType pagesize = vmstats.pagesize; | 
| 231 | > | double pages = vmstats.free_count; | 
| 232 | > | double pagesize = vmstats.pagesize; | 
| 233 |  |  | 
| 234 |  | if (0 <= pages && 0 <= pagesize) | 
| 235 |  | return pages * pagesize; | 
| 243 |  | size_t len = sizeof usermem; | 
| 244 |  | static int mib[2] = { CTL_HW, HW_USERMEM }; | 
| 245 |  |  | 
| 246 | < | if (sysctl (mib, 2, &usermem, &len, NULL, 0) == 0 | 
| 246 | > | if (sysctl (mib, ARRAY_SIZE (mib), &usermem, &len, NULL, 0) == 0 | 
| 247 |  | && len == sizeof (usermem)) | 
| 248 | < | return (RealType) usermem; | 
| 248 | > | return (double) usermem; | 
| 249 |  | } | 
| 250 |  | #endif | 
| 251 |  |  | 
| 264 |  | lms_ex.dwLength = sizeof lms_ex; | 
| 265 |  | if (!pfnex (&lms_ex)) | 
| 266 |  | return 0.0; | 
| 267 | < | return (RealType) lms_ex.ullAvailPhys; | 
| 267 | > | return (double) lms_ex.ullAvailPhys; | 
| 268 |  | } | 
| 269 |  |  | 
| 270 |  | /*  Fall back to GlobalMemoryStatus which is always available. | 
| 273 |  | { | 
| 274 |  | MEMORYSTATUS ms; | 
| 275 |  | GlobalMemoryStatus (&ms); | 
| 276 | < | return (RealType) ms.dwAvailPhys; | 
| 276 | > | return (double) ms.dwAvailPhys; | 
| 277 |  | } | 
| 278 |  | } | 
| 279 |  | #endif |