Added finalizers
[cacao.git] / src / mm / boehm.c
1 /* boehm.c *********************************************************************
2
3         Copyright (c) 1997 A. Krall, R. Grafl, M. Gschwind, M. Probst
4
5         See file COPYRIGHT for information on usage and disclaimer of warranties
6
7         Contains the interface to the Boehm GC
8
9         Authors: Reinhard Grafl      EMAIL: cacao@complang.tuwien.ac.at
10         Changes: Andi Krall          EMAIL: cacao@complang.tuwien.ac.at
11                  Mark Probst         EMAIL: cacao@complang.tuwien.ac.at
12                          Philipp Tomsich     EMAIL: cacao@complang.tuwien.ac.at
13
14         Last Change: $Id: boehm.c 256 2003-03-17 11:48:18Z stefan $
15
16 *******************************************************************************/
17
18 #include "global.h"
19 #include "threads/thread.h"
20
21 #include "gc.h"
22
23 void *asm_switchstackandcall (void *stack, void *func, void **stacktopsave, void *);
24
25 struct otherstackcall;
26
27 typedef void *(*calltwoargs)(void *, u4);
28
29 struct otherstackcall {
30         calltwoargs p2;
31         void *p;
32         u4 l;
33 };
34
35 static void *stackcall_twoargs(struct otherstackcall *p)
36 {
37         return (*p->p2)(p->p, p->l);
38 }
39
40 static void *stackcall_malloc(void *p, u4 bytelength)
41 {
42         return GC_MALLOC(bytelength);
43 }
44
45 static void *stackcall_malloc_atomic(void *p, u4 bytelength)
46 {
47         return GC_MALLOC_ATOMIC(bytelength);
48 }
49
50 static void *stackcall_malloc_uncollectable(void *p, u4 bytelength)
51 {
52         return GC_MALLOC_UNCOLLECTABLE(bytelength);
53 }
54
55 static void *stackcall_realloc(void *p, u4 bytelength)
56 {
57         return GC_REALLOC(p, bytelength);
58 }
59
60 #ifdef USE_THREADS
61 #define MAINTHREADCALL(r,m,pp,ll) \
62         if (currentThread == NULL || currentThread == mainThread) { \
63                 r = m(pp, ll); \
64         } else { \
65                 struct otherstackcall sc; \
66                 sc.p2 = m; \
67                 sc.p = pp; \
68                 sc.l = ll; \
69                 r = (*asm_switchstackandcall)(CONTEXT(mainThread).usedStackTop, \
70                                 stackcall_twoargs, \
71                                 (void**)&(CONTEXT(currentThread).usedStackTop), &sc); \
72         }
73 #else
74 #define MAINTHREADCALL(r,m,pp,ll) \
75         { r = m(pp, ll); }
76 #endif
77
78 void *heap_alloc_uncollectable(u4 bytelength)
79 {
80         void *result;
81         MAINTHREADCALL(result, stackcall_malloc_uncollectable, NULL, bytelength);
82         return result;
83 }
84
85 void runboehmfinalizer(void *o, void *p)
86 {
87         java_objectheader *ob = (java_objectheader *) o;
88         asm_calljavamethod(ob->vftbl->class->finalizer, ob, NULL, NULL, NULL);
89 }
90
91 void *heap_allocate (u4 bytelength, bool references, methodinfo *finalizer)
92 {
93         void *result;
94         if (references, 1)
95                 { MAINTHREADCALL(result, stackcall_malloc, NULL, bytelength); }
96         else
97                 { MAINTHREADCALL(result, stackcall_malloc_atomic, NULL, bytelength); }
98         if (finalizer)
99                 GC_REGISTER_FINALIZER(result, runboehmfinalizer, 0, 0, 0);
100         return (u1*) result;
101 }
102
103 void *heap_reallocate(void *p, u4 bytelength)
104 {
105         void *result;
106         MAINTHREADCALL(result, stackcall_realloc, p, bytelength);
107         return result;
108 }
109
110 void heap_init (u4 size, u4 startsize, void **stackbottom)
111 {
112 }
113
114 void heap_close()
115 {
116 }
117
118 void heap_addreference (void **reflocation)
119 {
120 }
121
122 int collectverbose;
123
124 void gc_init()
125 {
126 }
127
128 void gc_call()
129 {
130         GC_gcollect();
131 }
132
133 /*
134  * These are local overrides for various environment variables in Emacs.
135  * Please do not remove this and leave it at the end of the file, where
136  * Emacs will automagically detect them.
137  * ---------------------------------------------------------------------
138  * Local variables:
139  * mode: c
140  * indent-tabs-mode: t
141  * c-basic-offset: 4
142  * tab-width: 4
143  * End:
144  */