* runboehmfinalizer: Removed.
[cacao.git] / src / mm / boehm.c
1 /* src/mm/boehm.c - interface for boehm gc
2
3    Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4    R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5    C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6    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., 59 Temple Place - Suite 330, Boston, MA
23    02111-1307, USA.
24
25    Contact: cacao@complang.tuwien.ac.at
26
27    Authors: Stefan Ring
28
29    Changes: Christian Thalinger
30
31    $Id: boehm.c 3551 2005-11-03 20:43:01Z 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/tables.h"
67 #include "vm/jit/asmpart.h"
68
69
70 /* global variables ***********************************************************/
71
72 static bool in_gc_out_of_memory = false;    /* is GC out of memory?           */
73
74
75 static void
76 #ifdef __GNUC__
77         __attribute__ ((unused))
78 #endif
79 *stackcall_twoargs(struct otherstackcall *p)
80 {
81         return (*p->p2)(p->p, p->l);
82 }
83
84
85 static void *stackcall_malloc(void *p, u4 bytelength)
86 {
87         return GC_MALLOC(bytelength);
88 }
89
90
91 static void *stackcall_malloc_atomic(void *p, u4 bytelength)
92 {
93         return GC_MALLOC_ATOMIC(bytelength);
94 }
95
96
97 static void *stackcall_malloc_uncollectable(void *p, u4 bytelength)
98 {
99         return GC_MALLOC_UNCOLLECTABLE(bytelength);
100 }
101
102
103 static void *stackcall_free(void *p, u4 bytelength)
104 {
105         GC_FREE(p);
106         return NULL;
107 }
108
109
110 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
111 #define MAINTHREADCALL(r,m,pp,ll) \
112         if (currentThread == NULL || currentThread == mainThread) { \
113                 r = m(pp, ll); \
114         } else { \
115                 struct otherstackcall sc; \
116                 sc.p2 = m; \
117                 sc.p = pp; \
118                 sc.l = ll; \
119                 r = (*asm_switchstackandcall)(CONTEXT(mainThread).usedStackTop, \
120                                 stackcall_twoargs, \
121                                 (void**)&(CONTEXT(currentThread).usedStackTop), &sc); \
122         }
123 #else
124 #define MAINTHREADCALL(r,m,pp,ll) \
125         { r = m(pp, ll); }
126 #endif
127
128
129 void *heap_alloc_uncollectable(u4 bytelength)
130 {
131         void *result;
132
133         MAINTHREADCALL(result, stackcall_malloc_uncollectable, NULL, bytelength);
134
135         /* clear allocated memory region */
136
137         MSET(result, 0, u1, bytelength);
138
139         return result;
140 }
141
142
143 void *heap_allocate(u4 bytelength, bool references, methodinfo *finalizer)
144 {
145         void *result;
146
147         if (references) {
148                 MAINTHREADCALL(result, stackcall_malloc, NULL, bytelength);
149
150         } else {
151                 MAINTHREADCALL(result, stackcall_malloc_atomic, NULL, bytelength);
152         }
153
154         if (!result)
155                 return NULL;
156
157         if (finalizer)
158                 GC_REGISTER_FINALIZER(result, finalizer_add, 0, 0, 0);
159
160         /* clear allocated memory region */
161
162         MSET(result, 0, u1, bytelength);
163
164         return (u1 *) result;
165 }
166
167
168 void heap_free(void *p)
169 {
170         void *result;
171
172         MAINTHREADCALL(result, stackcall_free, p, 0);
173 }
174
175 static void gc_ignore_warnings(char *msg, GC_word arg)
176 {
177 }
178
179 void gc_init(u4 heapmaxsize, u4 heapstartsize)
180 {
181         size_t  heapcurrentsize;
182
183         GC_INIT();
184
185         /* set the maximal heap size */
186
187         GC_set_max_heap_size(heapmaxsize);
188
189         /* set the initial heap size */
190
191         heapcurrentsize = GC_get_heap_size();
192
193         if (heapstartsize > heapcurrentsize) {
194                 GC_expand_hp(heapstartsize - heapcurrentsize);
195         }
196
197         /* define OOM function */
198
199         GC_oom_fn = gc_out_of_memory;
200
201         /* suppress warnings */
202
203         GC_set_warn_proc(gc_ignore_warnings);
204 }
205
206
207 void gc_call(void)
208 {
209         if (opt_verbosegc)
210                 dolog("Garbage Collection:  previous/now = %d / %d ",
211                           0, 0);
212
213         GC_gcollect();
214 }
215
216
217 s8 gc_get_heap_size(void)
218 {
219         return GC_get_heap_size();
220 }
221
222
223 s8 gc_get_free_bytes(void)
224 {
225         return GC_get_free_bytes();
226 }
227
228
229 s8 gc_get_max_heap_size(void)
230 {
231         return GC_get_max_heap_size();
232 }
233
234
235 void gc_invoke_finalizers(void)
236 {
237         GC_invoke_finalizers();
238 }
239
240
241 void gc_finalize_all(void)
242 {
243         GC_finalize_all();
244 }
245
246
247 /* gc_out_of_memory ************************************************************
248
249    This function is called when boehm detects that it is OOM.
250
251 *******************************************************************************/
252
253 void *gc_out_of_memory(size_t bytes_requested)
254 {
255         /* if this happens, we are REALLY out of memory */
256
257         if (in_gc_out_of_memory) {
258                 /* this is all we can do... */
259                 throw_cacao_exception_exit(string_java_lang_InternalError,
260                                                                    "Out of memory");
261         }
262
263         in_gc_out_of_memory = true;
264
265         /* try to release some memory */
266
267         gc_call();
268
269         /* now instantiate the exception */
270
271         *exceptionptr = new_exception(string_java_lang_OutOfMemoryError);
272
273         in_gc_out_of_memory = false;
274
275         return NULL;
276 }
277
278
279 /*
280  * These are local overrides for various environment variables in Emacs.
281  * Please do not remove this and leave it at the end of the file, where
282  * Emacs will automagically detect them.
283  * ---------------------------------------------------------------------
284  * Local variables:
285  * mode: c
286  * indent-tabs-mode: t
287  * c-basic-offset: 4
288  * tab-width: 4
289  * End:
290  */