|
Packit |
d28291 |
/*
|
|
Packit |
d28291 |
* Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
|
|
Packit |
d28291 |
* Copyright (c) 1999-2004 Hewlett-Packard Development Company, L.P.
|
|
Packit |
d28291 |
*
|
|
Packit |
d28291 |
* THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
|
|
Packit |
d28291 |
* OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
|
|
Packit |
d28291 |
*
|
|
Packit |
d28291 |
* Permission is hereby granted to use or copy this program
|
|
Packit |
d28291 |
* for any purpose, provided the above notices are retained on all copies.
|
|
Packit |
d28291 |
* Permission to modify the code and to distribute modified code is granted,
|
|
Packit |
d28291 |
* provided the above notices are retained, and a notice that the code was
|
|
Packit |
d28291 |
* modified is included with the above copyright notice.
|
|
Packit |
d28291 |
*
|
|
Packit |
d28291 |
*/
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
#include "private/gc_pmark.h" /* includes gc_priv.h */
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
#ifdef GC_GCJ_SUPPORT
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
/*
|
|
Packit |
d28291 |
* This is an allocator interface tuned for gcj (the GNU static
|
|
Packit |
d28291 |
* java compiler).
|
|
Packit |
d28291 |
*
|
|
Packit |
d28291 |
* Each allocated object has a pointer in its first word to a vtable,
|
|
Packit |
d28291 |
* which for our purposes is simply a structure describing the type of
|
|
Packit |
d28291 |
* the object.
|
|
Packit |
d28291 |
* This descriptor structure contains a GC marking descriptor at offset
|
|
Packit |
d28291 |
* MARK_DESCR_OFFSET.
|
|
Packit |
d28291 |
*
|
|
Packit |
d28291 |
* It is hoped that this interface may also be useful for other systems,
|
|
Packit |
d28291 |
* possibly with some tuning of the constants. But the immediate goal
|
|
Packit |
d28291 |
* is to get better gcj performance.
|
|
Packit |
d28291 |
*
|
|
Packit |
d28291 |
* We assume:
|
|
Packit |
d28291 |
* 1) Counting on explicit initialization of this interface is OK;
|
|
Packit |
d28291 |
* 2) FASTLOCK is not a significant win.
|
|
Packit |
d28291 |
*/
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
#include "gc_gcj.h"
|
|
Packit |
d28291 |
#include "private/dbg_mlc.h"
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
#ifdef GC_ASSERTIONS
|
|
Packit |
d28291 |
GC_INNER /* variable is also used in thread_local_alloc.c */
|
|
Packit |
d28291 |
#else
|
|
Packit |
d28291 |
STATIC
|
|
Packit |
d28291 |
#endif
|
|
Packit |
d28291 |
GC_bool GC_gcj_malloc_initialized = FALSE;
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
int GC_gcj_kind = 0; /* Object kind for objects with descriptors */
|
|
Packit |
d28291 |
/* in "vtable". */
|
|
Packit |
d28291 |
int GC_gcj_debug_kind = 0;
|
|
Packit |
d28291 |
/* The kind of objects that is always marked */
|
|
Packit |
d28291 |
/* with a mark proc call. */
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
GC_INNER ptr_t * GC_gcjobjfreelist = NULL;
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
STATIC struct GC_ms_entry * GC_gcj_fake_mark_proc(word * addr GC_ATTR_UNUSED,
|
|
Packit |
d28291 |
struct GC_ms_entry *mark_stack_ptr,
|
|
Packit |
d28291 |
struct GC_ms_entry * mark_stack_limit GC_ATTR_UNUSED,
|
|
Packit |
d28291 |
word env GC_ATTR_UNUSED)
|
|
Packit |
d28291 |
{
|
|
Packit |
d28291 |
ABORT_RET("No client gcj mark proc is specified");
|
|
Packit |
d28291 |
return mark_stack_ptr;
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
/* Caller does not hold allocation lock. */
|
|
Packit |
d28291 |
GC_API void GC_CALL GC_init_gcj_malloc(int mp_index,
|
|
Packit |
d28291 |
void * /* really GC_mark_proc */mp)
|
|
Packit |
d28291 |
{
|
|
Packit |
d28291 |
GC_bool ignore_gcj_info;
|
|
Packit |
d28291 |
DCL_LOCK_STATE;
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
if (mp == 0) /* In case GC_DS_PROC is unused. */
|
|
Packit |
d28291 |
mp = (void *)(word)GC_gcj_fake_mark_proc;
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
GC_init(); /* In case it's not already done. */
|
|
Packit |
d28291 |
LOCK();
|
|
Packit |
d28291 |
if (GC_gcj_malloc_initialized) {
|
|
Packit |
d28291 |
UNLOCK();
|
|
Packit |
d28291 |
return;
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
GC_gcj_malloc_initialized = TRUE;
|
|
Packit |
d28291 |
# ifdef GC_IGNORE_GCJ_INFO
|
|
Packit |
d28291 |
/* This is useful for debugging on platforms with missing getenv(). */
|
|
Packit |
d28291 |
ignore_gcj_info = 1;
|
|
Packit |
d28291 |
# else
|
|
Packit |
d28291 |
ignore_gcj_info = (0 != GETENV("GC_IGNORE_GCJ_INFO"));
|
|
Packit |
d28291 |
# endif
|
|
Packit |
d28291 |
if (ignore_gcj_info) {
|
|
Packit |
d28291 |
GC_COND_LOG_PRINTF("Gcj-style type information is disabled!\n");
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
GC_ASSERT(GC_mark_procs[mp_index] == (GC_mark_proc)0); /* unused */
|
|
Packit |
d28291 |
GC_mark_procs[mp_index] = (GC_mark_proc)(word)mp;
|
|
Packit |
d28291 |
if ((unsigned)mp_index >= GC_n_mark_procs)
|
|
Packit |
d28291 |
ABORT("GC_init_gcj_malloc: bad index");
|
|
Packit |
d28291 |
/* Set up object kind gcj-style indirect descriptor. */
|
|
Packit |
d28291 |
GC_gcjobjfreelist = (ptr_t *)GC_new_free_list_inner();
|
|
Packit |
d28291 |
if (ignore_gcj_info) {
|
|
Packit |
d28291 |
/* Use a simple length-based descriptor, thus forcing a fully */
|
|
Packit |
d28291 |
/* conservative scan. */
|
|
Packit |
d28291 |
GC_gcj_kind = GC_new_kind_inner((void **)GC_gcjobjfreelist,
|
|
Packit |
d28291 |
/* 0 | */ GC_DS_LENGTH,
|
|
Packit |
d28291 |
TRUE, TRUE);
|
|
Packit |
d28291 |
} else {
|
|
Packit |
d28291 |
GC_gcj_kind = GC_new_kind_inner(
|
|
Packit |
d28291 |
(void **)GC_gcjobjfreelist,
|
|
Packit |
d28291 |
(((word)(-(signed_word)MARK_DESCR_OFFSET
|
|
Packit |
d28291 |
- GC_INDIR_PER_OBJ_BIAS))
|
|
Packit |
d28291 |
| GC_DS_PER_OBJECT),
|
|
Packit |
d28291 |
FALSE, TRUE);
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
/* Set up object kind for objects that require mark proc call. */
|
|
Packit |
d28291 |
if (ignore_gcj_info) {
|
|
Packit |
d28291 |
GC_gcj_debug_kind = GC_gcj_kind;
|
|
Packit |
d28291 |
} else {
|
|
Packit |
d28291 |
GC_gcj_debug_kind = GC_new_kind_inner(GC_new_free_list_inner(),
|
|
Packit |
d28291 |
GC_MAKE_PROC(mp_index,
|
|
Packit |
d28291 |
1 /* allocated with debug info */),
|
|
Packit |
d28291 |
FALSE, TRUE);
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
UNLOCK();
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
#define GENERAL_MALLOC_INNER(lb,k) \
|
|
Packit |
d28291 |
GC_clear_stack(GC_generic_malloc_inner(lb, k))
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
#define GENERAL_MALLOC_INNER_IOP(lb,k) \
|
|
Packit |
d28291 |
GC_clear_stack(GC_generic_malloc_inner_ignore_off_page(lb, k))
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
/* We need a mechanism to release the lock and invoke finalizers. */
|
|
Packit |
d28291 |
/* We don't really have an opportunity to do this on a rarely executed */
|
|
Packit |
d28291 |
/* path on which the lock is not held. Thus we check at a */
|
|
Packit |
d28291 |
/* rarely executed point at which it is safe to release the lock. */
|
|
Packit |
d28291 |
/* We do this even where we could just call GC_INVOKE_FINALIZERS, */
|
|
Packit |
d28291 |
/* since it's probably cheaper and certainly more uniform. */
|
|
Packit |
d28291 |
/* FIXME - Consider doing the same elsewhere? */
|
|
Packit |
d28291 |
static void maybe_finalize(void)
|
|
Packit |
d28291 |
{
|
|
Packit |
d28291 |
static word last_finalized_no = 0;
|
|
Packit |
d28291 |
DCL_LOCK_STATE;
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
if (GC_gc_no == last_finalized_no ||
|
|
Packit |
d28291 |
!EXPECT(GC_is_initialized, TRUE)) return;
|
|
Packit |
d28291 |
UNLOCK();
|
|
Packit |
d28291 |
GC_INVOKE_FINALIZERS();
|
|
Packit |
d28291 |
LOCK();
|
|
Packit |
d28291 |
last_finalized_no = GC_gc_no;
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
/* Allocate an object, clear it, and store the pointer to the */
|
|
Packit |
d28291 |
/* type structure (vtable in gcj). */
|
|
Packit |
d28291 |
/* This adds a byte at the end of the object if GC_malloc would.*/
|
|
Packit |
d28291 |
#ifdef THREAD_LOCAL_ALLOC
|
|
Packit |
d28291 |
GC_INNER void * GC_core_gcj_malloc(size_t lb,
|
|
Packit |
d28291 |
void * ptr_to_struct_containing_descr)
|
|
Packit |
d28291 |
#else
|
|
Packit |
d28291 |
GC_API GC_ATTR_MALLOC void * GC_CALL GC_gcj_malloc(size_t lb,
|
|
Packit |
d28291 |
void * ptr_to_struct_containing_descr)
|
|
Packit |
d28291 |
#endif
|
|
Packit |
d28291 |
{
|
|
Packit |
d28291 |
ptr_t op;
|
|
Packit |
d28291 |
DCL_LOCK_STATE;
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
GC_DBG_COLLECT_AT_MALLOC(lb);
|
|
Packit |
d28291 |
if(SMALL_OBJ(lb)) {
|
|
Packit |
d28291 |
word lg = GC_size_map[lb];
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
LOCK();
|
|
Packit |
d28291 |
op = GC_gcjobjfreelist[lg];
|
|
Packit |
d28291 |
if(EXPECT(0 == op, FALSE)) {
|
|
Packit |
d28291 |
maybe_finalize();
|
|
Packit |
d28291 |
op = (ptr_t)GENERAL_MALLOC_INNER((word)lb, GC_gcj_kind);
|
|
Packit |
d28291 |
if (0 == op) {
|
|
Packit |
d28291 |
GC_oom_func oom_fn = GC_oom_fn;
|
|
Packit |
d28291 |
UNLOCK();
|
|
Packit |
d28291 |
return((*oom_fn)(lb));
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
} else {
|
|
Packit |
d28291 |
GC_gcjobjfreelist[lg] = obj_link(op);
|
|
Packit |
d28291 |
GC_bytes_allocd += GRANULES_TO_BYTES((word)lg);
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
*(void **)op = ptr_to_struct_containing_descr;
|
|
Packit |
d28291 |
GC_ASSERT(((void **)op)[1] == 0);
|
|
Packit |
d28291 |
UNLOCK();
|
|
Packit |
d28291 |
} else {
|
|
Packit |
d28291 |
LOCK();
|
|
Packit |
d28291 |
maybe_finalize();
|
|
Packit |
d28291 |
op = (ptr_t)GENERAL_MALLOC_INNER((word)lb, GC_gcj_kind);
|
|
Packit |
d28291 |
if (0 == op) {
|
|
Packit |
d28291 |
GC_oom_func oom_fn = GC_oom_fn;
|
|
Packit |
d28291 |
UNLOCK();
|
|
Packit |
d28291 |
return((*oom_fn)(lb));
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
*(void **)op = ptr_to_struct_containing_descr;
|
|
Packit |
d28291 |
UNLOCK();
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
return((void *) op);
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
/* Similar to GC_gcj_malloc, but add debug info. This is allocated */
|
|
Packit |
d28291 |
/* with GC_gcj_debug_kind. */
|
|
Packit |
d28291 |
GC_API GC_ATTR_MALLOC void * GC_CALL GC_debug_gcj_malloc(size_t lb,
|
|
Packit |
d28291 |
void * ptr_to_struct_containing_descr, GC_EXTRA_PARAMS)
|
|
Packit |
d28291 |
{
|
|
Packit |
d28291 |
void * result;
|
|
Packit |
d28291 |
DCL_LOCK_STATE;
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
/* We're careful to avoid extra calls, which could */
|
|
Packit |
d28291 |
/* confuse the backtrace. */
|
|
Packit |
d28291 |
LOCK();
|
|
Packit |
d28291 |
maybe_finalize();
|
|
Packit |
d28291 |
result = GC_generic_malloc_inner(SIZET_SAT_ADD(lb, DEBUG_BYTES),
|
|
Packit |
d28291 |
GC_gcj_debug_kind);
|
|
Packit |
d28291 |
if (result == 0) {
|
|
Packit |
d28291 |
GC_oom_func oom_fn = GC_oom_fn;
|
|
Packit |
d28291 |
UNLOCK();
|
|
Packit |
d28291 |
GC_err_printf("GC_debug_gcj_malloc(%lu, %p) returning NULL (%s:%d)\n",
|
|
Packit |
d28291 |
(unsigned long)lb, ptr_to_struct_containing_descr, s, i);
|
|
Packit |
d28291 |
return((*oom_fn)(lb));
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
*((void **)((ptr_t)result + sizeof(oh))) = ptr_to_struct_containing_descr;
|
|
Packit |
d28291 |
UNLOCK();
|
|
Packit |
d28291 |
if (!GC_debugging_started) {
|
|
Packit |
d28291 |
GC_start_debugging();
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
ADD_CALL_CHAIN(result, ra);
|
|
Packit |
d28291 |
return (GC_store_debug_info(result, (word)lb, s, i));
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
/* There is no THREAD_LOCAL_ALLOC for GC_gcj_malloc_ignore_off_page(). */
|
|
Packit |
d28291 |
GC_API GC_ATTR_MALLOC void * GC_CALL GC_gcj_malloc_ignore_off_page(size_t lb,
|
|
Packit |
d28291 |
void * ptr_to_struct_containing_descr)
|
|
Packit |
d28291 |
{
|
|
Packit |
d28291 |
ptr_t op;
|
|
Packit |
d28291 |
DCL_LOCK_STATE;
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
GC_DBG_COLLECT_AT_MALLOC(lb);
|
|
Packit |
d28291 |
if(SMALL_OBJ(lb)) {
|
|
Packit |
d28291 |
word lg = GC_size_map[lb];
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
LOCK();
|
|
Packit |
d28291 |
op = GC_gcjobjfreelist[lg];
|
|
Packit |
d28291 |
if (EXPECT(0 == op, FALSE)) {
|
|
Packit |
d28291 |
maybe_finalize();
|
|
Packit |
d28291 |
op = (ptr_t)GENERAL_MALLOC_INNER_IOP(lb, GC_gcj_kind);
|
|
Packit |
d28291 |
if (0 == op) {
|
|
Packit |
d28291 |
GC_oom_func oom_fn = GC_oom_fn;
|
|
Packit |
d28291 |
UNLOCK();
|
|
Packit |
d28291 |
return((*oom_fn)(lb));
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
} else {
|
|
Packit |
d28291 |
GC_gcjobjfreelist[lg] = obj_link(op);
|
|
Packit |
d28291 |
GC_bytes_allocd += GRANULES_TO_BYTES((word)lg);
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
} else {
|
|
Packit |
d28291 |
LOCK();
|
|
Packit |
d28291 |
maybe_finalize();
|
|
Packit |
d28291 |
op = (ptr_t)GENERAL_MALLOC_INNER_IOP(lb, GC_gcj_kind);
|
|
Packit |
d28291 |
if (0 == op) {
|
|
Packit |
d28291 |
GC_oom_func oom_fn = GC_oom_fn;
|
|
Packit |
d28291 |
UNLOCK();
|
|
Packit |
d28291 |
return((*oom_fn)(lb));
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
*(void **)op = ptr_to_struct_containing_descr;
|
|
Packit |
d28291 |
UNLOCK();
|
|
Packit |
d28291 |
return((void *) op);
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
#endif /* GC_GCJ_SUPPORT */
|