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 8371 2007-08-20 22:05:08Z michi $
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"
55 #include "vmcore/rt-timing.h"
58 /* global variables ***********************************************************/
60 static bool in_gc_out_of_memory = false; /* is GC out of memory? */
63 /* prototype static functions *************************************************/
65 static void gc_ignore_warnings(char *msg, GC_word arg);
68 /* gc_init *********************************************************************
70 Initializes the boehm garbage collector.
72 *******************************************************************************/
74 void gc_init(u4 heapmaxsize, u4 heapstartsize)
76 size_t heapcurrentsize;
78 /* just to be sure (should be set to 1 by JAVA_FINALIZATION macro) */
80 GC_java_finalization = 1;
82 /* Ignore pointers that do not point to the start of an object. */
84 GC_all_interior_pointers = 0;
86 /* suppress warnings */
88 GC_set_warn_proc(gc_ignore_warnings);
90 /* install a GC notifier */
92 GC_finalize_on_demand = 1;
93 GC_finalizer_notifier = finalizer_notify;
95 /* define OOM function */
97 GC_oom_fn = gc_out_of_memory;
101 /* set the maximal heap size */
103 GC_set_max_heap_size(heapmaxsize);
105 /* set the initial heap size */
107 heapcurrentsize = GC_get_heap_size();
109 if (heapstartsize > heapcurrentsize)
110 GC_expand_hp(heapstartsize - heapcurrentsize);
114 static void gc_ignore_warnings(char *msg, GC_word arg)
119 void *heap_alloc_uncollectable(u4 size)
123 p = GC_MALLOC_UNCOLLECTABLE(size);
125 /* clear allocated memory region */
127 MSET(p, 0, u1, size);
133 /* heap_alloc ******************************************************************
135 Allocates memory on the Java heap.
137 *******************************************************************************/
139 void *heap_alloc(u4 size, u4 references, methodinfo *finalizer, bool collect)
142 #if defined(ENABLE_RT_TIMING)
143 struct timespec time_start, time_end;
146 RT_TIMING_GET_TIME(time_start);
148 /* We can't use a bool here for references, as it's passed as a
149 bitmask in builtin_new. Thus we check for != 0. */
154 p = GC_MALLOC_ATOMIC(size);
159 if (finalizer != NULL)
160 GC_REGISTER_FINALIZER_NO_ORDER(p, finalizer_run, 0, 0, 0);
162 /* clear allocated memory region */
164 MSET(p, 0, u1, size);
166 RT_TIMING_GET_TIME(time_end);
167 RT_TIMING_TIME_DIFF(time_start, time_end, RT_TIMING_GC_ALLOC);
173 void heap_free(void *p)
181 dolog("Garbage Collection: previous/now = %d / %d ",
188 s8 gc_get_heap_size(void)
190 return GC_get_heap_size();
194 s8 gc_get_free_bytes(void)
196 return GC_get_free_bytes();
200 /* gc_get_total_bytes **********************************************************
202 Returns the number of total bytes currently used on the Java heap.
204 *******************************************************************************/
206 s8 gc_get_total_bytes(void)
208 return GC_get_total_bytes();
212 s8 gc_get_max_heap_size(void)
214 return GC_get_max_heap_size();
218 void gc_invoke_finalizers(void)
220 GC_invoke_finalizers();
224 void gc_finalize_all(void)
230 /* gc_out_of_memory ************************************************************
232 This function is called when boehm detects that it is OOM.
234 *******************************************************************************/
236 void *gc_out_of_memory(size_t bytes_requested)
238 /* if this happens, we are REALLY out of memory */
240 if (in_gc_out_of_memory) {
241 /* this is all we can do... */
242 vm_abort("gc_out_of_memory: out of memory");
245 in_gc_out_of_memory = true;
247 /* try to release some memory */
251 /* now instantiate the exception */
253 exceptions_throw_outofmemoryerror();
255 in_gc_out_of_memory = false;
262 * These are local overrides for various environment variables in Emacs.
263 * Please do not remove this and leave it at the end of the file, where
264 * Emacs will automagically detect them.
265 * ---------------------------------------------------------------------
268 * indent-tabs-mode: t