Imported version version 6.0alpha7.
[gcc.git] / boehm-gc / gcj_mlc.c
1 /*
2 * Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
3 * Copyright (c) 1999 by Hewlett-Packard Company. All rights reserved.
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 /* Boehm, July 31, 1995 5:02 pm PDT */
16
17 #ifdef GC_GCJ_SUPPORT
18
19 /*
20 * This is an allocator interface tuned for gcj (the GNU static
21 * java compiler).
22 *
23 * Each allocated object has a pointer in its first word to a vtable,
24 * which for our purposes is simply a structure describing the type of
25 * the object.
26 * This descriptor structure contains a GC marking descriptor at offset
27 * MARK_DESCR_OFFSET.
28 *
29 * It is hoped that this interface may also be useful for other systems,
30 * possibly with some tuning of the constants. But the immediate goal
31 * is to get better gcj performance.
32 *
33 * We assume:
34 * 1) We have an ANSI conforming C compiler.
35 * 2) Counting on explicit initialization of this interface is OK.
36 * 3) FASTLOCK is not a significant win.
37 */
38
39 #include "private/gc_priv.h"
40 #include "private/gc_pmark.h"
41 #include "gc_gcj.h"
42 #include "private/dbg_mlc.h"
43
44 GC_bool GC_gcj_malloc_initialized = FALSE;
45
46 int GC_gcj_kind; /* Object kind for objects with descriptors */
47 /* in "vtable". */
48 int GC_gcj_debug_kind; /* The kind of objects that is always marked */
49 /* with a mark proc call. */
50
51 ptr_t * GC_gcjobjfreelist;
52 ptr_t * GC_gcjdebugobjfreelist;
53
54 /* Caller does not hold allocation lock. */
55 void GC_init_gcj_malloc(int mp_index, void * /* really GC_mark_proc */mp)
56 {
57 register int i;
58 DCL_LOCK_STATE;
59
60 GC_init(); /* In case it's not already done. */
61 DISABLE_SIGNALS();
62 LOCK();
63 if (GC_gcj_malloc_initialized) {
64 UNLOCK();
65 ENABLE_SIGNALS();
66 return;
67 }
68 GC_gcj_malloc_initialized = TRUE;
69 GC_mark_procs[mp_index] = (GC_mark_proc)mp;
70 if (mp_index >= GC_n_mark_procs) ABORT("GC_init_gcj_malloc: bad index");
71 /* Set up object kind gcj-style indirect descriptor. */
72 GC_gcjobjfreelist = (ptr_t *)
73 GC_INTERNAL_MALLOC((MAXOBJSZ+1)*sizeof(ptr_t), PTRFREE);
74 if (GC_gcjobjfreelist == 0) ABORT("Couldn't allocate GC_gcjobjfreelist");
75 BZERO(GC_gcjobjfreelist, (MAXOBJSZ+1)*sizeof(ptr_t));
76 GC_gcj_kind = GC_n_kinds++;
77 GC_obj_kinds[GC_gcj_kind].ok_freelist = GC_gcjobjfreelist;
78 GC_obj_kinds[GC_gcj_kind].ok_reclaim_list = 0;
79 GC_obj_kinds[GC_gcj_kind].ok_descriptor =
80 (((word)(-MARK_DESCR_OFFSET - GC_INDIR_PER_OBJ_BIAS)) | GC_DS_PER_OBJECT);
81 GC_obj_kinds[GC_gcj_kind].ok_relocate_descr = FALSE;
82 GC_obj_kinds[GC_gcj_kind].ok_init = TRUE;
83 /* Set up object kind for objects that require mark proc call. */
84 GC_gcjdebugobjfreelist = (ptr_t *)
85 GC_INTERNAL_MALLOC((MAXOBJSZ+1)*sizeof(ptr_t), PTRFREE);
86 if (GC_gcjdebugobjfreelist == 0)
87 ABORT("Couldn't allocate GC_gcjdebugobjfreelist");
88 BZERO(GC_gcjdebugobjfreelist, (MAXOBJSZ+1)*sizeof(ptr_t));
89 GC_gcj_debug_kind = GC_n_kinds++;
90 GC_obj_kinds[GC_gcj_debug_kind].ok_freelist = GC_gcjdebugobjfreelist;
91 GC_obj_kinds[GC_gcj_debug_kind].ok_reclaim_list = 0;
92 GC_obj_kinds[GC_gcj_debug_kind].ok_descriptor =
93 GC_MAKE_PROC(mp_index, 1 /* allocated with debug info */);
94 GC_obj_kinds[GC_gcj_debug_kind].ok_relocate_descr = FALSE;
95 GC_obj_kinds[GC_gcj_debug_kind].ok_init = TRUE;
96 UNLOCK();
97 ENABLE_SIGNALS();
98 }
99
100 ptr_t GC_clear_stack();
101
102 #define GENERAL_MALLOC(lb,k) \
103 (GC_PTR)GC_clear_stack(GC_generic_malloc_inner((word)lb, k))
104
105 #define GENERAL_MALLOC_IOP(lb,k) \
106 (GC_PTR)GC_clear_stack(GC_generic_malloc_inner_ignore_off_page(lb, k))
107
108 /* Allocate an object, clear it, and store the pointer to the */
109 /* type structure (vtable in gcj). */
110 /* This adds a byte at the end of the object if GC_malloc would.*/
111 void * GC_gcj_malloc(size_t lb, void * ptr_to_struct_containing_descr)
112 {
113 register ptr_t op;
114 register ptr_t * opp;
115 register word lw;
116 DCL_LOCK_STATE;
117
118 if( EXPECT(SMALL_OBJ(lb), 1) ) {
119 # ifdef MERGE_SIZES
120 lw = GC_size_map[lb];
121 # else
122 lw = ALIGNED_WORDS(lb);
123 # endif
124 opp = &(GC_gcjobjfreelist[lw]);
125 LOCK();
126 op = *opp;
127 if( EXPECT(op == 0, 0)) {
128 op = (ptr_t)GENERAL_MALLOC((word)lb, GC_gcj_kind);
129 if (0 == op) {
130 UNLOCK();
131 return(GC_oom_fn(lb));
132 }
133 # ifdef MERGE_SIZES
134 lw = GC_size_map[lb]; /* May have been uninitialized. */
135 # endif
136 } else {
137 *opp = obj_link(op);
138 GC_words_allocd += lw;
139 }
140 *(void **)op = ptr_to_struct_containing_descr;
141 UNLOCK();
142 } else {
143 LOCK();
144 op = (ptr_t)GENERAL_MALLOC((word)lb, GC_gcj_kind);
145 if (0 == op) {
146 UNLOCK();
147 return(GC_oom_fn(lb));
148 }
149 *(void **)op = ptr_to_struct_containing_descr;
150 UNLOCK();
151 }
152 return((GC_PTR) op);
153 }
154
155 /* Similar to GC_gcj_malloc, but add debug info. This is allocated */
156 /* with GC_gcj_debug_kind. */
157 GC_PTR GC_debug_gcj_malloc(size_t lb, void * ptr_to_struct_containing_descr,
158 GC_EXTRA_PARAMS)
159 {
160 GC_PTR result;
161
162 /* We clone the code from GC_debug_gcj_malloc, so that we */
163 /* dont end up with extra frames on the stack, which could */
164 /* confuse the backtrace. */
165 LOCK();
166 result = GC_generic_malloc_inner(lb + DEBUG_BYTES, GC_gcj_debug_kind);
167 if (result == 0) {
168 UNLOCK();
169 GC_err_printf2("GC_debug_gcj_malloc(%ld, 0x%lx) returning NIL (",
170 (unsigned long) lb,
171 (unsigned long) ptr_to_struct_containing_descr);
172 GC_err_puts(s);
173 GC_err_printf1(":%ld)\n", (unsigned long)i);
174 return(GC_oom_fn(lb));
175 }
176 *((void **)((ptr_t)result + sizeof(oh))) = ptr_to_struct_containing_descr;
177 UNLOCK();
178 if (!GC_debugging_started) {
179 GC_start_debugging();
180 }
181 ADD_CALL_CHAIN(result, ra);
182 return (GC_store_debug_info(result, (word)lb, s, (word)i));
183 }
184
185 /* Similar to GC_gcj_malloc, but the size is in words, and we don't */
186 /* adjust it. The size is assumed to be such that it can be */
187 /* allocated as a small object. */
188 void * GC_gcj_fast_malloc(size_t lw, void * ptr_to_struct_containing_descr)
189 {
190 ptr_t op;
191 ptr_t * opp;
192 DCL_LOCK_STATE;
193
194 opp = &(GC_gcjobjfreelist[lw]);
195 LOCK();
196 op = *opp;
197 if( EXPECT(op == 0, 0) ) {
198 op = (ptr_t)GC_clear_stack(
199 GC_generic_malloc_words_small_inner(lw, GC_gcj_kind));
200 if (0 == op) {
201 UNLOCK();
202 return GC_oom_fn(WORDS_TO_BYTES(lw));
203 }
204 } else {
205 *opp = obj_link(op);
206 GC_words_allocd += lw;
207 }
208 *(void **)op = ptr_to_struct_containing_descr;
209 UNLOCK();
210 return((GC_PTR) op);
211 }
212
213 /* And a debugging version of the above: */
214 void * GC_debug_gcj_fast_malloc(size_t lw,
215 void * ptr_to_struct_containing_descr,
216 GC_EXTRA_PARAMS)
217 {
218 GC_PTR result;
219 size_t lb = WORDS_TO_BYTES(lw);
220
221 /* We clone the code from GC_debug_gcj_malloc, so that we */
222 /* dont end up with extra frames on the stack, which could */
223 /* confuse the backtrace. */
224 LOCK();
225 result = GC_generic_malloc_inner(lb + DEBUG_BYTES, GC_gcj_debug_kind);
226 if (result == 0) {
227 UNLOCK();
228 GC_err_printf2("GC_debug_gcj_fast_malloc(%ld, 0x%lx) returning NIL (",
229 (unsigned long) lw,
230 (unsigned long) ptr_to_struct_containing_descr);
231 GC_err_puts(s);
232 GC_err_printf1(":%ld)\n", (unsigned long)i);
233 return GC_oom_fn(WORDS_TO_BYTES(lw));
234 }
235 *((void **)((ptr_t)result + sizeof(oh))) = ptr_to_struct_containing_descr;
236 UNLOCK();
237 if (!GC_debugging_started) {
238 GC_start_debugging();
239 }
240 ADD_CALL_CHAIN(result, ra);
241 return (GC_store_debug_info(result, (word)lb, s, (word)i));
242 }
243
244 void * GC_gcj_malloc_ignore_off_page(size_t lb,
245 void * ptr_to_struct_containing_descr)
246 {
247 register ptr_t op;
248 register ptr_t * opp;
249 register word lw;
250 DCL_LOCK_STATE;
251
252 if( SMALL_OBJ(lb) ) {
253 # ifdef MERGE_SIZES
254 lw = GC_size_map[lb];
255 # else
256 lw = ALIGNED_WORDS(lb);
257 # endif
258 opp = &(GC_gcjobjfreelist[lw]);
259 LOCK();
260 if( (op = *opp) == 0 ) {
261 op = (ptr_t)GENERAL_MALLOC_IOP(lb, GC_gcj_kind);
262 # ifdef MERGE_SIZES
263 lw = GC_size_map[lb]; /* May have been uninitialized. */
264 # endif
265 } else {
266 *opp = obj_link(op);
267 GC_words_allocd += lw;
268 }
269 *(void **)op = ptr_to_struct_containing_descr;
270 UNLOCK();
271 } else {
272 op = (ptr_t)GENERAL_MALLOC_IOP(lb, GC_gcj_kind);
273 if (0 != op) {
274 *(void **)op = ptr_to_struct_containing_descr;
275 }
276 UNLOCK();
277 }
278 return((GC_PTR) op);
279 }
280
281 #else
282
283 char GC_no_gcj_support;
284
285 #endif /* GC_GCJ_SUPPORT */