Merged trunk and subtype.
[cacao.git] / src / mm / cacao-gc / heap.c
index 2b73ccc5c1f9135b224fba07f53a6e5bd8488352..f3630c7a3b1d8963db94b7703bcbe89700905fbc 100644 (file)
@@ -1,9 +1,7 @@
 /* mm/cacao-gc/heap.c - GC module for heap management
 
-   Copyright (C) 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) 2006, 2008
+   CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
 
    This file is part of CACAO.
 
@@ -22,8 +20,6 @@
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   $Id$
-
 */
 
 
 #include "region.h"
 #include "mm/memory.h"
 #include "native/include/java_lang_String.h"
+#include "native/llni.h"
 #include "toolbox/logging.h"
+
 #include "vm/global.h"
-#include "vm/stringlocal.h"
-#include "vm/vm.h"
-#include "vmcore/rt-timing.h"
+#include "vm/options.h"
+#include "vm/rt-timing.h"
+#include "vm/string.hpp"
+#include "vm/vm.hpp"
 
 
 /* Global Variables ***********************************************************/
@@ -238,15 +237,21 @@ static java_object_t *heap_alloc_intern(u4 bytelength, regioninfo_t *region, boo
        GC_ASSERT(region);
        GC_ASSERT(bytelength >= sizeof(java_object_t));
 
+#if !defined(NDEBUG) && defined(ENABLE_THREADS)
+       /* check the current VM state for sanity */
+       GC_ASSERT(!THREADOBJECT->gc_critical);
+       GC_ASSERT(THREADOBJECT->flags & THREAD_FLAG_IN_NATIVE);
+#endif
+
        /* align objects in memory */
        bytelength = GC_ALIGN(bytelength, GC_ALIGN_SIZE);
 
        /* lock the region */
        LOCK_MONITOR_ENTER(region);
 
-#if 0
+#if !defined(NDEBUG)
        /* heavy stress test */
-       if (collect)
+       if (opt_GCStress && collect)
                gc_collect(0);
 #endif
 
@@ -256,7 +261,15 @@ static java_object_t *heap_alloc_intern(u4 bytelength, regioninfo_t *region, boo
 
                if (collect) {
                        gc_collect(0);
+#if 0
                        GC_ASSERT(region->free >= bytelength);
+#else
+                       if (region->free < bytelength) {
+                               dolog("GC: OOM OOM OOM OOM OOM OOM OOM OOM OOM OOM");
+                               exceptions_throw_outofmemoryerror();
+                               return NULL;
+                       }
+#endif
                } else
                        return NULL;
        }
@@ -289,6 +302,7 @@ static java_object_t *heap_alloc_intern(u4 bytelength, regioninfo_t *region, boo
 void *heap_alloc(u4 size, u4 references, methodinfo *finalizer, bool collect)
 {
        java_object_t *p;
+       java_handle_t *h;
 #if defined(ENABLE_RT_TIMING)
        struct timespec time_start, time_end;
 #endif
@@ -313,10 +327,12 @@ void *heap_alloc(u4 size, u4 references, methodinfo *finalizer, bool collect)
                final_register(p, finalizer);
        }
 
+       h = LLNI_WRAP(p);
+
        RT_TIMING_GET_TIME(time_end);
        RT_TIMING_TIME_DIFF(time_start, time_end, RT_TIMING_GC_ALLOC);
 
-       return p;
+       return h;
 }
 
 
@@ -377,8 +393,8 @@ void heap_print_object_flags(java_object_t *o)
 #if !defined(NDEBUG)
 void heap_print_object(java_object_t *o)
 {
-       java_arrayheader  *a;
-       classinfo         *c;
+       java_array_t *a;
+       classinfo    *c;
 
        /* check for null pointers */
        if (o == NULL) {
@@ -421,7 +437,7 @@ void heap_print_object(java_object_t *o)
        } else if (/*IS_ARRAY*/ o->vftbl->arraydesc != NULL) {
 
                /* get the array information */
-               a = (java_arrayheader *) o;
+               a = (java_array_t *) o;
                c = o->vftbl->class;
 
                /* print the array information */
@@ -486,9 +502,9 @@ void heap_dump_region(regioninfo_t *region, bool marked_only)
 
 s4 get_object_size(java_object_t *o)
 {
-       java_arrayheader *a;
-       classinfo        *c;
-       s4                o_size;
+       java_array_t *a;
+       classinfo    *c;
+       s4            o_size;
 
        /* we can assume someone initialized the header */
        GC_ASSERT(o->hdrflags != 0);
@@ -510,7 +526,7 @@ s4 get_object_size(java_object_t *o)
 
                } else if (/*IS_ARRAY*/ o->vftbl->arraydesc != NULL) {
                        /* compute size of this array */
-                       a = (java_arrayheader *) o;
+                       a = (java_array_t *) o;
                        c = o->vftbl->class;
                        o_size = c->vftbl->arraydesc->dataoffset +
                                        a->size * c->vftbl->arraydesc->componentsize;