Line data Source code
1 : /* src/mm/gc.hpp - gc independant interface for heap managment
2 :
3 : Copyright (C) 1996-2005, 2006, 2007, 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 : #ifndef _GC_HPP
27 : #define _GC_HPP
28 :
29 : #include <stddef.h> // for size_t
30 : #include <stdint.h> // for int32_t, etc.
31 : #include "threads/thread.hpp"
32 : #include "vm/global.hpp" // for java_object_t
33 :
34 : class GC {
35 : public:
36 : };
37 :
38 :
39 : /**
40 : * Critical section for the GC.
41 : */
42 : class GCCriticalSection {
43 : public:
44 3987669 : GCCriticalSection() { enter(); }
45 3987638 : ~GCCriticalSection() { leave(); }
46 :
47 : inline static void enter ();
48 : inline static void leave ();
49 : inline static bool inside();
50 : };
51 :
52 : /**
53 : * Enters a LLNI critical section which prevents the GC from moving
54 : * objects around on the collected heap.
55 : *
56 : * There are no race conditions possible while entering such a critical
57 : * section, because each thread only modifies its own thread local flag
58 : * and the GC reads the flags while the world is stopped.
59 : */
60 3987683 : void GCCriticalSection::enter()
61 : {
62 : #if defined(ENABLE_GC_CACAO)
63 : threadobject* t = thread_get_current();
64 :
65 : // Sanity check.
66 : assert(t->gc_critical == false);
67 :
68 : t->gc_critical = true;
69 : #endif
70 3987683 : }
71 :
72 : /**
73 : * Leaves a LLNI critical section and allows the GC to move objects
74 : * around on the collected heap again.
75 : */
76 3987597 : void GCCriticalSection::leave()
77 : {
78 : #if defined(ENABLE_GC_CACAO)
79 : threadobject* t = thread_get_current();
80 :
81 : // Sanity check.
82 : assert(t->gc_critical == true);
83 :
84 : t->gc_critical = false;
85 : #endif
86 3987597 : }
87 :
88 :
89 : /**
90 : * Checks if the calling thread is inside a GC critical section.
91 : *
92 : * @return true if inside, false otherwise.
93 : */
94 : bool GCCriticalSection::inside()
95 : {
96 : #if defined(ENABLE_GC_CACAO)
97 : threadobject* t = thread_get_current();
98 : return t->gc_critical;
99 : #else
100 : return true;
101 : #endif
102 : }
103 :
104 : /* reference types ************************************************************/
105 :
106 : enum {
107 : GC_REFTYPE_THREADOBJECT,
108 : GC_REFTYPE_CLASSLOADER,
109 : GC_REFTYPE_JNI_GLOBALREF,
110 : GC_REFTYPE_FINALIZER,
111 : GC_REFTYPE_LOCALREF,
112 : GC_REFTYPE_STACK,
113 : GC_REFTYPE_CLASSREF,
114 : GC_REFTYPE_LOCKRECORD
115 : };
116 :
117 : /* function prototypes ********************************************************/
118 :
119 : void gc_init(size_t heapmaxsize, size_t heapstartsize);
120 :
121 : void* heap_alloc_uncollectable(size_t size);
122 : void* heap_alloc(size_t size, int references, methodinfo *finalizer, bool collect);
123 : void heap_free(void *p);
124 :
125 : #if defined(ENABLE_GC_CACAO)
126 : void heap_init_objectheader(java_object_t *o, uint32_t size);
127 : int32_t heap_get_hashcode(java_object_t *o);
128 :
129 : void gc_reference_register(java_object_t **ref, int32_t reftype);
130 : void gc_reference_unregister(java_object_t **ref);
131 :
132 : void gc_weakreference_register(java_object_t **ref, int32_t reftype);
133 : void gc_weakreference_unregister(java_object_t **ref);
134 : #endif
135 :
136 : void gc_call(void);
137 : int64_t gc_get_heap_size(void);
138 : int64_t gc_get_free_bytes(void);
139 : int64_t gc_get_total_bytes(void);
140 : int64_t gc_get_max_heap_size(void);
141 : void gc_invoke_finalizers(void);
142 : void gc_finalize_all(void);
143 : void* gc_out_of_memory(size_t bytes_requested);
144 :
145 : void gc_register_current_thread();
146 : void gc_unregister_current_thread();
147 :
148 : /* inlined functions **********************************************************/
149 :
150 55546 : static inline int32_t heap_hashcode(java_object_t* obj)
151 : {
152 : #if defined(ENABLE_GC_CACAO)
153 : return heap_get_hashcode(obj);
154 : #else
155 55546 : return (int32_t)(intptr_t) obj;
156 : #endif
157 : }
158 :
159 : #endif // _GC_HPP
160 :
161 :
162 : /*
163 : * These are local overrides for various environment variables in Emacs.
164 : * Please do not remove this and leave it at the end of the file, where
165 : * Emacs will automagically detect them.
166 : * ---------------------------------------------------------------------
167 : * Local variables:
168 : * mode: c++
169 : * indent-tabs-mode: t
170 : * c-basic-offset: 4
171 : * tab-width: 4
172 : * End:
173 : * vim:noexpandtab:sw=4:ts=4:
174 : */
|