030ec23ad92cbb84ad5d838e23e4ac087ad0b147
[cacao.git] / src / mm / boehm.c
1 /* mm/boehm.c - interface for boehm gc
2
3    Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
4    R. Grafl, A. Krall, C. Kruegel, C. Oates, R. Obermaisser,
5    M. Probst, S. Ring, E. Steiner, C. Thalinger, D. Thuernbeck,
6    P. Tomsich, J. Wenninger
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    $Id: boehm.c 1621 2004-11-30 13:06:55Z twisti $
30
31 */
32
33 #if defined(USE_THREADS) && defined(NATIVE_THREADS) && defined(__LINUX__)
34 #define GC_LINUX_THREADS
35 #endif
36 #if defined(USE_THREADS) && defined(NATIVE_THREADS) && defined(__IRIX__)
37 #define GC_IRIX_THREADS
38 #endif
39
40 #include "boehm-gc/include/gc.h"
41 #include "mm/boehm.h"
42
43 #if defined(USE_THREADS)
44 # if defined(NATIVE_THREADS)
45 #  include "threads/native/threads.h"
46 # else
47 #  include "threads/green/threads.h"
48 # endif
49 #endif
50
51 #include "toolbox/logging.h"
52 #include "vm/options.h"
53 #include "vm/builtin.h"
54 #include "vm/exceptions.h"
55 #include "vm/global.h"
56 #include "vm/loader.h"
57 #include "vm/tables.h"
58 #include "vm/jit/asmpart.h"
59
60
61 static bool in_gc_out_of_memory = false;    /* is GC out of memory?           */
62
63
64 static void
65 #ifdef __GNUC__
66         __attribute__ ((unused))
67 #endif
68 *stackcall_twoargs(struct otherstackcall *p)
69 {
70         return (*p->p2)(p->p, p->l);
71 }
72
73
74 static void *stackcall_malloc(void *p, u4 bytelength)
75 {
76         return GC_MALLOC(bytelength);
77 }
78
79
80 static void *stackcall_malloc_atomic(void *p, u4 bytelength)
81 {
82         return GC_MALLOC_ATOMIC(bytelength);
83 }
84
85
86 static void *stackcall_malloc_uncollectable(void *p, u4 bytelength)
87 {
88         return GC_MALLOC_UNCOLLECTABLE(bytelength);
89 }
90
91
92 static void *stackcall_realloc(void *p, u4 bytelength)
93 {
94         return GC_REALLOC(p, bytelength);
95 }
96
97 static void *stackcall_free(void *p, u4 bytelength)
98 {
99         GC_FREE(p);
100         return NULL;
101 }
102
103
104 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
105 #define MAINTHREADCALL(r,m,pp,ll) \
106         if (currentThread == NULL || currentThread == mainThread) { \
107                 r = m(pp, ll); \
108         } else { \
109                 struct otherstackcall sc; \
110                 sc.p2 = m; \
111                 sc.p = pp; \
112                 sc.l = ll; \
113                 r = (*asm_switchstackandcall)(CONTEXT(mainThread).usedStackTop, \
114                                 stackcall_twoargs, \
115                                 (void**)&(CONTEXT(currentThread).usedStackTop), &sc); \
116         }
117 #else
118 #define MAINTHREADCALL(r,m,pp,ll) \
119         { r = m(pp, ll); }
120 #endif
121
122
123 void *heap_alloc_uncollectable(u4 bytelength)
124 {
125         void *result;
126         MAINTHREADCALL(result, stackcall_malloc_uncollectable, NULL, bytelength);
127         return result;
128 }
129
130
131 void runboehmfinalizer(void *o, void *p)
132 {
133         java_objectheader *ob = (java_objectheader *) o;
134
135         asm_calljavafunction(ob->vftbl->class->finalizer, ob, NULL, NULL, NULL);
136         
137         /* if we had an exception in the finalizer, ignore it */
138         *exceptionptr = NULL;
139 }
140
141
142 void *heap_allocate(u4 bytelength, bool references, methodinfo *finalizer)
143 {
144         void *result;
145
146         if (references) {
147                 MAINTHREADCALL(result, stackcall_malloc, NULL, bytelength);
148
149         } else {
150                 MAINTHREADCALL(result, stackcall_malloc_atomic, NULL, bytelength);
151         }
152
153         if (!result) {
154                 return NULL;
155         }
156
157         if (finalizer)
158                 GC_REGISTER_FINALIZER(result, runboehmfinalizer, 0, 0, 0);
159
160         return (u1*) result;
161 }
162
163
164 void *heap_reallocate(void *p, u4 bytelength)
165 {
166         void *result;
167
168         MAINTHREADCALL(result, stackcall_realloc, p, bytelength);
169
170         return result;
171 }
172
173 void heap_free(void *p)
174 {
175         void *result;
176
177         MAINTHREADCALL(result, stackcall_free, p, 0);
178 }
179
180 static void gc_ignore_warnings(char *msg, GC_word arg)
181 {
182 }
183
184 void gc_init(u4 heapmaxsize, u4 heapstartsize)
185 {
186         size_t heapcurrentsize;
187
188         GC_INIT();
189
190         /* set the maximal heap size */
191         GC_set_max_heap_size(heapmaxsize);
192
193         /* set the initial heap size */
194         heapcurrentsize = GC_get_heap_size();
195         if (heapstartsize > heapcurrentsize) {
196                 GC_expand_hp(heapstartsize - heapcurrentsize);
197         }
198
199         /* define OOM function */
200         GC_oom_fn = gc_out_of_memory;
201
202         /* suppress warnings */
203         GC_set_warn_proc(gc_ignore_warnings);
204 }
205
206
207 void gc_call()
208 {
209         if (collectverbose)
210                 dolog("Garbage Collection:  previous/now = %d / %d ",
211                           0, 0);
212
213         GC_gcollect();
214 }
215
216
217 s8 gc_get_heap_size()
218 {
219         return GC_get_heap_size();
220 }
221
222
223 s8 gc_get_free_bytes()
224 {
225         return GC_get_free_bytes();
226 }
227
228
229 s8 gc_get_max_heap_size()
230 {
231         return GC_get_max_heap_size();
232 }
233
234
235 void gc_invoke_finalizers()
236 {
237         GC_invoke_finalizers();
238 }
239
240
241 void gc_finalize_all()
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()
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  */