* src/vmcore/rt-timing.c: Added rt-timing for heap allocation time.
[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 7443 2007-03-03 00:20:18Z 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         GC_INIT();
79
80         /* set the maximal heap size */
81
82         GC_set_max_heap_size(heapmaxsize);
83
84         /* set the initial heap size */
85
86         heapcurrentsize = GC_get_heap_size();
87
88         if (heapstartsize > heapcurrentsize) {
89                 GC_expand_hp(heapstartsize - heapcurrentsize);
90         }
91
92         /* define OOM function */
93
94         GC_oom_fn = gc_out_of_memory;
95
96         /* just to be sure (should be set to 1 by JAVA_FINALIZATION macro) */
97
98         GC_java_finalization = 1;
99
100         /* suppress warnings */
101
102         GC_set_warn_proc(gc_ignore_warnings);
103
104         /* install a GC notifier */
105
106         GC_finalize_on_demand = 1;
107         GC_finalizer_notifier = finalizer_notify;
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 #if defined(ENABLE_RT_TIMING)
140         struct timespec time_start, time_end;
141 #endif
142
143         RT_TIMING_GET_TIME(time_start);
144
145         /* We can't use a bool here for references, as it's passed as a
146            bitmask in builtin_new.  Thus we check for != 0. */
147
148         if (references != 0)
149                 p = GC_MALLOC(bytelength);
150         else
151                 p = GC_MALLOC_ATOMIC(bytelength);
152
153         if (p == NULL)
154                 return NULL;
155
156         if (finalizer != NULL)
157                 GC_REGISTER_FINALIZER(p, finalizer_run, 0, 0, 0);
158
159         /* clear allocated memory region */
160
161         MSET(p, 0, u1, bytelength);
162
163         RT_TIMING_GET_TIME(time_end);
164         RT_TIMING_TIME_DIFF(time_start, time_end, RT_TIMING_GC_ALLOC);
165
166         return p;
167 }
168
169
170 void heap_free(void *p)
171 {
172         GC_FREE(p);
173 }
174
175 void gc_call(void)
176 {
177         if (opt_verbosegc)
178                 dolog("Garbage Collection:  previous/now = %d / %d ",
179                           0, 0);
180
181         GC_gcollect();
182 }
183
184
185 s8 gc_get_heap_size(void)
186 {
187         return GC_get_heap_size();
188 }
189
190
191 s8 gc_get_free_bytes(void)
192 {
193         return GC_get_free_bytes();
194 }
195
196
197 /* gc_get_total_bytes **********************************************************
198
199    Returns the number of total bytes currently used on the Java heap.
200
201 *******************************************************************************/
202
203 s8 gc_get_total_bytes(void)
204 {
205         return GC_get_total_bytes();
206 }
207
208
209 s8 gc_get_max_heap_size(void)
210 {
211         return GC_get_max_heap_size();
212 }
213
214
215 void gc_invoke_finalizers(void)
216 {
217         GC_invoke_finalizers();
218 }
219
220
221 void gc_finalize_all(void)
222 {
223         GC_finalize_all();
224 }
225
226
227 /* gc_out_of_memory ************************************************************
228
229    This function is called when boehm detects that it is OOM.
230
231 *******************************************************************************/
232
233 void *gc_out_of_memory(size_t bytes_requested)
234 {
235         /* if this happens, we are REALLY out of memory */
236
237         if (in_gc_out_of_memory) {
238                 /* this is all we can do... */
239                 vm_abort("gc_out_of_memory: out of memory");
240         }
241
242         in_gc_out_of_memory = true;
243
244         /* try to release some memory */
245
246         gc_call();
247
248         /* now instantiate the exception */
249
250         exceptions_throw_outofmemoryerror();
251
252         in_gc_out_of_memory = false;
253
254         return NULL;
255 }
256
257
258 /*
259  * These are local overrides for various environment variables in Emacs.
260  * Please do not remove this and leave it at the end of the file, where
261  * Emacs will automagically detect them.
262  * ---------------------------------------------------------------------
263  * Local variables:
264  * mode: c
265  * indent-tabs-mode: t
266  * c-basic-offset: 4
267  * tab-width: 4
268  * End:
269  */