2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2005 - INRIA - Allan CORNET
4 * Copyright (C) 2009 - Dan McMahill (Fix of bug 4299 and 4312)
6 * Copyright (C) 2012 - 2016 - Scilab Enterprises
8 * This file is hereby licensed under the terms of the GNU GPL v2.0,
9 * pursuant to article 5.3.4 of the CeCILL v.2.1.
10 * This file was originally licensed under the terms of the CeCILL v2.1,
11 * and continues to be available under such terms.
12 * For more information, see the COPYING file which you should have received
13 * along with this program.
18 @sa http://nixdoc.net/man-pages/Tru64/man2/getsysinfo.2.html
19 @sa http://www.opensource.apple.com/darwinsource/projects/other/gccfast-1621.1/libiberty/physmem.c
20 @sa http://lists.gnu.org/archive/html/bug-gnulib/2003-08/msg00102.html
21 @sa http://netbsd.gw.com/cgi-bin/man-cgi?sysctl+3+NetBSD-4.0
22 @sa http://cvsweb.netbsd.org/bsdweb.cgi/pkgsrc/math/scilab/patches/patch-aj?annotate=1.9
25 #if defined(__linux__)
26 #define _GNU_SOURCE /* Bug 5673 fix: avoid dependency on GLIBC_2.7 */
29 #include "getmemory.h"
35 #if defined(__NetBSD__) || defined(__DragonFly__)
36 #include <sys/param.h>
37 #include <sys/sysctl.h>
38 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
39 #define PAGESHIFT_UNDEF -100
40 #elif defined(__FreeBSD__)
41 #include <sys/param.h>
42 #include <sys/sysctl.h>
49 int getfreememory(void)
55 GlobalMemoryStatus (&stat);
56 return (int)(stat.dwAvailPhys / kooctet);
60 struct pst_static pst;
61 /* pstat_getstatic(&pst, sizeof(pst), (size_t) 1, 0);
62 memorysizeKO=(pst.psd_free)/kooctet;*/
65 #elif defined(__APPLE__)
67 vm_statistics_data_t page_info;
69 mach_msg_type_number_t count;
73 kret = host_page_size (mach_host_self(), &pagesize);
74 count = HOST_VM_INFO_COUNT;
76 kret = host_statistics (mach_host_self(), HOST_VM_INFO, (host_info_t)&page_info, &count);
77 return page_info.free_count * pagesize / 1024;
79 #elif HAVE_TABLE && defined TBL_VMSTATS
81 /* This works on Tru64 UNIX V4/5. */
82 struct tbl_vmstats vmstats;
84 if (table (TBL_VMSTATS, 0, &vmstats, 1, sizeof (vmstats)) == 1)
86 double pages = vmstats.free_count;
87 double pagesize = vmstats.pagesize;
89 if (0 <= pages && 0 <= pagesize)
91 return pages * pagesize;
99 #elif defined(__linux__)
109 FILE *fp = fopen("/proc/meminfo", "r");
112 /* Read Cached, Buffers and MemFree from /proc/meminfo */
113 while (fscanf(fp, "%8s %lld %3s\n", field, &data, unit) != EOF)
115 if (!strncmp("MemFree:", field, 8))
119 else if (!strncmp("Buffers:", field, 8))
123 else if (!strncmp("Cached:", field, 8))
130 /* Read successful, convert unit and return the result */
131 if (buffers >= 0 && cached >= 0 && free >= 0)
133 free += cached + buffers;
138 free *= kooctet*kooctet;
153 /* Strange, /proc not mounted ? new and unknown format ?
154 fall back to inaccurate sysconf() */
155 return (sysconf(_SC_AVPHYS_PAGES) * sysconf(_SC_PAGESIZE)) / kooctet;
157 #elif defined(__NetBSD__) || defined(__DragonFly__)
159 /* This works on *bsd. */
161 struct uvmexp_sysctl uvmexp;
163 size_t lenu = sizeof uvmexp;
164 unsigned int pagesize;
165 static int pageshift = PAGESHIFT_UNDEF;
166 size_t lenp = sizeof pagesize;
169 if (pageshift == PAGESHIFT_UNDEF)
171 /* Figure out the page size */
172 mib[1] = HW_PAGESIZE;
173 if (sysctl (mib, ARRAY_SIZE (mib), &pagesize, &lenp, NULL, 0) != 0 || lenp != sizeof (pagesize) )
175 /* sysctl failed -- what to do?? */
191 if ( (sysctl (mib, ARRAY_SIZE (mib), &uvmexp, &lenu, NULL, 0) == 0) && (lenu == sizeof (uvmexp)) )
193 return (uvmexp.free << pageshift);
198 #elif defined(__FreeBSD__)
200 size_t oldlenp = sizeof (avphys_pages);
201 sysctlbyname("vm.stats.vm.v_free_count", &avphys_pages, &oldlenp, NULL, NULL);
202 return (avphys_pages / kooctet) * sysconf(_SC_PAGESIZE);
205 /* Solaris and others assumed*/
206 return (sysconf(_SC_AVPHYS_PAGES) / kooctet) * sysconf(_SC_PAGESIZE);
209 /*--------------------------------------------------------------------------*/
212 #if defined(_MSC_VER)
215 GlobalMemoryStatus (&stat);
216 return (int)(stat.dwTotalPhys / kooctet);
220 struct pst_static pst;
221 pstat_getstatic(&pst, sizeof(pst), (size_t) 1, 0);
222 return (int)((pst.physical_memory) / kooctet);
224 #elif defined(__APPLE__) || defined(__NetBSD__) || defined(__DragonFly__)
226 /* this works on *bsd and darwin */
233 len = sizeof (total);
235 sysctl(mib, 2, &total, &len, NULL, 0);
238 #elif HAVE_GETSYSINFO && defined GSI_PHYSMEM
240 /* This works on Tru64 UNIX V4/5. */
243 if (getsysinfo (GSI_PHYSMEM, (caddr_t) &physmem, sizeof (physmem),
244 NULL, NULL, NULL) == 1)
246 double kbytes = physmem;
250 return kbytes * 1024.0;
259 /* Linux ,Solaris and others */
260 return (sysconf(_SC_PHYS_PAGES) / kooctet) * sysconf(_SC_PAGESIZE);
263 /*--------------------------------------------------------------------------*/