Automatically enable Bohem's GC if available.
authorJean Privat <jean@pryen.org>
Wed, 10 Sep 2008 22:10:16 +0000 (18:10 -0400)
committerJean Privat <jean@pryen.org>
Wed, 10 Sep 2008 22:10:16 +0000 (18:10 -0400)
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
doc/advanced_options [new file with mode: 0644]
lib/nit_common.h
lib/nit_main.c

index 2a31b1e..e89833f 100755 (executable)
--- 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 <<END
+#include <stdlib.h>
+#include <gc/gc.h>
+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 (file)
index 0000000..ae49003
--- /dev/null
@@ -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.
+
index e0b22bf..02e3dff 100644 (file)
@@ -5,6 +5,12 @@
 #include <stdio.h>
 #include <string.h>
 
+#ifdef WITH_LIBGC
+#include <gc/gc.h>
+#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 */
index 047cd14..1b181c6 100644 (file)
@@ -2,6 +2,19 @@
 #include <signal.h>
 #include <stdarg.h>
 
+#ifdef WITH_LIBGC
+#define GC_DEBUG
+#include <gc/gc.h>
+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);