500391b93dc1f8b9fc820493a35bcb5729813c95
[cacao.git] / src / mm / cacao-gc / mark.c
1 /* mm/cacao-gc/mark.c - GC module for marking heap objects
2
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
7
8    This file is part of CACAO.
9
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.
14
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.
19
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
23    02110-1301, USA.
24
25 */
26
27
28 #include "config.h"
29
30 #include "gc.h"
31 #include "final.h"
32 #include "heap.h"
33 #include "mark.h"
34 #include "rootset.h"
35 #include "mm/memory.h"
36 #include "toolbox/logging.h"
37 #include "vm/global.h"
38 #include "vm/vm.h"
39 #include "vmcore/linker.h"
40
41
42 /* Helper Macros **************************************************************/
43
44 #define MARK(o) \
45         GCSTAT_COUNT_MAX(gcstat_mark_depth, gcstat_mark_depth_max); \
46         mark_recursive(o); \
47         GCSTAT_DEC(gcstat_mark_depth);
48
49
50 /* mark_recursive **************************************************************
51
52    Recursively mark all objects (including this) which are referenced.
53
54    TODO, XXX: We need to implement a non-recursive version of this!!!
55
56    IN:
57           o.....heap-object to be marked (either OBJECT or ARRAY)
58
59 *******************************************************************************/
60
61 void mark_recursive(java_object_t *o)
62 {
63         vftbl_t            *t;
64         classinfo          *c;
65         fieldinfo          *f;
66         java_objectarray_t *oa;
67         arraydescriptor    *desc;
68         java_object_t      *ref;
69         void *start, *end;
70         int i;
71
72         /* TODO: this needs cleanup!!! */
73         start = heap_region_main->base;
74         end = heap_region_main->ptr;
75
76         /* uncollectable objects should never get marked this way */
77         /* the reference should point into the heap */
78         GC_ASSERT(o);
79         GC_ASSERT(!GC_TEST_FLAGS(o, HDRFLAG_UNCOLLECTABLE));
80         GC_ASSERT(POINTS_INTO(o, start, end));
81
82         /* mark this object */
83         GC_SET_MARKED(o);
84         GCSTAT_COUNT(gcstat_mark_count);
85
86         /* get the class of this object */
87         /* TODO: maybe we do not need this yet, look to move down! */
88         t = o->vftbl;
89         GC_ASSERT(t);
90         c = t->class;
91         GC_ASSERT(c);
92
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))
97                 return;
98 #endif
99
100         /* check if we are marking an array */
101         if ((desc = t->arraydesc) != NULL) {
102                 /* this is an ARRAY */
103
104                 /* check if the array contains references */
105                 if (desc->arraytype != ARRAYTYPE_OBJECT)
106                         return;
107
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++) {
111
112                         /* load the reference value */
113                         ref = (java_object_t *) (oa->data[i]);
114
115                         /* check for outside or null pointers */
116                         if (!POINTS_INTO(ref, start, end))
117                                 continue;
118
119                         GC_LOG2( printf("Found (%p) from Array\n", (void *) ref); );
120
121                         /* do the recursive marking */
122                         if (!GC_IS_MARKED(ref)) {
123                                 GCSTAT_COUNT_MAX(gcstat_mark_depth, gcstat_mark_depth_max);
124                                 mark_recursive(ref);
125                                 GCSTAT_DEC(gcstat_mark_depth);
126                         }
127
128                 }
129
130         } else {
131                 /* this is an OBJECT */
132
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++) {
136                         f = &(c->fields[i]);
137
138                         /* check if this field contains a non-static reference */
139                         if (!IS_ADR_TYPE(f->type) || (f->flags & ACC_STATIC))
140                                 continue;
141
142                         /* load the reference value */
143                         ref = *( (java_object_t **) ((s1 *) o + f->offset) );
144
145                         /* check for outside or null pointers */
146                         if (!POINTS_INTO(ref, start, end))
147                                 continue;
148
149                         GC_LOG2( printf("Found (%p) from Field ", (void *) ref);
150                                         field_print(f); printf("\n"); );
151
152                         /* do the recursive marking */
153                         if (!GC_IS_MARKED(ref)) {
154                                 GCSTAT_COUNT_MAX(gcstat_mark_depth, gcstat_mark_depth_max);
155                                 mark_recursive(ref);
156                                 GCSTAT_DEC(gcstat_mark_depth);
157                         }
158
159                 }
160                 }
161
162         }
163
164 }
165
166
167 /* mark_classes ****************************************************************
168
169    Marks all the references from classinfo structures (static fields)
170
171    IN:
172       start.....Region to be marked starts here
173       end.......Region to be marked ends here 
174
175 *******************************************************************************/
176
177 void mark_classes(void *start, void *end)
178 {
179         java_object_t *ref;
180         classinfo     *c;
181         fieldinfo     *f;
182         void *sys_start, *sys_end;
183         int i;
184
185         GC_LOG( dolog("GC: Marking from classes ..."); );
186
187         /* TODO: cleanup!!! */
188         sys_start = heap_region_sys->base;
189         sys_end = heap_region_sys->ptr;
190
191         /* walk through all classinfo blocks */
192         for (c = sys_start; c < (classinfo *) sys_end; c++) {
193
194                 /* walk through all fields */
195                 f = c->fields;
196                 for (i = 0; i < c->fieldscount; i++, f++) {
197
198                         /* check if this is a static reference */
199                         if (!IS_ADR_TYPE(f->type) || !(f->flags & ACC_STATIC))
200                                 continue;
201
202                         /* load the reference */
203                         ref = (java_object_t *) (f->value);
204
205                         /* check for outside or null pointers */
206                         if (!POINTS_INTO(ref, start, end))
207                                 continue;
208
209                         /* mark the reference */
210                         MARK(ref);
211
212                 }
213
214         }
215
216 }
217
218
219 /* mark_me *********************************************************************
220
221    Marks all Heap Objects which are reachable from a given root-set.
222
223    REMEMBER: Assumes all threads are stopped!
224
225    IN:
226           rs.....root set containing the references
227
228 *******************************************************************************/
229
230 void mark_me(rootset_t *rs)
231 {
232         java_object_t      *ref;
233 #if defined(GCCONF_FINALIZER)
234         list_final_entry_t *fe;
235 #endif
236         u4                  f_type;
237         void *start, *end;
238         int i;
239
240         /* TODO: this needs cleanup!!! */
241         start = heap_region_main->base;
242         end = heap_region_main->ptr;
243
244         GCSTAT_INIT(gcstat_mark_count);
245         GCSTAT_INIT(gcstat_mark_depth);
246         GCSTAT_INIT(gcstat_mark_depth_max);
247
248         /* recursively mark all references from classes */
249         /*mark_classes(heap_region_main->base, heap_region_main->ptr);*/
250
251         while (rs) {
252                 GC_LOG( dolog("GC: Marking from rootset (%d entries) ...", rs->refcount); );
253
254                 /* mark all references of the rootset */
255                 for (i = 0; i < rs->refcount; i++) {
256
257                         /* is this a marking reference? */
258                         if (!rs->refs[i].marks)
259                                 continue;
260
261                         /* load the reference */
262                         ref = *( rs->refs[i].ref );
263
264                         /* check for outside or null pointers */
265                         if (!POINTS_INTO(ref, start, end))
266                                 continue;
267
268                         /* do the marking here */
269                         MARK(ref);
270
271                 }
272
273                 rs = rs->next;
274         }
275
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);
281         while (fe) {
282                 f_type = fe->type;
283                 ref    = fe->o;
284
285                 /* we do not care about objects which have been marked already */
286                 if (!GC_IS_MARKED(ref)) {
287
288                         switch (f_type) {
289
290                         case FINAL_REACHABLE: /* object was reachable before */
291                                 GC_LOG2( printf("Finalizer triggered for: ");
292                                                 heap_print_object(ref); printf("\n"); );
293
294                                 /* object is now reclaimable */
295                                 fe->type = FINAL_RECLAIMABLE;
296
297                                 /* notify the finalizer after collection finished */
298                                 gc_notify_finalizer = true;
299
300                                 /* keep the object alive until finalizer finishes */
301                                 MARK(ref);
302                                 break;
303
304                         case FINAL_RECLAIMABLE: /* object not yet finalized */
305                                 GC_LOG( printf("Finalizer not yet started for: ");
306                                                 heap_print_object(ref); printf("\n"); );
307
308                                 /* keep the object alive until finalizer finishes */
309                                 MARK(ref);
310                                 break;
311
312 #if 0
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"); );
316
317                                 /* keep the object alive until finalizer finishes */
318                                 MARK(ref);
319                                 break;
320 #endif
321
322                         default: /* case not yet covered */
323                                 vm_abort("mark_me: uncovered case (type=%d)", f_type);
324
325                         }
326                 }
327
328                 fe = list_next_unsynced(final_list, fe);
329         }
330 #endif /*defined(GCCONF_FINALIZER)*/
331
332         GC_LOG( dolog("GC: Marking finished."); );
333
334 #if defined(ENABLE_STATISTICS)
335         GC_ASSERT(gcstat_mark_depth == 0);
336 #endif
337 }
338
339
340 /*
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  * ---------------------------------------------------------------------
345  * Local variables:
346  * mode: c
347  * indent-tabs-mode: t
348  * c-basic-offset: 4
349  * tab-width: 4
350  * End:
351  * vim:noexpandtab:sw=4:ts=4:
352  */