From 1b713ab4da50099557b2f42a871ee9194d502b7c Mon Sep 17 00:00:00 2001 From: Chris Hanson Date: Sat, 17 May 2003 02:21:22 +0000 Subject: [PATCH] Generalize the mmap-based heap allocator so that it can be used on any operating system that supports the required features. (This is needed for solaris.) --- v7/src/microcode/acconfig.h | 17 +++++++- v7/src/microcode/configure.in | 6 +-- v7/src/microcode/confshared.h | 22 ++++------ v7/src/microcode/ux.c | 80 +++++++++++++++++++++-------------- v7/src/microcode/ux.h | 16 ++++++- 5 files changed, 89 insertions(+), 52 deletions(-) diff --git a/v7/src/microcode/acconfig.h b/v7/src/microcode/acconfig.h index f5697340d..4129acbb9 100644 --- a/v7/src/microcode/acconfig.h +++ b/v7/src/microcode/acconfig.h @@ -1,8 +1,8 @@ /* -*-C-*- -$Id: acconfig.h,v 11.6 2003/02/14 18:28:14 cph Exp $ +$Id: acconfig.h,v 11.7 2003/05/17 02:21:04 cph Exp $ -Copyright (c) 2000-2001 Massachusetts Institute of Technology +Copyright 2002,2001,2003 Massachusetts Institute of Technology This file is part of MIT/GNU Scheme. @@ -144,6 +144,19 @@ USA. # endif #endif +#ifdef HAVE_SYS_MMAN_H +# include +#endif + +#ifdef HAVE_MMAP +# if defined(MAP_ANON) && !defined(MAP_ANONYMOUS) +# define MAP_ANONYMOUS MAP_ANON +# endif +# ifdef MAP_ANONYMOUS +# define USE_MMAP_HEAP_MALLOC +# endif +#endif + /* Include the shared configuration header. */ #include "confshared.h" diff --git a/v7/src/microcode/configure.in b/v7/src/microcode/configure.in index ff1d4001a..fa00a8bae 100644 --- a/v7/src/microcode/configure.in +++ b/v7/src/microcode/configure.in @@ -19,7 +19,7 @@ dnl along with MIT/GNU Scheme; if not, write to the Free Software dnl Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA dnl 02111-1307, USA. -AC_REVISION([$Id: configure.in,v 11.21 2003/05/12 20:02:24 cph Exp $]) +AC_REVISION([$Id: configure.in,v 11.22 2003/05/17 02:21:09 cph Exp $]) AC_INIT(boot.c) AC_CONFIG_HEADER(config.h) @@ -117,7 +117,7 @@ AC_CHECK_HEADERS(sys/file.h sys/ioctl.h sys/mount.h sys/param.h sys/poll.h) AC_CHECK_HEADERS(sys/ptyio.h sys/socket.h sys/time.h sys/un.h sys/vfs.h) AC_CHECK_HEADERS(stdbool.h termio.h termios.h unistd.h utime.h) AC_CHECK_HEADERS(openssl/blowfish.h openssl/md5.h blowfish.h md5.h) -AC_CHECK_HEADERS(mhash.h mcrypt.h gdbm.h curses.h term.h dlfcn.h) +AC_CHECK_HEADERS(mhash.h mcrypt.h gdbm.h curses.h term.h dlfcn.h sys/mman.h) dnl Checks for typedefs AC_TYPE_MODE_T @@ -380,7 +380,7 @@ AC_FUNC_WAIT3 AC_CHECK_FUNCS(ctermid) AC_CHECK_FUNCS(dup2) AC_CHECK_FUNCS(fcntl floor fpathconf frexp ftruncate) -AC_CHECK_FUNCS(getcwd gethostbyname gethostname getlogin getpgrp) +AC_CHECK_FUNCS(getcwd gethostbyname gethostname getlogin getpagesize getpgrp) AC_CHECK_FUNCS(gettimeofday getwd grantpt) AC_CHECK_FUNCS(kill) AC_CHECK_FUNCS(lockf) diff --git a/v7/src/microcode/confshared.h b/v7/src/microcode/confshared.h index 2d07ce280..3d1d04f64 100644 --- a/v7/src/microcode/confshared.h +++ b/v7/src/microcode/confshared.h @@ -1,8 +1,8 @@ /* -*-C-*- -$Id: confshared.h,v 11.5 2003/02/14 18:28:18 cph Exp $ +$Id: confshared.h,v 11.6 2003/05/17 02:21:13 cph Exp $ -Copyright (c) 2000, 2002 Massachusetts Institute of Technology +Copyright 2000,2002,2003 Massachusetts Institute of Technology This file is part of MIT/GNU Scheme. @@ -314,18 +314,6 @@ typedef unsigned long SCHEME_OBJECT; # define MACHINE_TYPE "IA-32" #endif -#ifdef __linux__ - extern void * linux_heap_malloc (unsigned long); -# define HEAP_MALLOC linux_heap_malloc -# define HEAP_FREE(address) -#endif - -#ifdef __FreeBSD__ - extern void * freebsd_heap_malloc (unsigned long); -# define HEAP_MALLOC freebsd_heap_malloc -# define HEAP_FREE(address) -#endif - #endif /* __IA32__ */ #ifdef mips @@ -388,6 +376,12 @@ typedef unsigned long SCHEME_OBJECT; #endif #endif /* __alpha */ + +#ifdef USE_MMAP_HEAP_MALLOC + extern void * mmap_heap_malloc (unsigned long); +# define HEAP_MALLOC mmap_heap_malloc +# define HEAP_FREE(address) +#endif #ifdef __OS2__ diff --git a/v7/src/microcode/ux.c b/v7/src/microcode/ux.c index 72f74108e..44c6e56d0 100644 --- a/v7/src/microcode/ux.c +++ b/v7/src/microcode/ux.c @@ -1,8 +1,9 @@ /* -*-C-*- -$Id: ux.c,v 1.23 2003/02/14 18:28:24 cph Exp $ +$Id: ux.c,v 1.24 2003/05/17 02:21:17 cph Exp $ -Copyright (c) 1990-2002 Massachusetts Institute of Technology +Copyright 1991,1992,1993,1996,1997,2000 Massachusetts Institute of Technology +Copyright 2002,2003 Massachusetts Institute of Technology This file is part of MIT/GNU Scheme. @@ -634,46 +635,63 @@ DEFUN (OS_free, (ptr), void * ptr) UX_free (ptr); } -#ifdef __linux__ +#ifdef EMULATE_GETPAGESIZE +#ifdef HAVE_SYSCONF -#include - -void * -linux_heap_malloc (unsigned long requested_length) +unsigned long +DEFUN_VOID (UX_getpagesize) { - unsigned long ps = (getpagesize ()); - void * addr - = (mmap (((void *) -#ifndef VALGRIND_MODE - ps -#else - 0x10000 -#endif - ), - (((requested_length + (ps - 1)) / ps) * ps), - (PROT_EXEC | PROT_READ | PROT_WRITE), - (MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED), - 0, 0)); - return ((addr == ((void *) (-1))) ? 0 : addr); + static int vp = 0; + static long v; + if (!vp) + { + v = (sysconf (_SC_PAGESIZE)); + vp = 1; + } + return ((v <= 0) ? 0x1000 : v); } -#endif /* __linux__ */ +#endif /* HAVE_SYSCONF */ +#endif /* EMULATE_GETPAGESIZE */ -#ifdef __FreeBSD__ +#ifdef USE_MMAP_HEAP_MALLOC -#include +#ifdef VALGRIND_MODE +# define MMAP_BASE_ADDRESS 0x10000 +#else +# define MMAP_BASE_ADDRESS (UX_getpagesize ()) +#endif -void * -freebsd_heap_malloc (unsigned long requested_length) +#ifndef MAP_FAILED +# define MAP_FAILED ((void *) (-1)) +#endif + +static void * +mmap_heap_malloc_1 (unsigned long requested_length, int fixedp) { - unsigned long ps = (getpagesize ()); + unsigned long ps = (UX_getpagesize ()); void * addr - = (mmap (((void *) ps), + = (mmap (((void *) MMAP_BASE_ADDRESS), (((requested_length + (ps - 1)) / ps) * ps), (PROT_EXEC | PROT_READ | PROT_WRITE), - (MAP_PRIVATE | MAP_ANON | MAP_FIXED), - (-1), 0)); + (MAP_PRIVATE | MAP_ANONYMOUS | (fixedp ? MAP_FIXED : 0)), + /* Ignored by GNU/Linux, required by FreeBSD and Solaris. */ + (-1), + 0)); return ((addr == MAP_FAILED) ? 0 : addr); } -#endif /* __FreeBSD__ */ +void * +mmap_heap_malloc (unsigned long requested_length) +{ + void * addr = (mmap_heap_malloc_1 (requested_length, 1)); + if (addr == 0) + /* Can't get exact address; try for something nearby. */ + addr = (mmap_heap_malloc_1 (requested_length, 0)); + if (addr == 0) + /* mmap not returning anything useful. */ + addr = (UX_malloc (requested_length)); + return (addr); +} + +#endif /* USE_MMAP_HEAP_MALLOC */ diff --git a/v7/src/microcode/ux.h b/v7/src/microcode/ux.h index adb3126d3..e552f88f9 100644 --- a/v7/src/microcode/ux.h +++ b/v7/src/microcode/ux.h @@ -1,8 +1,9 @@ /* -*-C-*- -$Id: ux.h,v 1.76 2003/02/14 18:28:24 cph Exp $ +$Id: ux.h,v 1.77 2003/05/17 02:21:22 cph Exp $ -Copyright (c) 1988-2000 Massachusetts Institute of Technology +Copyright 1990,1991,1992,1993,1994,1995 Massachusetts Institute of Technology +Copyright 1996,1997,1998,1999,2000,2003 Massachusetts Institute of Technology This file is part of MIT/GNU Scheme. @@ -102,6 +103,10 @@ USA. # include #endif +#ifdef HAVE_SYS_MMAN_H +# include +#endif + /* GNU C library defines environ if __USE_GNU is defined. */ #ifndef __USE_GNU extern char ** environ; @@ -562,6 +567,13 @@ typedef RETSIGTYPE (*Tsignal_handler) (); extern int EXFUN (UX_kill, (pid_t pid, int sig)); # define EMULATE_KILL #endif + +#ifdef HAVE_GETPAGESIZE +# define UX_getpagesize getpagesize +#else + extern unsigned long EXFUN (UX_getpagesize, (void)); +# define EMULATE_GETPAGESIZE +#endif #ifdef HAVE_POLL # ifndef INFTIM -- 2.25.1