From 0a5fb6026e1f08d44cd3442f5fc7e74c5fb971e1 Mon Sep 17 00:00:00 2001 From: Jean Privat Date: Wed, 10 Sep 2008 18:10:16 -0400 Subject: [PATCH] Automatically enable Bohem's GC if available. On Debian based system, just run # aptitude install libgc-dev Remark: it is only a trade off until we have our own GC. --- bin/gccx | 29 ++++++++++++++++++++++++++++- doc/advanced_options | 7 +++++++ lib/nit_common.h | 6 ++++++ lib/nit_main.c | 17 +++++++++++++++++ 4 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 doc/advanced_options diff --git a/bin/gccx b/bin/gccx index 2a31b1e..e89833f 100755 --- a/bin/gccx +++ b/bin/gccx @@ -23,6 +23,7 @@ CC="gcc --ansi --pedantic" # Default compiler call ext="_savo _sav" # Default flavor to reuse out="a.out" # Default output binary filename dir="" # Default tmp dir +nolibgc="false" # Try to use libgc ? CKSUM="cksum" # Tool that perfors checksum. cksum seems to be very portable usage() @@ -35,10 +36,31 @@ Usage: $e [options] modulename [options for module execution] -I path Add a include directory -o name Call name the executable -d Create temporary files in a specific directory +-nolibgc Do not include libgc -h This help END } +test_libgc() { +cat > .tmp.c < +#include +int main(void) { + void *r = GC_malloc(1); + return r == NULL; + } +END +gcc .tmp.c -lgc -o .tmp.bin 2> /dev/null +res=$? +rm .tmp.c +if [ $res = 0 ]; then + ./.tmp.bin + res=$? + rm .tmp.bin +fi +return $res +} + stop=false while [ $stop = false ]; do case $1 in @@ -47,16 +69,21 @@ while [ $stop = false ]; do -I) OPTS="$OPTS -I $2"; shift; shift;; -o) out="$2"; shift; shift;; -d) dir="$2/"; shift; shift;; + --nolibgc) nolibgc=true; shift;; -x) OPTS="$OPTS $2"; shift; shift;; -h|"") usage; exit;; *) stop=true esac done +if [ $nolibgc != true ] && test_libgc; then + OPTS="$OPTS -DWITH_LIBGC -lgc" +fi + for i in "$@"; do j=`basename "$i" .c` found="false" - cksum=`gcc -E $i 2> /dev/null | $CKSUM` + cksum=`gcc -E $OPTS $i 2> /dev/null | $CKSUM` for e in $ext; do o="$dir$j.$e.o" cksumfile="$dir$j.$e.cksum" diff --git a/doc/advanced_options b/doc/advanced_options new file mode 100644 index 0000000..ae49003 --- /dev/null +++ b/doc/advanced_options @@ -0,0 +1,7 @@ +* GC ************************************************************************** + +If Bohem's GC is available, it will be automatically used within compiled NIT +programs. + +On GC enabled NIT program, GC can be dynamically disabled if the NIT_NOGC envvar is set. + diff --git a/lib/nit_common.h b/lib/nit_common.h index e0b22bf..02e3dff 100644 --- a/lib/nit_common.h +++ b/lib/nit_common.h @@ -5,6 +5,12 @@ #include #include +#ifdef WITH_LIBGC +#include +#define malloc(x) GC_MALLOC((x)) +#define calloc(x,y) GC_MALLOC((x)*(y)) +#endif + /* *** Types *** */ typedef int (*fun_t)(int); /* generic function pointer */ typedef unsigned int cid_t; /* class identifier */ diff --git a/lib/nit_main.c b/lib/nit_main.c index 047cd14..1b181c6 100644 --- a/lib/nit_main.c +++ b/lib/nit_main.c @@ -2,6 +2,19 @@ #include #include +#ifdef WITH_LIBGC +#define GC_DEBUG +#include +int nolibgc = 0; +void *simple_alloc(size_t s0); +void *alloc(size_t s0) +{ + if (!nolibgc) { return GC_MALLOC(s0); } + else return simple_alloc(s0); +} +#define alloc simple_alloc +#endif + void * alloc(size_t s0) { static char * alloc_pos = NULL; @@ -27,6 +40,10 @@ void exithandler(int s) { nit_exit(1); } void prepare_signals(void) { +#if WITH_LIBGC + nolibgc = (getenv("NIT_NOLIBGC") != NULL); + if (!nolibgc) GC_INIT(); +#endif signal(SIGINT,exithandler); signal(SIGABRT,exithandler); signal(SIGSEGV,exithandler); -- 1.7.9.5