1 /* src/mm/boehm.c - interface for boehm gc
3 Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
4 C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5 E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6 J. Wenninger, 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., 51 Franklin Street, Fifth Floor, Boston, MA
25 Contact: cacao@cacaojvm.org
29 Changes: Christian Thalinger
31 $Id: boehm.c 4357 2006-01-22 23:33:38Z 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/jit/asmpart.h"
69 /* global variables ***********************************************************/
71 static bool in_gc_out_of_memory = false; /* is GC out of memory? */
76 __attribute__ ((unused))
78 *stackcall_twoargs(struct otherstackcall *p)
80 return (*p->p2)(p->p, p->l);
83 /* prototype static functions *************************************************/
85 static void gc_ignore_warnings(char *msg, GC_word arg);
88 /* gc_init *********************************************************************
90 Initializes the boehm garbage collector.
92 *******************************************************************************/
94 void gc_init(u4 heapmaxsize, u4 heapstartsize)
96 size_t heapcurrentsize;
100 /* set the maximal heap size */
102 GC_set_max_heap_size(heapmaxsize);
104 /* set the initial heap size */
106 heapcurrentsize = GC_get_heap_size();
108 if (heapstartsize > heapcurrentsize) {
109 GC_expand_hp(heapstartsize - heapcurrentsize);
112 /* define OOM function */
114 GC_oom_fn = gc_out_of_memory;
116 /* just to be sure (should be set to 1 by JAVA_FINALIZATION macro) */
118 GC_java_finalization = 1;
120 /* suppress warnings */
122 GC_set_warn_proc(gc_ignore_warnings);
124 /* install a GC notifier */
126 GC_finalize_on_demand = 1;
127 GC_finalizer_notifier = finalizer_notify;
131 static void gc_ignore_warnings(char *msg, GC_word arg)
135 static void *stackcall_malloc(void *p, u4 bytelength)
137 return GC_MALLOC(bytelength);
141 static void *stackcall_malloc_atomic(void *p, u4 bytelength)
143 return GC_MALLOC_ATOMIC(bytelength);
147 static void *stackcall_malloc_uncollectable(void *p, u4 bytelength)
149 return GC_MALLOC_UNCOLLECTABLE(bytelength);
153 static void *stackcall_free(void *p, u4 bytelength)
160 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
161 #define MAINTHREADCALL(r,m,pp,ll) \
162 if (currentThread == NULL || currentThread == mainThread) { \
165 struct otherstackcall sc; \
169 r = (*asm_switchstackandcall)(CONTEXT(mainThread).usedStackTop, \
171 (void**)&(CONTEXT(currentThread).usedStackTop), &sc); \
174 #define MAINTHREADCALL(r,m,pp,ll) \
179 void *heap_alloc_uncollectable(u4 bytelength)
183 MAINTHREADCALL(result, stackcall_malloc_uncollectable, NULL, bytelength);
185 /* clear allocated memory region */
187 MSET(result, 0, u1, bytelength);
193 void *heap_allocate(u4 bytelength, bool references, methodinfo *finalizer)
198 MAINTHREADCALL(result, stackcall_malloc, NULL, bytelength);
201 MAINTHREADCALL(result, stackcall_malloc_atomic, NULL, bytelength);
208 GC_REGISTER_FINALIZER(result, finalizer_run, 0, 0, 0);
210 /* clear allocated memory region */
212 MSET(result, 0, u1, bytelength);
214 return (u1 *) result;
218 void heap_free(void *p)
222 MAINTHREADCALL(result, stackcall_free, p, 0);
228 dolog("Garbage Collection: previous/now = %d / %d ",
235 s8 gc_get_heap_size(void)
237 return GC_get_heap_size();
241 s8 gc_get_free_bytes(void)
243 return GC_get_free_bytes();
247 s8 gc_get_max_heap_size(void)
249 return GC_get_max_heap_size();
253 void gc_invoke_finalizers(void)
255 GC_invoke_finalizers();
259 void gc_finalize_all(void)
265 /* gc_out_of_memory ************************************************************
267 This function is called when boehm detects that it is OOM.
269 *******************************************************************************/
271 void *gc_out_of_memory(size_t bytes_requested)
273 /* if this happens, we are REALLY out of memory */
275 if (in_gc_out_of_memory) {
276 /* this is all we can do... */
277 exceptions_throw_outofmemory_exit();
280 in_gc_out_of_memory = true;
282 /* try to release some memory */
286 /* now instantiate the exception */
288 *exceptionptr = new_exception(string_java_lang_OutOfMemoryError);
290 in_gc_out_of_memory = false;
297 * These are local overrides for various environment variables in Emacs.
298 * Please do not remove this and leave it at the end of the file, where
299 * Emacs will automagically detect them.
300 * ---------------------------------------------------------------------
303 * indent-tabs-mode: t