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 2896 2005-07-04 20:38:33Z twisti $
35 #if defined(USE_THREADS) && defined(NATIVE_THREADS) && defined(__LINUX__)
36 #define GC_LINUX_THREADS
38 #if defined(USE_THREADS) && defined(NATIVE_THREADS) && defined(__IRIX__)
39 #define GC_IRIX_THREADS
42 #include "boehm-gc/include/gc.h"
45 #if defined(USE_THREADS)
46 # if defined(NATIVE_THREADS)
47 # include "threads/native/threads.h"
49 # include "threads/green/threads.h"
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"
64 static bool in_gc_out_of_memory = false; /* is GC out of memory? */
69 __attribute__ ((unused))
71 *stackcall_twoargs(struct otherstackcall *p)
73 return (*p->p2)(p->p, p->l);
77 static void *stackcall_malloc(void *p, u4 bytelength)
79 return GC_MALLOC(bytelength);
83 static void *stackcall_malloc_atomic(void *p, u4 bytelength)
85 return GC_MALLOC_ATOMIC(bytelength);
89 static void *stackcall_malloc_uncollectable(void *p, u4 bytelength)
91 return GC_MALLOC_UNCOLLECTABLE(bytelength);
95 static void *stackcall_realloc(void *p, u4 bytelength)
97 return GC_REALLOC(p, bytelength);
100 static void *stackcall_free(void *p, u4 bytelength)
107 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
108 #define MAINTHREADCALL(r,m,pp,ll) \
109 if (currentThread == NULL || currentThread == mainThread) { \
112 struct otherstackcall sc; \
116 r = (*asm_switchstackandcall)(CONTEXT(mainThread).usedStackTop, \
118 (void**)&(CONTEXT(currentThread).usedStackTop), &sc); \
121 #define MAINTHREADCALL(r,m,pp,ll) \
126 void *heap_alloc_uncollectable(u4 bytelength)
129 MAINTHREADCALL(result, stackcall_malloc_uncollectable, NULL, bytelength);
134 void runboehmfinalizer(void *o, void *p)
136 java_objectheader *ob = (java_objectheader *) o;
138 asm_calljavafunction(ob->vftbl->class->finalizer, ob, NULL, NULL, NULL);
140 /* if we had an exception in the finalizer, ignore it */
141 *exceptionptr = NULL;
145 void *heap_allocate(u4 bytelength, bool references, methodinfo *finalizer)
150 MAINTHREADCALL(result, stackcall_malloc, NULL, bytelength);
153 MAINTHREADCALL(result, stackcall_malloc_atomic, NULL, bytelength);
161 GC_REGISTER_FINALIZER(result, runboehmfinalizer, 0, 0, 0);
167 void *heap_reallocate(void *p, u4 bytelength)
171 MAINTHREADCALL(result, stackcall_realloc, p, bytelength);
176 void heap_free(void *p)
180 MAINTHREADCALL(result, stackcall_free, p, 0);
183 static void gc_ignore_warnings(char *msg, GC_word arg)
187 void gc_init(u4 heapmaxsize, u4 heapstartsize)
189 size_t heapcurrentsize;
193 /* set the maximal heap size */
194 GC_set_max_heap_size(heapmaxsize);
196 /* set the initial heap size */
197 heapcurrentsize = GC_get_heap_size();
198 if (heapstartsize > heapcurrentsize) {
199 GC_expand_hp(heapstartsize - heapcurrentsize);
202 /* define OOM function */
203 GC_oom_fn = gc_out_of_memory;
205 /* suppress warnings */
206 GC_set_warn_proc(gc_ignore_warnings);
213 dolog("Garbage Collection: previous/now = %d / %d ",
220 s8 gc_get_heap_size(void)
222 return GC_get_heap_size();
226 s8 gc_get_free_bytes(void)
228 return GC_get_free_bytes();
232 s8 gc_get_max_heap_size(void)
234 return GC_get_max_heap_size();
238 void gc_invoke_finalizers(void)
240 GC_invoke_finalizers();
244 void gc_finalize_all(void)
250 /* gc_out_of_memory ************************************************************
252 This function is called when boehm detects that it is OOM.
254 *******************************************************************************/
256 void *gc_out_of_memory(size_t bytes_requested)
258 /* if this happens, we are REALLY out of memory */
260 if (in_gc_out_of_memory) {
261 /* this is all we can do... */
262 throw_cacao_exception_exit(string_java_lang_InternalError,
266 in_gc_out_of_memory = true;
268 /* try to release some memory */
272 /* now instantiate the exception */
274 *exceptionptr = new_exception(string_java_lang_OutOfMemoryError);
276 in_gc_out_of_memory = false;
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 * ---------------------------------------------------------------------
289 * indent-tabs-mode: t