Skip to content

Commit

Permalink
Make config/gen/platform/generic/sysmem.c more generic.
Browse files Browse the repository at this point in the history
Include the two most likely methods (sysconf and sysctl) in the
generic file, based on various #ifdef probes.  This way, new systems
are more likely to work without first needing their own sysmem.c file.

If neither sysconf() nor sysctl() seems to work, assume a default of
512 MB.  One could certainly argue for putting an error message here
instead.

Signed-off-by: Jonathan "Duke" Leto <jonathan@leto.net>
  • Loading branch information
Andy Dougherty authored and leto committed Dec 4, 2010
1 parent d2c3de0 commit 72908f9
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 144 deletions.
2 changes: 0 additions & 2 deletions MANIFEST
Expand Up @@ -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 []
Expand All @@ -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 []
Expand Down
2 changes: 1 addition & 1 deletion config/auto/headers.pm
Expand Up @@ -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" ) {
Expand Down
69 changes: 0 additions & 69 deletions config/gen/platform/freebsd/sysmem.c

This file was deleted.

56 changes: 53 additions & 3 deletions config/gen/platform/generic/sysmem.c
Expand Up @@ -20,11 +20,12 @@ Get system memory information.
*/

#include <stdio.h>
#include <unistd.h>
#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 <sys/sysctl.h>
#endif

/*
Expand All @@ -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;
}

/*
Expand Down
69 changes: 0 additions & 69 deletions config/gen/platform/netbsd/sysmem.c

This file was deleted.

0 comments on commit 72908f9

Please sign in to comment.