exceptionptr update for native threads
[cacao.git] / src / mm / boehm.c
1 /* mm/boehm.c - interface for boehm gc
2
3    Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
4    R. Grafl, A. Krall, C. Kruegel, C. Oates, R. Obermaisser,
5    M. Probst, S. Ring, E. Steiner, C. Thalinger, D. Thuernbeck,
6    P. Tomsich, J. Wenninger
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., 59 Temple Place - Suite 330, Boston, MA
23    02111-1307, USA.
24
25    Contact: cacao@complang.tuwien.ac.at
26
27    Authors: Stefan Ring
28
29    $Id: boehm.c 862 2004-01-06 23:42:01Z stefan $
30
31 */
32
33
34 #include "main.h"
35 #include "boehm.h"
36 #include "global.h"
37 #include "native.h"
38 #include "asmpart.h"
39 #include "builtin.h"
40 #include "threads/thread.h"
41 #include "toolbox/loging.h"
42
43 /* this is temporary workaround */
44 #if defined(__X86_64__)
45 #define GC_DEBUG
46 #endif
47
48 #include "gc.h"
49
50
51 static void *stackcall_twoargs(struct otherstackcall *p)
52 {
53         return (*p->p2)(p->p, p->l);
54 }
55
56
57 static void *stackcall_malloc(void *p, u4 bytelength)
58 {
59         return GC_MALLOC(bytelength);
60 }
61
62
63 static void *stackcall_malloc_atomic(void *p, u4 bytelength)
64 {
65         return GC_MALLOC_ATOMIC(bytelength);
66 }
67
68
69 static void *stackcall_malloc_uncollectable(void *p, u4 bytelength)
70 {
71         return GC_MALLOC_UNCOLLECTABLE(bytelength);
72 }
73
74
75 static void *stackcall_realloc(void *p, u4 bytelength)
76 {
77         return GC_REALLOC(p, bytelength);
78 }
79
80 static void *stackcall_free(void *p, u4 bytelength)
81 {
82         GC_FREE(p);
83         return NULL;
84 }
85
86
87 #ifdef USE_THREADS
88 #define MAINTHREADCALL(r,m,pp,ll) \
89         if (currentThread == NULL || currentThread == mainThread) { \
90                 r = m(pp, ll); \
91         } else { \
92                 struct otherstackcall sc; \
93                 sc.p2 = m; \
94                 sc.p = pp; \
95                 sc.l = ll; \
96                 r = (*asm_switchstackandcall)(CONTEXT(mainThread).usedStackTop, \
97                                 stackcall_twoargs, \
98                                 (void**)&(CONTEXT(currentThread).usedStackTop), &sc); \
99         }
100 #else
101 #define MAINTHREADCALL(r,m,pp,ll) \
102         { r = m(pp, ll); }
103 #endif
104
105
106 void *heap_alloc_uncollectable(u4 bytelength)
107 {
108         void *result;
109         MAINTHREADCALL(result, stackcall_malloc_uncollectable, NULL, bytelength);
110         return result;
111 }
112
113
114 void runboehmfinalizer(void *o, void *p)
115 {
116         java_objectheader *ob = (java_objectheader *) o;
117         asm_calljavafunction(ob->vftbl->class->finalizer, ob, NULL, NULL, NULL);
118         
119         /* if we had an exception in the finalizer, ignore it */
120         *exceptionptr = NULL;
121 }
122
123
124 void *heap_allocate(u4 bytelength, bool references, methodinfo *finalizer)
125 {
126         void *result;
127
128         if (references) {
129                 MAINTHREADCALL(result, stackcall_malloc, NULL, bytelength);
130
131         } else {
132                 MAINTHREADCALL(result, stackcall_malloc_atomic, NULL, bytelength);
133         }
134
135         if (finalizer)
136                 GC_REGISTER_FINALIZER(result, runboehmfinalizer, 0, 0, 0);
137
138         return (u1*) result;
139 }
140
141
142 void *heap_reallocate(void *p, u4 bytelength)
143 {
144         void *result;
145
146         MAINTHREADCALL(result, stackcall_realloc, p, bytelength);
147
148         return result;
149 }
150
151 void heap_free(void *p)
152 {
153         void *result;
154
155         MAINTHREADCALL(result, stackcall_free, p, 0);
156 }
157
158
159 void heap_init(u4 size, u4 startsize, void **stackbottom)
160 {
161         GC_INIT();
162 }
163
164
165 void heap_close()
166 {
167 }
168
169
170 void gc_init()
171 {
172 }
173
174
175 void gc_call()
176 {
177         if (collectverbose)
178                 dolog("Garbage Collection:  previous/now = %d / %d ",
179                           0, 0);
180
181         GC_gcollect();
182 }
183
184
185 /*
186  * These are local overrides for various environment variables in Emacs.
187  * Please do not remove this and leave it at the end of the file, where
188  * Emacs will automagically detect them.
189  * ---------------------------------------------------------------------
190  * Local variables:
191  * mode: c
192  * indent-tabs-mode: t
193  * c-basic-offset: 4
194  * tab-width: 4
195  * End:
196  */