#include "vm/stringlocal.h"
[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 2463 2005-05-12 23:54:07Z twisti $
32
33 */
34
35 #if defined(USE_THREADS) && defined(NATIVE_THREADS) && defined(__LINUX__)
36 #define GC_LINUX_THREADS
37 #endif
38 #if defined(USE_THREADS) && defined(NATIVE_THREADS) && defined(__IRIX__)
39 #define GC_IRIX_THREADS
40 #endif
41
42 #include "boehm-gc/include/gc.h"
43 #include "mm/boehm.h"
44
45 #if defined(USE_THREADS)
46 # if defined(NATIVE_THREADS)
47 #  include "threads/native/threads.h"
48 # else
49 #  include "threads/green/threads.h"
50 # endif
51 #endif
52
53 #include "toolbox/logging.h"
54 #include "vm/options.h"
55 #include "vm/builtin.h"
56 #include "vm/exceptions.h"
57 #include "vm/global.h"
58 #include "vm/loader.h"
59 #include "vm/stringlocal.h"
60 #include "vm/tables.h"
61 #include "vm/jit/asmpart.h"
62
63
64 static bool in_gc_out_of_memory = false;    /* is GC out of memory?           */
65
66
67 static void
68 #ifdef __GNUC__
69         __attribute__ ((unused))
70 #endif
71 *stackcall_twoargs(struct otherstackcall *p)
72 {
73         return (*p->p2)(p->p, p->l);
74 }
75
76
77 static void *stackcall_malloc(void *p, u4 bytelength)
78 {
79         return GC_MALLOC(bytelength);
80 }
81
82
83 static void *stackcall_malloc_atomic(void *p, u4 bytelength)
84 {
85         return GC_MALLOC_ATOMIC(bytelength);
86 }
87
88
89 static void *stackcall_malloc_uncollectable(void *p, u4 bytelength)
90 {
91         return GC_MALLOC_UNCOLLECTABLE(bytelength);
92 }
93
94
95 static void *stackcall_realloc(void *p, u4 bytelength)
96 {
97         return GC_REALLOC(p, bytelength);
98 }
99
100 static void *stackcall_free(void *p, u4 bytelength)
101 {
102         GC_FREE(p);
103         return NULL;
104 }
105
106
107 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
108 #define MAINTHREADCALL(r,m,pp,ll) \
109         if (currentThread == NULL || currentThread == mainThread) { \
110                 r = m(pp, ll); \
111         } else { \
112                 struct otherstackcall sc; \
113                 sc.p2 = m; \
114                 sc.p = pp; \
115                 sc.l = ll; \
116                 r = (*asm_switchstackandcall)(CONTEXT(mainThread).usedStackTop, \
117                                 stackcall_twoargs, \
118                                 (void**)&(CONTEXT(currentThread).usedStackTop), &sc); \
119         }
120 #else
121 #define MAINTHREADCALL(r,m,pp,ll) \
122         { r = m(pp, ll); }
123 #endif
124
125
126 void *heap_alloc_uncollectable(u4 bytelength)
127 {
128         void *result;
129         MAINTHREADCALL(result, stackcall_malloc_uncollectable, NULL, bytelength);
130         return result;
131 }
132
133
134 void runboehmfinalizer(void *o, void *p)
135 {
136         java_objectheader *ob = (java_objectheader *) o;
137
138         asm_calljavafunction(ob->vftbl->class->finalizer, ob, NULL, NULL, NULL);
139         
140         /* if we had an exception in the finalizer, ignore it */
141         *exceptionptr = NULL;
142 }
143
144
145 void *heap_allocate(u4 bytelength, bool references, methodinfo *finalizer)
146 {
147         void *result;
148
149         if (references) {
150                 MAINTHREADCALL(result, stackcall_malloc, NULL, bytelength);
151
152         } else {
153                 MAINTHREADCALL(result, stackcall_malloc_atomic, NULL, bytelength);
154         }
155
156         if (!result) {
157                 return NULL;
158         }
159
160         if (finalizer)
161                 GC_REGISTER_FINALIZER(result, runboehmfinalizer, 0, 0, 0);
162
163         return (u1*) result;
164 }
165
166
167 void *heap_reallocate(void *p, u4 bytelength)
168 {
169         void *result;
170
171         MAINTHREADCALL(result, stackcall_realloc, p, bytelength);
172
173         return result;
174 }
175
176 void heap_free(void *p)
177 {
178         void *result;
179
180         MAINTHREADCALL(result, stackcall_free, p, 0);
181 }
182
183 static void gc_ignore_warnings(char *msg, GC_word arg)
184 {
185 }
186
187 void gc_init(u4 heapmaxsize, u4 heapstartsize)
188 {
189         size_t heapcurrentsize;
190
191         GC_INIT();
192
193         /* set the maximal heap size */
194         GC_set_max_heap_size(heapmaxsize);
195
196         /* set the initial heap size */
197         heapcurrentsize = GC_get_heap_size();
198         if (heapstartsize > heapcurrentsize) {
199                 GC_expand_hp(heapstartsize - heapcurrentsize);
200         }
201
202         /* define OOM function */
203         GC_oom_fn = gc_out_of_memory;
204
205         /* suppress warnings */
206         GC_set_warn_proc(gc_ignore_warnings);
207 }
208
209
210 void gc_call(void)
211 {
212         if (collectverbose)
213                 dolog("Garbage Collection:  previous/now = %d / %d ",
214                           0, 0);
215
216         GC_gcollect();
217 }
218
219
220 s8 gc_get_heap_size(void)
221 {
222         return GC_get_heap_size();
223 }
224
225
226 s8 gc_get_free_bytes(void)
227 {
228         return GC_get_free_bytes();
229 }
230
231
232 s8 gc_get_max_heap_size(void)
233 {
234         return GC_get_max_heap_size();
235 }
236
237
238 void gc_invoke_finalizers(void)
239 {
240         GC_invoke_finalizers();
241 }
242
243
244 void gc_finalize_all(void)
245 {
246         GC_finalize_all();
247 }
248
249
250 /* gc_out_of_memory ************************************************************
251
252    This function is called when boehm detects that it is OOM.
253
254 *******************************************************************************/
255
256 void *gc_out_of_memory(size_t bytes_requested)
257 {
258         /* if this happens, we are REALLY out of memory */
259
260         if (in_gc_out_of_memory) {
261                 /* this is all we can do... */
262                 throw_cacao_exception_exit(string_java_lang_InternalError,
263                                                                    "Out of memory");
264         }
265
266         in_gc_out_of_memory = true;
267
268         /* try to release some memory */
269
270         gc_call();
271
272         /* now instantiate the exception */
273
274         *exceptionptr = new_exception(string_java_lang_OutOfMemoryError);
275
276         in_gc_out_of_memory = false;
277
278         return NULL;
279 }
280
281
282 /*
283  * These are local overrides for various environment variables in Emacs.
284  * Please do not remove this and leave it at the end of the file, where
285  * Emacs will automagically detect them.
286  * ---------------------------------------------------------------------
287  * Local variables:
288  * mode: c
289  * indent-tabs-mode: t
290  * c-basic-offset: 4
291  * tab-width: 4
292  * End:
293  */