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