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