* src/mm/boehm.h: Renamed to gc-common.h
[cacao.git] / src / mm / boehm.c
1 /* src/mm/boehm.c - interface for boehm gc
2
3    Copyright (C) 1996-2005, 2006 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    Contact: cacao@cacaojvm.org
26
27    Authors: Stefan Ring
28
29    Changes: Christian Thalinger
30
31    $Id: boehm.c 5900 2006-11-04 17:30:44Z michi $
32
33 */
34
35
36 #include "config.h"
37 #include "vm/types.h"
38
39 #if defined(ENABLE_THREADS) && defined(__LINUX__)
40 #define GC_LINUX_THREADS
41 #endif
42 #if defined(ENABLE_THREADS) && defined(__IRIX__)
43 #define GC_IRIX_THREADS
44 #endif
45
46 #include "boehm-gc/include/gc.h"
47 #include "mm/gc-common.h"
48 #include "mm/memory.h"
49
50 #if defined(ENABLE_THREADS)
51 # include "threads/native/threads.h"
52 #endif
53
54 #include "toolbox/logging.h"
55 #include "vm/options.h"
56 #include "vm/builtin.h"
57 #include "vm/exceptions.h"
58 #include "vm/finalizer.h"
59 #include "vm/global.h"
60 #include "vm/loader.h"
61 #include "vm/stringlocal.h"
62
63
64 /* global variables ***********************************************************/
65
66 static bool in_gc_out_of_memory = false;    /* is GC out of memory?           */
67
68
69 /* prototype static functions *************************************************/
70
71 static void gc_ignore_warnings(char *msg, GC_word arg);
72
73
74 /* gc_init *********************************************************************
75
76    Initializes the boehm garbage collector.
77
78 *******************************************************************************/
79
80 void gc_init(u4 heapmaxsize, u4 heapstartsize)
81 {
82         size_t heapcurrentsize;
83
84         GC_INIT();
85
86         /* set the maximal heap size */
87
88         GC_set_max_heap_size(heapmaxsize);
89
90         /* set the initial heap size */
91
92         heapcurrentsize = GC_get_heap_size();
93
94         if (heapstartsize > heapcurrentsize) {
95                 GC_expand_hp(heapstartsize - heapcurrentsize);
96         }
97
98         /* define OOM function */
99
100         GC_oom_fn = gc_out_of_memory;
101
102         /* just to be sure (should be set to 1 by JAVA_FINALIZATION macro) */
103
104         GC_java_finalization = 1;
105
106         /* suppress warnings */
107
108         GC_set_warn_proc(gc_ignore_warnings);
109
110         /* install a GC notifier */
111
112         GC_finalize_on_demand = 1;
113         GC_finalizer_notifier = finalizer_notify;
114 }
115
116
117 static void gc_ignore_warnings(char *msg, GC_word arg)
118 {
119 }
120
121
122 void *heap_alloc_uncollectable(u4 bytelength)
123 {
124         void *p;
125
126         p = GC_MALLOC_UNCOLLECTABLE(bytelength);
127
128         /* clear allocated memory region */
129
130         MSET(p, 0, u1, bytelength);
131
132         return p;
133 }
134
135
136 /* heap_allocate ***************************************************************
137
138    Allocates memory on the Java heap.
139
140 *******************************************************************************/
141
142 void *heap_allocate(u4 bytelength, u4 references, methodinfo *finalizer)
143 {
144         void *p;
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(bytelength);
151         else
152                 p = GC_MALLOC_ATOMIC(bytelength);
153
154         if (p == NULL)
155                 return NULL;
156
157         if (finalizer != NULL)
158                 GC_REGISTER_FINALIZER(p, finalizer_run, 0, 0, 0);
159
160         /* clear allocated memory region */
161
162         MSET(p, 0, u1, bytelength);
163
164         return p;
165 }
166
167
168 void heap_free(void *p)
169 {
170         GC_FREE(p);
171 }
172
173 void gc_call(void)
174 {
175         if (opt_verbosegc)
176                 dolog("Garbage Collection:  previous/now = %d / %d ",
177                           0, 0);
178
179         GC_gcollect();
180 }
181
182
183 s8 gc_get_heap_size(void)
184 {
185         return GC_get_heap_size();
186 }
187
188
189 s8 gc_get_free_bytes(void)
190 {
191         return GC_get_free_bytes();
192 }
193
194
195 /* gc_get_total_bytes **********************************************************
196
197    Returns the number of total bytes currently used on the Java heap.
198
199 *******************************************************************************/
200
201 s8 gc_get_total_bytes(void)
202 {
203         return GC_get_total_bytes();
204 }
205
206
207 s8 gc_get_max_heap_size(void)
208 {
209         return GC_get_max_heap_size();
210 }
211
212
213 void gc_invoke_finalizers(void)
214 {
215         GC_invoke_finalizers();
216 }
217
218
219 void gc_finalize_all(void)
220 {
221         GC_finalize_all();
222 }
223
224
225 /* gc_out_of_memory ************************************************************
226
227    This function is called when boehm detects that it is OOM.
228
229 *******************************************************************************/
230
231 void *gc_out_of_memory(size_t bytes_requested)
232 {
233         /* if this happens, we are REALLY out of memory */
234
235         if (in_gc_out_of_memory) {
236                 /* this is all we can do... */
237                 exceptions_throw_outofmemory_exit();
238         }
239
240         in_gc_out_of_memory = true;
241
242         /* try to release some memory */
243
244         gc_call();
245
246         /* now instantiate the exception */
247
248         *exceptionptr = new_exception(string_java_lang_OutOfMemoryError);
249
250         in_gc_out_of_memory = false;
251
252         return NULL;
253 }
254
255
256 /*
257  * These are local overrides for various environment variables in Emacs.
258  * Please do not remove this and leave it at the end of the file, where
259  * Emacs will automagically detect them.
260  * ---------------------------------------------------------------------
261  * Local variables:
262  * mode: c
263  * indent-tabs-mode: t
264  * c-basic-offset: 4
265  * tab-width: 4
266  * End:
267  */