|
Packit |
d28291 |
/*
|
|
Packit |
d28291 |
* Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
|
|
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 |
#include "private/gc_priv.h"
|
|
Packit |
d28291 |
#include "gc_inline.h" /* for GC_malloc_kind */
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
#include <stdio.h>
|
|
Packit |
d28291 |
#include <string.h>
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
/* Allocate reclaim list for kind: */
|
|
Packit |
d28291 |
/* Return TRUE on success */
|
|
Packit |
d28291 |
STATIC GC_bool GC_alloc_reclaim_list(struct obj_kind *kind)
|
|
Packit |
d28291 |
{
|
|
Packit |
d28291 |
struct hblk ** result = (struct hblk **)
|
|
Packit |
d28291 |
GC_scratch_alloc((MAXOBJGRANULES+1) * sizeof(struct hblk *));
|
|
Packit |
d28291 |
if (result == 0) return(FALSE);
|
|
Packit |
d28291 |
BZERO(result, (MAXOBJGRANULES+1)*sizeof(struct hblk *));
|
|
Packit |
d28291 |
kind -> ok_reclaim_list = result;
|
|
Packit |
d28291 |
return(TRUE);
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
GC_INNER GC_bool GC_collect_or_expand(word needed_blocks,
|
|
Packit |
d28291 |
GC_bool ignore_off_page,
|
|
Packit |
d28291 |
GC_bool retry); /* from alloc.c */
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
/* Allocate a large block of size lb bytes. */
|
|
Packit |
d28291 |
/* The block is not cleared. */
|
|
Packit |
d28291 |
/* Flags is 0 or IGNORE_OFF_PAGE. */
|
|
Packit |
d28291 |
/* EXTRA_BYTES were already added to lb. */
|
|
Packit |
d28291 |
GC_INNER ptr_t GC_alloc_large(size_t lb, int k, unsigned flags)
|
|
Packit |
d28291 |
{
|
|
Packit |
d28291 |
struct hblk * h;
|
|
Packit |
d28291 |
word n_blocks;
|
|
Packit |
d28291 |
ptr_t result;
|
|
Packit |
d28291 |
GC_bool retry = FALSE;
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
GC_ASSERT(I_HOLD_LOCK());
|
|
Packit |
d28291 |
lb = ROUNDUP_GRANULE_SIZE(lb);
|
|
Packit |
d28291 |
n_blocks = OBJ_SZ_TO_BLOCKS_CHECKED(lb);
|
|
Packit |
d28291 |
if (!EXPECT(GC_is_initialized, TRUE)) {
|
|
Packit |
d28291 |
DCL_LOCK_STATE;
|
|
Packit |
d28291 |
UNLOCK(); /* just to unset GC_lock_holder */
|
|
Packit |
d28291 |
GC_init();
|
|
Packit |
d28291 |
LOCK();
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
/* Do our share of marking work */
|
|
Packit |
d28291 |
if (GC_incremental && !GC_dont_gc)
|
|
Packit |
d28291 |
GC_collect_a_little_inner((int)n_blocks);
|
|
Packit |
d28291 |
h = GC_allochblk(lb, k, flags);
|
|
Packit |
d28291 |
# ifdef USE_MUNMAP
|
|
Packit |
d28291 |
if (0 == h) {
|
|
Packit |
d28291 |
GC_merge_unmapped();
|
|
Packit |
d28291 |
h = GC_allochblk(lb, k, flags);
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
# endif
|
|
Packit |
d28291 |
while (0 == h && GC_collect_or_expand(n_blocks, flags != 0, retry)) {
|
|
Packit |
d28291 |
h = GC_allochblk(lb, k, flags);
|
|
Packit |
d28291 |
retry = TRUE;
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
if (h == 0) {
|
|
Packit |
d28291 |
result = 0;
|
|
Packit |
d28291 |
} else {
|
|
Packit |
d28291 |
size_t total_bytes = n_blocks * HBLKSIZE;
|
|
Packit |
d28291 |
if (n_blocks > 1) {
|
|
Packit |
d28291 |
GC_large_allocd_bytes += total_bytes;
|
|
Packit |
d28291 |
if (GC_large_allocd_bytes > GC_max_large_allocd_bytes)
|
|
Packit |
d28291 |
GC_max_large_allocd_bytes = GC_large_allocd_bytes;
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
/* FIXME: Do we need some way to reset GC_max_large_allocd_bytes? */
|
|
Packit |
d28291 |
result = h -> hb_body;
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
return result;
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
/* Allocate a large block of size lb bytes. Clear if appropriate. */
|
|
Packit |
d28291 |
/* EXTRA_BYTES were already added to lb. */
|
|
Packit |
d28291 |
STATIC ptr_t GC_alloc_large_and_clear(size_t lb, int k, unsigned flags)
|
|
Packit |
d28291 |
{
|
|
Packit |
d28291 |
ptr_t result;
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
GC_ASSERT(I_HOLD_LOCK());
|
|
Packit |
d28291 |
result = GC_alloc_large(lb, k, flags);
|
|
Packit |
d28291 |
if (result != NULL
|
|
Packit |
d28291 |
&& (GC_debugging_started || GC_obj_kinds[k].ok_init)) {
|
|
Packit |
d28291 |
word n_blocks = OBJ_SZ_TO_BLOCKS(lb);
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
/* Clear the whole block, in case of GC_realloc call. */
|
|
Packit |
d28291 |
BZERO(result, n_blocks * HBLKSIZE);
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
return result;
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
/* allocate lb bytes for an object of kind k. */
|
|
Packit |
d28291 |
/* Should not be used to directly to allocate */
|
|
Packit |
d28291 |
/* objects such as STUBBORN objects that */
|
|
Packit |
d28291 |
/* require special handling on allocation. */
|
|
Packit |
d28291 |
GC_INNER void * GC_generic_malloc_inner(size_t lb, int k)
|
|
Packit |
d28291 |
{
|
|
Packit |
d28291 |
void *op;
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
GC_ASSERT(I_HOLD_LOCK());
|
|
Packit |
d28291 |
GC_ASSERT(k < MAXOBJKINDS);
|
|
Packit |
d28291 |
if (SMALL_OBJ(lb)) {
|
|
Packit |
d28291 |
struct obj_kind * kind = GC_obj_kinds + k;
|
|
Packit |
d28291 |
size_t lg = GC_size_map[lb];
|
|
Packit |
d28291 |
void ** opp = &(kind -> ok_freelist[lg]);
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
op = *opp;
|
|
Packit |
d28291 |
if (EXPECT(0 == op, FALSE)) {
|
|
Packit |
d28291 |
if (lg == 0) {
|
|
Packit |
d28291 |
if (!EXPECT(GC_is_initialized, TRUE)) {
|
|
Packit |
d28291 |
DCL_LOCK_STATE;
|
|
Packit |
d28291 |
UNLOCK(); /* just to unset GC_lock_holder */
|
|
Packit |
d28291 |
GC_init();
|
|
Packit |
d28291 |
LOCK();
|
|
Packit |
d28291 |
lg = GC_size_map[lb];
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
if (0 == lg) {
|
|
Packit |
d28291 |
GC_extend_size_map(lb);
|
|
Packit |
d28291 |
lg = GC_size_map[lb];
|
|
Packit |
d28291 |
GC_ASSERT(lg != 0);
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
/* Retry */
|
|
Packit |
d28291 |
opp = &(kind -> ok_freelist[lg]);
|
|
Packit |
d28291 |
op = *opp;
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
if (0 == op) {
|
|
Packit |
d28291 |
if (0 == kind -> ok_reclaim_list &&
|
|
Packit |
d28291 |
!GC_alloc_reclaim_list(kind))
|
|
Packit |
d28291 |
return NULL;
|
|
Packit |
d28291 |
op = GC_allocobj(lg, k);
|
|
Packit |
d28291 |
if (0 == op)
|
|
Packit |
d28291 |
return NULL;
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
*opp = obj_link(op);
|
|
Packit |
d28291 |
obj_link(op) = 0;
|
|
Packit |
d28291 |
GC_bytes_allocd += GRANULES_TO_BYTES((word)lg);
|
|
Packit |
d28291 |
} else {
|
|
Packit |
d28291 |
op = (ptr_t)GC_alloc_large_and_clear(ADD_SLOP(lb), k, 0);
|
|
Packit |
d28291 |
if (op != NULL)
|
|
Packit |
d28291 |
GC_bytes_allocd += lb;
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
return op;
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
#if defined(DBG_HDRS_ALL) || defined(GC_GCJ_SUPPORT) \
|
|
Packit |
d28291 |
|| !defined(GC_NO_FINALIZATION)
|
|
Packit |
d28291 |
/* Allocate a composite object of size n bytes. The caller */
|
|
Packit |
d28291 |
/* guarantees that pointers past the first page are not relevant. */
|
|
Packit |
d28291 |
GC_INNER void * GC_generic_malloc_inner_ignore_off_page(size_t lb, int k)
|
|
Packit |
d28291 |
{
|
|
Packit |
d28291 |
word lb_adjusted;
|
|
Packit |
d28291 |
void * op;
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
GC_ASSERT(I_HOLD_LOCK());
|
|
Packit |
d28291 |
if (lb <= HBLKSIZE)
|
|
Packit |
d28291 |
return GC_generic_malloc_inner(lb, k);
|
|
Packit |
d28291 |
GC_ASSERT(k < MAXOBJKINDS);
|
|
Packit |
d28291 |
lb_adjusted = ADD_SLOP(lb);
|
|
Packit |
d28291 |
op = GC_alloc_large_and_clear(lb_adjusted, k, IGNORE_OFF_PAGE);
|
|
Packit |
d28291 |
if (op != NULL)
|
|
Packit |
d28291 |
GC_bytes_allocd += lb_adjusted;
|
|
Packit |
d28291 |
return op;
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
#endif
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
#ifdef GC_COLLECT_AT_MALLOC
|
|
Packit |
d28291 |
/* Parameter to force GC at every malloc of size greater or equal to */
|
|
Packit |
d28291 |
/* the given value. This might be handy during debugging. */
|
|
Packit |
d28291 |
# if defined(CPPCHECK)
|
|
Packit |
d28291 |
size_t GC_dbg_collect_at_malloc_min_lb = 16*1024; /* e.g. */
|
|
Packit |
d28291 |
# else
|
|
Packit |
d28291 |
size_t GC_dbg_collect_at_malloc_min_lb = (GC_COLLECT_AT_MALLOC);
|
|
Packit |
d28291 |
# endif
|
|
Packit |
d28291 |
#endif
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
GC_API GC_ATTR_MALLOC void * GC_CALL GC_generic_malloc(size_t lb, int k)
|
|
Packit |
d28291 |
{
|
|
Packit |
d28291 |
void * result;
|
|
Packit |
d28291 |
DCL_LOCK_STATE;
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
GC_ASSERT(k < MAXOBJKINDS);
|
|
Packit |
d28291 |
if (EXPECT(GC_have_errors, FALSE))
|
|
Packit |
d28291 |
GC_print_all_errors();
|
|
Packit |
d28291 |
GC_INVOKE_FINALIZERS();
|
|
Packit |
d28291 |
GC_DBG_COLLECT_AT_MALLOC(lb);
|
|
Packit |
d28291 |
if (SMALL_OBJ(lb)) {
|
|
Packit |
d28291 |
LOCK();
|
|
Packit |
d28291 |
result = GC_generic_malloc_inner(lb, k);
|
|
Packit |
d28291 |
UNLOCK();
|
|
Packit |
d28291 |
} else {
|
|
Packit |
d28291 |
size_t lg;
|
|
Packit |
d28291 |
size_t lb_rounded;
|
|
Packit |
d28291 |
word n_blocks;
|
|
Packit |
d28291 |
GC_bool init;
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
lg = ROUNDED_UP_GRANULES(lb);
|
|
Packit |
d28291 |
lb_rounded = GRANULES_TO_BYTES(lg);
|
|
Packit |
d28291 |
n_blocks = OBJ_SZ_TO_BLOCKS(lb_rounded);
|
|
Packit |
d28291 |
init = GC_obj_kinds[k].ok_init;
|
|
Packit |
d28291 |
LOCK();
|
|
Packit |
d28291 |
result = (ptr_t)GC_alloc_large(lb_rounded, k, 0);
|
|
Packit |
d28291 |
if (0 != result) {
|
|
Packit |
d28291 |
if (GC_debugging_started) {
|
|
Packit |
d28291 |
BZERO(result, n_blocks * HBLKSIZE);
|
|
Packit |
d28291 |
} else {
|
|
Packit |
d28291 |
# ifdef THREADS
|
|
Packit |
d28291 |
/* Clear any memory that might be used for GC descriptors */
|
|
Packit |
d28291 |
/* before we release the lock. */
|
|
Packit |
d28291 |
((word *)result)[0] = 0;
|
|
Packit |
d28291 |
((word *)result)[1] = 0;
|
|
Packit |
d28291 |
((word *)result)[GRANULES_TO_WORDS(lg)-1] = 0;
|
|
Packit |
d28291 |
((word *)result)[GRANULES_TO_WORDS(lg)-2] = 0;
|
|
Packit |
d28291 |
# endif
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
GC_bytes_allocd += lb_rounded;
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
UNLOCK();
|
|
Packit |
d28291 |
if (init && !GC_debugging_started && 0 != result) {
|
|
Packit |
d28291 |
BZERO(result, n_blocks * HBLKSIZE);
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
if (0 == result) {
|
|
Packit |
d28291 |
return((*GC_get_oom_fn())(lb));
|
|
Packit |
d28291 |
} else {
|
|
Packit |
d28291 |
return(result);
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
GC_API GC_ATTR_MALLOC void * GC_CALL GC_malloc_kind_global(size_t lb, int k)
|
|
Packit |
d28291 |
{
|
|
Packit |
d28291 |
GC_ASSERT(k < MAXOBJKINDS);
|
|
Packit |
d28291 |
if (SMALL_OBJ(lb)) {
|
|
Packit |
d28291 |
void *op;
|
|
Packit |
d28291 |
void **opp;
|
|
Packit |
d28291 |
size_t lg = GC_size_map[lb];
|
|
Packit |
d28291 |
DCL_LOCK_STATE;
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
GC_DBG_COLLECT_AT_MALLOC(lb);
|
|
Packit |
d28291 |
LOCK();
|
|
Packit |
d28291 |
opp = &GC_obj_kinds[k].ok_freelist[lg];
|
|
Packit |
d28291 |
op = *opp;
|
|
Packit |
d28291 |
if (EXPECT(op != NULL, TRUE)) {
|
|
Packit |
d28291 |
if (k == PTRFREE) {
|
|
Packit |
d28291 |
*opp = obj_link(op);
|
|
Packit |
d28291 |
} else {
|
|
Packit |
d28291 |
GC_ASSERT(0 == obj_link(op)
|
|
Packit |
d28291 |
|| ((word)obj_link(op)
|
|
Packit |
d28291 |
<= (word)GC_greatest_plausible_heap_addr
|
|
Packit |
d28291 |
&& (word)obj_link(op)
|
|
Packit |
d28291 |
>= (word)GC_least_plausible_heap_addr));
|
|
Packit |
d28291 |
*opp = obj_link(op);
|
|
Packit |
d28291 |
obj_link(op) = 0;
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
GC_bytes_allocd += GRANULES_TO_BYTES((word)lg);
|
|
Packit |
d28291 |
UNLOCK();
|
|
Packit |
d28291 |
return op;
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
UNLOCK();
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
return GENERAL_MALLOC(lb, k);
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
#if defined(THREADS) && !defined(THREAD_LOCAL_ALLOC)
|
|
Packit |
d28291 |
GC_API GC_ATTR_MALLOC void * GC_CALL GC_malloc_kind(size_t lb, int k)
|
|
Packit |
d28291 |
{
|
|
Packit |
d28291 |
return GC_malloc_kind_global(lb, k);
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
#endif
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
/* Allocate lb bytes of atomic (pointer-free) data. */
|
|
Packit |
d28291 |
GC_API GC_ATTR_MALLOC void * GC_CALL GC_malloc_atomic(size_t lb)
|
|
Packit |
d28291 |
{
|
|
Packit |
d28291 |
return GC_malloc_kind(lb, PTRFREE);
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
/* Allocate lb bytes of composite (pointerful) data. */
|
|
Packit |
d28291 |
GC_API GC_ATTR_MALLOC void * GC_CALL GC_malloc(size_t lb)
|
|
Packit |
d28291 |
{
|
|
Packit |
d28291 |
return GC_malloc_kind(lb, NORMAL);
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
GC_API GC_ATTR_MALLOC void * GC_CALL GC_generic_malloc_uncollectable(
|
|
Packit |
d28291 |
size_t lb, int k)
|
|
Packit |
d28291 |
{
|
|
Packit |
d28291 |
void *op;
|
|
Packit |
d28291 |
DCL_LOCK_STATE;
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
GC_ASSERT(k < MAXOBJKINDS);
|
|
Packit |
d28291 |
if (SMALL_OBJ(lb)) {
|
|
Packit |
d28291 |
void **opp;
|
|
Packit |
d28291 |
size_t lg;
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
GC_DBG_COLLECT_AT_MALLOC(lb);
|
|
Packit |
d28291 |
if (EXTRA_BYTES != 0 && lb != 0) lb--;
|
|
Packit |
d28291 |
/* We don't need the extra byte, since this won't be */
|
|
Packit |
d28291 |
/* collected anyway. */
|
|
Packit |
d28291 |
lg = GC_size_map[lb];
|
|
Packit |
d28291 |
LOCK();
|
|
Packit |
d28291 |
opp = &GC_obj_kinds[k].ok_freelist[lg];
|
|
Packit |
d28291 |
op = *opp;
|
|
Packit |
d28291 |
if (EXPECT(op != NULL, TRUE)) {
|
|
Packit |
d28291 |
*opp = obj_link(op);
|
|
Packit |
d28291 |
obj_link(op) = 0;
|
|
Packit |
d28291 |
GC_bytes_allocd += GRANULES_TO_BYTES((word)lg);
|
|
Packit |
d28291 |
/* Mark bit was already set on free list. It will be */
|
|
Packit |
d28291 |
/* cleared only temporarily during a collection, as a */
|
|
Packit |
d28291 |
/* result of the normal free list mark bit clearing. */
|
|
Packit |
d28291 |
GC_non_gc_bytes += GRANULES_TO_BYTES((word)lg);
|
|
Packit |
d28291 |
UNLOCK();
|
|
Packit |
d28291 |
} else {
|
|
Packit |
d28291 |
UNLOCK();
|
|
Packit |
d28291 |
op = GC_generic_malloc(lb, k);
|
|
Packit |
d28291 |
/* For small objects, the free lists are completely marked. */
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
GC_ASSERT(0 == op || GC_is_marked(op));
|
|
Packit |
d28291 |
} else {
|
|
Packit |
d28291 |
hdr * hhdr;
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
op = GC_generic_malloc(lb, k);
|
|
Packit |
d28291 |
if (NULL == op)
|
|
Packit |
d28291 |
return NULL;
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
GC_ASSERT(((word)op & (HBLKSIZE - 1)) == 0); /* large block */
|
|
Packit |
d28291 |
hhdr = HDR(op);
|
|
Packit |
d28291 |
/* We don't need the lock here, since we have an undisguised */
|
|
Packit |
d28291 |
/* pointer. We do need to hold the lock while we adjust */
|
|
Packit |
d28291 |
/* mark bits. */
|
|
Packit |
d28291 |
LOCK();
|
|
Packit |
d28291 |
set_mark_bit_from_hdr(hhdr, 0); /* Only object. */
|
|
Packit |
d28291 |
# ifndef THREADS
|
|
Packit |
d28291 |
GC_ASSERT(hhdr -> hb_n_marks == 0);
|
|
Packit |
d28291 |
/* This is not guaranteed in the multi-threaded case */
|
|
Packit |
d28291 |
/* because the counter could be updated before locking. */
|
|
Packit |
d28291 |
# endif
|
|
Packit |
d28291 |
hhdr -> hb_n_marks = 1;
|
|
Packit |
d28291 |
UNLOCK();
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
return op;
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
/* Allocate lb bytes of pointerful, traced, but not collectible data. */
|
|
Packit |
d28291 |
GC_API GC_ATTR_MALLOC void * GC_CALL GC_malloc_uncollectable(size_t lb)
|
|
Packit |
d28291 |
{
|
|
Packit |
d28291 |
return GC_generic_malloc_uncollectable(lb, UNCOLLECTABLE);
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
#ifdef GC_ATOMIC_UNCOLLECTABLE
|
|
Packit |
d28291 |
/* Allocate lb bytes of pointer-free, untraced, uncollectible data */
|
|
Packit |
d28291 |
/* This is normally roughly equivalent to the system malloc. */
|
|
Packit |
d28291 |
/* But it may be useful if malloc is redefined. */
|
|
Packit |
d28291 |
GC_API GC_ATTR_MALLOC void * GC_CALL
|
|
Packit |
d28291 |
GC_malloc_atomic_uncollectable(size_t lb)
|
|
Packit |
d28291 |
{
|
|
Packit |
d28291 |
return GC_generic_malloc_uncollectable(lb, AUNCOLLECTABLE);
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
#endif /* GC_ATOMIC_UNCOLLECTABLE */
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
#if defined(REDIRECT_MALLOC) && !defined(REDIRECT_MALLOC_IN_HEADER)
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
# ifndef MSWINCE
|
|
Packit |
d28291 |
# include <errno.h>
|
|
Packit |
d28291 |
# endif
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
/* Avoid unnecessary nested procedure calls here, by #defining some */
|
|
Packit |
d28291 |
/* malloc replacements. Otherwise we end up saving a meaningless */
|
|
Packit |
d28291 |
/* return address in the object. It also speeds things up, but it is */
|
|
Packit |
d28291 |
/* admittedly quite ugly. */
|
|
Packit |
d28291 |
# define GC_debug_malloc_replacement(lb) GC_debug_malloc(lb, GC_DBG_EXTRAS)
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
# if defined(CPPCHECK)
|
|
Packit |
d28291 |
# define REDIRECT_MALLOC_F GC_malloc /* e.g. */
|
|
Packit |
d28291 |
# else
|
|
Packit |
d28291 |
# define REDIRECT_MALLOC_F REDIRECT_MALLOC
|
|
Packit |
d28291 |
# endif
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
void * malloc(size_t lb)
|
|
Packit |
d28291 |
{
|
|
Packit |
d28291 |
/* It might help to manually inline the GC_malloc call here. */
|
|
Packit |
d28291 |
/* But any decent compiler should reduce the extra procedure call */
|
|
Packit |
d28291 |
/* to at most a jump instruction in this case. */
|
|
Packit |
d28291 |
# if defined(I386) && defined(GC_SOLARIS_THREADS)
|
|
Packit |
d28291 |
/* Thread initialization can call malloc before we are ready for. */
|
|
Packit |
d28291 |
/* It is not clear that this is enough to help matters. */
|
|
Packit |
d28291 |
/* The thread implementation may well call malloc at other */
|
|
Packit |
d28291 |
/* inopportune times. */
|
|
Packit |
d28291 |
if (!EXPECT(GC_is_initialized, TRUE)) return sbrk(lb);
|
|
Packit |
d28291 |
# endif
|
|
Packit |
d28291 |
return (void *)REDIRECT_MALLOC_F(lb);
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
# if defined(GC_LINUX_THREADS)
|
|
Packit |
d28291 |
STATIC ptr_t GC_libpthread_start = 0;
|
|
Packit |
d28291 |
STATIC ptr_t GC_libpthread_end = 0;
|
|
Packit |
d28291 |
STATIC ptr_t GC_libld_start = 0;
|
|
Packit |
d28291 |
STATIC ptr_t GC_libld_end = 0;
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
STATIC void GC_init_lib_bounds(void)
|
|
Packit |
d28291 |
{
|
|
Packit |
d28291 |
IF_CANCEL(int cancel_state;)
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
if (GC_libpthread_start != 0) return;
|
|
Packit |
d28291 |
DISABLE_CANCEL(cancel_state);
|
|
Packit |
d28291 |
GC_init(); /* if not called yet */
|
|
Packit |
d28291 |
if (!GC_text_mapping("libpthread-",
|
|
Packit |
d28291 |
&GC_libpthread_start, &GC_libpthread_end)) {
|
|
Packit |
d28291 |
WARN("Failed to find libpthread.so text mapping: Expect crash\n", 0);
|
|
Packit |
d28291 |
/* This might still work with some versions of libpthread, */
|
|
Packit |
d28291 |
/* so we don't abort. Perhaps we should. */
|
|
Packit |
d28291 |
/* Generate message only once: */
|
|
Packit |
d28291 |
GC_libpthread_start = (ptr_t)1;
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
if (!GC_text_mapping("ld-", &GC_libld_start, &GC_libld_end)) {
|
|
Packit |
d28291 |
WARN("Failed to find ld.so text mapping: Expect crash\n", 0);
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
RESTORE_CANCEL(cancel_state);
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
# endif /* GC_LINUX_THREADS */
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
void * calloc(size_t n, size_t lb)
|
|
Packit |
d28291 |
{
|
|
Packit |
d28291 |
if ((lb | n) > GC_SQRT_SIZE_MAX /* fast initial test */
|
|
Packit |
d28291 |
&& lb && n > GC_SIZE_MAX / lb)
|
|
Packit |
d28291 |
return (*GC_get_oom_fn())(GC_SIZE_MAX); /* n*lb overflow */
|
|
Packit |
d28291 |
# if defined(GC_LINUX_THREADS)
|
|
Packit |
d28291 |
/* libpthread allocated some memory that is only pointed to by */
|
|
Packit |
d28291 |
/* mmapped thread stacks. Make sure it is not collectible. */
|
|
Packit |
d28291 |
{
|
|
Packit |
d28291 |
static GC_bool lib_bounds_set = FALSE;
|
|
Packit |
d28291 |
ptr_t caller = (ptr_t)__builtin_return_address(0);
|
|
Packit |
d28291 |
/* This test does not need to ensure memory visibility, since */
|
|
Packit |
d28291 |
/* the bounds will be set when/if we create another thread. */
|
|
Packit |
d28291 |
if (!EXPECT(lib_bounds_set, TRUE)) {
|
|
Packit |
d28291 |
GC_init_lib_bounds();
|
|
Packit |
d28291 |
lib_bounds_set = TRUE;
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
if (((word)caller >= (word)GC_libpthread_start
|
|
Packit |
d28291 |
&& (word)caller < (word)GC_libpthread_end)
|
|
Packit |
d28291 |
|| ((word)caller >= (word)GC_libld_start
|
|
Packit |
d28291 |
&& (word)caller < (word)GC_libld_end))
|
|
Packit |
d28291 |
return GC_generic_malloc_uncollectable(n * lb, UNCOLLECTABLE);
|
|
Packit |
d28291 |
/* The two ranges are actually usually adjacent, so there may */
|
|
Packit |
d28291 |
/* be a way to speed this up. */
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
# endif
|
|
Packit |
d28291 |
return (void *)REDIRECT_MALLOC_F(n * lb);
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
# ifndef strdup
|
|
Packit |
d28291 |
char *strdup(const char *s)
|
|
Packit |
d28291 |
{
|
|
Packit |
d28291 |
size_t lb = strlen(s) + 1;
|
|
Packit |
d28291 |
char *result = (char *)REDIRECT_MALLOC_F(lb);
|
|
Packit |
d28291 |
if (result == 0) {
|
|
Packit |
d28291 |
errno = ENOMEM;
|
|
Packit |
d28291 |
return 0;
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
BCOPY(s, result, lb);
|
|
Packit |
d28291 |
return result;
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
# endif /* !defined(strdup) */
|
|
Packit |
d28291 |
/* If strdup is macro defined, we assume that it actually calls malloc, */
|
|
Packit |
d28291 |
/* and thus the right thing will happen even without overriding it. */
|
|
Packit |
d28291 |
/* This seems to be true on most Linux systems. */
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
# ifndef strndup
|
|
Packit |
d28291 |
/* This is similar to strdup(). */
|
|
Packit |
d28291 |
char *strndup(const char *str, size_t size)
|
|
Packit |
d28291 |
{
|
|
Packit |
d28291 |
char *copy;
|
|
Packit |
d28291 |
size_t len = strlen(str);
|
|
Packit |
d28291 |
if (len > size)
|
|
Packit |
d28291 |
len = size;
|
|
Packit |
d28291 |
copy = (char *)REDIRECT_MALLOC_F(len + 1);
|
|
Packit |
d28291 |
if (copy == NULL) {
|
|
Packit |
d28291 |
errno = ENOMEM;
|
|
Packit |
d28291 |
return NULL;
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
BCOPY(str, copy, len);
|
|
Packit |
d28291 |
copy[len] = '\0';
|
|
Packit |
d28291 |
return copy;
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
# endif /* !strndup */
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
# undef GC_debug_malloc_replacement
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
#endif /* REDIRECT_MALLOC */
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
/* Explicitly deallocate an object p. */
|
|
Packit |
d28291 |
GC_API void GC_CALL GC_free(void * p)
|
|
Packit |
d28291 |
{
|
|
Packit |
d28291 |
struct hblk *h;
|
|
Packit |
d28291 |
hdr *hhdr;
|
|
Packit |
d28291 |
size_t sz; /* In bytes */
|
|
Packit |
d28291 |
size_t ngranules; /* sz in granules */
|
|
Packit |
d28291 |
int knd;
|
|
Packit |
d28291 |
struct obj_kind * ok;
|
|
Packit |
d28291 |
DCL_LOCK_STATE;
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
if (p == 0) return;
|
|
Packit |
d28291 |
/* Required by ANSI. It's not my fault ... */
|
|
Packit |
d28291 |
# ifdef LOG_ALLOCS
|
|
Packit |
d28291 |
GC_log_printf("GC_free(%p) after GC #%lu\n",
|
|
Packit |
d28291 |
p, (unsigned long)GC_gc_no);
|
|
Packit |
d28291 |
# endif
|
|
Packit |
d28291 |
h = HBLKPTR(p);
|
|
Packit |
d28291 |
hhdr = HDR(h);
|
|
Packit |
d28291 |
# if defined(REDIRECT_MALLOC) && \
|
|
Packit |
d28291 |
((defined(NEED_CALLINFO) && defined(GC_HAVE_BUILTIN_BACKTRACE)) \
|
|
Packit |
d28291 |
|| defined(GC_SOLARIS_THREADS) || defined(GC_LINUX_THREADS) \
|
|
Packit |
d28291 |
|| defined(MSWIN32))
|
|
Packit |
d28291 |
/* This might be called indirectly by GC_print_callers to free */
|
|
Packit |
d28291 |
/* the result of backtrace_symbols. */
|
|
Packit |
d28291 |
/* For Solaris, we have to redirect malloc calls during */
|
|
Packit |
d28291 |
/* initialization. For the others, this seems to happen */
|
|
Packit |
d28291 |
/* implicitly. */
|
|
Packit |
d28291 |
/* Don't try to deallocate that memory. */
|
|
Packit |
d28291 |
if (0 == hhdr) return;
|
|
Packit |
d28291 |
# endif
|
|
Packit |
d28291 |
GC_ASSERT(GC_base(p) == p);
|
|
Packit |
d28291 |
sz = hhdr -> hb_sz;
|
|
Packit |
d28291 |
ngranules = BYTES_TO_GRANULES(sz);
|
|
Packit |
d28291 |
knd = hhdr -> hb_obj_kind;
|
|
Packit |
d28291 |
ok = &GC_obj_kinds[knd];
|
|
Packit |
d28291 |
if (EXPECT(ngranules <= MAXOBJGRANULES, TRUE)) {
|
|
Packit |
d28291 |
void **flh;
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
LOCK();
|
|
Packit |
d28291 |
GC_bytes_freed += sz;
|
|
Packit |
d28291 |
if (IS_UNCOLLECTABLE(knd)) GC_non_gc_bytes -= sz;
|
|
Packit |
d28291 |
/* Its unnecessary to clear the mark bit. If the */
|
|
Packit |
d28291 |
/* object is reallocated, it doesn't matter. O.w. the */
|
|
Packit |
d28291 |
/* collector will do it, since it's on a free list. */
|
|
Packit |
d28291 |
if (ok -> ok_init) {
|
|
Packit |
d28291 |
BZERO((word *)p + 1, sz-sizeof(word));
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
flh = &(ok -> ok_freelist[ngranules]);
|
|
Packit |
d28291 |
obj_link(p) = *flh;
|
|
Packit |
d28291 |
*flh = (ptr_t)p;
|
|
Packit |
d28291 |
UNLOCK();
|
|
Packit |
d28291 |
} else {
|
|
Packit |
d28291 |
size_t nblocks = OBJ_SZ_TO_BLOCKS(sz);
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
LOCK();
|
|
Packit |
d28291 |
GC_bytes_freed += sz;
|
|
Packit |
d28291 |
if (IS_UNCOLLECTABLE(knd)) GC_non_gc_bytes -= sz;
|
|
Packit |
d28291 |
if (nblocks > 1) {
|
|
Packit |
d28291 |
GC_large_allocd_bytes -= nblocks * HBLKSIZE;
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
GC_freehblk(h);
|
|
Packit |
d28291 |
UNLOCK();
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
/* Explicitly deallocate an object p when we already hold lock. */
|
|
Packit |
d28291 |
/* Only used for internally allocated objects, so we can take some */
|
|
Packit |
d28291 |
/* shortcuts. */
|
|
Packit |
d28291 |
#ifdef THREADS
|
|
Packit |
d28291 |
GC_INNER void GC_free_inner(void * p)
|
|
Packit |
d28291 |
{
|
|
Packit |
d28291 |
struct hblk *h;
|
|
Packit |
d28291 |
hdr *hhdr;
|
|
Packit |
d28291 |
size_t sz; /* bytes */
|
|
Packit |
d28291 |
size_t ngranules; /* sz in granules */
|
|
Packit |
d28291 |
int knd;
|
|
Packit |
d28291 |
struct obj_kind * ok;
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
h = HBLKPTR(p);
|
|
Packit |
d28291 |
hhdr = HDR(h);
|
|
Packit |
d28291 |
knd = hhdr -> hb_obj_kind;
|
|
Packit |
d28291 |
sz = hhdr -> hb_sz;
|
|
Packit |
d28291 |
ngranules = BYTES_TO_GRANULES(sz);
|
|
Packit |
d28291 |
ok = &GC_obj_kinds[knd];
|
|
Packit |
d28291 |
if (ngranules <= MAXOBJGRANULES) {
|
|
Packit |
d28291 |
void ** flh;
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
GC_bytes_freed += sz;
|
|
Packit |
d28291 |
if (IS_UNCOLLECTABLE(knd)) GC_non_gc_bytes -= sz;
|
|
Packit |
d28291 |
if (ok -> ok_init) {
|
|
Packit |
d28291 |
BZERO((word *)p + 1, sz-sizeof(word));
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
flh = &(ok -> ok_freelist[ngranules]);
|
|
Packit |
d28291 |
obj_link(p) = *flh;
|
|
Packit |
d28291 |
*flh = (ptr_t)p;
|
|
Packit |
d28291 |
} else {
|
|
Packit |
d28291 |
size_t nblocks = OBJ_SZ_TO_BLOCKS(sz);
|
|
Packit |
d28291 |
GC_bytes_freed += sz;
|
|
Packit |
d28291 |
if (IS_UNCOLLECTABLE(knd)) GC_non_gc_bytes -= sz;
|
|
Packit |
d28291 |
if (nblocks > 1) {
|
|
Packit |
d28291 |
GC_large_allocd_bytes -= nblocks * HBLKSIZE;
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
GC_freehblk(h);
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
#endif /* THREADS */
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
#if defined(REDIRECT_MALLOC) && !defined(REDIRECT_FREE)
|
|
Packit |
d28291 |
# define REDIRECT_FREE GC_free
|
|
Packit |
d28291 |
#endif
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
#if defined(REDIRECT_FREE) && !defined(REDIRECT_MALLOC_IN_HEADER)
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
# if defined(CPPCHECK)
|
|
Packit |
d28291 |
# define REDIRECT_FREE_F GC_free /* e.g. */
|
|
Packit |
d28291 |
# else
|
|
Packit |
d28291 |
# define REDIRECT_FREE_F REDIRECT_FREE
|
|
Packit |
d28291 |
# endif
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
void free(void * p)
|
|
Packit |
d28291 |
{
|
|
Packit |
d28291 |
# ifndef IGNORE_FREE
|
|
Packit |
d28291 |
# if defined(GC_LINUX_THREADS) && !defined(USE_PROC_FOR_LIBRARIES)
|
|
Packit |
d28291 |
/* Don't bother with initialization checks. If nothing */
|
|
Packit |
d28291 |
/* has been initialized, the check fails, and that's safe, */
|
|
Packit |
d28291 |
/* since we have not allocated uncollectible objects neither. */
|
|
Packit |
d28291 |
ptr_t caller = (ptr_t)__builtin_return_address(0);
|
|
Packit |
d28291 |
/* This test does not need to ensure memory visibility, since */
|
|
Packit |
d28291 |
/* the bounds will be set when/if we create another thread. */
|
|
Packit |
d28291 |
if (((word)caller >= (word)GC_libpthread_start
|
|
Packit |
d28291 |
&& (word)caller < (word)GC_libpthread_end)
|
|
Packit |
d28291 |
|| ((word)caller >= (word)GC_libld_start
|
|
Packit |
d28291 |
&& (word)caller < (word)GC_libld_end)) {
|
|
Packit |
d28291 |
GC_free(p);
|
|
Packit |
d28291 |
return;
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
# endif
|
|
Packit |
d28291 |
REDIRECT_FREE_F(p);
|
|
Packit |
d28291 |
# endif
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
#endif /* REDIRECT_FREE */
|