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"
42 GC_INNER GC_bool GC_gcj_malloc_initialized = FALSE;
44 int GC_gcj_kind = 0; /* Object kind for objects with descriptors */
46 int GC_gcj_debug_kind = 0;
47 /* The kind of objects that is always marked */
48 /* with a mark proc call. */
50 GC_INNER ptr_t * GC_gcjobjfreelist = NULL;
52 STATIC ptr_t * GC_gcjdebugobjfreelist = NULL;
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,
60 ABORT("No client gcj mark proc is specified");
61 return mark_stack_ptr;
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)
68 GC_bool ignore_gcj_info;
71 if (mp == 0) /* In case GC_DS_PROC is unused. */
72 mp = (void *)(word)GC_gcj_fake_mark_proc;
74 GC_init(); /* In case it's not already done. */
76 if (GC_gcj_malloc_initialized) {
80 GC_gcj_malloc_initialized = TRUE;
81 # ifdef GC_IGNORE_GCJ_INFO
82 /* This is useful for debugging on platforms with missing getenv(). */
85 ignore_gcj_info = (0 != GETENV("GC_IGNORE_GCJ_INFO"));
87 if (GC_print_stats && ignore_gcj_info) {
88 GC_log_printf("Gcj-style type information is disabled!\n");
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,
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))
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;
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 */),
125 #define GENERAL_MALLOC_INNER(lb,k) \
126 GC_clear_stack(GC_generic_malloc_inner(lb, k))
128 #define GENERAL_MALLOC_INNER_IOP(lb,k) \
129 GC_clear_stack(GC_generic_malloc_inner_ignore_off_page(lb, k))
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)
140 static word last_finalized_no = 0;
142 if (GC_gc_no == last_finalized_no) return;
143 if (!GC_is_initialized) return;
145 GC_INVOKE_FINALIZERS();
147 last_finalized_no = GC_gc_no;
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)
157 GC_API void * GC_CALL GC_gcj_malloc(size_t lb,
158 void * ptr_to_struct_containing_descr)
167 lg = GC_size_map[lb];
168 opp = &(GC_gcjobjfreelist[lg]);
171 if(EXPECT(op == 0, 0)) {
173 op = (ptr_t)GENERAL_MALLOC_INNER((word)lb, GC_gcj_kind);
175 GC_oom_func oom_fn = GC_oom_fn;
177 return((*oom_fn)(lb));
181 GC_bytes_allocd += GRANULES_TO_BYTES(lg);
183 *(void **)op = ptr_to_struct_containing_descr;
184 GC_ASSERT(((void **)op)[1] == 0);
189 op = (ptr_t)GENERAL_MALLOC_INNER((word)lb, GC_gcj_kind);
191 GC_oom_func oom_fn = GC_oom_fn;
193 return((*oom_fn)(lb));
195 *(void **)op = ptr_to_struct_containing_descr;
201 GC_INNER void GC_start_debugging(void);
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)
210 /* We're careful to avoid extra calls, which could */
211 /* confuse the backtrace. */
214 result = GC_generic_malloc_inner(lb + DEBUG_BYTES, GC_gcj_debug_kind);
216 GC_oom_func oom_fn = GC_oom_fn;
218 GC_err_printf("GC_debug_gcj_malloc(%ld, %p) returning NIL (",
219 (unsigned long)lb, ptr_to_struct_containing_descr);
221 GC_err_printf(":%d)\n", i);
222 return((*oom_fn)(lb));
224 *((void **)((ptr_t)result + sizeof(oh))) = ptr_to_struct_containing_descr;
226 if (!GC_debugging_started) {
227 GC_start_debugging();
229 ADD_CALL_CHAIN(result, ra);
230 return (GC_store_debug_info(result, (word)lb, s, (word)i));
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)
243 lg = GC_size_map[lb];
244 opp = &(GC_gcjobjfreelist[lg]);
246 if( (op = *opp) == 0 ) {
248 op = (ptr_t)GENERAL_MALLOC_INNER_IOP(lb, GC_gcj_kind);
250 GC_oom_func oom_fn = GC_oom_fn;
252 return((*oom_fn)(lb));
256 GC_bytes_allocd += GRANULES_TO_BYTES(lg);
261 op = (ptr_t)GENERAL_MALLOC_INNER_IOP(lb, GC_gcj_kind);
263 GC_oom_func oom_fn = GC_oom_fn;
265 return((*oom_fn)(lb));
268 *(void **)op = ptr_to_struct_containing_descr;
273 #endif /* GC_GCJ_SUPPORT */