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