Merged trunk and subtype.
[cacao.git] / src / mm / cacao-gc / final.c
1 /* mm/cacao-gc/final.c - GC module for finalization and weak references
2
3    Copyright (C) 2006, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #include "config.h"
27 #include "vm/types.h"
28
29 #include "gc.h"
30 #include "final.h"
31 #include "heap.h"
32 #include "mm/memory.h"
33 #include "vm/finalizer.h"
34
35
36 /* Global Variables ***********************************************************/
37
38 list_t *final_list;
39
40
41
42 void final_init()
43 {
44         final_list = list_create(OFFSET(list_final_entry_t, linkage));
45 }
46
47 void final_register(java_object_t *o, methodinfo *finalizer)
48 {
49         list_final_entry_t *fe;
50
51         fe = NEW(list_final_entry_t);
52         fe->type      = FINAL_REACHABLE;
53         fe->o         = o;
54         fe->finalizer = finalizer;
55
56         list_add_first(final_list, fe);
57
58         GC_LOG2( printf("Finalizer registered for: %p\n", (void *) o); );
59 }
60
61 void final_invoke()
62 {
63         list_final_entry_t *fe;
64         list_final_entry_t *fe_next;
65
66         fe = list_first(final_list);
67         fe_next = NULL;
68         while (fe) {
69                 fe_next = list_next(final_list, fe);
70
71                 if (fe->type == FINAL_RECLAIMABLE) {
72
73                         GC_LOG( printf("Finalizer starting for: ");
74                                         heap_print_object(fe->o); printf("\n"); );
75
76                         GC_ASSERT(fe->finalizer == fe->o->vftbl->class->finalizer);
77
78                         fe->type = FINAL_FINALIZING;
79
80                         finalizer_run(fe->o, NULL);
81
82                         fe->type = FINAL_FINALIZED;
83
84                         list_remove(final_list, fe);
85                         FREE(fe, list_final_entry_t);
86                 }
87
88                 fe = fe_next;
89         }
90 }
91
92 void final_set_all_reclaimable()
93 {
94         list_final_entry_t *fe;
95
96         fe = list_first(final_list);
97         while (fe) {
98
99                 if (fe->type == FINAL_REACHABLE)
100                         fe->type = FINAL_RECLAIMABLE;
101
102                 fe = list_next(final_list, fe);
103         }
104 }
105
106
107 /*
108  * These are local overrides for various environment variables in Emacs.
109  * Please do not remove this and leave it at the end of the file, where
110  * Emacs will automagically detect them.
111  * ---------------------------------------------------------------------
112  * Local variables:
113  * mode: c
114  * indent-tabs-mode: t
115  * c-basic-offset: 4
116  * tab-width: 4
117  * End:
118  * vim:noexpandtab:sw=4:ts=4:
119  */