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