Merged trunk and subtype.
[cacao.git] / src / mm / gc.hpp
1 /* src/mm/gc.hpp - gc independant interface for heap managment
2
3    Copyright (C) 1996-2005, 2006, 2007, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #ifndef _GC_HPP
27 #define _GC_HPP
28
29 #include "config.h"
30
31 #include <assert.h>
32 #include <stdint.h>
33
34 #if defined(ENABLE_GC_CACAO)
35 # include "threads/thread.hpp"
36 #endif
37
38 #include "vm/global.h"
39 #include "vm/method.h"
40
41
42 #ifdef __cplusplus
43
44 class GC {
45 public:
46         // Critical section functions.
47         static void critical_enter(void);
48         static void critical_leave(void);
49 };
50
51 extern "C" {
52 #endif
53
54
55 /* reference types ************************************************************/
56
57 enum {
58         GC_REFTYPE_THREADOBJECT,
59         GC_REFTYPE_CLASSLOADER,
60         GC_REFTYPE_JNI_GLOBALREF,
61         GC_REFTYPE_FINALIZER,
62         GC_REFTYPE_LOCALREF,
63         GC_REFTYPE_STACK,
64         GC_REFTYPE_CLASSREF,
65         GC_REFTYPE_LOCKRECORD
66 };
67
68
69 /* function prototypes ********************************************************/
70
71 void    gc_init(size_t heapmaxsize, size_t heapstartsize);
72
73 void*   heap_alloc_uncollectable(size_t size);
74 void*   heap_alloc(size_t size, int references, methodinfo *finalizer, bool collect);
75 void    heap_free(void *p);
76
77 #if defined(ENABLE_GC_CACAO)
78 void    heap_init_objectheader(java_object_t *o, uint32_t size);
79 int32_t heap_get_hashcode(java_object_t *o);
80
81 void    gc_reference_register(java_object_t **ref, int32_t reftype);
82 void    gc_reference_unregister(java_object_t **ref);
83
84 void    gc_weakreference_register(java_object_t **ref, int32_t reftype);
85 void    gc_weakreference_unregister(java_object_t **ref);
86 #endif
87
88 void    gc_call(void);
89 int64_t gc_get_heap_size(void);
90 int64_t gc_get_free_bytes(void);
91 int64_t gc_get_total_bytes(void);
92 int64_t gc_get_max_heap_size(void);
93 void    gc_invoke_finalizers(void);
94 void    gc_finalize_all(void);
95 void*   gc_out_of_memory(size_t bytes_requested);
96
97
98 /* inlined functions **********************************************************/
99
100 static inline int32_t heap_hashcode(java_object_t *obj)
101 {
102 #if defined(ENABLE_GC_CACAO)
103         return heap_get_hashcode(obj);
104 #else
105         return (int32_t)(intptr_t) obj;
106 #endif
107 }
108
109 #ifdef __cplusplus
110 }
111
112 /**
113  * Enters a LLNI critical section which prevents the GC from moving
114  * objects around on the collected heap.
115  *
116  * There are no race conditions possible while entering such a critical
117  * section, because each thread only modifies its own thread local flag
118  * and the GC reads the flags while the world is stopped.
119  */
120 inline void GC::critical_enter()
121 {
122 #if defined(ENABLE_GC_CACAO)
123         threadobject *t;
124
125         t = THREADOBJECT;
126         assert(!t->gc_critical);
127         t->gc_critical = true;
128 #endif
129 }
130
131 /**
132  * Leaves a LLNI critical section and allows the GC to move objects
133  * around on the collected heap again.
134  */
135 inline void GC::critical_leave()
136 {
137 #if defined(ENABLE_GC_CACAO)
138         threadobject *t;
139
140         t = THREADOBJECT;
141         assert(t->gc_critical);
142         t->gc_critical = false;
143 #endif
144 }
145
146 #endif
147
148 #endif /* _GC_HPP */
149
150
151 /*
152  * These are local overrides for various environment variables in Emacs.
153  * Please do not remove this and leave it at the end of the file, where
154  * Emacs will automagically detect them.
155  * ---------------------------------------------------------------------
156  * Local variables:
157  * mode: c
158  * indent-tabs-mode: t
159  * c-basic-offset: 4
160  * tab-width: 4
161  * End:
162  */