1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
/*-----------------------------------------------------------------------------------*/
/* INRIA 2005 */
/* Allan CORNET */
/* References
http://nixdoc.net/man-pages/Tru64/man2/getsysinfo.2.html
http://www.opensource.apple.com/darwinsource/projects/other/gccfast-1621.1/libiberty/physmem.c
http://lists.gnu.org/archive/html/bug-gnulib/2003-08/msg00102.html
*/
#include "getmemory.h"
int getfreememory()
{
#define kooctet 1024
#undef MEMOK
#if defined(WIN32)
{
#define MEMOK 1
MEMORYSTATUS stat;
GlobalMemoryStatus (&stat);
return stat.dwAvailPhys/kooctet;
}
#endif
#if defined(hpux)
#define MEMOK 1
{
struct pst_static pst;
/* pstat_getstatic(&pst, sizeof(pst), (size_t) 1, 0);
memorysizeKO=(pst.psd_free)/kooctet;*/
return 0;
}
#endif
#if defined(__APPLE__)
#define MEMOK 1
{
vm_statistics_data_t page_info;
vm_size_t pagesize;
mach_msg_type_number_t count;
kern_return_t kret;
pagesize = 0;
kret = host_page_size (mach_host_self(), &pagesize);
count = HOST_VM_INFO_COUNT;
kret = host_statistics (mach_host_self(), HOST_VM_INFO,(host_info_t)&page_info, &count);
return page_info.free_count*pagesize / 1024;
}
#endif
#if HAVE_TABLE && defined TBL_VMSTATS
#define MEMOK 1
{ /* This works on Tru64 UNIX V4/5. */
struct tbl_vmstats vmstats;
if (table (TBL_VMSTATS, 0, &vmstats, 1, sizeof (vmstats)) == 1)
{
double pages = vmstats.free_count;
double pagesize = vmstats.pagesize;
if (0 <= pages && 0 <= pagesize)
return pages * pagesize;
else
return 0;
}
}
#endif
#ifndef MEMOK
/* Linux ,Solaris and others assumed*/
return (sysconf(_SC_AVPHYS_PAGES)*sysconf(_SC_PAGESIZE))/kooctet;
#endif
}
/*-----------------------------------------------------------------------------------*/
int getmemorysize()
{
#define kooctet 1024
#undef MEMOK
#if defined(WIN32)
#define MEMOK 1
{
MEMORYSTATUS stat;
GlobalMemoryStatus (&stat);
return stat.dwTotalPhys/kooctet;
}
#endif
#if defined(hpux)
#define MEMOK 1
{
struct pst_static pst;
pstat_getstatic(&pst, sizeof(pst), (size_t) 1, 0);
return (pst.physical_memory)/kooctet;
}
#endif
#if defined(__APPLE__)
#define MEMOK 1
{
size_t len;
int total;
int mib[2];
mib[0] = CTL_HW;
mib[1] = HW_PHYSMEM;
len = sizeof (total);
sysctl(mib, 2, &total, &len, NULL, 0);
return total/1024;
}
#endif
#if HAVE_GETSYSINFO && defined GSI_PHYSMEM
#define MEMOK 1
{ /* This works on Tru64 UNIX V4/5. */
int physmem;
if (getsysinfo (GSI_PHYSMEM, (caddr_t) &physmem, sizeof (physmem),
NULL, NULL, NULL) == 1)
{
double kbytes = physmem;
if (0 <= kbytes)
return kbytes * 1024.0;
else
return 0;
}
}
#endif
#ifndef MEMOK
/* Linux ,Solaris and others */
return (sysconf(_SC_PHYS_PAGES)*sysconf(_SC_PAGESIZE))/kooctet;
#endif
}
/*-----------------------------------------------------------------------------------*/
|