* USE_CODEMMAP: 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 3388 2005-10-07 15:26:25Z 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         if (finalizer)
160                 GC_REGISTER_FINALIZER(result, runboehmfinalizer, 0, 0, 0);
161
162         /* clear allocated memory region */
163
164         MSET(result, 0, u1, bytelength);
165
166         return (u1 *) result;
167 }
168
169
170 void *heap_reallocate(void *p, u4 bytelength)
171 {
172         void *result;
173
174         MAINTHREADCALL(result, stackcall_realloc, p, bytelength);
175
176         return result;
177 }
178
179 void heap_free(void *p)
180 {
181         void *result;
182
183         MAINTHREADCALL(result, stackcall_free, p, 0);
184 }
185
186 static void gc_ignore_warnings(char *msg, GC_word arg)
187 {
188 }
189
190 void gc_init(u4 heapmaxsize, u4 heapstartsize)
191 {
192         size_t heapcurrentsize;
193
194         GC_INIT();
195
196         /* set the maximal heap size */
197         GC_set_max_heap_size(heapmaxsize);
198
199         /* set the initial heap size */
200         heapcurrentsize = GC_get_heap_size();
201         if (heapstartsize > heapcurrentsize) {
202                 GC_expand_hp(heapstartsize - heapcurrentsize);
203         }
204
205         /* define OOM function */
206         GC_oom_fn = gc_out_of_memory;
207
208         /* suppress warnings */
209         GC_set_warn_proc(gc_ignore_warnings);
210 }
211
212
213 void gc_call(void)
214 {
215         if (opt_verbosegc)
216                 dolog("Garbage Collection:  previous/now = %d / %d ",
217                           0, 0);
218
219         GC_gcollect();
220 }
221
222
223 s8 gc_get_heap_size(void)
224 {
225         return GC_get_heap_size();
226 }
227
228
229 s8 gc_get_free_bytes(void)
230 {
231         return GC_get_free_bytes();
232 }
233
234
235 s8 gc_get_max_heap_size(void)
236 {
237         return GC_get_max_heap_size();
238 }
239
240
241 void gc_invoke_finalizers(void)
242 {
243         GC_invoke_finalizers();
244 }
245
246
247 void gc_finalize_all(void)
248 {
249         GC_finalize_all();
250 }
251
252
253 /* gc_out_of_memory ************************************************************
254
255    This function is called when boehm detects that it is OOM.
256
257 *******************************************************************************/
258
259 void *gc_out_of_memory(size_t bytes_requested)
260 {
261         /* if this happens, we are REALLY out of memory */
262
263         if (in_gc_out_of_memory) {
264                 /* this is all we can do... */
265                 throw_cacao_exception_exit(string_java_lang_InternalError,
266                                                                    "Out of memory");
267         }
268
269         in_gc_out_of_memory = true;
270
271         /* try to release some memory */
272
273         gc_call();
274
275         /* now instantiate the exception */
276
277         *exceptionptr = new_exception(string_java_lang_OutOfMemoryError);
278
279         in_gc_out_of_memory = false;
280
281         return NULL;
282 }
283
284
285 /*
286  * These are local overrides for various environment variables in Emacs.
287  * Please do not remove this and leave it at the end of the file, where
288  * Emacs will automagically detect them.
289  * ---------------------------------------------------------------------
290  * Local variables:
291  * mode: c
292  * indent-tabs-mode: t
293  * c-basic-offset: 4
294  * tab-width: 4
295  * End:
296  */