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