1 /* src/mm/boehm.c - interface for boehm gc
3 Copyright (C) 1996-2005, 2006, 2007 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 $Id: boehm.c 7355 2007-02-14 10:57:32Z twisti $
33 #if defined(ENABLE_THREADS) && defined(__LINUX__)
34 #define GC_LINUX_THREADS
36 #if defined(ENABLE_THREADS) && defined(__IRIX__)
37 #define GC_IRIX_THREADS
40 #include "boehm-gc/include/gc.h"
41 #include "mm/gc-common.h"
42 #include "mm/memory.h"
44 #include "toolbox/logging.h"
46 #include "vm/builtin.h"
47 #include "vm/exceptions.h"
48 #include "vm/finalizer.h"
49 #include "vm/global.h"
50 #include "vm/stringlocal.h"
53 #include "vmcore/loader.h"
54 #include "vmcore/options.h"
57 /* global variables ***********************************************************/
59 static bool in_gc_out_of_memory = false; /* is GC out of memory? */
62 /* prototype static functions *************************************************/
64 static void gc_ignore_warnings(char *msg, GC_word arg);
67 /* gc_init *********************************************************************
69 Initializes the boehm garbage collector.
71 *******************************************************************************/
73 void gc_init(u4 heapmaxsize, u4 heapstartsize)
75 size_t heapcurrentsize;
79 /* set the maximal heap size */
81 GC_set_max_heap_size(heapmaxsize);
83 /* set the initial heap size */
85 heapcurrentsize = GC_get_heap_size();
87 if (heapstartsize > heapcurrentsize) {
88 GC_expand_hp(heapstartsize - heapcurrentsize);
91 /* define OOM function */
93 GC_oom_fn = gc_out_of_memory;
95 /* just to be sure (should be set to 1 by JAVA_FINALIZATION macro) */
97 GC_java_finalization = 1;
99 /* suppress warnings */
101 GC_set_warn_proc(gc_ignore_warnings);
103 /* install a GC notifier */
105 GC_finalize_on_demand = 1;
106 GC_finalizer_notifier = finalizer_notify;
110 static void gc_ignore_warnings(char *msg, GC_word arg)
115 void *heap_alloc_uncollectable(u4 bytelength)
119 p = GC_MALLOC_UNCOLLECTABLE(bytelength);
121 /* clear allocated memory region */
123 MSET(p, 0, u1, bytelength);
129 /* heap_allocate ***************************************************************
131 Allocates memory on the Java heap.
133 *******************************************************************************/
135 void *heap_allocate(u4 bytelength, u4 references, methodinfo *finalizer)
139 /* We can't use a bool here for references, as it's passed as a
140 bitmask in builtin_new. Thus we check for != 0. */
143 p = GC_MALLOC(bytelength);
145 p = GC_MALLOC_ATOMIC(bytelength);
150 if (finalizer != NULL)
151 GC_REGISTER_FINALIZER(p, finalizer_run, 0, 0, 0);
153 /* clear allocated memory region */
155 MSET(p, 0, u1, bytelength);
161 void heap_free(void *p)
169 dolog("Garbage Collection: previous/now = %d / %d ",
176 s8 gc_get_heap_size(void)
178 return GC_get_heap_size();
182 s8 gc_get_free_bytes(void)
184 return GC_get_free_bytes();
188 /* gc_get_total_bytes **********************************************************
190 Returns the number of total bytes currently used on the Java heap.
192 *******************************************************************************/
194 s8 gc_get_total_bytes(void)
196 return GC_get_total_bytes();
200 s8 gc_get_max_heap_size(void)
202 return GC_get_max_heap_size();
206 void gc_invoke_finalizers(void)
208 GC_invoke_finalizers();
212 void gc_finalize_all(void)
218 /* gc_out_of_memory ************************************************************
220 This function is called when boehm detects that it is OOM.
222 *******************************************************************************/
224 void *gc_out_of_memory(size_t bytes_requested)
226 /* if this happens, we are REALLY out of memory */
228 if (in_gc_out_of_memory) {
229 /* this is all we can do... */
230 vm_abort("gc_out_of_memory: out of memory");
233 in_gc_out_of_memory = true;
235 /* try to release some memory */
239 /* now instantiate the exception */
241 exceptions_throw_outofmemoryerror();
243 in_gc_out_of_memory = false;
250 * These are local overrides for various environment variables in Emacs.
251 * Please do not remove this and leave it at the end of the file, where
252 * Emacs will automagically detect them.
253 * ---------------------------------------------------------------------
256 * indent-tabs-mode: t