1 /* src/mm/boehm.c - interface for boehm gc
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
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 Changes: Christian Thalinger
31 $Id: boehm.c 3551 2005-11-03 20:43:01Z twisti $
39 #if defined(USE_THREADS) && defined(NATIVE_THREADS) && defined(__LINUX__)
40 #define GC_LINUX_THREADS
42 #if defined(USE_THREADS) && defined(NATIVE_THREADS) && defined(__IRIX__)
43 #define GC_IRIX_THREADS
46 #include "boehm-gc/include/gc.h"
48 #include "mm/memory.h"
50 #if defined(USE_THREADS)
51 # if defined(NATIVE_THREADS)
52 # include "threads/native/threads.h"
54 # include "threads/green/threads.h"
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"
70 /* global variables ***********************************************************/
72 static bool in_gc_out_of_memory = false; /* is GC out of memory? */
77 __attribute__ ((unused))
79 *stackcall_twoargs(struct otherstackcall *p)
81 return (*p->p2)(p->p, p->l);
85 static void *stackcall_malloc(void *p, u4 bytelength)
87 return GC_MALLOC(bytelength);
91 static void *stackcall_malloc_atomic(void *p, u4 bytelength)
93 return GC_MALLOC_ATOMIC(bytelength);
97 static void *stackcall_malloc_uncollectable(void *p, u4 bytelength)
99 return GC_MALLOC_UNCOLLECTABLE(bytelength);
103 static void *stackcall_free(void *p, u4 bytelength)
110 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
111 #define MAINTHREADCALL(r,m,pp,ll) \
112 if (currentThread == NULL || currentThread == mainThread) { \
115 struct otherstackcall sc; \
119 r = (*asm_switchstackandcall)(CONTEXT(mainThread).usedStackTop, \
121 (void**)&(CONTEXT(currentThread).usedStackTop), &sc); \
124 #define MAINTHREADCALL(r,m,pp,ll) \
129 void *heap_alloc_uncollectable(u4 bytelength)
133 MAINTHREADCALL(result, stackcall_malloc_uncollectable, NULL, bytelength);
135 /* clear allocated memory region */
137 MSET(result, 0, u1, bytelength);
143 void *heap_allocate(u4 bytelength, bool references, methodinfo *finalizer)
148 MAINTHREADCALL(result, stackcall_malloc, NULL, bytelength);
151 MAINTHREADCALL(result, stackcall_malloc_atomic, NULL, bytelength);
158 GC_REGISTER_FINALIZER(result, finalizer_add, 0, 0, 0);
160 /* clear allocated memory region */
162 MSET(result, 0, u1, bytelength);
164 return (u1 *) result;
168 void heap_free(void *p)
172 MAINTHREADCALL(result, stackcall_free, p, 0);
175 static void gc_ignore_warnings(char *msg, GC_word arg)
179 void gc_init(u4 heapmaxsize, u4 heapstartsize)
181 size_t heapcurrentsize;
185 /* set the maximal heap size */
187 GC_set_max_heap_size(heapmaxsize);
189 /* set the initial heap size */
191 heapcurrentsize = GC_get_heap_size();
193 if (heapstartsize > heapcurrentsize) {
194 GC_expand_hp(heapstartsize - heapcurrentsize);
197 /* define OOM function */
199 GC_oom_fn = gc_out_of_memory;
201 /* suppress warnings */
203 GC_set_warn_proc(gc_ignore_warnings);
210 dolog("Garbage Collection: previous/now = %d / %d ",
217 s8 gc_get_heap_size(void)
219 return GC_get_heap_size();
223 s8 gc_get_free_bytes(void)
225 return GC_get_free_bytes();
229 s8 gc_get_max_heap_size(void)
231 return GC_get_max_heap_size();
235 void gc_invoke_finalizers(void)
237 GC_invoke_finalizers();
241 void gc_finalize_all(void)
247 /* gc_out_of_memory ************************************************************
249 This function is called when boehm detects that it is OOM.
251 *******************************************************************************/
253 void *gc_out_of_memory(size_t bytes_requested)
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