mirror of https://github.com/svaarala/duktape.git
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.
104 lines
2.4 KiB
104 lines
2.4 KiB
12 years ago
|
/*
|
||
|
* Memory calls.
|
||
|
*/
|
||
|
|
||
|
#include "duk_internal.h"
|
||
|
|
||
10 years ago
|
DUK_EXTERNAL void *duk_alloc_raw(duk_context *ctx, duk_size_t size) {
|
||
12 years ago
|
duk_hthread *thr = (duk_hthread *) ctx;
|
||
|
|
||
10 years ago
|
DUK_ASSERT_CTX_VALID(ctx);
|
||
12 years ago
|
|
||
|
return DUK_ALLOC_RAW(thr->heap, size);
|
||
|
}
|
||
|
|
||
10 years ago
|
DUK_EXTERNAL void duk_free_raw(duk_context *ctx, void *ptr) {
|
||
12 years ago
|
duk_hthread *thr = (duk_hthread *) ctx;
|
||
|
|
||
10 years ago
|
DUK_ASSERT_CTX_VALID(ctx);
|
||
12 years ago
|
|
||
|
DUK_FREE_RAW(thr->heap, ptr);
|
||
|
}
|
||
|
|
||
10 years ago
|
DUK_EXTERNAL void *duk_realloc_raw(duk_context *ctx, void *ptr, duk_size_t size) {
|
||
12 years ago
|
duk_hthread *thr = (duk_hthread *) ctx;
|
||
|
|
||
10 years ago
|
DUK_ASSERT_CTX_VALID(ctx);
|
||
12 years ago
|
|
||
|
return DUK_REALLOC_RAW(thr->heap, ptr, size);
|
||
|
}
|
||
|
|
||
10 years ago
|
DUK_EXTERNAL void *duk_alloc(duk_context *ctx, duk_size_t size) {
|
||
12 years ago
|
duk_hthread *thr = (duk_hthread *) ctx;
|
||
|
|
||
10 years ago
|
DUK_ASSERT_CTX_VALID(ctx);
|
||
12 years ago
|
|
||
|
return DUK_ALLOC(thr->heap, size);
|
||
|
}
|
||
|
|
||
10 years ago
|
DUK_EXTERNAL void duk_free(duk_context *ctx, void *ptr) {
|
||
12 years ago
|
duk_hthread *thr = (duk_hthread *) ctx;
|
||
|
|
||
10 years ago
|
DUK_ASSERT_CTX_VALID(ctx);
|
||
12 years ago
|
|
||
|
DUK_FREE(thr->heap, ptr);
|
||
|
}
|
||
|
|
||
10 years ago
|
DUK_EXTERNAL void *duk_realloc(duk_context *ctx, void *ptr, duk_size_t size) {
|
||
12 years ago
|
duk_hthread *thr = (duk_hthread *) ctx;
|
||
|
|
||
10 years ago
|
DUK_ASSERT_CTX_VALID(ctx);
|
||
12 years ago
|
|
||
|
/*
|
||
|
* Note: since this is an exposed API call, there should be
|
||
|
* no way a mark-and-sweep could have a side effect on the
|
||
|
* memory allocation behind 'ptr'; the pointer should never
|
||
|
* be something that Duktape wants to change.
|
||
|
*
|
||
|
* Thus, no need to use DUK_REALLOC_INDIRECT (and we don't
|
||
|
* have the storage location here anyway).
|
||
|
*/
|
||
|
|
||
|
return DUK_REALLOC(thr->heap, ptr, size);
|
||
|
}
|
||
|
|
||
10 years ago
|
DUK_EXTERNAL void duk_get_memory_functions(duk_context *ctx, duk_memory_functions *out_funcs) {
|
||
12 years ago
|
duk_hthread *thr = (duk_hthread *) ctx;
|
||
|
duk_heap *heap;
|
||
|
|
||
10 years ago
|
DUK_ASSERT_CTX_VALID(ctx);
|
||
12 years ago
|
DUK_ASSERT(out_funcs != NULL);
|
||
|
DUK_ASSERT(thr != NULL);
|
||
|
DUK_ASSERT(thr->heap != NULL);
|
||
|
|
||
|
heap = thr->heap;
|
||
10 years ago
|
out_funcs->alloc_func = heap->alloc_func;
|
||
|
out_funcs->realloc_func = heap->realloc_func;
|
||
|
out_funcs->free_func = heap->free_func;
|
||
10 years ago
|
out_funcs->udata = heap->heap_udata;
|
||
12 years ago
|
}
|
||
|
|
||
10 years ago
|
DUK_EXTERNAL void duk_gc(duk_context *ctx, duk_uint_t flags) {
|
||
11 years ago
|
#ifdef DUK_USE_MARK_AND_SWEEP
|
||
|
duk_hthread *thr = (duk_hthread *) ctx;
|
||
|
duk_heap *heap;
|
||
|
|
||
11 years ago
|
DUK_UNREF(flags);
|
||
|
|
||
10 years ago
|
/* NULL accepted */
|
||
11 years ago
|
if (!ctx) {
|
||
|
return;
|
||
|
}
|
||
10 years ago
|
DUK_ASSERT_CTX_VALID(ctx);
|
||
11 years ago
|
heap = thr->heap;
|
||
|
DUK_ASSERT(heap != NULL);
|
||
|
|
||
11 years ago
|
DUK_D(DUK_DPRINT("mark-and-sweep requested by application"));
|
||
11 years ago
|
duk_heap_mark_and_sweep(heap, 0);
|
||
|
#else
|
||
11 years ago
|
DUK_D(DUK_DPRINT("mark-and-sweep requested by application but mark-and-sweep not enabled, ignoring"));
|
||
11 years ago
|
DUK_UNREF(ctx);
|
||
|
DUK_UNREF(flags);
|
||
11 years ago
|
#endif
|
||
|
}
|