forked from svaarala/duktape
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathduk_hobject_alloc.c
More file actions
155 lines (125 loc) · 4.22 KB
/
Copy pathduk_hobject_alloc.c
File metadata and controls
155 lines (125 loc) · 4.22 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
* Hobject allocation.
*
* Provides primitive allocation functions for all object types (plain object,
* compiled function, native function, thread). The object return is not yet
* in "heap allocated" list and has a refcount of zero, so caller must careful.
*/
#include "duk_internal.h"
DUK_LOCAL void duk__init_object_parts(duk_heap *heap, duk_hobject *obj, duk_uint_t hobject_flags) {
#ifdef DUK_USE_EXPLICIT_NULL_INIT
obj->p = NULL;
#endif
/* XXX: macro? sets both heaphdr and object flags */
obj->hdr.h_flags = hobject_flags;
DUK_HEAPHDR_SET_TYPE(&obj->hdr, DUK_HTYPE_OBJECT); /* also goes into flags */
DUK_HEAP_INSERT_INTO_HEAP_ALLOCATED(heap, &obj->hdr);
/*
* obj->p is intentionally left as NULL, and duk_hobject_props.c must deal
* with this properly. This is intentional: empty objects consume a minimum
* amount of memory. Further, an initial allocation might fail and cause
* 'obj' to "leak" (require a mark-and-sweep) since it is not reachable yet.
*/
}
/*
* Allocate an duk_hobject.
*
* The allocated object has no allocation for properties; the caller may
* want to force a resize if a desired size is known.
*
* The allocated object has zero reference count and is not reachable.
* The caller MUST make the object reachable and increase its reference
* count before invoking any operation that might require memory allocation.
*/
DUK_INTERNAL duk_hobject *duk_hobject_alloc(duk_heap *heap, duk_uint_t hobject_flags) {
duk_hobject *res;
DUK_ASSERT(heap != NULL);
/* different memory layout, alloc size, and init */
DUK_ASSERT((hobject_flags & DUK_HOBJECT_FLAG_COMPILEDFUNCTION) == 0);
DUK_ASSERT((hobject_flags & DUK_HOBJECT_FLAG_NATIVEFUNCTION) == 0);
DUK_ASSERT((hobject_flags & DUK_HOBJECT_FLAG_THREAD) == 0);
res = (duk_hobject *) DUK_ALLOC(heap, sizeof(duk_hobject));
if (!res) {
return NULL;
}
DUK_MEMZERO(res, sizeof(duk_hobject));
duk__init_object_parts(heap, res, hobject_flags);
return res;
}
DUK_INTERNAL duk_hcompiledfunction *duk_hcompiledfunction_alloc(duk_heap *heap, duk_uint_t hobject_flags) {
duk_hcompiledfunction *res;
res = (duk_hcompiledfunction *) DUK_ALLOC(heap, sizeof(duk_hcompiledfunction));
if (!res) {
return NULL;
}
DUK_MEMZERO(res, sizeof(duk_hcompiledfunction));
duk__init_object_parts(heap, &res->obj, hobject_flags);
#ifdef DUK_USE_EXPLICIT_NULL_INIT
res->data = NULL;
res->funcs = NULL;
res->bytecode = NULL;
#endif
return res;
}
DUK_INTERNAL duk_hnativefunction *duk_hnativefunction_alloc(duk_heap *heap, duk_uint_t hobject_flags) {
duk_hnativefunction *res;
res = (duk_hnativefunction *) DUK_ALLOC(heap, sizeof(duk_hnativefunction));
if (!res) {
return NULL;
}
DUK_MEMZERO(res, sizeof(duk_hnativefunction));
duk__init_object_parts(heap, &res->obj, hobject_flags);
#ifdef DUK_USE_EXPLICIT_NULL_INIT
res->func = NULL;
#endif
return res;
}
/*
* Allocate a new thread.
*
* Leaves the built-ins array uninitialized. The caller must either
* initialize a new global context or share existing built-ins from
* another thread.
*/
DUK_INTERNAL duk_hthread *duk_hthread_alloc(duk_heap *heap, duk_uint_t hobject_flags) {
duk_hthread *res;
res = (duk_hthread *) DUK_ALLOC(heap, sizeof(duk_hthread));
if (!res) {
return NULL;
}
DUK_MEMZERO(res, sizeof(duk_hthread));
duk__init_object_parts(heap, &res->obj, hobject_flags);
#ifdef DUK_USE_EXPLICIT_NULL_INIT
res->heap = NULL;
res->valstack = NULL;
res->valstack_end = NULL;
res->valstack_bottom = NULL;
res->valstack_top = NULL;
res->callstack = NULL;
res->catchstack = NULL;
res->resumer = NULL;
res->strs = NULL;
{
int i;
for (i = 0; i < DUK_NUM_BUILTINS; i++) {
res->builtins[i] = NULL;
}
}
#endif
/* when nothing is running, API calls are in non-strict mode */
DUK_ASSERT(res->strict == 0);
res->heap = heap;
res->valstack_max = DUK_VALSTACK_DEFAULT_MAX;
res->callstack_max = DUK_CALLSTACK_DEFAULT_MAX;
res->catchstack_max = DUK_CATCHSTACK_DEFAULT_MAX;
return res;
}
#if 0 /* unused now */
DUK_INTERNAL duk_hobject *duk_hobject_alloc_checked(duk_hthread *thr, duk_uint_t hobject_flags) {
duk_hobject *res = duk_hobject_alloc(thr->heap, hobject_flags);
if (!res) {
DUK_ERROR(thr, DUK_ERR_ALLOC_ERROR, "failed to allocate an object");
}
return res;
}
#endif