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 5900 2006-11-04 17:30:44Z michi $
39 #if defined(ENABLE_THREADS) && defined(__LINUX__)
40 #define GC_LINUX_THREADS
42 #if defined(ENABLE_THREADS) && defined(__IRIX__)
43 #define GC_IRIX_THREADS
46 #include "boehm-gc/include/gc.h"
47 #include "mm/gc-common.h"
48 #include "mm/memory.h"
50 #if defined(ENABLE_THREADS)
51 # include "threads/native/threads.h"
54 #include "toolbox/logging.h"
55 #include "vm/options.h"
56 #include "vm/builtin.h"
57 #include "vm/exceptions.h"
58 #include "vm/finalizer.h"
59 #include "vm/global.h"
60 #include "vm/loader.h"
61 #include "vm/stringlocal.h"
64 /* global variables ***********************************************************/
66 static bool in_gc_out_of_memory = false; /* is GC out of memory? */
69 /* prototype static functions *************************************************/
71 static void gc_ignore_warnings(char *msg, GC_word arg);
74 /* gc_init *********************************************************************
76 Initializes the boehm garbage collector.
78 *******************************************************************************/
80 void gc_init(u4 heapmaxsize, u4 heapstartsize)
82 size_t heapcurrentsize;
86 /* set the maximal heap size */
88 GC_set_max_heap_size(heapmaxsize);
90 /* set the initial heap size */
92 heapcurrentsize = GC_get_heap_size();
94 if (heapstartsize > heapcurrentsize) {
95 GC_expand_hp(heapstartsize - heapcurrentsize);
98 /* define OOM function */
100 GC_oom_fn = gc_out_of_memory;
102 /* just to be sure (should be set to 1 by JAVA_FINALIZATION macro) */
104 GC_java_finalization = 1;
106 /* suppress warnings */
108 GC_set_warn_proc(gc_ignore_warnings);
110 /* install a GC notifier */
112 GC_finalize_on_demand = 1;
113 GC_finalizer_notifier = finalizer_notify;
117 static void gc_ignore_warnings(char *msg, GC_word arg)
122 void *heap_alloc_uncollectable(u4 bytelength)
126 p = GC_MALLOC_UNCOLLECTABLE(bytelength);
128 /* clear allocated memory region */
130 MSET(p, 0, u1, bytelength);
136 /* heap_allocate ***************************************************************
138 Allocates memory on the Java heap.
140 *******************************************************************************/
142 void *heap_allocate(u4 bytelength, u4 references, methodinfo *finalizer)
146 /* We can't use a bool here for references, as it's passed as a
147 bitmask in builtin_new. Thus we check for != 0. */
150 p = GC_MALLOC(bytelength);
152 p = GC_MALLOC_ATOMIC(bytelength);
157 if (finalizer != NULL)
158 GC_REGISTER_FINALIZER(p, finalizer_run, 0, 0, 0);
160 /* clear allocated memory region */
162 MSET(p, 0, u1, bytelength);
168 void heap_free(void *p)
176 dolog("Garbage Collection: previous/now = %d / %d ",
183 s8 gc_get_heap_size(void)
185 return GC_get_heap_size();
189 s8 gc_get_free_bytes(void)
191 return GC_get_free_bytes();
195 /* gc_get_total_bytes **********************************************************
197 Returns the number of total bytes currently used on the Java heap.
199 *******************************************************************************/
201 s8 gc_get_total_bytes(void)
203 return GC_get_total_bytes();
207 s8 gc_get_max_heap_size(void)
209 return GC_get_max_heap_size();
213 void gc_invoke_finalizers(void)
215 GC_invoke_finalizers();
219 void gc_finalize_all(void)
225 /* gc_out_of_memory ************************************************************
227 This function is called when boehm detects that it is OOM.
229 *******************************************************************************/
231 void *gc_out_of_memory(size_t bytes_requested)
233 /* if this happens, we are REALLY out of memory */
235 if (in_gc_out_of_memory) {
236 /* this is all we can do... */
237 exceptions_throw_outofmemory_exit();
240 in_gc_out_of_memory = true;
242 /* try to release some memory */
246 /* now instantiate the exception */
248 *exceptionptr = new_exception(string_java_lang_OutOfMemoryError);
250 in_gc_out_of_memory = false;
257 * These are local overrides for various environment variables in Emacs.
258 * Please do not remove this and leave it at the end of the file, where
259 * Emacs will automagically detect them.
260 * ---------------------------------------------------------------------
263 * indent-tabs-mode: t