Merged branch subtype-trunk into default.
[cacao.git] / src / mm / cacao-gc / heap.c
index 548860489bef1f4ae7eda40db0c45d338e19581d..66a9f8c5e5a269467987a9a10ec9ea0943fbf91c 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.
 
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   $Id$
-
 */
 
 
 #include "config.h"
 #include "vm/types.h"
 
-#include "threads/lock-common.h"
+#include "threads/lock.hpp"
 
 #include "gc.h"
 #include "final.h"
 #include "heap.h"
 #include "mark.h"
 #include "region.h"
-#include "mm/memory.h"
+#include "mm/memory.hpp"
 #include "native/include/java_lang_String.h"
-#include "toolbox/logging.h"
+#include "native/llni.h"
+#include "toolbox/logging.hpp"
+
 #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 ***********************************************************/
@@ -54,14 +53,7 @@ regioninfo_t *heap_region_sys;
 regioninfo_t *heap_region_main;
 
 
-/* Helper Macros **************************************************************/
-
-#define GC_ALIGN_SIZE SIZEOF_VOID_P
-#define GC_ALIGN(length,size) ((((length) + (size) - 1) / (size)) * (size))
-
-
-
-void heap_init_objectheader(java_objectheader *o, u4 bytelength)
+void heap_init_objectheader(java_object_t *o, u4 bytelength)
 {
        u4 wordcount;
 
@@ -91,12 +83,90 @@ void heap_init_objectheader(java_objectheader *o, u4 bytelength)
 }
 
 
-s4 heap_increase_size() {
-       void *p;
-       s4    increasesize;
-       s4    newsize;
+void heap_update_references(rootset_t *rs, regioninfo_t *region, u4 offset)
+{
+       java_object_t  *o;
+       java_object_t  *ref;
+       java_object_t **refptr;
+       u1* start;
+       u1* end;
+       int i;
+
+       GC_LOG( dolog("GC: Updating all references (offset=%x) ...", offset); );
+
+       start = region->base - offset;
+       end = region->ptr - offset;
+       GC_LOG( printf("Region previously was [ %p ; %p]\n", start, end); );
+
+       GC_LOG2( printf("updating in root-sets ..."); );
+
+       /* walk through all rootsets */
+       while (rs) {
+
+               /* walk through the references of this rootset */
+               for (i = 0; i < rs->refcount; i++) {
+
+                       /* load the reference */
+                       refptr = rs->refs[i].ref;
+                       ref = *( refptr );
+
+                       GC_LOG2( printf("\troot pointer to %p\n", (void *) ref); );
+
+                       /* update the references */
+                       if (POINTS_INTO(ref, start, end))
+                               *refptr = ((u1 *) ref) + offset;
+
+               }
+
+               /* skip to next rootset in chain */
+               rs = rs->next;
+
+       }
+
+
+       o = region->base;
+       while (o < region->ptr) {
+
+               GC_LOG2( printf("updating in %p ...\n", (void *) o); );
+
+               if (IS_ARRAY(o)) {
+
+                       /* walk through the references of an Array */
+                       FOREACH_ARRAY_REF(o,ref,refptr,
+
+                               GC_LOG2( printf("\tarray-entry %p -> %p\n", (void *) ref, ((u1 *) ref) + offset); );
 
-       /* TODO: locking for threads!!! */
+                               if (POINTS_INTO(ref, start, end))
+                                       *refptr = ((u1 *) ref) + offset;
+
+                       );
+
+               } else {
+
+                       /* walk through the references of an Object */
+                       FOREACH_OBJECT_REF(o,ref,refptr,
+
+                               GC_LOG2( printf("\tobject-field %p -> %p\n", (void *) ref, ((u1 *) ref) + offset); );
+
+                               if (POINTS_INTO(ref, start, end))
+                                       *refptr = ((u1 *) ref) + offset;
+
+                       );
+
+               }
+
+               /* skip to next object */
+               o = ((u1 *) o) + get_object_size(o);
+
+       }
+
+}
+
+
+void heap_increase_size(rootset_t *rs)
+{
+       s4 newsize;
+       s4 resize_offset;
 
        /* only a quick sanity check */
        GC_ASSERT(heap_current_size <= heap_maximal_size);
@@ -105,25 +175,21 @@ s4 heap_increase_size() {
        if (heap_current_size == heap_maximal_size)
                vm_abort("heap_increase_size: reached maximal heap size: out of memory");
 
-       /* TODO: find out how much to increase the heap??? */
-       increasesize = heap_maximal_size - heap_current_size;
-       GC_LOG( dolog("GC: Increasing Heap Size by %d", increasesize); );
+       /* find out how much to increase the heap??? */
+       newsize = 2 * heap_current_size; /* XXX TODO: better heuristic here */
+       dolog("GC: Increasing Heap Size to %d bytes", newsize); /* XXX remove me */
+       GC_LOG( dolog("GC: Increasing Heap Size to %d bytes", newsize); );
 
-       /* allocate new heap from the system */
-       newsize = heap_current_size + increasesize;
-       /*p = malloc(newsize);*/
-
-       /* check if the newly allocated heap exists */
-       if (p == NULL)
-               vm_abort("heap_increase_size: malloc failed: out of memory");
+       /* resize the main heap region */
+       resize_offset = region_resize(heap_region_main, newsize);
 
-       /* TODO: copy the old content to the new heap */
-       /* TODO: find a complete rootset and update it to the new position */
-       /* TODO: free the old heap */
+       /* update all references if necesarry */
+       if (resize_offset != 0)
+               heap_update_references(rs, heap_region_main, resize_offset);
+       else
+               dolog("GC WARNING: References are not updated after heap resizing!");
 
        /* set the new values */
-       /*heap_ptr = p + (heap_ptr - heap_base);
-       heap_base = p;*/
        heap_current_size = newsize;
 
        GC_LOG( dolog("GC: Increasing Heap Size was successful");
@@ -132,11 +198,10 @@ s4 heap_increase_size() {
        /* only a quick sanity check */
        GC_ASSERT(heap_current_size <= heap_maximal_size);
 
-       return increasesize;
 }
 
 
-s4 heap_get_hashcode(java_objectheader *o)
+s4 heap_get_hashcode(java_object_t *o)
 {
        s4 hashcode;
 
@@ -164,13 +229,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);
@@ -178,9 +249,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
 
@@ -190,13 +261,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;
 
@@ -222,7 +301,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
@@ -247,16 +327,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)
@@ -296,7 +378,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),
@@ -309,10 +391,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) {
@@ -355,7 +437,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 */
@@ -387,8 +469,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);
@@ -396,7 +478,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)) {
@@ -418,11 +500,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);
@@ -444,7 +526,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;