* src/vm/vm.c, src/vm/vm.h: Moved to .cpp.
[cacao.git] / src / mm / cacao-gc / heap.c
index 3ee5e26f0f8104672cb85a44f6386c5d2b0e4f6e..ae5adfd36fcbdc86c50e3d1a3de53a12c533d795 100644 (file)
@@ -22,8 +22,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 "vm/vm.hpp"
+#include "vmcore/options.h"
 #include "vmcore/rt-timing.h"
 
 
@@ -54,7 +54,7 @@ regioninfo_t *heap_region_sys;
 regioninfo_t *heap_region_main;
 
 
-void heap_init_objectheader(java_objectheader *o, u4 bytelength)
+void heap_init_objectheader(java_object_t *o, u4 bytelength)
 {
        u4 wordcount;
 
@@ -86,9 +86,9 @@ void heap_init_objectheader(java_objectheader *o, u4 bytelength)
 
 void heap_update_references(rootset_t *rs, regioninfo_t *region, u4 offset)
 {
-       java_objectheader  *o;
-       java_objectheader  *ref;
-       java_objectheader **refptr;
+       java_object_t  *o;
+       java_object_t  *ref;
+       java_object_t **refptr;
        u1* start;
        u1* end;
        int i;
@@ -202,7 +202,7 @@ void heap_increase_size(rootset_t *rs)
 }
 
 
-s4 heap_get_hashcode(java_objectheader *o)
+s4 heap_get_hashcode(java_object_t *o)
 {
        s4 hashcode;
 
@@ -230,13 +230,19 @@ s4 heap_get_hashcode(java_objectheader *o)
 }
 
 
-static java_objectheader *heap_alloc_intern(u4 bytelength, regioninfo_t *region, bool collect)
+static java_object_t *heap_alloc_intern(u4 bytelength, regioninfo_t *region, bool collect)
 {
-       java_objectheader *p;
+       java_object_t *p;
 
        /* only a quick sanity check */
        GC_ASSERT(region);
-       GC_ASSERT(bytelength >= sizeof(java_objectheader));
+       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);
@@ -244,9 +250,9 @@ static java_objectheader *heap_alloc_intern(u4 bytelength, regioninfo_t *region,
        /* 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,13 +262,21 @@ static java_objectheader *heap_alloc_intern(u4 bytelength, regioninfo_t *region,
 
                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;
        }
 
        /* allocate the object in this region */
-       p = (java_objectheader *) region->ptr;
+       p = (java_object_t *) region->ptr;
        region->ptr += bytelength;
        region->free -= bytelength;
 
@@ -288,7 +302,8 @@ static java_objectheader *heap_alloc_intern(u4 bytelength, regioninfo_t *region,
 
 void *heap_alloc(u4 size, u4 references, methodinfo *finalizer, bool collect)
 {
-       java_objectheader *p;
+       java_object_t *p;
+       java_handle_t *h;
 #if defined(ENABLE_RT_TIMING)
        struct timespec time_start, time_end;
 #endif
@@ -313,16 +328,18 @@ 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;
 }
 
 
 void *heap_alloc_uncollectable(u4 size)
 {
-       java_objectheader *p;
+       java_object_t *p;
 
        /* loader.c does this a lot for classes with fieldscount equal zero */
        if (size == 0)
@@ -362,7 +379,7 @@ void heap_println_usage()
 
 
 #if !defined(NDEBUG)
-void heap_print_object_flags(java_objectheader *o)
+void heap_print_object_flags(java_object_t *o)
 {
        printf("0x%02x [%s%s%s%s]",
                GC_GET_SIZE(o),
@@ -375,10 +392,10 @@ void heap_print_object_flags(java_objectheader *o)
 
 
 #if !defined(NDEBUG)
-void heap_print_object(java_objectheader *o)
+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 +438,7 @@ void heap_print_object(java_objectheader *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 */
@@ -453,8 +470,8 @@ void heap_print_object(java_objectheader *o)
 #if !defined(NDEBUG)
 void heap_dump_region(regioninfo_t *region, bool marked_only)
 {
-       java_objectheader *o;
-       u4                 o_size;
+       java_object_t *o;
+       u4             o_size;
 
        /* some basic sanity checks */
        GC_ASSERT(region->base <= region->ptr);
@@ -462,7 +479,7 @@ void heap_dump_region(regioninfo_t *region, bool marked_only)
        printf("Heap-Dump:\n");
 
        /* walk the region in a linear style */
-       o = (java_objectheader *) region->base;
+       o = (java_object_t *) region->base;
        while (o < region->ptr) {
 
                if (!marked_only || GC_IS_MARKED(o)) {
@@ -484,11 +501,11 @@ void heap_dump_region(regioninfo_t *region, bool marked_only)
 #endif
 
 
-s4 get_object_size(java_objectheader *o)
+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 +527,7 @@ s4 get_object_size(java_objectheader *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;