2 * Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
3 * Copyright (c) 1999-2004 Hewlett-Packard Development Company, L.P.
5 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
6 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
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.
16 #include "private/gc_pmark.h" /* includes gc_priv.h */
21 * This is an allocator interface tuned for gcj (the GNU static
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
27 * This descriptor structure contains a GC marking descriptor at offset
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.
35 * 1) Counting on explicit initialization of this interface is OK;
36 * 2) FASTLOCK is not a significant win.
40 #include "private/dbg_mlc.h"
43 GC_INNER /* variable is also used in thread_local_alloc.c */
47 GC_bool GC_gcj_malloc_initialized = FALSE;
49 int GC_gcj_kind = 0; /* Object kind for objects with descriptors */
51 int GC_gcj_debug_kind = 0;
52 /* The kind of objects that is always marked */
53 /* with a mark proc call. */
55 GC_INNER ptr_t * GC_gcjobjfreelist = NULL;
57 STATIC struct GC_ms_entry * GC_gcj_fake_mark_proc(word * addr GC_ATTR_UNUSED,
58 struct GC_ms_entry *mark_stack_ptr,
59 struct GC_ms_entry * mark_stack_limit GC_ATTR_UNUSED,
60 word env GC_ATTR_UNUSED)
62 ABORT_RET("No client gcj mark proc is specified");
63 return mark_stack_ptr;
66 /* Caller does not hold allocation lock. */
67 GC_API void GC_CALL GC_init_gcj_malloc(int mp_index,
68 void * /* really GC_mark_proc */mp)
70 # ifndef GC_IGNORE_GCJ_INFO
71 GC_bool ignore_gcj_info;
75 if (mp == 0) /* In case GC_DS_PROC is unused. */
76 mp = (void *)(word)GC_gcj_fake_mark_proc;
78 GC_init(); /* In case it's not already done. */
80 if (GC_gcj_malloc_initialized) {
84 GC_gcj_malloc_initialized = TRUE;
85 # ifdef GC_IGNORE_GCJ_INFO
86 /* This is useful for debugging on platforms with missing getenv(). */
87 # define ignore_gcj_info TRUE
89 ignore_gcj_info = (0 != GETENV("GC_IGNORE_GCJ_INFO"));
91 if (ignore_gcj_info) {
92 GC_COND_LOG_PRINTF("Gcj-style type information is disabled!\n");
94 GC_ASSERT(GC_mark_procs[mp_index] == (GC_mark_proc)0); /* unused */
95 GC_mark_procs[mp_index] = (GC_mark_proc)(word)mp;
96 if ((unsigned)mp_index >= GC_n_mark_procs)
97 ABORT("GC_init_gcj_malloc: bad index");
98 /* Set up object kind gcj-style indirect descriptor. */
99 GC_gcjobjfreelist = (ptr_t *)GC_new_free_list_inner();
100 if (ignore_gcj_info) {
101 /* Use a simple length-based descriptor, thus forcing a fully */
102 /* conservative scan. */
103 GC_gcj_kind = GC_new_kind_inner((void **)GC_gcjobjfreelist,
104 /* 0 | */ GC_DS_LENGTH,
107 GC_gcj_kind = GC_new_kind_inner(
108 (void **)GC_gcjobjfreelist,
109 (((word)(-(signed_word)MARK_DESCR_OFFSET
110 - GC_INDIR_PER_OBJ_BIAS))
114 /* Set up object kind for objects that require mark proc call. */
115 if (ignore_gcj_info) {
116 GC_gcj_debug_kind = GC_gcj_kind;
118 GC_gcj_debug_kind = GC_new_kind_inner(GC_new_free_list_inner(),
119 GC_MAKE_PROC(mp_index,
120 1 /* allocated with debug info */),
124 # undef ignore_gcj_info
127 #define GENERAL_MALLOC_INNER(lb,k) \
128 GC_clear_stack(GC_generic_malloc_inner(lb, k))
130 #define GENERAL_MALLOC_INNER_IOP(lb,k) \
131 GC_clear_stack(GC_generic_malloc_inner_ignore_off_page(lb, k))
133 /* We need a mechanism to release the lock and invoke finalizers. */
134 /* We don't really have an opportunity to do this on a rarely executed */
135 /* path on which the lock is not held. Thus we check at a */
136 /* rarely executed point at which it is safe to release the lock. */
137 /* We do this even where we could just call GC_INVOKE_FINALIZERS, */
138 /* since it's probably cheaper and certainly more uniform. */
139 /* TODO: Consider doing the same elsewhere? */
140 static void maybe_finalize(void)
142 static word last_finalized_no = 0;
145 if (GC_gc_no == last_finalized_no ||
146 !EXPECT(GC_is_initialized, TRUE)) return;
148 GC_INVOKE_FINALIZERS();
150 last_finalized_no = GC_gc_no;
153 /* Allocate an object, clear it, and store the pointer to the */
154 /* type structure (vtable in gcj). */
155 /* This adds a byte at the end of the object if GC_malloc would.*/
156 #ifdef THREAD_LOCAL_ALLOC
157 GC_INNER void * GC_core_gcj_malloc(size_t lb,
158 void * ptr_to_struct_containing_descr)
160 GC_API GC_ATTR_MALLOC void * GC_CALL GC_gcj_malloc(size_t lb,
161 void * ptr_to_struct_containing_descr)
167 GC_DBG_COLLECT_AT_MALLOC(lb);
172 lg = GC_size_map[lb];
173 op = GC_gcjobjfreelist[lg];
174 if(EXPECT(0 == op, FALSE)) {
176 op = (ptr_t)GENERAL_MALLOC_INNER((word)lb, GC_gcj_kind);
178 GC_oom_func oom_fn = GC_oom_fn;
180 return((*oom_fn)(lb));
183 GC_gcjobjfreelist[lg] = (ptr_t)obj_link(op);
184 GC_bytes_allocd += GRANULES_TO_BYTES((word)lg);
186 GC_ASSERT(((void **)op)[1] == 0);
190 op = (ptr_t)GENERAL_MALLOC_INNER((word)lb, GC_gcj_kind);
192 GC_oom_func oom_fn = GC_oom_fn;
194 return((*oom_fn)(lb));
197 *(void **)op = ptr_to_struct_containing_descr;
200 REACHABLE_AFTER_DIRTY(ptr_to_struct_containing_descr);
204 /* Similar to GC_gcj_malloc, but add debug info. This is allocated */
205 /* with GC_gcj_debug_kind. */
206 GC_API GC_ATTR_MALLOC void * GC_CALL GC_debug_gcj_malloc(size_t lb,
207 void * ptr_to_struct_containing_descr, GC_EXTRA_PARAMS)
212 /* We're careful to avoid extra calls, which could */
213 /* confuse the backtrace. */
216 result = GC_generic_malloc_inner(SIZET_SAT_ADD(lb, DEBUG_BYTES),
219 GC_oom_func oom_fn = GC_oom_fn;
221 GC_err_printf("GC_debug_gcj_malloc(%lu, %p) returning NULL (%s:%d)\n",
222 (unsigned long)lb, ptr_to_struct_containing_descr, s, i);
223 return((*oom_fn)(lb));
225 *((void **)((ptr_t)result + sizeof(oh))) = ptr_to_struct_containing_descr;
226 if (!GC_debugging_started) {
227 GC_start_debugging_inner();
229 ADD_CALL_CHAIN(result, ra);
230 result = GC_store_debug_info_inner(result, (word)lb, s, i);
233 REACHABLE_AFTER_DIRTY(ptr_to_struct_containing_descr);
237 /* There is no THREAD_LOCAL_ALLOC for GC_gcj_malloc_ignore_off_page(). */
238 GC_API GC_ATTR_MALLOC void * GC_CALL GC_gcj_malloc_ignore_off_page(size_t lb,
239 void * ptr_to_struct_containing_descr)
244 GC_DBG_COLLECT_AT_MALLOC(lb);
249 lg = GC_size_map[lb];
250 op = GC_gcjobjfreelist[lg];
251 if (EXPECT(0 == op, FALSE)) {
253 op = (ptr_t)GENERAL_MALLOC_INNER_IOP(lb, GC_gcj_kind);
255 GC_oom_func oom_fn = GC_oom_fn;
257 return((*oom_fn)(lb));
260 GC_gcjobjfreelist[lg] = (ptr_t)obj_link(op);
261 GC_bytes_allocd += GRANULES_TO_BYTES((word)lg);
266 op = (ptr_t)GENERAL_MALLOC_INNER_IOP(lb, GC_gcj_kind);
268 GC_oom_func oom_fn = GC_oom_fn;
270 return((*oom_fn)(lb));
273 *(void **)op = ptr_to_struct_containing_descr;
276 REACHABLE_AFTER_DIRTY(ptr_to_struct_containing_descr);
280 #endif /* GC_GCJ_SUPPORT */