1 /* mm/cacao-gc/mark.c - GC module for marking heap objects
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
35 #include "mm/memory.h"
36 #include "toolbox/logging.h"
37 #include "vm/global.h"
39 #include "vmcore/linker.h"
42 /* Helper Macros **************************************************************/
45 GCSTAT_COUNT_MAX(gcstat_mark_depth, gcstat_mark_depth_max); \
47 GCSTAT_DEC(gcstat_mark_depth);
50 /* mark_recursive **************************************************************
52 Recursively mark all objects (including this) which are referenced.
54 TODO, XXX: We need to implement a non-recursive version of this!!!
57 o.....heap-object to be marked (either OBJECT or ARRAY)
59 *******************************************************************************/
61 void mark_recursive(java_object_t *o)
66 java_objectarray_t *oa;
67 arraydescriptor *desc;
72 /* TODO: this needs cleanup!!! */
73 start = heap_region_main->base;
74 end = heap_region_main->ptr;
76 /* uncollectable objects should never get marked this way */
77 /* the reference should point into the heap */
79 GC_ASSERT(!GC_TEST_FLAGS(o, HDRFLAG_UNCOLLECTABLE));
80 GC_ASSERT(POINTS_INTO(o, start, end));
82 /* mark this object */
84 GCSTAT_COUNT(gcstat_mark_count);
86 /* get the class of this object */
87 /* TODO: maybe we do not need this yet, look to move down! */
93 #if defined(GCCONF_HDRFLAG_REFERENCING)
94 /* does this object has pointers? */
95 /* TODO: check how often this happens, maybe remove this check! */
96 if (!GC_TEST_FLAGS(o, HDRFLAG_REFERENCING))
100 /* check if we are marking an array */
101 if ((desc = t->arraydesc) != NULL) {
102 /* this is an ARRAY */
104 /* check if the array contains references */
105 if (desc->arraytype != ARRAYTYPE_OBJECT)
108 /* for object-arrays we need to check every entry */
109 oa = (java_objectarray_t *) o;
110 for (i = 0; i < oa->header.size; i++) {
112 /* load the reference value */
113 ref = (java_object_t *) (oa->data[i]);
115 /* check for outside or null pointers */
116 if (!POINTS_INTO(ref, start, end))
119 GC_LOG2( printf("Found (%p) from Array\n", (void *) ref); );
121 /* do the recursive marking */
122 if (!GC_IS_MARKED(ref)) {
123 GCSTAT_COUNT_MAX(gcstat_mark_depth, gcstat_mark_depth_max);
125 GCSTAT_DEC(gcstat_mark_depth);
131 /* this is an OBJECT */
133 /* for objects we need to check all (non-static) fields */
134 for (; c; c = c->super.cls) {
135 for (i = 0; i < c->fieldscount; i++) {
138 /* check if this field contains a non-static reference */
139 if (!IS_ADR_TYPE(f->type) || (f->flags & ACC_STATIC))
142 /* load the reference value */
143 ref = *( (java_object_t **) ((s1 *) o + f->offset) );
145 /* check for outside or null pointers */
146 if (!POINTS_INTO(ref, start, end))
149 GC_LOG2( printf("Found (%p) from Field ", (void *) ref);
150 field_print(f); printf("\n"); );
152 /* do the recursive marking */
153 if (!GC_IS_MARKED(ref)) {
154 GCSTAT_COUNT_MAX(gcstat_mark_depth, gcstat_mark_depth_max);
156 GCSTAT_DEC(gcstat_mark_depth);
167 /* mark_classes ****************************************************************
169 Marks all the references from classinfo structures (static fields)
172 start.....Region to be marked starts here
173 end.......Region to be marked ends here
175 *******************************************************************************/
177 void mark_classes(void *start, void *end)
182 void *sys_start, *sys_end;
185 GC_LOG( dolog("GC: Marking from classes ..."); );
187 /* TODO: cleanup!!! */
188 sys_start = heap_region_sys->base;
189 sys_end = heap_region_sys->ptr;
191 /* walk through all classinfo blocks */
192 for (c = sys_start; c < (classinfo *) sys_end; c++) {
194 /* walk through all fields */
196 for (i = 0; i < c->fieldscount; i++, f++) {
198 /* check if this is a static reference */
199 if (!IS_ADR_TYPE(f->type) || !(f->flags & ACC_STATIC))
202 /* load the reference */
203 ref = (java_object_t *) (f->value);
205 /* check for outside or null pointers */
206 if (!POINTS_INTO(ref, start, end))
209 /* mark the reference */
219 /* mark_me *********************************************************************
221 Marks all Heap Objects which are reachable from a given root-set.
223 REMEMBER: Assumes all threads are stopped!
226 rs.....root set containing the references
228 *******************************************************************************/
230 void mark_me(rootset_t *rs)
233 #if defined(GCCONF_FINALIZER)
234 list_final_entry_t *fe;
240 /* TODO: this needs cleanup!!! */
241 start = heap_region_main->base;
242 end = heap_region_main->ptr;
244 GCSTAT_INIT(gcstat_mark_count);
245 GCSTAT_INIT(gcstat_mark_depth);
246 GCSTAT_INIT(gcstat_mark_depth_max);
248 /* recursively mark all references from classes */
249 /*mark_classes(heap_region_main->base, heap_region_main->ptr);*/
252 GC_LOG( dolog("GC: Marking from rootset (%d entries) ...", rs->refcount); );
254 /* mark all references of the rootset */
255 for (i = 0; i < rs->refcount; i++) {
257 /* is this a marking reference? */
258 if (!rs->refs[i].marks)
261 /* load the reference */
262 ref = *( rs->refs[i].ref );
264 /* check for outside or null pointers */
265 if (!POINTS_INTO(ref, start, end))
268 /* do the marking here */
276 #if defined(GCCONF_FINALIZER)
277 /* objects with finalizers will also be marked here. if they have not been
278 * marked before the finalization is triggered */
279 /* REMEMBER: all threads are stopped, so we can use unsynced access here */
280 fe = list_first_unsynced(final_list);
285 /* we do not care about objects which have been marked already */
286 if (!GC_IS_MARKED(ref)) {
290 case FINAL_REACHABLE: /* object was reachable before */
291 GC_LOG2( printf("Finalizer triggered for: ");
292 heap_print_object(ref); printf("\n"); );
294 /* object is now reclaimable */
295 fe->type = FINAL_RECLAIMABLE;
297 /* notify the finalizer after collection finished */
298 gc_notify_finalizer = true;
300 /* keep the object alive until finalizer finishes */
304 case FINAL_RECLAIMABLE: /* object not yet finalized */
305 GC_LOG( printf("Finalizer not yet started for: ");
306 heap_print_object(ref); printf("\n"); );
308 /* keep the object alive until finalizer finishes */
313 case FINAL_FINALIZING: /* object is still being finalized */
314 GC_LOG( printf("Finalizer not yet finished for: ");
315 heap_print_object(ref); printf("\n"); );
317 /* keep the object alive until finalizer finishes */
322 default: /* case not yet covered */
323 vm_abort("mark_me: uncovered case (type=%d)", f_type);
328 fe = list_next_unsynced(final_list, fe);
330 #endif /*defined(GCCONF_FINALIZER)*/
332 GC_LOG( dolog("GC: Marking finished."); );
334 #if defined(ENABLE_STATISTICS)
335 GC_ASSERT(gcstat_mark_depth == 0);
341 * These are local overrides for various environment variables in Emacs.
342 * Please do not remove this and leave it at the end of the file, where
343 * Emacs will automagically detect them.
344 * ---------------------------------------------------------------------
347 * indent-tabs-mode: t
351 * vim:noexpandtab:sw=4:ts=4: