2 * Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
3 * Copyright (c) 1999 by Hewlett-Packard Company. All rights reserved.
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.
15 /* Boehm, July 31, 1995 5:02 pm PDT */
20 * This is an allocator interface tuned for gcj (the GNU static
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
26 * This descriptor structure contains a GC marking descriptor at offset
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.
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.
39 #include "private/gc_pmark.h"
41 #include "private/dbg_mlc.h"
43 GC_bool GC_gcj_malloc_initialized
= FALSE
;
45 int GC_gcj_kind
; /* Object kind for objects with descriptors */
47 int GC_gcj_debug_kind
; /* The kind of objects that is always marked */
48 /* with a mark proc call. */
50 ptr_t
* GC_gcjobjfreelist
;
51 ptr_t
* GC_gcjdebugobjfreelist
;
53 /* Caller does not hold allocation lock. */
54 void GC_init_gcj_malloc(int mp_index
, void * /* really GC_mark_proc */mp
)
57 GC_bool ignore_gcj_info
;
60 GC_init(); /* In case it's not already done. */
63 if (GC_gcj_malloc_initialized
) {
68 GC_gcj_malloc_initialized
= TRUE
;
69 ignore_gcj_info
= (0 != GETENV("GC_IGNORE_GCJ_INFO"));
71 if (GC_print_stats
&& ignore_gcj_info
) {
72 GC_printf0("Gcj-style type information is disabled!\n");
75 GC_mark_procs
[mp_index
] = (GC_mark_proc
)mp
;
76 if (mp_index
>= GC_n_mark_procs
) ABORT("GC_init_gcj_malloc: bad index");
77 /* Set up object kind gcj-style indirect descriptor. */
78 GC_gcjobjfreelist
= (ptr_t
*)
79 GC_INTERNAL_MALLOC((MAXOBJSZ
+1)*sizeof(ptr_t
), PTRFREE
);
80 if (GC_gcjobjfreelist
== 0) ABORT("Couldn't allocate GC_gcjobjfreelist");
81 BZERO(GC_gcjobjfreelist
, (MAXOBJSZ
+1)*sizeof(ptr_t
));
82 GC_gcj_kind
= GC_n_kinds
++;
83 GC_obj_kinds
[GC_gcj_kind
].ok_freelist
= GC_gcjobjfreelist
;
84 GC_obj_kinds
[GC_gcj_kind
].ok_reclaim_list
= 0;
85 if (ignore_gcj_info
) {
86 /* Use a simple length-based descriptor, thus forcing a fully */
87 /* conservative scan. */
88 GC_obj_kinds
[GC_gcj_kind
].ok_descriptor
= (0 | GC_DS_LENGTH
);
89 GC_obj_kinds
[GC_gcj_kind
].ok_relocate_descr
= TRUE
;
91 GC_obj_kinds
[GC_gcj_kind
].ok_descriptor
=
92 (((word
)(-MARK_DESCR_OFFSET
- GC_INDIR_PER_OBJ_BIAS
))
94 GC_obj_kinds
[GC_gcj_kind
].ok_relocate_descr
= FALSE
;
96 GC_obj_kinds
[GC_gcj_kind
].ok_init
= TRUE
;
97 /* Set up object kind for objects that require mark proc call. */
98 GC_gcjdebugobjfreelist
= (ptr_t
*)
99 GC_INTERNAL_MALLOC((MAXOBJSZ
+1)*sizeof(ptr_t
), PTRFREE
);
100 if (GC_gcjdebugobjfreelist
== 0)
101 ABORT("Couldn't allocate GC_gcjdebugobjfreelist");
102 BZERO(GC_gcjdebugobjfreelist
, (MAXOBJSZ
+1)*sizeof(ptr_t
));
103 GC_gcj_debug_kind
= GC_n_kinds
++;
104 GC_obj_kinds
[GC_gcj_debug_kind
].ok_freelist
= GC_gcjdebugobjfreelist
;
105 GC_obj_kinds
[GC_gcj_debug_kind
].ok_reclaim_list
= 0;
106 if (ignore_gcj_info
) {
107 GC_obj_kinds
[GC_gcj_kind
].ok_descriptor
= (0 | GC_DS_LENGTH
);
108 GC_obj_kinds
[GC_gcj_kind
].ok_relocate_descr
= TRUE
;
110 GC_obj_kinds
[GC_gcj_debug_kind
].ok_descriptor
=
111 GC_MAKE_PROC(mp_index
, 1 /* allocated with debug info */);
112 GC_obj_kinds
[GC_gcj_debug_kind
].ok_relocate_descr
= FALSE
;
114 GC_obj_kinds
[GC_gcj_debug_kind
].ok_init
= TRUE
;
119 ptr_t
GC_clear_stack();
121 #define GENERAL_MALLOC(lb,k) \
122 (GC_PTR)GC_clear_stack(GC_generic_malloc_inner((word)lb, k))
124 #define GENERAL_MALLOC_IOP(lb,k) \
125 (GC_PTR)GC_clear_stack(GC_generic_malloc_inner_ignore_off_page(lb, k))
127 /* Allocate an object, clear it, and store the pointer to the */
128 /* type structure (vtable in gcj). */
129 /* This adds a byte at the end of the object if GC_malloc would.*/
130 void * GC_gcj_malloc(size_t lb
, void * ptr_to_struct_containing_descr
)
133 register ptr_t
* opp
;
137 if( EXPECT(SMALL_OBJ(lb
), 1) ) {
139 lw
= GC_size_map
[lb
];
141 lw
= ALIGNED_WORDS(lb
);
143 opp
= &(GC_gcjobjfreelist
[lw
]);
146 if( EXPECT(op
== 0, 0)) {
147 op
= (ptr_t
)GENERAL_MALLOC((word
)lb
, GC_gcj_kind
);
150 return(GC_oom_fn(lb
));
153 lw
= GC_size_map
[lb
]; /* May have been uninitialized. */
157 GC_words_allocd
+= lw
;
159 *(void **)op
= ptr_to_struct_containing_descr
;
163 op
= (ptr_t
)GENERAL_MALLOC((word
)lb
, GC_gcj_kind
);
166 return(GC_oom_fn(lb
));
168 *(void **)op
= ptr_to_struct_containing_descr
;
174 /* Similar to GC_gcj_malloc, but add debug info. This is allocated */
175 /* with GC_gcj_debug_kind. */
176 GC_PTR
GC_debug_gcj_malloc(size_t lb
, void * ptr_to_struct_containing_descr
,
181 /* We clone the code from GC_debug_gcj_malloc, so that we */
182 /* dont end up with extra frames on the stack, which could */
183 /* confuse the backtrace. */
185 result
= GC_generic_malloc_inner(lb
+ DEBUG_BYTES
, GC_gcj_debug_kind
);
188 GC_err_printf2("GC_debug_gcj_malloc(%ld, 0x%lx) returning NIL (",
190 (unsigned long) ptr_to_struct_containing_descr
);
192 GC_err_printf1(":%ld)\n", (unsigned long)i
);
193 return(GC_oom_fn(lb
));
195 *((void **)((ptr_t
)result
+ sizeof(oh
))) = ptr_to_struct_containing_descr
;
197 if (!GC_debugging_started
) {
198 GC_start_debugging();
200 ADD_CALL_CHAIN(result
, ra
);
201 return (GC_store_debug_info(result
, (word
)lb
, s
, (word
)i
));
204 /* Similar to GC_gcj_malloc, but the size is in words, and we don't */
205 /* adjust it. The size is assumed to be such that it can be */
206 /* allocated as a small object. */
207 void * GC_gcj_fast_malloc(size_t lw
, void * ptr_to_struct_containing_descr
)
213 opp
= &(GC_gcjobjfreelist
[lw
]);
216 if( EXPECT(op
== 0, 0) ) {
217 op
= (ptr_t
)GC_clear_stack(
218 GC_generic_malloc_words_small_inner(lw
, GC_gcj_kind
));
221 return GC_oom_fn(WORDS_TO_BYTES(lw
));
225 GC_words_allocd
+= lw
;
227 *(void **)op
= ptr_to_struct_containing_descr
;
232 /* And a debugging version of the above: */
233 void * GC_debug_gcj_fast_malloc(size_t lw
,
234 void * ptr_to_struct_containing_descr
,
238 size_t lb
= WORDS_TO_BYTES(lw
);
240 /* We clone the code from GC_debug_gcj_malloc, so that we */
241 /* dont end up with extra frames on the stack, which could */
242 /* confuse the backtrace. */
244 result
= GC_generic_malloc_inner(lb
+ DEBUG_BYTES
, GC_gcj_debug_kind
);
247 GC_err_printf2("GC_debug_gcj_fast_malloc(%ld, 0x%lx) returning NIL (",
249 (unsigned long) ptr_to_struct_containing_descr
);
251 GC_err_printf1(":%ld)\n", (unsigned long)i
);
252 return GC_oom_fn(WORDS_TO_BYTES(lw
));
254 *((void **)((ptr_t
)result
+ sizeof(oh
))) = ptr_to_struct_containing_descr
;
256 if (!GC_debugging_started
) {
257 GC_start_debugging();
259 ADD_CALL_CHAIN(result
, ra
);
260 return (GC_store_debug_info(result
, (word
)lb
, s
, (word
)i
));
263 void * GC_gcj_malloc_ignore_off_page(size_t lb
,
264 void * ptr_to_struct_containing_descr
)
267 register ptr_t
* opp
;
271 if( SMALL_OBJ(lb
) ) {
273 lw
= GC_size_map
[lb
];
275 lw
= ALIGNED_WORDS(lb
);
277 opp
= &(GC_gcjobjfreelist
[lw
]);
279 if( (op
= *opp
) == 0 ) {
280 op
= (ptr_t
)GENERAL_MALLOC_IOP(lb
, GC_gcj_kind
);
282 lw
= GC_size_map
[lb
]; /* May have been uninitialized. */
286 GC_words_allocd
+= lw
;
288 *(void **)op
= ptr_to_struct_containing_descr
;
291 op
= (ptr_t
)GENERAL_MALLOC_IOP(lb
, GC_gcj_kind
);
293 *(void **)op
= ptr_to_struct_containing_descr
;
302 char GC_no_gcj_support
;
304 #endif /* GC_GCJ_SUPPORT */