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
31 #if defined(ENABLE_THREADS) && defined(__LINUX__)
32 #define GC_LINUX_THREADS
34 #if defined(ENABLE_THREADS) && defined(__IRIX__)
35 #define GC_IRIX_THREADS
38 #include "boehm-gc/include/gc.h"
39 #include "mm/gc-common.h"
40 #include "mm/memory.h"
42 #include "toolbox/logging.h"
44 #include "vm/builtin.h"
45 #include "vm/exceptions.h"
46 #include "vm/finalizer.h"
47 #include "vm/global.h"
48 #include "vm/stringlocal.h"
51 #include "vmcore/loader.h"
52 #include "vmcore/options.h"
53 #include "vmcore/rt-timing.h"
56 /* global variables ***********************************************************/
58 static bool in_gc_out_of_memory = false; /* is GC out of memory? */
61 /* prototype static functions *************************************************/
63 static void gc_ignore_warnings(char *msg, GC_word arg);
66 /* gc_init *********************************************************************
68 Initializes the boehm garbage collector.
70 *******************************************************************************/
72 void gc_init(u4 heapmaxsize, u4 heapstartsize)
74 size_t heapcurrentsize;
76 /* just to be sure (should be set to 1 by JAVA_FINALIZATION macro) */
78 GC_java_finalization = 1;
80 /* Ignore pointers that do not point to the start of an object. */
82 GC_all_interior_pointers = 0;
84 /* suppress warnings */
86 GC_set_warn_proc(gc_ignore_warnings);
88 /* install a GC notifier */
90 GC_finalize_on_demand = 1;
91 GC_finalizer_notifier = finalizer_notify;
93 /* define OOM function */
95 GC_oom_fn = gc_out_of_memory;
99 /* set the maximal heap size */
101 GC_set_max_heap_size(heapmaxsize);
103 /* set the initial heap size */
105 heapcurrentsize = GC_get_heap_size();
107 if (heapstartsize > heapcurrentsize)
108 GC_expand_hp(heapstartsize - heapcurrentsize);
112 static void gc_ignore_warnings(char *msg, GC_word arg)
117 void *heap_alloc_uncollectable(u4 size)
121 p = GC_MALLOC_UNCOLLECTABLE(size);
123 /* clear allocated memory region */
125 MSET(p, 0, u1, size);
131 /* heap_alloc ******************************************************************
133 Allocates memory on the Java heap.
135 *******************************************************************************/
137 void *heap_alloc(u4 size, u4 references, methodinfo *finalizer, bool collect)
140 #if defined(ENABLE_RT_TIMING)
141 struct timespec time_start, time_end;
144 RT_TIMING_GET_TIME(time_start);
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. */
152 p = GC_MALLOC_ATOMIC(size);
157 if (finalizer != NULL)
158 GC_REGISTER_FINALIZER_NO_ORDER(p, finalizer_run, 0, 0, 0);
160 /* clear allocated memory region */
162 MSET(p, 0, u1, size);
164 RT_TIMING_GET_TIME(time_end);
165 RT_TIMING_TIME_DIFF(time_start, time_end, RT_TIMING_GC_ALLOC);
171 void heap_free(void *p)
179 dolog("Garbage Collection: previous/now = %d / %d ",
186 s8 gc_get_heap_size(void)
188 return GC_get_heap_size();
192 s8 gc_get_free_bytes(void)
194 return GC_get_free_bytes();
198 /* gc_get_total_bytes **********************************************************
200 Returns the number of total bytes currently used on the Java heap.
202 *******************************************************************************/
204 s8 gc_get_total_bytes(void)
206 return GC_get_total_bytes();
210 s8 gc_get_max_heap_size(void)
212 return GC_get_max_heap_size();
216 void gc_invoke_finalizers(void)
218 GC_invoke_finalizers();
222 void gc_finalize_all(void)
228 /* gc_out_of_memory ************************************************************
230 This function is called when boehm detects that it is OOM.
232 *******************************************************************************/
234 void *gc_out_of_memory(size_t bytes_requested)
236 /* if this happens, we are REALLY out of memory */
238 if (in_gc_out_of_memory) {
239 /* this is all we can do... */
240 vm_abort("gc_out_of_memory: out of memory");
243 in_gc_out_of_memory = true;
245 /* try to release some memory */
249 /* now instantiate the exception */
251 exceptions_throw_outofmemoryerror();
253 in_gc_out_of_memory = false;
260 * These are local overrides for various environment variables in Emacs.
261 * Please do not remove this and leave it at the end of the file, where
262 * Emacs will automagically detect them.
263 * ---------------------------------------------------------------------
266 * indent-tabs-mode: t