* autogen.sh: Make it work with unpatched Boehm GC.
[cacao.git] / src / mm / gc-boehm.cpp
1 /* src/mm/gc-boehm.cpp - interface for boehm gc
2
3    Copyright (C) 1996-2005, 2006, 2007, 2008, 2010
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
28 #include <stdint.h>
29
30 #if defined(ENABLE_THREADS) && defined(__LINUX__)
31 #define GC_LINUX_THREADS
32 #endif
33 #if defined(ENABLE_THREADS) && defined(__IRIX__)
34 #define GC_IRIX_THREADS
35 #endif
36 #if defined(ENABLE_THREADS) && defined(__DARWIN__)
37 #define GC_DARWIN_THREADS
38 #endif
39 #if defined(ENABLE_THREADS) && defined(__SOLARIS__)
40 #define GC_SOLARIS_THREADS
41 #endif
42
43 #include "boehm-gc/include/gc.h"
44 #include "boehm-gc/include/javaxfc.h"
45 #include "mm/gc.hpp"
46 #include "mm/memory.hpp"
47
48 #include "toolbox/logging.hpp"
49
50 #include "vm/jit/builtin.hpp"
51 #include "vm/exceptions.hpp"
52 #include "vm/finalizer.hpp"
53 #include "vm/global.h"
54 #include "vm/loader.hpp"
55 #include "vm/options.h"
56 #include "vm/rt-timing.h"
57 #include "vm/string.hpp"
58 #include "vm/vm.hpp"
59
60
61 /* global variables ***********************************************************/
62
63 static bool in_gc_out_of_memory = false;    /* is GC out of memory?           */
64 static size_t gc_max_heap_size = 0;
65
66
67 /* prototype static functions *************************************************/
68
69 static void gc_ignore_warnings(char *msg, GC_word arg);
70
71
72 /* gc_init *********************************************************************
73
74    Initializes the boehm garbage collector.
75
76 *******************************************************************************/
77
78 void gc_init(size_t heapmaxsize, size_t heapstartsize)
79 {
80         size_t heapcurrentsize;
81
82         TRACESUBSYSTEMINITIALIZATION("gc_init");
83
84         /* just to be sure (should be set to 1 by JAVA_FINALIZATION macro) */
85
86         GC_java_finalization = 1;
87
88         /* Ignore pointers that do not point to the start of an object. */
89
90         GC_all_interior_pointers = 0;
91
92         /* suppress warnings */
93
94         GC_set_warn_proc(gc_ignore_warnings);
95
96         /* install a GC notifier */
97
98         GC_finalize_on_demand = 1;
99         GC_finalizer_notifier = finalizer_notify;
100
101         /* define OOM function */
102
103         GC_oom_fn = gc_out_of_memory;
104
105         GC_INIT();
106
107         /* set the maximal heap size */
108
109         GC_set_max_heap_size(heapmaxsize);
110         gc_max_heap_size = heapmaxsize;
111
112         /* set the initial heap size */
113
114         heapcurrentsize = GC_get_heap_size();
115
116         if (heapstartsize > heapcurrentsize)
117                 GC_expand_hp(heapstartsize - heapcurrentsize);
118 }
119
120
121 static void gc_ignore_warnings(char *msg, GC_word arg)
122 {
123 }
124
125
126 void *heap_alloc_uncollectable(size_t size)
127 {
128         void *p;
129
130         p = GC_MALLOC_UNCOLLECTABLE(size);
131
132         /* clear allocated memory region */
133
134         MSET(p, 0, uint8_t, size);
135
136         return p;
137 }
138
139
140 /* heap_alloc ******************************************************************
141
142    Allocates memory on the Java heap.
143
144 *******************************************************************************/
145
146 void *heap_alloc(size_t size, int references, methodinfo *finalizer, bool collect)
147 {
148         void *p;
149 #if defined(ENABLE_RT_TIMING)
150         struct timespec time_start, time_end;
151 #endif
152
153         RT_TIMING_GET_TIME(time_start);
154
155         /* We can't use a bool here for references, as it's passed as a
156            bitmask in builtin_new.  Thus we check for != 0. */
157
158         if (references != 0)
159                 p = GC_MALLOC(size);
160         else
161                 p = GC_MALLOC_ATOMIC(size);
162
163         if (p == NULL)
164                 return NULL;
165
166         if (finalizer != NULL)
167                 GC_REGISTER_FINALIZER_NO_ORDER(p, finalizer_run, 0, 0, 0);
168
169         /* clear allocated memory region */
170
171         MSET(p, 0, uint8_t, size);
172
173         RT_TIMING_GET_TIME(time_end);
174         RT_TIMING_TIME_DIFF(time_start, time_end, RT_TIMING_GC_ALLOC);
175
176         return p;
177 }
178
179
180 void heap_free(void *p)
181 {
182         GC_FREE(p);
183 }
184
185 void gc_call(void)
186 {
187         if (opt_verbosegc)
188                 dolog("Garbage Collection:  previous/now = %d / %d ",
189                           0, 0);
190
191         GC_gcollect();
192 }
193
194
195 int64_t gc_get_heap_size(void)
196 {
197         return GC_get_heap_size();
198 }
199
200
201 int64_t gc_get_free_bytes(void)
202 {
203         return GC_get_free_bytes();
204 }
205
206
207 /* gc_get_total_bytes **********************************************************
208
209    Returns the number of total bytes currently used on the Java heap.
210
211 *******************************************************************************/
212
213 int64_t gc_get_total_bytes(void)
214 {
215         return GC_get_total_bytes();
216 }
217
218
219 int64_t gc_get_max_heap_size(void)
220 {
221         return gc_max_heap_size;
222 }
223
224
225 void gc_invoke_finalizers(void)
226 {
227         GC_invoke_finalizers();
228 }
229
230
231 void gc_finalize_all(void)
232 {
233         GC_finalize_all();
234 }
235
236
237 /* gc_out_of_memory ************************************************************
238
239    This function is called when boehm detects that it is OOM.
240
241 *******************************************************************************/
242
243 void *gc_out_of_memory(size_t bytes_requested)
244 {
245         /* if this happens, we are REALLY out of memory */
246
247         if (in_gc_out_of_memory) {
248                 /* this is all we can do... */
249                 os::abort("gc_out_of_memory: out of memory");
250         }
251
252         in_gc_out_of_memory = true;
253
254         /* try to release some memory */
255
256         gc_call();
257
258         /* now instantiate the exception */
259
260         exceptions_throw_outofmemoryerror();
261
262         in_gc_out_of_memory = false;
263
264         return NULL;
265 }
266
267
268 /*
269  * These are local overrides for various environment variables in Emacs.
270  * Please do not remove this and leave it at the end of the file, where
271  * Emacs will automagically detect them.
272  * ---------------------------------------------------------------------
273  * Local variables:
274  * mode: c++
275  * indent-tabs-mode: t
276  * c-basic-offset: 4
277  * tab-width: 4
278  * End:
279  * vim:noexpandtab:sw=4:ts=4:
280  */