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