* Updated header: Added 2006. Changed address of FSF. Changed email
[cacao.git] / src / mm / boehm.c
1 /* src/mm/boehm.c - interface for boehm gc
2
3    Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
4    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6    J. Wenninger, Institut f. Computersprachen - TU Wien
7
8    This file is part of CACAO.
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25    Contact: cacao@cacaojvm.org
26
27    Authors: Stefan Ring
28
29    Changes: Christian Thalinger
30
31    $Id: boehm.c 4357 2006-01-22 23:33:38Z twisti $
32
33 */
34
35
36 #include "config.h"
37 #include "vm/types.h"
38
39 #if defined(USE_THREADS) && defined(NATIVE_THREADS) && defined(__LINUX__)
40 #define GC_LINUX_THREADS
41 #endif
42 #if defined(USE_THREADS) && defined(NATIVE_THREADS) && defined(__IRIX__)
43 #define GC_IRIX_THREADS
44 #endif
45
46 #include "boehm-gc/include/gc.h"
47 #include "mm/boehm.h"
48 #include "mm/memory.h"
49
50 #if defined(USE_THREADS)
51 # if defined(NATIVE_THREADS)
52 #  include "threads/native/threads.h"
53 # else
54 #  include "threads/green/threads.h"
55 # endif
56 #endif
57
58 #include "toolbox/logging.h"
59 #include "vm/options.h"
60 #include "vm/builtin.h"
61 #include "vm/exceptions.h"
62 #include "vm/finalizer.h"
63 #include "vm/global.h"
64 #include "vm/loader.h"
65 #include "vm/stringlocal.h"
66 #include "vm/jit/asmpart.h"
67
68
69 /* global variables ***********************************************************/
70
71 static bool in_gc_out_of_memory = false;    /* is GC out of memory?           */
72
73
74 static void
75 #ifdef __GNUC__
76         __attribute__ ((unused))
77 #endif
78 *stackcall_twoargs(struct otherstackcall *p)
79 {
80         return (*p->p2)(p->p, p->l);
81 }
82
83 /* prototype static functions *************************************************/
84
85 static void gc_ignore_warnings(char *msg, GC_word arg);
86
87
88 /* gc_init *********************************************************************
89
90    Initializes the boehm garbage collector.
91
92 *******************************************************************************/
93
94 void gc_init(u4 heapmaxsize, u4 heapstartsize)
95 {
96         size_t heapcurrentsize;
97
98         GC_INIT();
99
100         /* set the maximal heap size */
101
102         GC_set_max_heap_size(heapmaxsize);
103
104         /* set the initial heap size */
105
106         heapcurrentsize = GC_get_heap_size();
107
108         if (heapstartsize > heapcurrentsize) {
109                 GC_expand_hp(heapstartsize - heapcurrentsize);
110         }
111
112         /* define OOM function */
113
114         GC_oom_fn = gc_out_of_memory;
115
116         /* just to be sure (should be set to 1 by JAVA_FINALIZATION macro) */
117
118         GC_java_finalization = 1;
119
120         /* suppress warnings */
121
122         GC_set_warn_proc(gc_ignore_warnings);
123
124         /* install a GC notifier */
125
126         GC_finalize_on_demand = 1;
127         GC_finalizer_notifier = finalizer_notify;
128 }
129
130
131 static void gc_ignore_warnings(char *msg, GC_word arg)
132 {
133 }
134
135 static void *stackcall_malloc(void *p, u4 bytelength)
136 {
137         return GC_MALLOC(bytelength);
138 }
139
140
141 static void *stackcall_malloc_atomic(void *p, u4 bytelength)
142 {
143         return GC_MALLOC_ATOMIC(bytelength);
144 }
145
146
147 static void *stackcall_malloc_uncollectable(void *p, u4 bytelength)
148 {
149         return GC_MALLOC_UNCOLLECTABLE(bytelength);
150 }
151
152
153 static void *stackcall_free(void *p, u4 bytelength)
154 {
155         GC_FREE(p);
156         return NULL;
157 }
158
159
160 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
161 #define MAINTHREADCALL(r,m,pp,ll) \
162         if (currentThread == NULL || currentThread == mainThread) { \
163                 r = m(pp, ll); \
164         } else { \
165                 struct otherstackcall sc; \
166                 sc.p2 = m; \
167                 sc.p = pp; \
168                 sc.l = ll; \
169                 r = (*asm_switchstackandcall)(CONTEXT(mainThread).usedStackTop, \
170                                 stackcall_twoargs, \
171                                 (void**)&(CONTEXT(currentThread).usedStackTop), &sc); \
172         }
173 #else
174 #define MAINTHREADCALL(r,m,pp,ll) \
175         { r = m(pp, ll); }
176 #endif
177
178
179 void *heap_alloc_uncollectable(u4 bytelength)
180 {
181         void *result;
182
183         MAINTHREADCALL(result, stackcall_malloc_uncollectable, NULL, bytelength);
184
185         /* clear allocated memory region */
186
187         MSET(result, 0, u1, bytelength);
188
189         return result;
190 }
191
192
193 void *heap_allocate(u4 bytelength, bool references, methodinfo *finalizer)
194 {
195         void *result;
196
197         if (references) {
198                 MAINTHREADCALL(result, stackcall_malloc, NULL, bytelength);
199
200         } else {
201                 MAINTHREADCALL(result, stackcall_malloc_atomic, NULL, bytelength);
202         }
203
204         if (!result)
205                 return NULL;
206
207         if (finalizer)
208                 GC_REGISTER_FINALIZER(result, finalizer_run, 0, 0, 0);
209
210         /* clear allocated memory region */
211
212         MSET(result, 0, u1, bytelength);
213
214         return (u1 *) result;
215 }
216
217
218 void heap_free(void *p)
219 {
220         void *result;
221
222         MAINTHREADCALL(result, stackcall_free, p, 0);
223 }
224
225 void gc_call(void)
226 {
227         if (opt_verbosegc)
228                 dolog("Garbage Collection:  previous/now = %d / %d ",
229                           0, 0);
230
231         GC_gcollect();
232 }
233
234
235 s8 gc_get_heap_size(void)
236 {
237         return GC_get_heap_size();
238 }
239
240
241 s8 gc_get_free_bytes(void)
242 {
243         return GC_get_free_bytes();
244 }
245
246
247 s8 gc_get_max_heap_size(void)
248 {
249         return GC_get_max_heap_size();
250 }
251
252
253 void gc_invoke_finalizers(void)
254 {
255         GC_invoke_finalizers();
256 }
257
258
259 void gc_finalize_all(void)
260 {
261         GC_finalize_all();
262 }
263
264
265 /* gc_out_of_memory ************************************************************
266
267    This function is called when boehm detects that it is OOM.
268
269 *******************************************************************************/
270
271 void *gc_out_of_memory(size_t bytes_requested)
272 {
273         /* if this happens, we are REALLY out of memory */
274
275         if (in_gc_out_of_memory) {
276                 /* this is all we can do... */
277                 exceptions_throw_outofmemory_exit();
278         }
279
280         in_gc_out_of_memory = true;
281
282         /* try to release some memory */
283
284         gc_call();
285
286         /* now instantiate the exception */
287
288         *exceptionptr = new_exception(string_java_lang_OutOfMemoryError);
289
290         in_gc_out_of_memory = false;
291
292         return NULL;
293 }
294
295
296 /*
297  * These are local overrides for various environment variables in Emacs.
298  * Please do not remove this and leave it at the end of the file, where
299  * Emacs will automagically detect them.
300  * ---------------------------------------------------------------------
301  * Local variables:
302  * mode: c
303  * indent-tabs-mode: t
304  * c-basic-offset: 4
305  * tab-width: 4
306  * End:
307  */