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