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
33 #include "threads/lock-common.h"
40 #include "mm/memory.h"
41 #include "native/include/java_lang_String.h"
42 #include "toolbox/logging.h"
43 #include "vm/global.h"
44 #include "vm/stringlocal.h"
46 #include "vmcore/rt-timing.h"
49 /* Global Variables ***********************************************************/
51 s4 heap_current_size; /* current size of the heap */
52 s4 heap_maximal_size; /* maximal size of the heap */
53 regioninfo_t *heap_region_sys;
54 regioninfo_t *heap_region_main;
57 /* Helper Macros **************************************************************/
59 #define GC_ALIGN_SIZE SIZEOF_VOID_P
60 #define GC_ALIGN(length,size) ((((length) + (size) - 1) / (size)) * (size))
64 void heap_init_objectheader(java_objectheader *o, u4 bytelength)
68 /* initialize the header flags */
74 /* calculate the wordcount as stored in the header */
75 /* TODO: improve this to save wordcount and without header bytes */
76 if ((bytelength & 0x03) == 0) {
77 GC_ASSERT((bytelength & 0x03) == 0);
78 wordcount = (bytelength >> 2);
79 GC_ASSERT(wordcount != 0);
81 wordcount = GC_SIZE_DUMMY;
84 /* set the wordcount in the header */
85 if (wordcount >= GC_SIZE_DUMMY) {
86 GC_SET_SIZE(o, GC_SIZE_DUMMY);
88 GC_SET_SIZE(o, wordcount);
94 s4 heap_increase_size() {
99 /* TODO: locking for threads!!! */
101 /* only a quick sanity check */
102 GC_ASSERT(heap_current_size <= heap_maximal_size);
104 /* check if we are allowed to enlarge the heap */
105 if (heap_current_size == heap_maximal_size)
106 vm_abort("heap_increase_size: reached maximal heap size: out of memory");
108 /* TODO: find out how much to increase the heap??? */
109 increasesize = heap_maximal_size - heap_current_size;
110 GC_LOG( dolog("GC: Increasing Heap Size by %d", increasesize); );
112 /* allocate new heap from the system */
113 newsize = heap_current_size + increasesize;
114 /*p = malloc(newsize);*/
116 /* check if the newly allocated heap exists */
118 vm_abort("heap_increase_size: malloc failed: out of memory");
120 /* TODO: copy the old content to the new heap */
121 /* TODO: find a complete rootset and update it to the new position */
122 /* TODO: free the old heap */
124 /* set the new values */
125 /*heap_ptr = p + (heap_ptr - heap_base);
127 heap_current_size = newsize;
129 GC_LOG( dolog("GC: Increasing Heap Size was successful");
130 heap_println_usage(); );
132 /* only a quick sanity check */
133 GC_ASSERT(heap_current_size <= heap_maximal_size);
139 s4 heap_get_hashcode(java_objectheader *o)
146 /* TODO: we need to lock the object here i think!!! */
148 /* check if there is a hash attached to this object */
149 if (GC_TEST_FLAGS(o, HDRFLAG_HASH_ATTACHED)) {
151 hashcode = *( (s4 *) ( ((u1 *) o) + get_object_size(o) - SIZEOF_VOID_P ) ); /* TODO: clean this up!!! */
152 GC_LOG2( dolog("GC: Hash re-taken: %d (0x%08x)", hashcode, hashcode); );
156 GC_SET_FLAGS(o, HDRFLAG_HASH_TAKEN);
158 hashcode = (s4) (ptrint) o;
159 GC_LOG2( dolog("GC: Hash taken: %d (0x%08x)", hashcode, hashcode); );
167 static java_objectheader *heap_alloc_intern(u4 bytelength, regioninfo_t *region, bool collect)
169 java_objectheader *p;
171 /* only a quick sanity check */
173 GC_ASSERT(bytelength >= sizeof(java_objectheader));
175 /* align objects in memory */
176 bytelength = GC_ALIGN(bytelength, GC_ALIGN_SIZE);
178 /* lock the region */
179 LOCK_MONITOR_ENTER(region);
182 /* heavy stress test */
187 /* check for sufficient free space */
188 if (bytelength > region->free) {
189 dolog("GC: Region out of memory! (collect=%d)", collect);
193 GC_ASSERT(region->free >= bytelength);
198 /* allocate the object in this region */
199 p = (java_objectheader *) region->ptr;
200 region->ptr += bytelength;
201 region->free -= bytelength;
203 /* unlock the region */
204 LOCK_MONITOR_EXIT(region);
205 GC_LOG( region = NULL; );
207 /* clear allocated memory region */
209 MSET(p, 0, u1, bytelength);
211 /* set the header information */
212 heap_init_objectheader(p, bytelength);
218 /* heap_alloc ******************************************************************
220 Allocates memory on the Java heap.
222 *******************************************************************************/
224 void *heap_alloc(u4 size, u4 references, methodinfo *finalizer, bool collect)
226 java_objectheader *p;
227 #if defined(ENABLE_RT_TIMING)
228 struct timespec time_start, time_end;
231 RT_TIMING_GET_TIME(time_start);
233 p = heap_alloc_intern(size, heap_region_main, collect);
238 #if defined(GCCONF_HDRFLAG_REFERENCING)
239 /* We can't use a bool here for references, as it's passed as a
240 bitmask in builtin_new. Thus we check for != 0. */
241 if (references != 0) {
242 GC_SET_FLAGS(p, HDRFLAG_REFERENCING);
246 /* register the finalizer for this object */
247 if (finalizer != NULL) {
248 final_register(p, finalizer);
251 RT_TIMING_GET_TIME(time_end);
252 RT_TIMING_TIME_DIFF(time_start, time_end, RT_TIMING_GC_ALLOC);
258 void *heap_alloc_uncollectable(u4 size)
260 java_objectheader *p;
262 /* loader.c does this a lot for classes with fieldscount equal zero */
266 p = heap_alloc_intern(size, heap_region_sys, false);
271 /* TODO: can this be overwritten by cloning??? */
272 /* remember this object as uncollectable */
273 GC_SET_FLAGS(p, HDRFLAG_UNCOLLECTABLE);
279 void heap_free(void *p)
281 GC_LOG( dolog("GC: Free %p", p); );
286 /* Debugging ******************************************************************/
289 void heap_println_usage()
291 printf("Current Heap Usage: Size=%d Free=%d\n",
292 heap_current_size, heap_region_main->free);
294 GC_ASSERT(heap_current_size == heap_region_main->size);
300 void heap_print_object_flags(java_objectheader *o)
302 printf("0x%02x [%s%s%s%s]",
304 GC_TEST_FLAGS(o, HDRFLAG_HASH_ATTACHED) ? "A" : " ",
305 GC_TEST_FLAGS(o, HDRFLAG_HASH_TAKEN) ? "T" : " ",
306 GC_TEST_FLAGS(o, HDRFLAG_UNCOLLECTABLE) ? "U" : " ",
307 GC_TEST_FLAGS(o, GC_FLAG_MARKED) ? "M" : " ");
313 void heap_print_object(java_objectheader *o)
318 /* check for null pointers */
324 /* print general information */
325 #if SIZEOF_VOID_P == 8
326 printf("0x%016llx: ", (unsigned long long) o);
328 printf("0x%08lx: ", (unsigned long) o);
331 /* check for invalid heap references */
332 if (!POINTS_INTO(o, heap_region_main->base, heap_region_main->end) &&
333 !POINTS_INTO(o, heap_region_sys->base, heap_region_sys->end))
335 printf("<<< No Heap Reference >>>");
339 /* print object flags */
340 heap_print_object_flags(o);
346 /* maybe this is not really an object */
347 if (/*IS_CLASS*/ o->vftbl->class == class_java_lang_Class) {
349 /* get the class information */
352 /* print the class information */
356 } else if (/*IS_ARRAY*/ o->vftbl->arraydesc != NULL) {
358 /* get the array information */
359 a = (java_arrayheader *) o;
362 /* print the array information */
365 utf_display_printable_ascii_classname(c->name);
366 printf(" (size=%d)", a->size);
368 } else /*IS_OBJECT*/ {
370 /* get the object class */
373 /* print the object information */
376 utf_display_printable_ascii_classname(c->name);
377 if (c == class_java_lang_String) {
378 printf(" (string=\"");
379 utf_display_printable_ascii(
380 javastring_toutf((java_lang_String *) o, false));
389 void heap_dump_region(regioninfo_t *region, bool marked_only)
391 java_objectheader *o;
394 /* some basic sanity checks */
395 GC_ASSERT(region->base <= region->ptr);
397 printf("Heap-Dump:\n");
399 /* walk the region in a linear style */
400 o = (java_objectheader *) region->base;
401 while (o < region->ptr) {
403 if (!marked_only || GC_IS_MARKED(o)) {
405 heap_print_object(o);
409 /* get size of object */
410 o_size = get_object_size(o);
412 /* walk to next object */
413 GC_ASSERT(o_size != 0);
414 o = ((u1 *) o) + o_size;
417 printf("Heap-Dump finished.\n");
422 s4 get_object_size(java_objectheader *o)
428 /* we can assume someone initialized the header */
429 GC_ASSERT(o->hdrflags != 0);
431 /* get the wordcount from the header */
432 o_size = GC_GET_SIZE(o);
434 /* maybe we need to calculate the size by hand */
435 if (o_size != GC_SIZE_DUMMY) {
436 GC_ASSERT(o_size != 0);
437 o_size = o_size << 2;
441 /* maybe this is not really an object */
442 if (/*IS_CLASS*/ o->vftbl->class == class_java_lang_Class) {
443 /* we know the size of a classinfo */
444 o_size = sizeof(classinfo);
446 } else if (/*IS_ARRAY*/ o->vftbl->arraydesc != NULL) {
447 /* compute size of this array */
448 a = (java_arrayheader *) o;
450 o_size = c->vftbl->arraydesc->dataoffset +
451 a->size * c->vftbl->arraydesc->componentsize;
453 } else /*IS_OBJECT*/ {
454 /* get the object size */
456 o_size = c->instancesize;
457 GC_LOG( dolog("Got size (from Class): %d bytes", o_size); );
463 o_size = GC_ALIGN(o_size, GC_ALIGN_SIZE);
465 /* the hashcode attached to this object might increase the size */
466 if (GC_TEST_FLAGS(o, HDRFLAG_HASH_ATTACHED))
467 o_size += SIZEOF_VOID_P;
474 * These are local overrides for various environment variables in Emacs.
475 * Please do not remove this and leave it at the end of the file, where
476 * Emacs will automagically detect them.
477 * ---------------------------------------------------------------------
480 * indent-tabs-mode: t
484 * vim:noexpandtab:sw=4:ts=4: