X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2Fphysmem.c;h=58ef1588e4ae94259ee4599f14651ff4c509eae2;hb=1500d77252e6158b8e421de52f60057b0feedab8;hp=4f192d11b2bbb0124370bda595d1fc8947780710;hpb=33ad5e9f9a412dffe953ed2dacff65700827710d;p=gnulib.git diff --git a/lib/physmem.c b/lib/physmem.c index 4f192d11b..58ef1588e 100644 --- a/lib/physmem.c +++ b/lib/physmem.c @@ -1,10 +1,12 @@ /* Calculate the size of physical memory. - Copyright (C) 2000, 2001, 2003 Free Software Foundation, Inc. - This program is free software; you can redistribute it and/or modify + Copyright (C) 2000-2001, 2003, 2005-2006, 2009-2011 Free Software + Foundation, Inc. + + This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2, or (at your option) - any later version. + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -12,20 +14,15 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software Foundation, - Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ + along with this program. If not, see . */ /* Written by Paul Eggert. */ -#if HAVE_CONFIG_H -# include -#endif +#include #include "physmem.h" -#if HAVE_UNISTD_H -# include -#endif +#include #if HAVE_SYS_PSTAT_H # include @@ -82,7 +79,7 @@ typedef WINBOOL (WINAPI *PFN_MS_EX) (lMEMORYSTATUSEX*); /* Return the total amount of physical memory. */ double -physmem_total () +physmem_total (void) { #if defined _SC_PHYS_PAGES && defined _SC_PAGESIZE { /* This works on linux-gnu, solaris2 and cygwin. */ @@ -98,10 +95,10 @@ physmem_total () struct pst_static pss; if (0 <= pstat_getstatic (&pss, sizeof pss, 1, 0)) { - double pages = pss.physical_memory; - double pagesize = pss.page_size; - if (0 <= pages && 0 <= pagesize) - return pages * pagesize; + double pages = pss.physical_memory; + double pagesize = pss.page_size; + if (0 <= pages && 0 <= pagesize) + return pages * pagesize; } } #endif @@ -111,10 +108,10 @@ physmem_total () struct rminfo realmem; if (sysmp (MP_SAGET, MPSA_RMINFO, &realmem, sizeof realmem) == 0) { - double pagesize = sysconf (_SC_PAGESIZE); - double pages = realmem.physmem; - if (0 <= pages && 0 <= pagesize) - return pages * pagesize; + double pagesize = sysconf (_SC_PAGESIZE); + double pages = realmem.physmem; + if (0 <= pages && 0 <= pagesize) + return pages * pagesize; } } #endif @@ -124,12 +121,12 @@ physmem_total () int physmem; if (getsysinfo (GSI_PHYSMEM, (caddr_t) &physmem, sizeof (physmem), - NULL, NULL, NULL) == 1) + NULL, NULL, NULL) == 1) { - double kbytes = physmem; + double kbytes = physmem; - if (0 <= kbytes) - return kbytes * 1024.0; + if (0 <= kbytes) + return kbytes * 1024.0; } } #endif @@ -141,7 +138,7 @@ physmem_total () static int mib[2] = { CTL_HW, HW_PHYSMEM }; if (sysctl (mib, ARRAY_SIZE (mib), &physmem, &len, NULL, 0) == 0 - && len == sizeof (physmem)) + && len == sizeof (physmem)) return (double) physmem; } #endif @@ -162,20 +159,20 @@ physmem_total () /* Use GlobalMemoryStatusEx if available. */ if ((pfnex = (PFN_MS_EX) GetProcAddress (h, "GlobalMemoryStatusEx"))) { - lMEMORYSTATUSEX lms_ex; - lms_ex.dwLength = sizeof lms_ex; - if (!pfnex (&lms_ex)) - return 0.0; - return (double) lms_ex.ullTotalPhys; + lMEMORYSTATUSEX lms_ex; + lms_ex.dwLength = sizeof lms_ex; + if (!pfnex (&lms_ex)) + return 0.0; + return (double) lms_ex.ullTotalPhys; } /* Fall back to GlobalMemoryStatus which is always available. but returns wrong results for physical memory > 4GB. */ else { - MEMORYSTATUS ms; - GlobalMemoryStatus (&ms); - return (double) ms.dwTotalPhys; + MEMORYSTATUS ms; + GlobalMemoryStatus (&ms); + return (double) ms.dwTotalPhys; } } #endif @@ -186,7 +183,7 @@ physmem_total () /* Return the amount of physical memory available. */ double -physmem_available () +physmem_available (void) { #if defined _SC_AVPHYS_PAGES && defined _SC_PAGESIZE { /* This works on linux-gnu, solaris2 and cygwin. */ @@ -202,12 +199,12 @@ physmem_available () struct pst_static pss; struct pst_dynamic psd; if (0 <= pstat_getstatic (&pss, sizeof pss, 1, 0) - && 0 <= pstat_getdynamic (&psd, sizeof psd, 1, 0)) + && 0 <= pstat_getdynamic (&psd, sizeof psd, 1, 0)) { - double pages = psd.psd_free; - double pagesize = pss.page_size; - if (0 <= pages && 0 <= pagesize) - return pages * pagesize; + double pages = psd.psd_free; + double pagesize = pss.page_size; + if (0 <= pages && 0 <= pagesize) + return pages * pagesize; } } #endif @@ -217,10 +214,10 @@ physmem_available () struct rminfo realmem; if (sysmp (MP_SAGET, MPSA_RMINFO, &realmem, sizeof realmem) == 0) { - double pagesize = sysconf (_SC_PAGESIZE); - double pages = realmem.availrmem; - if (0 <= pages && 0 <= pagesize) - return pages * pagesize; + double pagesize = sysconf (_SC_PAGESIZE); + double pages = realmem.availrmem; + if (0 <= pages && 0 <= pagesize) + return pages * pagesize; } } #endif @@ -231,11 +228,11 @@ physmem_available () if (table (TBL_VMSTATS, 0, &vmstats, 1, sizeof (vmstats)) == 1) { - double pages = vmstats.free_count; - double pagesize = vmstats.pagesize; + double pages = vmstats.free_count; + double pagesize = vmstats.pagesize; - if (0 <= pages && 0 <= pagesize) - return pages * pagesize; + if (0 <= pages && 0 <= pagesize) + return pages * pagesize; } } #endif @@ -247,7 +244,7 @@ physmem_available () static int mib[2] = { CTL_HW, HW_USERMEM }; if (sysctl (mib, ARRAY_SIZE (mib), &usermem, &len, NULL, 0) == 0 - && len == sizeof (usermem)) + && len == sizeof (usermem)) return (double) usermem; } #endif @@ -263,20 +260,20 @@ physmem_available () /* Use GlobalMemoryStatusEx if available. */ if ((pfnex = (PFN_MS_EX) GetProcAddress (h, "GlobalMemoryStatusEx"))) { - lMEMORYSTATUSEX lms_ex; - lms_ex.dwLength = sizeof lms_ex; - if (!pfnex (&lms_ex)) - return 0.0; - return (double) lms_ex.ullAvailPhys; + lMEMORYSTATUSEX lms_ex; + lms_ex.dwLength = sizeof lms_ex; + if (!pfnex (&lms_ex)) + return 0.0; + return (double) lms_ex.ullAvailPhys; } /* Fall back to GlobalMemoryStatus which is always available. but returns wrong results for physical memory > 4GB */ else { - MEMORYSTATUS ms; - GlobalMemoryStatus (&ms); - return (double) ms.dwAvailPhys; + MEMORYSTATUS ms; + GlobalMemoryStatus (&ms); + return (double) ms.dwAvailPhys; } } #endif @@ -292,7 +289,7 @@ physmem_available () # include int -main () +main (void) { printf ("%12.f %12.f\n", physmem_total (), physmem_available ()); exit (0); @@ -302,6 +299,6 @@ main () /* Local Variables: -compile-command: "gcc -DDEBUG -DHAVE_CONFIG_H -I.. -g -O -Wall -W physmem.c" +compile-command: "gcc -DDEBUG -g -O -Wall -W physmem.c" End: */