cce19759f16a9af82df6411a966b4c4be6d94c73
[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 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    Contact: cacao@cacaojvm.org
26
27    Authors: Michael Starzinger
28
29    $Id$
30
31 */
32
33
34 #include "config.h"
35 #include "vm/types.h"
36
37 #include "gc.h"
38 #include "final.h"
39 #include "heap.h"
40 #include "mm/memory.h"
41 #include "vm/finalizer.h"
42
43
44 /* Global Variables ***********************************************************/
45
46 list *final_list;
47
48
49
50 void final_init()
51 {
52         final_list = list_create(OFFSET(final_entry, linkage));
53 }
54
55 void final_register(java_objectheader *o, methodinfo *finalizer)
56 {
57         final_entry *fe;
58
59         fe = NEW(final_entry);
60         fe->type      = FINAL_REACHABLE;
61         fe->o         = o;
62         fe->finalizer = finalizer;
63
64         list_add_last(final_list, fe);
65
66         GC_LOG( printf("Finalizer registered for: %p\n", (void *) o); );
67 }
68
69 void final_invoke()
70 {
71         final_entry *fe;
72         final_entry *fe_next;
73
74         fe = list_first(final_list);
75         fe_next = NULL;
76         while (fe) {
77                 fe_next = list_next(final_list, fe);
78
79                 if (fe->type == FINAL_RECLAIMABLE) {
80
81                         GC_LOG( printf("Finalizer starting for: ");
82                                         heap_print_object(fe->o); printf("\n"); );
83
84                         GC_ASSERT(fe->finalizer == fe->o->vftbl->class->finalizer);
85
86                         fe->type = FINAL_FINALIZING;
87
88                         finalizer_run(fe->o, NULL);
89
90                         fe->type = FINAL_FINALIZED;
91
92                         list_remove(final_list, fe);
93                         FREE(fe, final_entry);
94                 }
95
96                 fe = fe_next;
97         }
98 }
99
100
101 /*
102  * These are local overrides for various environment variables in Emacs.
103  * Please do not remove this and leave it at the end of the file, where
104  * Emacs will automagically detect them.
105  * ---------------------------------------------------------------------
106  * Local variables:
107  * mode: c
108  * indent-tabs-mode: t
109  * c-basic-offset: 4
110  * tab-width: 4
111  * End:
112  * vim:noexpandtab:sw=4:ts=4:
113  */