CACAO
final.c
Go to the documentation of this file.
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.hpp"
28 
29 #include "gc.h"
30 #include "final.h"
31 #include "heap.h"
32 #include "mm/memory.hpp"
33 #include "vm/finalizer.hpp"
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 
48 {
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 
62 {
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);
86  }
87 
88  fe = fe_next;
89  }
90 }
91 
93 {
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  */
java_object_t * o
Definition: final.h:53
#define NEW(type)
Definition: memory.hpp:93
#define FREE(ptr, type)
Definition: memory.hpp:94
#define GC_LOG(code)
Definition: gc.h:58
void final_register(java_object_t *o, methodinfo *finalizer)
Definition: final.c:47
u4 type
Definition: final.h:52
Definition: final.h:50
#define FINAL_FINALIZED
Definition: final.h:48
methodinfo * finalizer
Definition: final.h:54
#define GC_ASSERT(assertion)
Definition: gc.h:59
#define GC_LOG2(code)
Definition: gc.h:68
#define FINAL_REACHABLE
Definition: final.h:45
void final_invoke()
Definition: final.c:61
void heap_print_object(java_object_t *o)
Definition: heap.c:400
void final_set_all_reclaimable()
Definition: final.c:92
void finalizer_run(void *o, void *p)
Definition: finalizer.cpp:250
list_t * final_list
Definition: final.c:38
#define FINAL_RECLAIMABLE
Definition: final.h:46
#define FINAL_FINALIZING
Definition: final.h:47
vftbl_t * vftbl
Definition: global.hpp:264
#define OFFSET(s, el)
Definition: memory.hpp:90
#define printf(...)
Definition: ssa2.cpp:40
void final_init()
Definition: final.c:42