1 /*
2 * Memory calls.
3 */
4
5 #include "duk_internal.h"
6
duk_alloc_raw(duk_hthread * thr,duk_size_t size)7 DUK_EXTERNAL void *duk_alloc_raw(duk_hthread *thr, duk_size_t size) {
8 DUK_ASSERT_API_ENTRY(thr);
9
10 return DUK_ALLOC_RAW(thr->heap, size);
11 }
12
duk_free_raw(duk_hthread * thr,void * ptr)13 DUK_EXTERNAL void duk_free_raw(duk_hthread *thr, void *ptr) {
14 DUK_ASSERT_API_ENTRY(thr);
15
16 DUK_FREE_RAW(thr->heap, ptr);
17 }
18
duk_realloc_raw(duk_hthread * thr,void * ptr,duk_size_t size)19 DUK_EXTERNAL void *duk_realloc_raw(duk_hthread *thr, void *ptr, duk_size_t size) {
20 DUK_ASSERT_API_ENTRY(thr);
21
22 return DUK_REALLOC_RAW(thr->heap, ptr, size);
23 }
24
duk_alloc(duk_hthread * thr,duk_size_t size)25 DUK_EXTERNAL void *duk_alloc(duk_hthread *thr, duk_size_t size) {
26 DUK_ASSERT_API_ENTRY(thr);
27
28 return DUK_ALLOC(thr->heap, size);
29 }
30
duk_free(duk_hthread * thr,void * ptr)31 DUK_EXTERNAL void duk_free(duk_hthread *thr, void *ptr) {
32 DUK_ASSERT_API_ENTRY(thr);
33
34 DUK_FREE_CHECKED(thr, ptr);
35 }
36
duk_realloc(duk_hthread * thr,void * ptr,duk_size_t size)37 DUK_EXTERNAL void *duk_realloc(duk_hthread *thr, void *ptr, duk_size_t size) {
38 DUK_ASSERT_API_ENTRY(thr);
39
40 /*
41 * Note: since this is an exposed API call, there should be
42 * no way a mark-and-sweep could have a side effect on the
43 * memory allocation behind 'ptr'; the pointer should never
44 * be something that Duktape wants to change.
45 *
46 * Thus, no need to use DUK_REALLOC_INDIRECT (and we don't
47 * have the storage location here anyway).
48 */
49
50 return DUK_REALLOC(thr->heap, ptr, size);
51 }
52
duk_get_memory_functions(duk_hthread * thr,duk_memory_functions * out_funcs)53 DUK_EXTERNAL void duk_get_memory_functions(duk_hthread *thr, duk_memory_functions *out_funcs) {
54 duk_heap *heap;
55
56 DUK_ASSERT_API_ENTRY(thr);
57 DUK_ASSERT(out_funcs != NULL);
58 DUK_ASSERT(thr != NULL);
59 DUK_ASSERT(thr->heap != NULL);
60
61 heap = thr->heap;
62 out_funcs->alloc_func = heap->alloc_func;
63 out_funcs->realloc_func = heap->realloc_func;
64 out_funcs->free_func = heap->free_func;
65 out_funcs->udata = heap->heap_udata;
66 }
67
duk_gc(duk_hthread * thr,duk_uint_t flags)68 DUK_EXTERNAL void duk_gc(duk_hthread *thr, duk_uint_t flags) {
69 duk_heap *heap;
70 duk_small_uint_t ms_flags;
71
72 DUK_ASSERT_API_ENTRY(thr);
73 heap = thr->heap;
74 DUK_ASSERT(heap != NULL);
75
76 DUK_D(DUK_DPRINT("mark-and-sweep requested by application"));
77 DUK_ASSERT(DUK_GC_COMPACT == DUK_MS_FLAG_EMERGENCY); /* Compact flag is 1:1 with emergency flag which forces compaction. */
78 ms_flags = (duk_small_uint_t) flags;
79 duk_heap_mark_and_sweep(heap, ms_flags);
80 }
81