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