forked from svaarala/duktape
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathduk_api_memory.c
103 lines (76 loc) · 2.45 KB
/
duk_api_memory.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
* Memory calls.
*/
#include "duk_internal.h"
DUK_EXTERNAL void *duk_alloc_raw(duk_context *ctx, duk_size_t size) {
duk_hthread *thr = (duk_hthread *) ctx;
DUK_ASSERT_CTX_VALID(ctx);
return DUK_ALLOC_RAW(thr->heap, size);
}
DUK_EXTERNAL void duk_free_raw(duk_context *ctx, void *ptr) {
duk_hthread *thr = (duk_hthread *) ctx;
DUK_ASSERT_CTX_VALID(ctx);
DUK_FREE_RAW(thr->heap, ptr);
}
DUK_EXTERNAL void *duk_realloc_raw(duk_context *ctx, void *ptr, duk_size_t size) {
duk_hthread *thr = (duk_hthread *) ctx;
DUK_ASSERT_CTX_VALID(ctx);
return DUK_REALLOC_RAW(thr->heap, ptr, size);
}
DUK_EXTERNAL void *duk_alloc(duk_context *ctx, duk_size_t size) {
duk_hthread *thr = (duk_hthread *) ctx;
DUK_ASSERT_CTX_VALID(ctx);
return DUK_ALLOC(thr->heap, size);
}
DUK_EXTERNAL void duk_free(duk_context *ctx, void *ptr) {
duk_hthread *thr = (duk_hthread *) ctx;
DUK_ASSERT_CTX_VALID(ctx);
DUK_FREE(thr->heap, ptr);
}
DUK_EXTERNAL void *duk_realloc(duk_context *ctx, void *ptr, duk_size_t size) {
duk_hthread *thr = (duk_hthread *) ctx;
DUK_ASSERT_CTX_VALID(ctx);
/*
* 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);
}
DUK_EXTERNAL void duk_get_memory_functions(duk_context *ctx, duk_memory_functions *out_funcs) {
duk_hthread *thr = (duk_hthread *) ctx;
duk_heap *heap;
DUK_ASSERT_CTX_VALID(ctx);
DUK_ASSERT(out_funcs != NULL);
DUK_ASSERT(thr != NULL);
DUK_ASSERT(thr->heap != NULL);
heap = thr->heap;
out_funcs->alloc_func = heap->alloc_func;
out_funcs->realloc_func = heap->realloc_func;
out_funcs->free_func = heap->free_func;
out_funcs->udata = heap->heap_udata;
}
DUK_EXTERNAL void duk_gc(duk_context *ctx, duk_uint_t flags) {
#ifdef DUK_USE_MARK_AND_SWEEP
duk_hthread *thr = (duk_hthread *) ctx;
duk_heap *heap;
DUK_UNREF(flags);
/* NULL accepted */
if (!ctx) {
return;
}
DUK_ASSERT_CTX_VALID(ctx);
heap = thr->heap;
DUK_ASSERT(heap != NULL);
DUK_D(DUK_DPRINT("mark-and-sweep requested by application"));
duk_heap_mark_and_sweep(heap, 0);
#else
DUK_D(DUK_DPRINT("mark-and-sweep requested by application but mark-and-sweep not enabled, ignoring"));
DUK_UNREF(ctx);
DUK_UNREF(flags);
#endif
}