1 /* mm/cacao-gc/heap.c - GC module for heap management
3 Copyright (C) 2006 R. Grafl, A. Krall, C. Kruegel,
4 C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5 E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6 J. Wenninger, Institut f. Computersprachen - TU Wien
8 This file is part of CACAO.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2, or (at
13 your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 Contact: cacao@cacaojvm.org
27 Authors: Michael Starzinger
37 #if defined(ENABLE_THREADS)
38 # include "threads/native/lock.h"
40 # include "threads/none/lock.h"
47 #include "mm/memory.h"
48 #include "src/native/include/java_lang_String.h" /* TODO: fix me! */
49 #include "toolbox/logging.h"
50 #include "vm/global.h"
51 #include "vmcore/rt-timing.h"
54 /* Global Variables ***********************************************************/
56 s4 heap_current_size; /* current size of the heap */
57 s4 heap_maximal_size; /* maximal size of the heap */
58 regioninfo_t *heap_region_sys;
59 regioninfo_t *heap_region_main;
62 /* Helper Macros **************************************************************/
64 #define GC_ALIGN_SIZE SIZEOF_VOID_P
65 #define GC_ALIGN(length,size) ((((length) + (size) - 1) / (size)) * (size))
69 void heap_init_objectheader(java_objectheader *o, u4 bytelength)
73 /* initialize the header flags */
79 /* calculate the wordcount as stored in the header */
80 /* TODO: improve this to save wordcount and without header bytes */
81 if ((bytelength & 0x03) == 0) {
82 GC_ASSERT((bytelength & 0x03) == 0);
83 wordcount = (bytelength >> 2);
84 GC_ASSERT(wordcount != 0);
86 wordcount = GC_SIZE_DUMMY;
89 /* set the wordcount in the header */
90 if (wordcount >= GC_SIZE_DUMMY) {
91 GC_SET_SIZE(o, GC_SIZE_DUMMY);
93 GC_SET_SIZE(o, wordcount);
99 s4 heap_increase_size() {
104 /* TODO: locking for threads!!! */
106 /* only a quick sanity check */
107 GC_ASSERT(heap_current_size <= heap_maximal_size);
109 /* check if we are allowed to enlarge the heap */
110 if (heap_current_size == heap_maximal_size)
111 exceptions_throw_outofmemory_exit();
113 /* TODO: find out how much to increase the heap??? */
114 increasesize = heap_maximal_size - heap_current_size;
115 GC_LOG( dolog("GC: Increasing Heap Size by %d", increasesize); );
117 /* allocate new heap from the system */
118 newsize = heap_current_size + increasesize;
121 /* check if the newly allocated heap exists */
123 exceptions_throw_outofmemory_exit();
125 /* TODO: copy the old content to the new heap */
126 /* TODO: find a complete rootset and update it to the new position */
127 /* TODO: free the old heap */
129 /* set the new values */
130 /*heap_ptr = p + (heap_ptr - heap_base);
132 heap_current_size = newsize;
134 GC_LOG( dolog("GC: Increasing Heap Size was successful");
135 heap_println_usage(); );
137 /* only a quick sanity check */
138 GC_ASSERT(heap_current_size <= heap_maximal_size);
144 s4 heap_get_hashcode(java_objectheader *o)
151 /* TODO: we need to lock the object here i think!!! */
153 /* check if there is a hash attached to this object */
154 if (GC_TEST_FLAGS(o, HDRFLAG_HASH_ATTACHED)) {
156 hashcode = *( (s4 *) ( ((u1 *) o) + get_object_size(o) - SIZEOF_VOID_P ) ); /* TODO: clean this up!!! */
157 GC_LOG2( dolog("GC: Hash re-taken: %d (0x%08x)", hashcode, hashcode); );
161 GC_SET_FLAGS(o, HDRFLAG_HASH_TAKEN);
163 hashcode = (s4) (ptrint) o;
164 GC_LOG2( dolog("GC: Hash taken: %d (0x%08x)", hashcode, hashcode); );
172 static java_objectheader *heap_alloc_intern(u4 bytelength, regioninfo_t *region)
174 java_objectheader *p;
176 /* only a quick sanity check */
178 GC_ASSERT(bytelength >= sizeof(java_objectheader));
180 /* align objects in memory */
181 bytelength = GC_ALIGN(bytelength, GC_ALIGN_SIZE);
183 /* lock the region */
184 LOCK_MONITOR_ENTER(region);
186 /* check for sufficient free space */
187 if (bytelength > region->free) {
188 dolog("GC: Region out of memory!");
193 /* allocate the object in this region */
194 p = (java_objectheader *) region->ptr;
195 region->ptr += bytelength;
196 region->free -= bytelength;
198 /* unlock the region */
199 LOCK_MONITOR_EXIT(region);
200 GC_LOG( region = NULL; );
202 /* clear allocated memory region */
204 MSET(p, 0, u1, bytelength);
206 /* set the header information */
207 heap_init_objectheader(p, bytelength);
213 /* heap_allocate ***************************************************************
215 Allocates memory on the Java heap.
217 *******************************************************************************/
219 void *heap_allocate(u4 bytelength, u4 references, methodinfo *finalizer)
221 java_objectheader *p;
222 #if defined(ENABLE_RT_TIMING)
223 struct timespec time_start, time_end;
226 RT_TIMING_GET_TIME(time_start);
228 p = heap_alloc_intern(bytelength, heap_region_main);
233 #if defined(GCCONF_HDRFLAG_REFERENCING)
234 /* We can't use a bool here for references, as it's passed as a
235 bitmask in builtin_new. Thus we check for != 0. */
236 if (references != 0) {
237 GC_SET_FLAGS(p, HDRFLAG_REFERENCING);
241 /* take care of finalization stuff */
242 if (finalizer != NULL) {
244 /* set the header bit */
245 /* TODO: do we really need this??? */
246 /* TODO: can this be overwritten by cloning??? */
247 GC_SET_FLAGS(p, GC_FLAG_FINALIZER);
249 /* register the finalizer for this object */
250 final_register(p, finalizer);
253 RT_TIMING_GET_TIME(time_end);
254 RT_TIMING_TIME_DIFF(time_start, time_end, RT_TIMING_GC_ALLOC);
260 void *heap_alloc_uncollectable(u4 bytelength)
262 java_objectheader *p;
264 /* loader.c does this a lot for classes with fieldscount equal zero */
268 p = heap_alloc_intern(bytelength, heap_region_sys);
273 /* TODO: can this be overwritten by cloning??? */
274 /* remember this object as uncollectable */
275 GC_SET_FLAGS(p, HDRFLAG_UNCOLLECTABLE);
281 void heap_free(void *p)
283 GC_LOG( dolog("GC: Free %p", p); );
288 /* Debugging ******************************************************************/
291 void heap_println_usage()
293 printf("Current Heap Usage: Size=%d Free=%d\n",
294 heap_current_size, heap_region_main->free);
296 GC_ASSERT(heap_current_size == heap_region_main->size);
302 void heap_print_object_flags(java_objectheader *o)
304 printf("0x%02x [%s%s%s%s%s]",
306 GC_TEST_FLAGS(o, GC_FLAG_FINALIZER) ? "F" : " ",
307 GC_TEST_FLAGS(o, HDRFLAG_HASH_ATTACHED) ? "A" : " ",
308 GC_TEST_FLAGS(o, HDRFLAG_HASH_TAKEN) ? "T" : " ",
309 GC_TEST_FLAGS(o, HDRFLAG_UNCOLLECTABLE) ? "U" : " ",
310 GC_TEST_FLAGS(o, GC_FLAG_MARKED) ? "M" : " ");
316 void heap_print_object(java_objectheader *o)
321 /* check for null pointers */
327 /* print general information */
328 #if SIZEOF_VOID_P == 8
331 printf("0x%08x: ", (void *) o);
333 heap_print_object_flags(o);
337 /* maybe this is not really an object */
338 if (/*IS_CLASS*/ o->vftbl->class == class_java_lang_Class) {
340 /* get the class information */
343 /* print the class information */
347 } else if (/*IS_ARRAY*/ o->vftbl->arraydesc != NULL) {
349 /* get the array information */
350 a = (java_arrayheader *) o;
353 /* print the array information */
356 utf_display_printable_ascii_classname(c->name);
357 printf(" (size=%d)", a->size);
359 } else /*IS_OBJECT*/ {
361 /* get the object class */
364 /* print the object information */
367 utf_display_printable_ascii_classname(c->name);
368 if (c == class_java_lang_String) {
369 printf(" (string=\"");
370 utf_display_printable_ascii(
371 javastring_toutf((java_lang_String *) o, false));
380 void heap_dump_region(regioninfo_t *region, bool marked_only)
382 java_objectheader *o;
385 /* some basic sanity checks */
386 GC_ASSERT(region->base < region->ptr);
388 printf("Heap-Dump:\n");
390 /* walk the region in a linear style */
391 o = (java_objectheader *) region->base;
392 while (o < region->ptr) {
394 if (!marked_only || GC_IS_MARKED(o)) {
396 heap_print_object(o);
400 /* get size of object */
401 o_size = get_object_size(o);
403 /* walk to next object */
404 GC_ASSERT(o_size != 0);
405 o = ((u1 *) o) + o_size;
408 printf("Heap-Dump finished.\n");
413 s4 get_object_size(java_objectheader *o)
419 /* we can assume someone initialized the header */
420 GC_ASSERT(o->hdrflags != 0);
422 /* get the wordcount from the header */
423 o_size = GC_GET_SIZE(o);
425 /* maybe we need to calculate the size by hand */
426 if (o_size != GC_SIZE_DUMMY) {
427 GC_ASSERT(o_size != 0);
428 o_size = o_size << 2;
432 /* maybe this is not really an object */
433 if (/*IS_CLASS*/ o->vftbl->class == class_java_lang_Class) {
434 /* we know the size of a classinfo */
435 o_size = sizeof(classinfo);
437 } else if (/*IS_ARRAY*/ o->vftbl->arraydesc != NULL) {
438 /* compute size of this array */
439 a = (java_arrayheader *) o;
441 o_size = c->vftbl->arraydesc->dataoffset +
442 a->size * c->vftbl->arraydesc->componentsize;
444 } else /*IS_OBJECT*/ {
445 /* get the object size */
447 o_size = c->instancesize;
448 GC_LOG( dolog("Got size (from Class): %d bytes", o_size); );
454 o_size = GC_ALIGN(o_size, GC_ALIGN_SIZE);
456 /* the hashcode attached to this object might increase the size */
457 if (GC_TEST_FLAGS(o, HDRFLAG_HASH_ATTACHED))
458 o_size += SIZEOF_VOID_P;
465 * These are local overrides for various environment variables in Emacs.
466 * Please do not remove this and leave it at the end of the file, where
467 * Emacs will automagically detect them.
468 * ---------------------------------------------------------------------
471 * indent-tabs-mode: t
475 * vim:noexpandtab:sw=4:ts=4: