You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

359 lines
9.3 KiB

12 years ago
/*
* Memory allocation handling.
*/
#include "duk_internal.h"
/*
* Voluntary GC check
12 years ago
*/
#if defined(DUK_USE_VOLUNTARY_GC)
DUK_LOCAL DUK_INLINE void duk__check_voluntary_gc(duk_heap *heap) {
if (DUK_UNLIKELY(--(heap)->ms_trigger_counter < 0)) {
#if defined(DUK_USE_DEBUG)
if (heap->ms_prevent_count == 0) {
DUK_D(DUK_DPRINT("triggering voluntary mark-and-sweep"));
} else {
DUK_DD(DUK_DDPRINT("gc blocked -> skip voluntary mark-and-sweep now"));
}
#endif
12 years ago
/* Prevention checks in the call target handle cases where
* voluntary GC is not allowed. The voluntary GC trigger
* counter is only rewritten if mark-and-sweep actually runs.
*/
duk_heap_mark_and_sweep(heap, DUK_MS_FLAG_VOLUNTARY /*flags*/);
12 years ago
}
}
#define DUK__VOLUNTARY_PERIODIC_GC(heap) do { duk__check_voluntary_gc((heap)); } while (0)
#else
#define DUK__VOLUNTARY_PERIODIC_GC(heap) /* no voluntary gc */
#endif /* DUK_USE_VOLUNTARY_GC */
12 years ago
/*
* Allocate memory with garbage collection
*/
DUK_INTERNAL void *duk_heap_mem_alloc(duk_heap *heap, duk_size_t size) {
12 years ago
void *res;
duk_small_int_t i;
12 years ago
DUK_ASSERT(heap != NULL);
11 years ago
DUK_ASSERT_DISABLE(size >= 0);
12 years ago
/*
* Voluntary periodic GC (if enabled)
12 years ago
*/
DUK__VOLUNTARY_PERIODIC_GC(heap);
12 years ago
/*
* First attempt
*/
#if defined(DUK_USE_GC_TORTURE)
12 years ago
/* simulate alloc failure on every alloc (except when mark-and-sweep is running) */
if (heap->ms_prevent_count == 0) {
DUK_DDD(DUK_DDDPRINT("gc torture enabled, pretend that first alloc attempt fails"));
12 years ago
res = NULL;
DUK_UNREF(res);
12 years ago
goto skip_attempt;
}
#endif
res = heap->alloc_func(heap->heap_udata, size);
if (DUK_LIKELY(res || size == 0)) {
12 years ago
/* for zero size allocations NULL is allowed */
return res;
}
#if defined(DUK_USE_GC_TORTURE)
12 years ago
skip_attempt:
#endif
DUK_D(DUK_DPRINT("first alloc attempt failed, attempt to gc and retry"));
12 years ago
#if 0
12 years ago
/*
* Avoid a GC if GC is already running. This can happen at a late
* stage in a GC when we try to e.g. resize the stringtable
* or compact objects.
*
* NOTE: explicit handling isn't actually be needed: if the GC is
* not allowed, duk_heap_mark_and_sweep() will reject it for every
* attempt in the loop below, resulting in a NULL same as here.
12 years ago
*/
if (heap->ms_prevent_count != 0) {
DUK_D(DUK_DPRINT("duk_heap_mem_alloc() failed, gc in progress (gc skipped), alloc size %ld", (long) size));
12 years ago
return NULL;
}
#endif
12 years ago
/*
* Retry with several GC attempts. Initial attempts are made without
* emergency mode; later attempts use emergency mode which minimizes
* memory allocations forcibly.
*/
for (i = 0; i < DUK_HEAP_ALLOC_FAIL_MARKANDSWEEP_LIMIT; i++) {
duk_small_uint_t flags;
12 years ago
flags = 0;
if (i >= DUK_HEAP_ALLOC_FAIL_MARKANDSWEEP_EMERGENCY_LIMIT - 1) {
flags |= DUK_MS_FLAG_EMERGENCY;
}
duk_heap_mark_and_sweep(heap, flags);
12 years ago
res = heap->alloc_func(heap->heap_udata, size);
12 years ago
if (res) {
DUK_D(DUK_DPRINT("duk_heap_mem_alloc() succeeded after gc (pass %ld), alloc size %ld",
(long) (i + 1), (long) size));
12 years ago
return res;
}
}
DUK_D(DUK_DPRINT("duk_heap_mem_alloc() failed even after gc, alloc size %ld", (long) size));
12 years ago
return NULL;
}
DUK_INTERNAL void *duk_heap_mem_alloc_zeroed(duk_heap *heap, duk_size_t size) {
12 years ago
void *res;
DUK_ASSERT(heap != NULL);
11 years ago
DUK_ASSERT_DISABLE(size >= 0);
12 years ago
res = DUK_ALLOC(heap, size);
if (DUK_LIKELY(res != NULL)) {
12 years ago
/* assume memset with zero size is OK */
DUK_MEMZERO(res, size);
12 years ago
}
return res;
}
DUK_INTERNAL void *duk_heap_mem_alloc_checked(duk_hthread *thr, duk_size_t size) {
void *res;
DUK_ASSERT(thr != NULL);
res = duk_heap_mem_alloc(thr->heap, size);
if (DUK_LIKELY(res != NULL || size == 0)) {
return res;
}
DUK_ERROR_ALLOC_FAILED(thr);
return NULL;
}
DUK_INTERNAL void *duk_heap_mem_alloc_checked_zeroed(duk_hthread *thr, duk_size_t size) {
void *res;
DUK_ASSERT(thr != NULL);
res = duk_heap_mem_alloc_zeroed(thr->heap, size);
if (DUK_LIKELY(res != NULL || size == 0)) {
return res;
}
DUK_ERROR_ALLOC_FAILED(thr);
return NULL;
}
12 years ago
/*
* Reallocate memory with garbage collection
*/
DUK_INTERNAL void *duk_heap_mem_realloc(duk_heap *heap, void *ptr, duk_size_t newsize) {
12 years ago
void *res;
duk_small_int_t i;
12 years ago
DUK_ASSERT(heap != NULL);
/* ptr may be NULL */
11 years ago
DUK_ASSERT_DISABLE(newsize >= 0);
12 years ago
/*
* Voluntary periodic GC (if enabled)
12 years ago
*/
DUK__VOLUNTARY_PERIODIC_GC(heap);
12 years ago
/*
* First attempt
*/
#if defined(DUK_USE_GC_TORTURE)
12 years ago
/* simulate alloc failure on every realloc (except when mark-and-sweep is running) */
if (heap->ms_prevent_count == 0) {
DUK_DDD(DUK_DDDPRINT("gc torture enabled, pretend that first realloc attempt fails"));
12 years ago
res = NULL;
DUK_UNREF(res);
12 years ago
goto skip_attempt;
}
#endif
res = heap->realloc_func(heap->heap_udata, ptr, newsize);
if (DUK_LIKELY(res || newsize == 0)) {
12 years ago
/* for zero size allocations NULL is allowed */
return res;
}
#if defined(DUK_USE_GC_TORTURE)
12 years ago
skip_attempt:
#endif
DUK_D(DUK_DPRINT("first realloc attempt failed, attempt to gc and retry"));
12 years ago
#if 0
12 years ago
/*
* Avoid a GC if GC is already running. See duk_heap_mem_alloc().
*/
if (heap->ms_prevent_count != 0) {
DUK_D(DUK_DPRINT("duk_heap_mem_realloc() failed, gc in progress (gc skipped), alloc size %ld", (long) newsize));
12 years ago
return NULL;
}
#endif
12 years ago
/*
* Retry with several GC attempts. Initial attempts are made without
* emergency mode; later attempts use emergency mode which minimizes
* memory allocations forcibly.
*/
for (i = 0; i < DUK_HEAP_ALLOC_FAIL_MARKANDSWEEP_LIMIT; i++) {
duk_small_uint_t flags;
12 years ago
flags = 0;
if (i >= DUK_HEAP_ALLOC_FAIL_MARKANDSWEEP_EMERGENCY_LIMIT - 1) {
flags |= DUK_MS_FLAG_EMERGENCY;
}
duk_heap_mark_and_sweep(heap, flags);
12 years ago
res = heap->realloc_func(heap->heap_udata, ptr, newsize);
if (res || newsize == 0) {
DUK_D(DUK_DPRINT("duk_heap_mem_realloc() succeeded after gc (pass %ld), alloc size %ld",
(long) (i + 1), (long) newsize));
12 years ago
return res;
}
}
DUK_D(DUK_DPRINT("duk_heap_mem_realloc() failed even after gc, alloc size %ld", (long) newsize));
12 years ago
return NULL;
}
/*
* Reallocate memory with garbage collection, using a callback to provide
* the current allocated pointer. This variant is used when a mark-and-sweep
* (e.g. finalizers) might change the original pointer.
12 years ago
*/
DUK_INTERNAL void *duk_heap_mem_realloc_indirect(duk_heap *heap, duk_mem_getptr cb, void *ud, duk_size_t newsize) {
12 years ago
void *res;
duk_small_int_t i;
12 years ago
DUK_ASSERT(heap != NULL);
11 years ago
DUK_ASSERT_DISABLE(newsize >= 0);
12 years ago
/*
* Voluntary periodic GC (if enabled)
12 years ago
*/
DUK__VOLUNTARY_PERIODIC_GC(heap);
12 years ago
/*
* First attempt
*/
#if defined(DUK_USE_GC_TORTURE)
12 years ago
/* simulate alloc failure on every realloc (except when mark-and-sweep is running) */
if (heap->ms_prevent_count == 0) {
DUK_DDD(DUK_DDDPRINT("gc torture enabled, pretend that first indirect realloc attempt fails"));
12 years ago
res = NULL;
DUK_UNREF(res);
12 years ago
goto skip_attempt;
}
#endif
res = heap->realloc_func(heap->heap_udata, cb(heap, ud), newsize);
if (DUK_LIKELY(res || newsize == 0)) {
12 years ago
/* for zero size allocations NULL is allowed */
return res;
}
#if defined(DUK_USE_GC_TORTURE)
12 years ago
skip_attempt:
#endif
DUK_D(DUK_DPRINT("first indirect realloc attempt failed, attempt to gc and retry"));
12 years ago
#if 0
12 years ago
/*
* Avoid a GC if GC is already running. See duk_heap_mem_alloc().
*/
if (heap->ms_prevent_count != 0) {
DUK_D(DUK_DPRINT("duk_heap_mem_realloc_indirect() failed, gc in progress (gc skipped), alloc size %ld", (long) newsize));
12 years ago
return NULL;
}
#endif
12 years ago
/*
* Retry with several GC attempts. Initial attempts are made without
* emergency mode; later attempts use emergency mode which minimizes
* memory allocations forcibly.
*/
for (i = 0; i < DUK_HEAP_ALLOC_FAIL_MARKANDSWEEP_LIMIT; i++) {
duk_small_uint_t flags;
12 years ago
#if defined(DUK_USE_ASSERTIONS)
12 years ago
void *ptr_pre; /* ptr before mark-and-sweep */
void *ptr_post;
12 years ago
#endif
#if defined(DUK_USE_ASSERTIONS)
ptr_pre = cb(heap, ud);
12 years ago
#endif
flags = 0;
if (i >= DUK_HEAP_ALLOC_FAIL_MARKANDSWEEP_EMERGENCY_LIMIT - 1) {
flags |= DUK_MS_FLAG_EMERGENCY;
}
duk_heap_mark_and_sweep(heap, flags);
#if defined(DUK_USE_ASSERTIONS)
ptr_post = cb(heap, ud);
if (ptr_pre != ptr_post) {
12 years ago
/* useful for debugging */
DUK_DD(DUK_DDPRINT("note: base pointer changed by mark-and-sweep: %p -> %p",
(void *) ptr_pre, (void *) ptr_post));
12 years ago
}
#endif
/* Note: key issue here is to re-lookup the base pointer on every attempt.
* The pointer being reallocated may change after every mark-and-sweep.
12 years ago
*/
res = heap->realloc_func(heap->heap_udata, cb(heap, ud), newsize);
if (res || newsize == 0) {
DUK_D(DUK_DPRINT("duk_heap_mem_realloc_indirect() succeeded after gc (pass %ld), alloc size %ld",
(long) (i + 1), (long) newsize));
12 years ago
return res;
}
}
DUK_D(DUK_DPRINT("duk_heap_mem_realloc_indirect() failed even after gc, alloc size %ld", (long) newsize));
12 years ago
return NULL;
}
/*
* Free memory
*/
DUK_INTERNAL void duk_heap_mem_free(duk_heap *heap, void *ptr) {
12 years ago
DUK_ASSERT(heap != NULL);
/* ptr may be NULL */
/* Must behave like a no-op with NULL and any pointer returned from
* malloc/realloc with zero size.
*/
heap->free_func(heap->heap_udata, ptr);
12 years ago
/* Never perform a GC (even voluntary) in a memory free, otherwise
* all call sites doing frees would need to deal with the side effects.
* No need to update voluntary GC counter either.
12 years ago
*/
}