Compile without warnings with -Wall.
[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 487 2003-10-20 17:23:21Z twisti $
15
16 *******************************************************************************/
17
18 #include "global.h"
19 #include "threads/thread.h"
20 #include "asmpart.h"
21
22 #include "gc.h"
23
24 void *asm_switchstackandcall (void *stack, void *func, void **stacktopsave, void *);
25
26 struct otherstackcall;
27
28 typedef void *(*calltwoargs)(void *, u4);
29
30 struct otherstackcall {
31         calltwoargs p2;
32         void *p;
33         u4 l;
34 };
35
36 static void *stackcall_twoargs(struct otherstackcall *p)
37 {
38         return (*p->p2)(p->p, p->l);
39 }
40
41 static void *stackcall_malloc(void *p, u4 bytelength)
42 {
43         return GC_MALLOC(bytelength);
44 }
45
46 static void *stackcall_malloc_atomic(void *p, u4 bytelength)
47 {
48         return GC_MALLOC_ATOMIC(bytelength);
49 }
50
51 static void *stackcall_malloc_uncollectable(void *p, u4 bytelength)
52 {
53         return GC_MALLOC_UNCOLLECTABLE(bytelength);
54 }
55
56 static void *stackcall_realloc(void *p, u4 bytelength)
57 {
58         return GC_REALLOC(p, bytelength);
59 }
60
61 #ifdef USE_THREADS
62 #define MAINTHREADCALL(r,m,pp,ll) \
63         if (currentThread == NULL || currentThread == mainThread) { \
64                 r = m(pp, ll); \
65         } else { \
66                 struct otherstackcall sc; \
67                 sc.p2 = m; \
68                 sc.p = pp; \
69                 sc.l = ll; \
70                 r = (*asm_switchstackandcall)(CONTEXT(mainThread).usedStackTop, \
71                                 stackcall_twoargs, \
72                                 (void**)&(CONTEXT(currentThread).usedStackTop), &sc); \
73         }
74 #else
75 #define MAINTHREADCALL(r,m,pp,ll) \
76         { r = m(pp, ll); }
77 #endif
78
79 void *heap_alloc_uncollectable(u4 bytelength)
80 {
81         void *result;
82         MAINTHREADCALL(result, stackcall_malloc_uncollectable, NULL, bytelength);
83         return result;
84 }
85
86 void runboehmfinalizer(void *o, void *p)
87 {
88         java_objectheader *ob = (java_objectheader *) o;
89         asm_calljavamethod(ob->vftbl->class->finalizer, ob, NULL, NULL, NULL);
90 }
91
92 void *heap_allocate (u4 bytelength, bool references, methodinfo *finalizer)
93 {
94         void *result;
95         if (references)
96                 { MAINTHREADCALL(result, stackcall_malloc, NULL, bytelength); }
97         else
98                 { MAINTHREADCALL(result, stackcall_malloc_atomic, NULL, bytelength); }
99         if (finalizer)
100                 GC_REGISTER_FINALIZER(result, runboehmfinalizer, 0, 0, 0);
101         return (u1*) result;
102 }
103
104 void *heap_reallocate(void *p, u4 bytelength)
105 {
106         void *result;
107         MAINTHREADCALL(result, stackcall_realloc, p, bytelength);
108         return result;
109 }
110
111 void heap_init (u4 size, u4 startsize, void **stackbottom)
112 {
113         GC_INIT();
114 }
115
116 void heap_close()
117 {
118 }
119
120 void heap_addreference (void **reflocation)
121 {
122 }
123
124 int collectverbose;
125
126 void gc_init()
127 {
128 }
129
130 void gc_call()
131 {
132         if (collectverbose) {
133                 sprintf(logtext, "Garbage Collection:  previous/now = %d / %d ",
134                                 0, 0);
135                 dolog();
136         }
137
138         GC_gcollect();
139 }
140
141 /*
142  * These are local overrides for various environment variables in Emacs.
143  * Please do not remove this and leave it at the end of the file, where
144  * Emacs will automagically detect them.
145  * ---------------------------------------------------------------------
146  * Local variables:
147  * mode: c
148  * indent-tabs-mode: t
149  * c-basic-offset: 4
150  * tab-width: 4
151  * End:
152  */