519d8c0954e79187185270ef281bf3fa7b7a2984
[cacao.git] / src / mm / boehm-gc / pcr_interface.c
1 /* 
2  * Copyright (c) 1991-1994 by Xerox Corporation.  All rights reserved.
3  *
4  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
5  * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
6  *
7  * Permission is hereby granted to use or copy this program
8  * for any purpose,  provided the above notices are retained on all copies.
9  * Permission to modify the code and to distribute modified code is granted,
10  * provided the above notices are retained, and a notice that the code was
11  * modified is included with the above copyright notice.
12  */
13
14 #include "config.h"
15
16 # include "private/gc_priv.h"
17
18 # ifdef PCR
19 /*
20  * Note that POSIX PCR requires an ANSI C compiler.  Hence we are allowed
21  * to make the same assumption here.
22  * We wrap all of the allocator functions to avoid questions of
23  * compatibility between the prototyped and nonprototyped versions of the f
24  */
25 # include "config/PCR_StdTypes.h"
26 # include "mm/PCR_MM.h"
27 # include <errno.h>
28
29 # define MY_MAGIC 17L
30 # define MY_DEBUGMAGIC 42L
31
32 void * GC_AllocProc(size_t size, PCR_Bool ptrFree, PCR_Bool clear )
33 {
34     if (ptrFree) {
35         void * result = (void *)GC_malloc_atomic(size);
36         if (clear && result != 0) BZERO(result, size);
37         return(result);
38     } else {
39         return((void *)GC_malloc(size));
40     }
41 }
42
43 void * GC_DebugAllocProc(size_t size, PCR_Bool ptrFree, PCR_Bool clear )
44 {
45     if (ptrFree) {
46         void * result = (void *)GC_debug_malloc_atomic(size, __FILE__,
47                                                              __LINE__);
48         if (clear && result != 0) BZERO(result, size);
49         return(result);
50     } else {
51         return((void *)GC_debug_malloc(size, __FILE__, __LINE__));
52     }
53 }
54
55 # define GC_ReallocProc GC_realloc
56 void * GC_DebugReallocProc(void * old_object, size_t new_size_in_bytes)
57 {
58     return(GC_debug_realloc(old_object, new_size_in_bytes, __FILE__, __LINE__));
59 }
60
61 # define GC_FreeProc GC_free
62 # define GC_DebugFreeProc GC_debug_free
63
64 typedef struct {
65   PCR_ERes (*ed_proc)(void *p, size_t size, PCR_Any data);
66   GC_bool ed_pointerfree;
67   PCR_ERes ed_fail_code;
68   PCR_Any ed_client_data;
69 } enumerate_data;
70
71 void GC_enumerate_block(struct hblk *h; enumerate_data * ed)
72 {
73     register hdr * hhdr;
74     register int sz;
75     ptr_t p;
76     ptr_t lim;
77     word descr;
78 #   error This code was updated without testing.
79 #   error and its precursor was clearly broken.
80     
81     hhdr = HDR(h);
82     descr = hhdr -> hb_descr;
83     sz = hhdr -> hb_sz;
84     if (descr != 0 && ed -> ed_pointerfree
85         || descr == 0 && !(ed -> ed_pointerfree)) return;
86     lim = (ptr_t)(h+1) - sz;
87     p = (ptr_t)h;
88     do {
89         if (PCR_ERes_IsErr(ed -> ed_fail_code)) return;
90         ed -> ed_fail_code =
91             (*(ed -> ed_proc))(p, sz, ed -> ed_client_data);
92         p+= sz;
93     } while (p <= lim);
94 }
95
96 struct PCR_MM_ProcsRep * GC_old_allocator = 0;
97
98 PCR_ERes GC_EnumerateProc(
99     PCR_Bool ptrFree,
100     PCR_ERes (*proc)(void *p, size_t size, PCR_Any data),
101     PCR_Any data
102 )
103 {
104     enumerate_data ed;
105     
106     ed.ed_proc = proc;
107     ed.ed_pointerfree = ptrFree;
108     ed.ed_fail_code = PCR_ERes_okay;
109     ed.ed_client_data = data;
110     GC_apply_to_all_blocks(GC_enumerate_block, &ed);
111     if (ed.ed_fail_code != PCR_ERes_okay) {
112         return(ed.ed_fail_code);
113     } else {
114         /* Also enumerate objects allocated by my predecessors */
115         return((*(GC_old_allocator->mmp_enumerate))(ptrFree, proc, data));
116     }
117 }
118
119 void GC_DummyFreeProc(void *p) {}
120
121 void GC_DummyShutdownProc(void) {}
122
123 struct PCR_MM_ProcsRep GC_Rep = {
124         MY_MAGIC,
125         GC_AllocProc,
126         GC_ReallocProc,
127         GC_DummyFreeProc,       /* mmp_free */
128         GC_FreeProc,            /* mmp_unsafeFree */
129         GC_EnumerateProc,
130         GC_DummyShutdownProc    /* mmp_shutdown */
131 };
132
133 struct PCR_MM_ProcsRep GC_DebugRep = {
134         MY_DEBUGMAGIC,
135         GC_DebugAllocProc,
136         GC_DebugReallocProc,
137         GC_DummyFreeProc,       /* mmp_free */
138         GC_DebugFreeProc,               /* mmp_unsafeFree */
139         GC_EnumerateProc,
140         GC_DummyShutdownProc    /* mmp_shutdown */
141 };
142
143 GC_bool GC_use_debug = 0;
144
145 void GC_pcr_install()
146 {
147     PCR_MM_Install((GC_use_debug? &GC_DebugRep : &GC_Rep), &GC_old_allocator);
148 }
149
150 PCR_ERes
151 PCR_GC_Setup(void)
152 {
153     return PCR_ERes_okay;
154 }
155
156 PCR_ERes
157 PCR_GC_Run(void)
158 {
159
160     if( !PCR_Base_TestPCRArg("-nogc") ) {
161         GC_quiet = ( PCR_Base_TestPCRArg("-gctrace") ? 0 : 1 );
162         GC_use_debug = (GC_bool)PCR_Base_TestPCRArg("-debug_alloc");
163         GC_init();
164         if( !PCR_Base_TestPCRArg("-nogc_incremental") ) {
165             /*
166              * awful hack to test whether VD is implemented ...
167              */
168             if( PCR_VD_Start( 0, NIL, 0) != PCR_ERes_FromErr(ENOSYS) ) {
169                 GC_enable_incremental();
170             }
171         }
172     }
173     return PCR_ERes_okay;
174 }
175
176 void GC_push_thread_structures(void)
177 {
178     /* PCR doesn't work unless static roots are pushed.  Can't get here. */
179     ABORT("In GC_push_thread_structures()");
180 }
181
182 # endif