* src/vm/vm.c, src/vm/vm.h: Moved to .cpp.
[cacao.git] / src / mm / boehm.c
index 686d30984836fbdc0dc9ddd058d63643ed9e8581..796f2ef2654d30a3b28de4296d01e1829c017b7e 100644 (file)
@@ -1,9 +1,7 @@
 /* src/mm/boehm.c - interface for boehm gc
 
-   Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
-   C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
-   E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
-   J. Wenninger, Institut f. Computersprachen - TU Wien
+   Copyright (C) 1996-2005, 2006, 2007, 2008
+   CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
 
    This file is part of CACAO.
 
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   Contact: cacao@cacaojvm.org
-
-   Authors: Stefan Ring
-
-   Changes: Christian Thalinger
-
-   $Id: boehm.c 4921 2006-05-15 14:24:36Z twisti $
-
 */
 
 
 #include "config.h"
-#include "vm/types.h"
+
+#include <stdint.h>
 
 #if defined(ENABLE_THREADS) && defined(__LINUX__)
 #define GC_LINUX_THREADS
 #if defined(ENABLE_THREADS) && defined(__IRIX__)
 #define GC_IRIX_THREADS
 #endif
+#if defined(ENABLE_THREADS) && defined(__DARWIN__)
+#define GC_DARWIN_THREADS
+#endif
 
 #include "boehm-gc/include/gc.h"
-#include "mm/boehm.h"
+#include "mm/gc.hpp"
 #include "mm/memory.h"
 
-#if defined(ENABLE_THREADS)
-# include "threads/native/threads.h"
-#endif
-
 #include "toolbox/logging.h"
-#include "vm/options.h"
+
 #include "vm/builtin.h"
 #include "vm/exceptions.h"
 #include "vm/finalizer.h"
 #include "vm/global.h"
-#include "vm/loader.h"
 #include "vm/stringlocal.h"
-#include "vm/jit/asmpart.h"
+#include "vm/vm.hpp"
+
+#include "vmcore/loader.h"
+#include "vmcore/options.h"
+#include "vmcore/rt-timing.h"
 
 
 /* global variables ***********************************************************/
@@ -78,32 +71,20 @@ static void gc_ignore_warnings(char *msg, GC_word arg);
 
 *******************************************************************************/
 
-void gc_init(u4 heapmaxsize, u4 heapstartsize)
+void gc_init(size_t heapmaxsize, size_t heapstartsize)
 {
        size_t heapcurrentsize;
 
-       GC_INIT();
-
-       /* set the maximal heap size */
-
-       GC_set_max_heap_size(heapmaxsize);
-
-       /* set the initial heap size */
-
-       heapcurrentsize = GC_get_heap_size();
-
-       if (heapstartsize > heapcurrentsize) {
-               GC_expand_hp(heapstartsize - heapcurrentsize);
-       }
-
-       /* define OOM function */
-
-       GC_oom_fn = gc_out_of_memory;
+       TRACESUBSYSTEMINITIALIZATION("gc_init");
 
        /* just to be sure (should be set to 1 by JAVA_FINALIZATION macro) */
 
        GC_java_finalization = 1;
 
+       /* Ignore pointers that do not point to the start of an object. */
+
+       GC_all_interior_pointers = 0;
+
        /* suppress warnings */
 
        GC_set_warn_proc(gc_ignore_warnings);
@@ -112,59 +93,82 @@ void gc_init(u4 heapmaxsize, u4 heapstartsize)
 
        GC_finalize_on_demand = 1;
        GC_finalizer_notifier = finalizer_notify;
-}
 
+       /* define OOM function */
 
-static void gc_ignore_warnings(char *msg, GC_word arg)
-{
-}
+       GC_oom_fn = gc_out_of_memory;
 
-static void *stackcall_malloc_atomic(void *p, u4 bytelength)
-{
-       return GC_MALLOC_ATOMIC(bytelength);
+       GC_INIT();
+
+       /* set the maximal heap size */
+
+       GC_set_max_heap_size(heapmaxsize);
+
+       /* set the initial heap size */
+
+       heapcurrentsize = GC_get_heap_size();
+
+       if (heapstartsize > heapcurrentsize)
+               GC_expand_hp(heapstartsize - heapcurrentsize);
 }
 
 
-static void *stackcall_malloc_uncollectable(void *p, u4 bytelength)
+static void gc_ignore_warnings(char *msg, GC_word arg)
 {
-       return GC_MALLOC_UNCOLLECTABLE(bytelength);
 }
 
 
-void *heap_alloc_uncollectable(u4 bytelength)
+void *heap_alloc_uncollectable(size_t size)
 {
-       void *result;
+       void *p;
 
-       result = stackcall_malloc_uncollectable(NULL, bytelength);
+       p = GC_MALLOC_UNCOLLECTABLE(size);
 
        /* clear allocated memory region */
 
-       MSET(result, 0, u1, bytelength);
+       MSET(p, 0, uint8_t, size);
 
-       return result;
+       return p;
 }
 
 
-void *heap_allocate(u4 bytelength, bool references, methodinfo *finalizer)
+/* heap_alloc ******************************************************************
+
+   Allocates memory on the Java heap.
+
+*******************************************************************************/
+
+void *heap_alloc(size_t size, int references, methodinfo *finalizer, bool collect)
 {
-       void *result;
+       void *p;
+#if defined(ENABLE_RT_TIMING)
+       struct timespec time_start, time_end;
+#endif
 
-       if (references)
-               result = GC_MALLOC(bytelength);
+       RT_TIMING_GET_TIME(time_start);
+
+       /* We can't use a bool here for references, as it's passed as a
+          bitmask in builtin_new.  Thus we check for != 0. */
+
+       if (references != 0)
+               p = GC_MALLOC(size);
        else
-               result = stackcall_malloc_atomic(NULL, bytelength);
+               p = GC_MALLOC_ATOMIC(size);
 
-       if (!result)
+       if (p == NULL)
                return NULL;
 
-       if (finalizer)
-               GC_REGISTER_FINALIZER(result, finalizer_run, 0, 0, 0);
+       if (finalizer != NULL)
+               GC_REGISTER_FINALIZER_NO_ORDER(p, finalizer_run, 0, 0, 0);
 
        /* clear allocated memory region */
 
-       MSET(result, 0, u1, bytelength);
+       MSET(p, 0, uint8_t, size);
 
-       return (u1 *) result;
+       RT_TIMING_GET_TIME(time_end);
+       RT_TIMING_TIME_DIFF(time_start, time_end, RT_TIMING_GC_ALLOC);
+
+       return p;
 }
 
 
@@ -183,19 +187,31 @@ void gc_call(void)
 }
 
 
-s8 gc_get_heap_size(void)
+int64_t gc_get_heap_size(void)
 {
        return GC_get_heap_size();
 }
 
 
-s8 gc_get_free_bytes(void)
+int64_t gc_get_free_bytes(void)
 {
        return GC_get_free_bytes();
 }
 
 
-s8 gc_get_max_heap_size(void)
+/* gc_get_total_bytes **********************************************************
+
+   Returns the number of total bytes currently used on the Java heap.
+
+*******************************************************************************/
+
+int64_t gc_get_total_bytes(void)
+{
+       return GC_get_total_bytes();
+}
+
+
+int64_t gc_get_max_heap_size(void)
 {
        return GC_get_max_heap_size();
 }
@@ -225,7 +241,7 @@ void *gc_out_of_memory(size_t bytes_requested)
 
        if (in_gc_out_of_memory) {
                /* this is all we can do... */
-               exceptions_throw_outofmemory_exit();
+               vm_abort("gc_out_of_memory: out of memory");
        }
 
        in_gc_out_of_memory = true;
@@ -236,7 +252,7 @@ void *gc_out_of_memory(size_t bytes_requested)
 
        /* now instantiate the exception */
 
-       *exceptionptr = new_exception(string_java_lang_OutOfMemoryError);
+       exceptions_throw_outofmemoryerror();
 
        in_gc_out_of_memory = false;