* Merged with exact-gc branch at rev 2d4098fc2ea4.
[cacao.git] / src / mm / boehm.c
1 /* src/mm/boehm.c - interface for boehm gc
2
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
7
8    This file is part of CACAO.
9
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.
14
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.
19
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
23    02110-1301, USA.
24
25 */
26
27
28 #include "config.h"
29 #include "vm/types.h"
30
31 #if defined(ENABLE_THREADS) && defined(__LINUX__)
32 #define GC_LINUX_THREADS
33 #endif
34 #if defined(ENABLE_THREADS) && defined(__IRIX__)
35 #define GC_IRIX_THREADS
36 #endif
37
38 #include "boehm-gc/include/gc.h"
39 #include "mm/gc-common.h"
40 #include "mm/memory.h"
41
42 #include "toolbox/logging.h"
43
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"
49 #include "vm/vm.h"
50
51 #include "vmcore/loader.h"
52 #include "vmcore/options.h"
53 #include "vmcore/rt-timing.h"
54
55
56 /* global variables ***********************************************************/
57
58 static bool in_gc_out_of_memory = false;    /* is GC out of memory?           */
59
60
61 /* prototype static functions *************************************************/
62
63 static void gc_ignore_warnings(char *msg, GC_word arg);
64
65
66 /* gc_init *********************************************************************
67
68    Initializes the boehm garbage collector.
69
70 *******************************************************************************/
71
72 void gc_init(u4 heapmaxsize, u4 heapstartsize)
73 {
74         size_t heapcurrentsize;
75
76         /* just to be sure (should be set to 1 by JAVA_FINALIZATION macro) */
77
78         GC_java_finalization = 1;
79
80         /* Ignore pointers that do not point to the start of an object. */
81
82         GC_all_interior_pointers = 0;
83
84         /* suppress warnings */
85
86         GC_set_warn_proc(gc_ignore_warnings);
87
88         /* install a GC notifier */
89
90         GC_finalize_on_demand = 1;
91         GC_finalizer_notifier = finalizer_notify;
92
93         /* define OOM function */
94
95         GC_oom_fn = gc_out_of_memory;
96
97         GC_INIT();
98
99         /* set the maximal heap size */
100
101         GC_set_max_heap_size(heapmaxsize);
102
103         /* set the initial heap size */
104
105         heapcurrentsize = GC_get_heap_size();
106
107         if (heapstartsize > heapcurrentsize)
108                 GC_expand_hp(heapstartsize - heapcurrentsize);
109 }
110
111
112 static void gc_ignore_warnings(char *msg, GC_word arg)
113 {
114 }
115
116
117 void *heap_alloc_uncollectable(u4 size)
118 {
119         void *p;
120
121         p = GC_MALLOC_UNCOLLECTABLE(size);
122
123         /* clear allocated memory region */
124
125         MSET(p, 0, u1, size);
126
127         return p;
128 }
129
130
131 /* heap_alloc ******************************************************************
132
133    Allocates memory on the Java heap.
134
135 *******************************************************************************/
136
137 void *heap_alloc(u4 size, u4 references, methodinfo *finalizer, bool collect)
138 {
139         void *p;
140 #if defined(ENABLE_RT_TIMING)
141         struct timespec time_start, time_end;
142 #endif
143
144         RT_TIMING_GET_TIME(time_start);
145
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. */
148
149         if (references != 0)
150                 p = GC_MALLOC(size);
151         else
152                 p = GC_MALLOC_ATOMIC(size);
153
154         if (p == NULL)
155                 return NULL;
156
157         if (finalizer != NULL)
158                 GC_REGISTER_FINALIZER_NO_ORDER(p, finalizer_run, 0, 0, 0);
159
160         /* clear allocated memory region */
161
162         MSET(p, 0, u1, size);
163
164         RT_TIMING_GET_TIME(time_end);
165         RT_TIMING_TIME_DIFF(time_start, time_end, RT_TIMING_GC_ALLOC);
166
167         return p;
168 }
169
170
171 void heap_free(void *p)
172 {
173         GC_FREE(p);
174 }
175
176 void gc_call(void)
177 {
178         if (opt_verbosegc)
179                 dolog("Garbage Collection:  previous/now = %d / %d ",
180                           0, 0);
181
182         GC_gcollect();
183 }
184
185
186 s8 gc_get_heap_size(void)
187 {
188         return GC_get_heap_size();
189 }
190
191
192 s8 gc_get_free_bytes(void)
193 {
194         return GC_get_free_bytes();
195 }
196
197
198 /* gc_get_total_bytes **********************************************************
199
200    Returns the number of total bytes currently used on the Java heap.
201
202 *******************************************************************************/
203
204 s8 gc_get_total_bytes(void)
205 {
206         return GC_get_total_bytes();
207 }
208
209
210 s8 gc_get_max_heap_size(void)
211 {
212         return GC_get_max_heap_size();
213 }
214
215
216 void gc_invoke_finalizers(void)
217 {
218         GC_invoke_finalizers();
219 }
220
221
222 void gc_finalize_all(void)
223 {
224         GC_finalize_all();
225 }
226
227
228 /* gc_out_of_memory ************************************************************
229
230    This function is called when boehm detects that it is OOM.
231
232 *******************************************************************************/
233
234 void *gc_out_of_memory(size_t bytes_requested)
235 {
236         /* if this happens, we are REALLY out of memory */
237
238         if (in_gc_out_of_memory) {
239                 /* this is all we can do... */
240                 vm_abort("gc_out_of_memory: out of memory");
241         }
242
243         in_gc_out_of_memory = true;
244
245         /* try to release some memory */
246
247         gc_call();
248
249         /* now instantiate the exception */
250
251         exceptions_throw_outofmemoryerror();
252
253         in_gc_out_of_memory = false;
254
255         return NULL;
256 }
257
258
259 /*
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  * ---------------------------------------------------------------------
264  * Local variables:
265  * mode: c
266  * indent-tabs-mode: t
267  * c-basic-offset: 4
268  * tab-width: 4
269  * End:
270  */