diff --git a/MANIFEST b/MANIFEST index eec496a5d5..bb111e865a 100644 --- a/MANIFEST +++ b/MANIFEST @@ -240,7 +240,6 @@ config/gen/platform/cygwin/math.c [] config/gen/platform/darwin/begin.c [] config/gen/platform/darwin/hires_timer.c [] config/gen/platform/darwin/sysmem.c [] -config/gen/platform/freebsd/sysmem.c [] config/gen/platform/generic/dl.c [] config/gen/platform/generic/dl.h [] config/gen/platform/generic/env.c [] @@ -264,7 +263,6 @@ config/gen/platform/ia64/asm.s [] config/gen/platform/netbsd/math.c [] config/gen/platform/netbsd/misc.c [] config/gen/platform/netbsd/misc.h [] -config/gen/platform/netbsd/sysmem.c [] config/gen/platform/openbsd/math.c [] config/gen/platform/openbsd/memexec.c [] config/gen/platform/openbsd/misc.h [] diff --git a/config/auto/headers.pm b/config/auto/headers.pm index f52b3be3ca..8f2338dbe0 100644 --- a/config/auto/headers.pm +++ b/config/auto/headers.pm @@ -97,7 +97,7 @@ sub _list_extra_headers { # the header. my @extra_headers = qw(malloc.h fcntl.h setjmp.h pthread.h signal.h sys/types.h sys/socket.h netinet/in.h arpa/inet.h - sys/stat.h sysexit.h limits.h); + sys/stat.h sysexit.h limits.h sys/sysctl.h); # more extra_headers needed on mingw/msys; *BSD fails if they are present if ( $conf->data->get('OSNAME_provisional') eq "msys" ) { diff --git a/config/gen/platform/freebsd/sysmem.c b/config/gen/platform/freebsd/sysmem.c deleted file mode 100644 index 4d21d74cdc..0000000000 --- a/config/gen/platform/freebsd/sysmem.c +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright (C) 2010, Parrot Foundation. - */ - -/* - -=head1 NAME - -config/gen/platform/freebsd/sysmem.c - -=head1 DESCRIPTION - -Get system memory information. - -=head2 Functions - -=over 4 - -=cut - -*/ -#include -#include - -/* - -=item C - -Get information about available physical memory. - -=cut - -*/ - -size_t -Parrot_sysmem_amount(PARROT_INTERP) -{ - int err = 0; - size_t memsize = 0; - char *err_msg; - unsigned long length = sizeof (memsize); - - int selection[2] = { CTL_HW, HW_PHYSMEM }; - - err = sysctl(selection, 2, &memsize, &length, NULL, 0); - - if (err) { - err_msg = strerror(err); - Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_EXTERNAL_ERROR, - "sysctl failed: %s", err_msg); - } - - return memsize; -} - -/* - -=back - -=cut - -*/ - -/* - * Local variables: - * c-file-style: "parrot" - * End: - * vim: expandtab shiftwidth=4 cinoptions='\:2=2' : - */ diff --git a/config/gen/platform/generic/sysmem.c b/config/gen/platform/generic/sysmem.c index d2f2c82aac..95695b138a 100644 --- a/config/gen/platform/generic/sysmem.c +++ b/config/gen/platform/generic/sysmem.c @@ -20,11 +20,12 @@ Get system memory information. */ +#include #include #include "parrot/sysmem.h" -#ifdef _SC_PAGESIZE /* some SVR4 systems omit an underscore */ -# define _SC_PAGE_SIZE _SC_PAGESIZE +#if defined(PARROT_HAS_HEADER_SYSSYSCTL) +# include #endif /* @@ -37,10 +38,59 @@ Get information about available physical memory. */ +/* + * The POSIX name is _SC_PAGESIZE, but apparently some systems + * require _SC_PAGE_SIZE instead. (Specific citations would + * be useful.) + */ +#if !defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE) +# define _SC_PAGESIZE _SC_PAGE_SIZE +#endif + size_t Parrot_sysmem_amount(PARROT_INTERP) { - return sysconf(_SC_AVPHYS_PAGES) * sysconf(_SC_PAGE_SIZE); + size_t memsize = 0; + +#if defined(_SC_PAGESIZE) && defined(_SC_AVPHYS_PAGES) + /* Method 1: sysconf(). + * Works on Linux and Solaris, and probably other SVR4-related systems. + * This should really check for #ifdef HAS_SYSCONF, but Configure + * doesn't probe for that yet. + */ + memsize = sysconf(_SC_AVPHYS_PAGES) * sysconf(_SC_PAGESIZE); + +#elif defined(PARROT_HAS_HEADER_SYSSYSCTL) && defined(CTL_HW) && defined(HW_PHYSMEM) + + /* Method 2: sysctl(). Works on BSD-derived systems. Darwin is + * slightly different, and has its own implementation. + * Configure really should test for HAS_SYSCTL, but, for now, test for + * the header sys/sysctl.h and the appropriate constants. + */ + + int err = 0; + char *err_msg; + unsigned long length = sizeof (memsize); + + int selection[2] = { CTL_HW, HW_PHYSMEM }; + + err = sysctl(selection, 2, &memsize, &length, NULL, 0); + + if (err) { + err_msg = strerror(err); + Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_EXTERNAL_ERROR, + "sysctl failed: %s", err_msg); + } + +#else + /* Method 3: Random guess. Simply guess 512 MB. This way, parrot + * will at least build. Arguably, one could also just put an error + * here and instruct the user to fix it manually. + */ + memsize = 512 * 1024 * 1024; +#endif + + return memsize; } /* diff --git a/config/gen/platform/netbsd/sysmem.c b/config/gen/platform/netbsd/sysmem.c deleted file mode 100644 index 37594d764e..0000000000 --- a/config/gen/platform/netbsd/sysmem.c +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright (C) 2010, Parrot Foundation. - */ - -/* - -=head1 NAME - -config/gen/platform/netbsd/sysmem.c - -=head1 DESCRIPTION - -Get system memory information. - -=head2 Functions - -=over 4 - -=cut - -*/ -#include -#include - -/* - -=item C - -Get information about available physical memory. - -=cut - -*/ - -size_t -Parrot_sysmem_amount(PARROT_INTERP) -{ - int err = 0; - size_t memsize = 0; - char *err_msg; - unsigned long length = sizeof (memsize); - - int selection[2] = { CTL_HW, HW_PHYSMEM }; - - err = sysctl(selection, 2, &memsize, &length, NULL, 0); - - if (err) { - err_msg = strerror(err); - Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_EXTERNAL_ERROR, - "sysctl failed: %s", err_msg); - } - - return memsize; -} - -/* - -=back - -=cut - -*/ - -/* - * Local variables: - * c-file-style: "parrot" - * End: - * vim: expandtab shiftwidth=4 cinoptions='\:2=2' : - */