* Removed all Id tags.
[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
54
55 /* global variables ***********************************************************/
56
57 static bool in_gc_out_of_memory = false;    /* is GC out of memory?           */
58
59
60 /* prototype static functions *************************************************/
61
62 static void gc_ignore_warnings(char *msg, GC_word arg);
63
64
65 /* gc_init *********************************************************************
66
67    Initializes the boehm garbage collector.
68
69 *******************************************************************************/
70
71 void gc_init(u4 heapmaxsize, u4 heapstartsize)
72 {
73         size_t heapcurrentsize;
74
75         /* just to be sure (should be set to 1 by JAVA_FINALIZATION macro) */
76
77         GC_java_finalization = 1;
78
79         /* Ignore pointers that do not point to the start of an object. */
80
81         GC_all_interior_pointers = 0;
82
83         /* suppress warnings */
84
85         GC_set_warn_proc(gc_ignore_warnings);
86
87         /* install a GC notifier */
88
89         GC_finalize_on_demand = 1;
90         GC_finalizer_notifier = finalizer_notify;
91
92         /* define OOM function */
93
94         GC_oom_fn = gc_out_of_memory;
95
96         GC_INIT();
97
98         /* set the maximal heap size */
99
100         GC_set_max_heap_size(heapmaxsize);
101
102         /* set the initial heap size */
103
104         heapcurrentsize = GC_get_heap_size();
105
106         if (heapstartsize > heapcurrentsize)
107                 GC_expand_hp(heapstartsize - heapcurrentsize);
108 }
109
110
111 static void gc_ignore_warnings(char *msg, GC_word arg)
112 {
113 }
114
115
116 void *heap_alloc_uncollectable(u4 bytelength)
117 {
118         void *p;
119
120         p = GC_MALLOC_UNCOLLECTABLE(bytelength);
121
122         /* clear allocated memory region */
123
124         MSET(p, 0, u1, bytelength);
125
126         return p;
127 }
128
129
130 /* heap_allocate ***************************************************************
131
132    Allocates memory on the Java heap.
133
134 *******************************************************************************/
135
136 void *heap_allocate(u4 bytelength, u4 references, methodinfo *finalizer)
137 {
138         void *p;
139
140         /* We can't use a bool here for references, as it's passed as a
141            bitmask in builtin_new.  Thus we check for != 0. */
142
143         if (references != 0)
144                 p = GC_MALLOC(bytelength);
145         else
146                 p = GC_MALLOC_ATOMIC(bytelength);
147
148         if (p == NULL)
149                 return NULL;
150
151         if (finalizer != NULL)
152                 GC_REGISTER_FINALIZER_NO_ORDER(p, finalizer_run, 0, 0, 0);
153
154         /* clear allocated memory region */
155
156         MSET(p, 0, u1, bytelength);
157
158         return p;
159 }
160
161
162 void heap_free(void *p)
163 {
164         GC_FREE(p);
165 }
166
167 void gc_call(void)
168 {
169         if (opt_verbosegc)
170                 dolog("Garbage Collection:  previous/now = %d / %d ",
171                           0, 0);
172
173         GC_gcollect();
174 }
175
176
177 s8 gc_get_heap_size(void)
178 {
179         return GC_get_heap_size();
180 }
181
182
183 s8 gc_get_free_bytes(void)
184 {
185         return GC_get_free_bytes();
186 }
187
188
189 /* gc_get_total_bytes **********************************************************
190
191    Returns the number of total bytes currently used on the Java heap.
192
193 *******************************************************************************/
194
195 s8 gc_get_total_bytes(void)
196 {
197         return GC_get_total_bytes();
198 }
199
200
201 s8 gc_get_max_heap_size(void)
202 {
203         return GC_get_max_heap_size();
204 }
205
206
207 void gc_invoke_finalizers(void)
208 {
209         GC_invoke_finalizers();
210 }
211
212
213 void gc_finalize_all(void)
214 {
215         GC_finalize_all();
216 }
217
218
219 /* gc_out_of_memory ************************************************************
220
221    This function is called when boehm detects that it is OOM.
222
223 *******************************************************************************/
224
225 void *gc_out_of_memory(size_t bytes_requested)
226 {
227         /* if this happens, we are REALLY out of memory */
228
229         if (in_gc_out_of_memory) {
230                 /* this is all we can do... */
231                 vm_abort("gc_out_of_memory: out of memory");
232         }
233
234         in_gc_out_of_memory = true;
235
236         /* try to release some memory */
237
238         gc_call();
239
240         /* now instantiate the exception */
241
242         exceptions_throw_outofmemoryerror();
243
244         in_gc_out_of_memory = false;
245
246         return NULL;
247 }
248
249
250 /*
251  * These are local overrides for various environment variables in Emacs.
252  * Please do not remove this and leave it at the end of the file, where
253  * Emacs will automagically detect them.
254  * ---------------------------------------------------------------------
255  * Local variables:
256  * mode: c
257  * indent-tabs-mode: t
258  * c-basic-offset: 4
259  * tab-width: 4
260  * End:
261  */