Debian Codegraph @

gc/gcj_mlc.c

raw
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: counting on explicit initialization of this interface is OK.
35 */
36
37 #include "gc_gcj.h"
38 #include "private/dbg_mlc.h"
39
40 int GC_gcj_kind = 0; /* Object kind for objects with descriptors */
41 /* in "vtable". */
42 int GC_gcj_debug_kind = 0;
43 /* The kind of objects that is always marked */
44 /* with a mark proc call. */
45
46 STATIC struct GC_ms_entry * GC_gcj_fake_mark_proc(word * addr GC_ATTR_UNUSED,
47 struct GC_ms_entry *mark_stack_ptr,
48 struct GC_ms_entry * mark_stack_limit GC_ATTR_UNUSED,
49 word env GC_ATTR_UNUSED)
50 {
51 ABORT_RET("No client gcj mark proc is specified");
52 return mark_stack_ptr;
53 }
54
55 /* Caller does not hold allocation lock. */
56 GC_API void GC_CALL GC_init_gcj_malloc(int mp_index,
57 void * /* really GC_mark_proc */mp)
58 {
59 # ifndef GC_IGNORE_GCJ_INFO
60 GC_bool ignore_gcj_info;
61 # endif
62 DCL_LOCK_STATE;
63
64 if (mp == 0) /* In case GC_DS_PROC is unused. */
65 mp = (void *)(word)GC_gcj_fake_mark_proc;
66
67 GC_init(); /* In case it's not already done. */
68 LOCK();
69 if (GC_gcjobjfreelist != NULL) {
70 /* Already initialized. */
71 UNLOCK();
72 return;
73 }
74 # ifdef GC_IGNORE_GCJ_INFO
75 /* This is useful for debugging on platforms with missing getenv(). */
76 # define ignore_gcj_info TRUE
77 # else
78 ignore_gcj_info = (0 != GETENV("GC_IGNORE_GCJ_INFO"));
79 # endif
80 if (ignore_gcj_info) {
81 GC_COND_LOG_PRINTF("Gcj-style type information is disabled!\n");
82 }
83 GC_ASSERT(GC_mark_procs[mp_index] == (GC_mark_proc)0); /* unused */
84 GC_mark_procs[mp_index] = (GC_mark_proc)(word)mp;
85 if ((unsigned)mp_index >= GC_n_mark_procs)
86 ABORT("GC_init_gcj_malloc: bad index");
87 /* Set up object kind gcj-style indirect descriptor. */
88 GC_gcjobjfreelist = (ptr_t *)GC_new_free_list_inner();
89 if (ignore_gcj_info) {
90 /* Use a simple length-based descriptor, thus forcing a fully */
91 /* conservative scan. */
92 GC_gcj_kind = GC_new_kind_inner((void **)GC_gcjobjfreelist,
93 /* 0 | */ GC_DS_LENGTH,
94 TRUE, TRUE);
95 GC_gcj_debug_kind = GC_gcj_kind;
96 } else {
97 GC_gcj_kind = GC_new_kind_inner(
98 (void **)GC_gcjobjfreelist,
99 (((word)(-(signed_word)MARK_DESCR_OFFSET
100 - GC_INDIR_PER_OBJ_BIAS))
101 | GC_DS_PER_OBJECT),
102 FALSE, TRUE);
103 /* Set up object kind for objects that require mark proc call. */
104 GC_gcj_debug_kind = GC_new_kind_inner(GC_new_free_list_inner(),
105 GC_MAKE_PROC(mp_index,
106 1 /* allocated with debug info */),
107 FALSE, TRUE);
108 }
109 UNLOCK();
110 # undef ignore_gcj_info
111 }
112
113 #define GENERAL_MALLOC_INNER(lb,k) \
114 GC_clear_stack(GC_generic_malloc_inner(lb, k))
115
116 #define GENERAL_MALLOC_INNER_IOP(lb,k) \
117 GC_clear_stack(GC_generic_malloc_inner_ignore_off_page(lb, k))
118
119 /* We need a mechanism to release the lock and invoke finalizers. */
120 /* We don't really have an opportunity to do this on a rarely executed */
121 /* path on which the lock is not held. Thus we check at a */
122 /* rarely executed point at which it is safe to release the lock. */
123 /* We do this even where we could just call GC_INVOKE_FINALIZERS, */
124 /* since it's probably cheaper and certainly more uniform. */
125 /* TODO: Consider doing the same elsewhere? */
126 static void maybe_finalize(void)
127 {
128 static word last_finalized_no = 0;
129 DCL_LOCK_STATE;
130
131 if (GC_gc_no == last_finalized_no ||
132 !EXPECT(GC_is_initialized, TRUE)) return;
133 UNLOCK();
134 GC_INVOKE_FINALIZERS();
135 LOCK();
136 last_finalized_no = GC_gc_no;
137 }
138
139 /* Allocate an object, clear it, and store the pointer to the */
140 /* type structure (vtable in gcj). */
141 /* This adds a byte at the end of the object if GC_malloc would.*/
142 #ifdef THREAD_LOCAL_ALLOC
143 GC_INNER void * GC_core_gcj_malloc(size_t lb,
144 void * ptr_to_struct_containing_descr)
145 #else
146 GC_API GC_ATTR_MALLOC void * GC_CALL GC_gcj_malloc(size_t lb,
147 void * ptr_to_struct_containing_descr)
148 #endif
149 {
150 ptr_t op;
151 DCL_LOCK_STATE;
152
153 GC_DBG_COLLECT_AT_MALLOC(lb);
154 if(SMALL_OBJ(lb)) {
155 word lg;
156
157 LOCK();
158 lg = GC_size_map[lb];
159 op = GC_gcjobjfreelist[lg];
160 if(EXPECT(0 == op, FALSE)) {
161 maybe_finalize();
162 op = (ptr_t)GENERAL_MALLOC_INNER((word)lb, GC_gcj_kind);
163 if (0 == op) {
164 GC_oom_func oom_fn = GC_oom_fn;
165 UNLOCK();
166 return((*oom_fn)(lb));
167 }
168 } else {
169 GC_gcjobjfreelist[lg] = (ptr_t)obj_link(op);
170 GC_bytes_allocd += GRANULES_TO_BYTES((word)lg);
171 }
172 GC_ASSERT(((void **)op)[1] == 0);
173 } else {
174 LOCK();
175 maybe_finalize();
176 op = (ptr_t)GENERAL_MALLOC_INNER((word)lb, GC_gcj_kind);
177 if (0 == op) {
178 GC_oom_func oom_fn = GC_oom_fn;
179 UNLOCK();
180 return((*oom_fn)(lb));
181 }
182 }
183 *(void **)op = ptr_to_struct_containing_descr;
184 UNLOCK();
185 GC_dirty(op);
186 REACHABLE_AFTER_DIRTY(ptr_to_struct_containing_descr);
187 return (void *)op;
188 }
189
190 /* Similar to GC_gcj_malloc, but add debug info. This is allocated */
191 /* with GC_gcj_debug_kind. */
192 GC_API GC_ATTR_MALLOC void * GC_CALL GC_debug_gcj_malloc(size_t lb,
193 void * ptr_to_struct_containing_descr, GC_EXTRA_PARAMS)
194 {
195 void *base, *result;
196 DCL_LOCK_STATE;
197
198 /* We're careful to avoid extra calls, which could */
199 /* confuse the backtrace. */
200 LOCK();
201 maybe_finalize();
202 base = GC_generic_malloc_inner(SIZET_SAT_ADD(lb, DEBUG_BYTES),
203 GC_gcj_debug_kind);
204 if (NULL == base) {
205 GC_oom_func oom_fn = GC_oom_fn;
206 UNLOCK();
207 GC_err_printf("GC_debug_gcj_malloc(%lu, %p) returning NULL (%s:%d)\n",
208 (unsigned long)lb, ptr_to_struct_containing_descr, s, i);
209 return((*oom_fn)(lb));
210 }
211 *((void **)((ptr_t)base + sizeof(oh))) = ptr_to_struct_containing_descr;
212 if (!GC_debugging_started) {
213 GC_start_debugging_inner();
214 }
215 result = GC_store_debug_info_inner(base, (word)lb, s, i);
216 ADD_CALL_CHAIN(base, ra);
217 UNLOCK();
218 GC_dirty(result);
219 REACHABLE_AFTER_DIRTY(ptr_to_struct_containing_descr);
220 return result;
221 }
222
223 /* There is no THREAD_LOCAL_ALLOC for GC_gcj_malloc_ignore_off_page(). */
224 GC_API GC_ATTR_MALLOC void * GC_CALL GC_gcj_malloc_ignore_off_page(size_t lb,
225 void * ptr_to_struct_containing_descr)
226 {
227 ptr_t op;
228 DCL_LOCK_STATE;
229
230 GC_DBG_COLLECT_AT_MALLOC(lb);
231 if(SMALL_OBJ(lb)) {
232 word lg;
233
234 LOCK();
235 lg = GC_size_map[lb];
236 op = GC_gcjobjfreelist[lg];
237 if (EXPECT(0 == op, FALSE)) {
238 maybe_finalize();
239 op = (ptr_t)GENERAL_MALLOC_INNER_IOP(lb, GC_gcj_kind);
240 if (0 == op) {
241 GC_oom_func oom_fn = GC_oom_fn;
242 UNLOCK();
243 return((*oom_fn)(lb));
244 }
245 } else {
246 GC_gcjobjfreelist[lg] = (ptr_t)obj_link(op);
247 GC_bytes_allocd += GRANULES_TO_BYTES((word)lg);
248 }
249 } else {
250 LOCK();
251 maybe_finalize();
252 op = (ptr_t)GENERAL_MALLOC_INNER_IOP(lb, GC_gcj_kind);
253 if (0 == op) {
254 GC_oom_func oom_fn = GC_oom_fn;
255 UNLOCK();
256 return((*oom_fn)(lb));
257 }
258 }
259 *(void **)op = ptr_to_struct_containing_descr;
260 UNLOCK();
261 GC_dirty(op);
262 REACHABLE_AFTER_DIRTY(ptr_to_struct_containing_descr);
263 return (void *)op;
264 }
265
266 #endif /* GC_GCJ_SUPPORT */