578d21947cffb8eedbdba8d6e16d678d97459c36
[cacao.git] / src / mm / cacao-gc / gc.c
1 /* src/mm/cacao-gc/gc.c - main garbage collector methods
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: Michael Starzinger
28
29    $Id$
30
31 */
32
33
34 #include "config.h"
35 #include <signal.h>
36 #include <stdlib.h>
37 #include "vm/types.h"
38
39 #if defined(ENABLE_THREADS)
40 # include "threads/native/threads.h"
41 #else
42 # include "threads/none/threads.h"
43 #endif
44
45 #include "gc.h"
46 #include "heap.h"
47 #include "mark.h"
48 #include "mm/memory.h"
49 #include "toolbox/logging.h"
50 #include "vm/exceptions.h"
51 #include "vm/options.h"
52
53
54 /* Development Break **********************************************************/
55
56 #if defined(ENABLE_THREADS)
57 # error "GC does not work with threads enabled!"
58 #endif
59
60 #if defined(ENABLE_INTRP)
61 # error "GC does not work with interpreter enabled!"
62 #endif
63
64 #if defined(ENABLE_JVMTI)
65 # error "GC does not work with JVMTI enabled!"
66 #endif
67
68
69 /* gc_init *********************************************************************
70
71    Initializes the garbage collector.
72
73 *******************************************************************************/
74
75 void gc_init(u4 heapmaxsize, u4 heapstartsize)
76 {
77         if (opt_verbosegc)
78                 dolog("GC: Initialising with heap-size %d (max. %d)",
79                         heapstartsize, heapmaxsize);
80
81         heap_base = malloc(heapstartsize);
82
83         if (heap_base == NULL)
84                 exceptions_throw_outofmemory_exit();
85
86         /* this is needed for linear allocation */
87         heap_ptr = heap_base;
88
89         heap_current_size = heapstartsize;
90         heap_maximal_size = heapmaxsize;
91         heap_free_size = heap_current_size;
92         heap_used_size = 0;
93 }
94
95
96 /* gc_call *********************************************************************
97
98    Forces a full collection of the whole Java Heap.
99    This is the function which is called by System.VMRuntime.gc()
100
101 *******************************************************************************/
102
103 void gc_call(void)
104 {
105         rootset_t *rs;
106         s4         dumpsize;
107
108         if (opt_verbosegc)
109                 dolog("GC: Forced Collection ...");
110
111         /* TODO: move the following to gc_collect() */
112
113         /* remember start of dump memory area */
114         dumpsize = dump_size();
115
116         GC_LOG( heap_println_usage(); );
117         /*GC_LOG( heap_dump_region(heap_base, heap_ptr, false); );*/
118
119         /* find the rootset for the current thread */
120         rs = DNEW(rootset_t);
121         mark_rootset_from_thread(THREADOBJECT, rs);
122
123         /* mark the objects considering the given rootset */
124         mark_me(rs);
125         GC_LOG( heap_dump_region(heap_base, heap_ptr, true); );
126
127         /* compact the heap */
128         /*compact_me(rs, heap_base, heap_ptr);*/
129
130         /* TODO: check my return value! */
131         /*heap_increase_size();*/
132
133     /* free dump memory area */
134     dump_release(dumpsize);
135
136         if (opt_verbosegc)
137                 dolog("GC: Forced Collection finished.");
138 }
139
140
141 /* Informational getter functions *********************************************/
142
143 s8 gc_get_heap_size(void)     { return heap_current_size; }
144 s8 gc_get_free_bytes(void)    { return heap_free_size; }
145 s8 gc_get_total_bytes(void)   { return heap_used_size; }
146 s8 gc_get_max_heap_size(void) { return heap_maximal_size; }
147
148
149 /* Thread specific stuff ******************************************************/
150
151 #if defined(ENABLE_THREADS)
152 int GC_signum1()
153 {
154         return SIGUSR1;
155 }
156
157 int GC_signum2()
158 {
159         return SIGUSR2;
160 }
161 #endif
162
163
164 /*
165  * These are local overrides for various environment variables in Emacs.
166  * Please do not remove this and leave it at the end of the file, where
167  * Emacs will automagically detect them.
168  * ---------------------------------------------------------------------
169  * Local variables:
170  * mode: c
171  * indent-tabs-mode: t
172  * c-basic-offset: 4
173  * tab-width: 4
174  * End:
175  * vim:noexpandtab:sw=4:ts=4:
176  */