Upgrade Boehm GC to 7.2alpha4.
[cacao.git] / src / mm / boehm-gc / gcj_mlc.c
1 /*
2  * Copyright (c) 1991-1994 by Xerox Corporation.  All rights reserved.
3  * Copyright (c) 1999-2004 Hewlett-Packard Development Company, L.P.
4  *
5  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
6  * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
7  *
8  * Permission is hereby granted to use or copy this program
9  * for any purpose,  provided the above notices are retained on all copies.
10  * Permission to modify the code and to distribute modified code is granted,
11  * provided the above notices are retained, and a notice that the code was
12  * modified is included with the above copyright notice.
13  *
14  */
15
16 #include "private/gc_pmark.h"  /* includes gc_priv.h */
17
18 #ifdef GC_GCJ_SUPPORT
19
20 /*
21  * This is an allocator interface tuned for gcj (the GNU static
22  * java compiler).
23  *
24  * Each allocated object has a pointer in its first word to a vtable,
25  * which for our purposes is simply a structure describing the type of
26  * the object.
27  * This descriptor structure contains a GC marking descriptor at offset
28  * MARK_DESCR_OFFSET.
29  *
30  * It is hoped that this interface may also be useful for other systems,
31  * possibly with some tuning of the constants.  But the immediate goal
32  * is to get better gcj performance.
33  *
34  * We assume:
35  *  1) Counting on explicit initialization of this interface is OK;
36  *  2) FASTLOCK is not a significant win.
37  */
38
39 #include "gc_gcj.h"
40 #include "private/dbg_mlc.h"
41
42 GC_INNER GC_bool GC_gcj_malloc_initialized = FALSE;
43
44 int GC_gcj_kind = 0;    /* Object kind for objects with descriptors     */
45                         /* in "vtable".                                 */
46 int GC_gcj_debug_kind = 0;
47                         /* The kind of objects that is always marked    */
48                         /* with a mark proc call.                       */
49
50 GC_INNER ptr_t * GC_gcjobjfreelist = NULL;
51
52 STATIC ptr_t * GC_gcjdebugobjfreelist = NULL;
53
54 /*ARGSUSED*/
55 STATIC struct GC_ms_entry * GC_gcj_fake_mark_proc(word * addr,
56                                         struct GC_ms_entry *mark_stack_ptr,
57                                         struct GC_ms_entry *mark_stack_limit,
58                                         word env)
59 {
60     ABORT("No client gcj mark proc is specified");
61     return mark_stack_ptr;
62 }
63
64 /* Caller does not hold allocation lock. */
65 GC_API void GC_CALL GC_init_gcj_malloc(int mp_index,
66                                        void * /* really GC_mark_proc */mp)
67 {
68     GC_bool ignore_gcj_info;
69     DCL_LOCK_STATE;
70
71     if (mp == 0)        /* In case GC_DS_PROC is unused.        */
72       mp = (void *)(word)GC_gcj_fake_mark_proc;
73
74     GC_init();  /* In case it's not already done.       */
75     LOCK();
76     if (GC_gcj_malloc_initialized) {
77       UNLOCK();
78       return;
79     }
80     GC_gcj_malloc_initialized = TRUE;
81 #   ifdef GC_IGNORE_GCJ_INFO
82       /* This is useful for debugging on platforms with missing getenv(). */
83       ignore_gcj_info = 1;
84 #   else
85       ignore_gcj_info = (0 != GETENV("GC_IGNORE_GCJ_INFO"));
86 #   endif
87     if (GC_print_stats && ignore_gcj_info) {
88         GC_log_printf("Gcj-style type information is disabled!\n");
89     }
90     GC_ASSERT(GC_mark_procs[mp_index] == (GC_mark_proc)0); /* unused */
91     GC_mark_procs[mp_index] = (GC_mark_proc)(word)mp;
92     if ((unsigned)mp_index >= GC_n_mark_procs)
93         ABORT("GC_init_gcj_malloc: bad index");
94     /* Set up object kind gcj-style indirect descriptor. */
95       GC_gcjobjfreelist = (ptr_t *)GC_new_free_list_inner();
96       if (ignore_gcj_info) {
97         /* Use a simple length-based descriptor, thus forcing a fully   */
98         /* conservative scan.                                           */
99         GC_gcj_kind = GC_new_kind_inner((void **)GC_gcjobjfreelist,
100                                         (0 | GC_DS_LENGTH),
101                                         TRUE, TRUE);
102       } else {
103         GC_gcj_kind = GC_new_kind_inner(
104                         (void **)GC_gcjobjfreelist,
105                         (((word)(-(signed_word)MARK_DESCR_OFFSET
106                                  - GC_INDIR_PER_OBJ_BIAS))
107                          | GC_DS_PER_OBJECT),
108                         FALSE, TRUE);
109       }
110     /* Set up object kind for objects that require mark proc call.      */
111       if (ignore_gcj_info) {
112         GC_gcj_debug_kind = GC_gcj_kind;
113         GC_gcjdebugobjfreelist = GC_gcjobjfreelist;
114       } else {
115         GC_gcjdebugobjfreelist = (ptr_t *)GC_new_free_list_inner();
116         GC_gcj_debug_kind = GC_new_kind_inner(
117                                 (void **)GC_gcjdebugobjfreelist,
118                                 GC_MAKE_PROC(mp_index,
119                                              1 /* allocated with debug info */),
120                                 FALSE, TRUE);
121       }
122     UNLOCK();
123 }
124
125 #define GENERAL_MALLOC_INNER(lb,k) \
126     GC_clear_stack(GC_generic_malloc_inner(lb, k))
127
128 #define GENERAL_MALLOC_INNER_IOP(lb,k) \
129     GC_clear_stack(GC_generic_malloc_inner_ignore_off_page(lb, k))
130
131 /* We need a mechanism to release the lock and invoke finalizers.       */
132 /* We don't really have an opportunity to do this on a rarely executed  */
133 /* path on which the lock is not held.  Thus we check at a              */
134 /* rarely executed point at which it is safe to release the lock.       */
135 /* We do this even where we could just call GC_INVOKE_FINALIZERS,       */
136 /* since it's probably cheaper and certainly more uniform.              */
137 /* FIXME - Consider doing the same elsewhere?                           */
138 static void maybe_finalize(void)
139 {
140    static word last_finalized_no = 0;
141
142    if (GC_gc_no == last_finalized_no) return;
143    if (!GC_is_initialized) return;
144    UNLOCK();
145    GC_INVOKE_FINALIZERS();
146    LOCK();
147    last_finalized_no = GC_gc_no;
148 }
149
150 /* Allocate an object, clear it, and store the pointer to the   */
151 /* type structure (vtable in gcj).                              */
152 /* This adds a byte at the end of the object if GC_malloc would.*/
153 #ifdef THREAD_LOCAL_ALLOC
154   GC_INNER void * GC_core_gcj_malloc(size_t lb,
155                                      void * ptr_to_struct_containing_descr)
156 #else
157   GC_API void * GC_CALL GC_gcj_malloc(size_t lb,
158                                       void * ptr_to_struct_containing_descr)
159 #endif
160 {
161     ptr_t op;
162     ptr_t * opp;
163     word lg;
164     DCL_LOCK_STATE;
165
166     if(SMALL_OBJ(lb)) {
167         lg = GC_size_map[lb];
168         opp = &(GC_gcjobjfreelist[lg]);
169         LOCK();
170         op = *opp;
171         if(EXPECT(op == 0, 0)) {
172             maybe_finalize();
173             op = (ptr_t)GENERAL_MALLOC_INNER((word)lb, GC_gcj_kind);
174             if (0 == op) {
175                 GC_oom_func oom_fn = GC_oom_fn;
176                 UNLOCK();
177                 return((*oom_fn)(lb));
178             }
179         } else {
180             *opp = obj_link(op);
181             GC_bytes_allocd += GRANULES_TO_BYTES(lg);
182         }
183         *(void **)op = ptr_to_struct_containing_descr;
184         GC_ASSERT(((void **)op)[1] == 0);
185         UNLOCK();
186     } else {
187         LOCK();
188         maybe_finalize();
189         op = (ptr_t)GENERAL_MALLOC_INNER((word)lb, GC_gcj_kind);
190         if (0 == op) {
191             GC_oom_func oom_fn = GC_oom_fn;
192             UNLOCK();
193             return((*oom_fn)(lb));
194         }
195         *(void **)op = ptr_to_struct_containing_descr;
196         UNLOCK();
197     }
198     return((void *) op);
199 }
200
201 GC_INNER void GC_start_debugging(void);
202
203 /* Similar to GC_gcj_malloc, but add debug info.  This is allocated     */
204 /* with GC_gcj_debug_kind.                                              */
205 GC_API void * GC_CALL GC_debug_gcj_malloc(size_t lb,
206                 void * ptr_to_struct_containing_descr, GC_EXTRA_PARAMS)
207 {
208     void * result;
209
210     /* We're careful to avoid extra calls, which could           */
211     /* confuse the backtrace.                                   */
212     LOCK();
213     maybe_finalize();
214     result = GC_generic_malloc_inner(lb + DEBUG_BYTES, GC_gcj_debug_kind);
215     if (result == 0) {
216         GC_oom_func oom_fn = GC_oom_fn;
217         UNLOCK();
218         GC_err_printf("GC_debug_gcj_malloc(%ld, %p) returning NIL (",
219                       (unsigned long)lb, ptr_to_struct_containing_descr);
220         GC_err_puts(s);
221         GC_err_printf(":%d)\n", i);
222         return((*oom_fn)(lb));
223     }
224     *((void **)((ptr_t)result + sizeof(oh))) = ptr_to_struct_containing_descr;
225     UNLOCK();
226     if (!GC_debugging_started) {
227         GC_start_debugging();
228     }
229     ADD_CALL_CHAIN(result, ra);
230     return (GC_store_debug_info(result, (word)lb, s, (word)i));
231 }
232
233 /* There is no THREAD_LOCAL_ALLOC for GC_gcj_malloc_ignore_off_page().  */
234 GC_API void * GC_CALL GC_gcj_malloc_ignore_off_page(size_t lb,
235                                      void * ptr_to_struct_containing_descr)
236 {
237     ptr_t op;
238     ptr_t * opp;
239     word lg;
240     DCL_LOCK_STATE;
241
242     if(SMALL_OBJ(lb)) {
243         lg = GC_size_map[lb];
244         opp = &(GC_gcjobjfreelist[lg]);
245         LOCK();
246         if( (op = *opp) == 0 ) {
247             maybe_finalize();
248             op = (ptr_t)GENERAL_MALLOC_INNER_IOP(lb, GC_gcj_kind);
249             if (0 == op) {
250                 GC_oom_func oom_fn = GC_oom_fn;
251                 UNLOCK();
252                 return((*oom_fn)(lb));
253             }
254         } else {
255             *opp = obj_link(op);
256             GC_bytes_allocd += GRANULES_TO_BYTES(lg);
257         }
258     } else {
259         LOCK();
260         maybe_finalize();
261         op = (ptr_t)GENERAL_MALLOC_INNER_IOP(lb, GC_gcj_kind);
262         if (0 == op) {
263             GC_oom_func oom_fn = GC_oom_fn;
264             UNLOCK();
265             return((*oom_fn)(lb));
266         }
267     }
268     *(void **)op = ptr_to_struct_containing_descr;
269     UNLOCK();
270     return((void *) op);
271 }
272
273 #endif  /* GC_GCJ_SUPPORT */