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