1 /* mm/boehm.c - interface for boehm gc
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
8 This file is part of CACAO.
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.
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.
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
25 Contact: cacao@complang.tuwien.ac.at
29 $Id: boehm.c 1621 2004-11-30 13:06:55Z twisti $
33 #if defined(USE_THREADS) && defined(NATIVE_THREADS) && defined(__LINUX__)
34 #define GC_LINUX_THREADS
36 #if defined(USE_THREADS) && defined(NATIVE_THREADS) && defined(__IRIX__)
37 #define GC_IRIX_THREADS
40 #include "boehm-gc/include/gc.h"
43 #if defined(USE_THREADS)
44 # if defined(NATIVE_THREADS)
45 # include "threads/native/threads.h"
47 # include "threads/green/threads.h"
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"
61 static bool in_gc_out_of_memory = false; /* is GC out of memory? */
66 __attribute__ ((unused))
68 *stackcall_twoargs(struct otherstackcall *p)
70 return (*p->p2)(p->p, p->l);
74 static void *stackcall_malloc(void *p, u4 bytelength)
76 return GC_MALLOC(bytelength);
80 static void *stackcall_malloc_atomic(void *p, u4 bytelength)
82 return GC_MALLOC_ATOMIC(bytelength);
86 static void *stackcall_malloc_uncollectable(void *p, u4 bytelength)
88 return GC_MALLOC_UNCOLLECTABLE(bytelength);
92 static void *stackcall_realloc(void *p, u4 bytelength)
94 return GC_REALLOC(p, bytelength);
97 static void *stackcall_free(void *p, u4 bytelength)
104 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
105 #define MAINTHREADCALL(r,m,pp,ll) \
106 if (currentThread == NULL || currentThread == mainThread) { \
109 struct otherstackcall sc; \
113 r = (*asm_switchstackandcall)(CONTEXT(mainThread).usedStackTop, \
115 (void**)&(CONTEXT(currentThread).usedStackTop), &sc); \
118 #define MAINTHREADCALL(r,m,pp,ll) \
123 void *heap_alloc_uncollectable(u4 bytelength)
126 MAINTHREADCALL(result, stackcall_malloc_uncollectable, NULL, bytelength);
131 void runboehmfinalizer(void *o, void *p)
133 java_objectheader *ob = (java_objectheader *) o;
135 asm_calljavafunction(ob->vftbl->class->finalizer, ob, NULL, NULL, NULL);
137 /* if we had an exception in the finalizer, ignore it */
138 *exceptionptr = NULL;
142 void *heap_allocate(u4 bytelength, bool references, methodinfo *finalizer)
147 MAINTHREADCALL(result, stackcall_malloc, NULL, bytelength);
150 MAINTHREADCALL(result, stackcall_malloc_atomic, NULL, bytelength);
158 GC_REGISTER_FINALIZER(result, runboehmfinalizer, 0, 0, 0);
164 void *heap_reallocate(void *p, u4 bytelength)
168 MAINTHREADCALL(result, stackcall_realloc, p, bytelength);
173 void heap_free(void *p)
177 MAINTHREADCALL(result, stackcall_free, p, 0);
180 static void gc_ignore_warnings(char *msg, GC_word arg)
184 void gc_init(u4 heapmaxsize, u4 heapstartsize)
186 size_t heapcurrentsize;
190 /* set the maximal heap size */
191 GC_set_max_heap_size(heapmaxsize);
193 /* set the initial heap size */
194 heapcurrentsize = GC_get_heap_size();
195 if (heapstartsize > heapcurrentsize) {
196 GC_expand_hp(heapstartsize - heapcurrentsize);
199 /* define OOM function */
200 GC_oom_fn = gc_out_of_memory;
202 /* suppress warnings */
203 GC_set_warn_proc(gc_ignore_warnings);
210 dolog("Garbage Collection: previous/now = %d / %d ",
217 s8 gc_get_heap_size()
219 return GC_get_heap_size();
223 s8 gc_get_free_bytes()
225 return GC_get_free_bytes();
229 s8 gc_get_max_heap_size()
231 return GC_get_max_heap_size();
235 void gc_invoke_finalizers()
237 GC_invoke_finalizers();
241 void gc_finalize_all()
247 /* gc_out_of_memory ************************************************************
249 This function is called when boehm detects that it is OOM.
251 *******************************************************************************/
253 void *gc_out_of_memory()
255 /* if this happens, we are REALLY out of memory */
257 if (in_gc_out_of_memory) {
258 /* this is all we can do... */
259 throw_cacao_exception_exit(string_java_lang_InternalError,
263 in_gc_out_of_memory = true;
265 /* try to release some memory */
269 /* now instantiate the exception */
271 *exceptionptr = new_exception(string_java_lang_OutOfMemoryError);
273 in_gc_out_of_memory = false;
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 * ---------------------------------------------------------------------
286 * indent-tabs-mode: t