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