]> git.proxmox.com Git - ceph.git/blob - ceph/src/civetweb/src/third_party/duktape-1.3.0/src-separate/duk_hobject_alloc.c
bump version to 12.2.12-pve1
[ceph.git] / ceph / src / civetweb / src / third_party / duktape-1.3.0 / src-separate / duk_hobject_alloc.c
1 /*
2 * Hobject allocation.
3 *
4 * Provides primitive allocation functions for all object types (plain object,
5 * compiled function, native function, thread). The object return is not yet
6 * in "heap allocated" list and has a refcount of zero, so caller must careful.
7 */
8
9 #include "duk_internal.h"
10
11 DUK_LOCAL void duk__init_object_parts(duk_heap *heap, duk_hobject *obj, duk_uint_t hobject_flags) {
12 #ifdef DUK_USE_EXPLICIT_NULL_INIT
13 DUK_HOBJECT_SET_PROPS(heap, obj, NULL);
14 #endif
15
16 /* XXX: macro? sets both heaphdr and object flags */
17 obj->hdr.h_flags = hobject_flags;
18 DUK_HEAPHDR_SET_TYPE(&obj->hdr, DUK_HTYPE_OBJECT); /* also goes into flags */
19
20 #if defined(DUK_USE_HEAPPTR16)
21 /* Zero encoded pointer is required to match NULL */
22 DUK_HEAPHDR_SET_NEXT(heap, &obj->hdr, NULL);
23 #if defined(DUK_USE_DOUBLE_LINKED_HEAP)
24 DUK_HEAPHDR_SET_PREV(heap, &obj->hdr, NULL);
25 #endif
26 #endif
27 DUK_HEAP_INSERT_INTO_HEAP_ALLOCATED(heap, &obj->hdr);
28
29 /*
30 * obj->props is intentionally left as NULL, and duk_hobject_props.c must deal
31 * with this properly. This is intentional: empty objects consume a minimum
32 * amount of memory. Further, an initial allocation might fail and cause
33 * 'obj' to "leak" (require a mark-and-sweep) since it is not reachable yet.
34 */
35 }
36
37 /*
38 * Allocate an duk_hobject.
39 *
40 * The allocated object has no allocation for properties; the caller may
41 * want to force a resize if a desired size is known.
42 *
43 * The allocated object has zero reference count and is not reachable.
44 * The caller MUST make the object reachable and increase its reference
45 * count before invoking any operation that might require memory allocation.
46 */
47
48 DUK_INTERNAL duk_hobject *duk_hobject_alloc(duk_heap *heap, duk_uint_t hobject_flags) {
49 duk_hobject *res;
50
51 DUK_ASSERT(heap != NULL);
52
53 /* different memory layout, alloc size, and init */
54 DUK_ASSERT((hobject_flags & DUK_HOBJECT_FLAG_COMPILEDFUNCTION) == 0);
55 DUK_ASSERT((hobject_flags & DUK_HOBJECT_FLAG_NATIVEFUNCTION) == 0);
56 DUK_ASSERT((hobject_flags & DUK_HOBJECT_FLAG_THREAD) == 0);
57
58 res = (duk_hobject *) DUK_ALLOC(heap, sizeof(duk_hobject));
59 if (!res) {
60 return NULL;
61 }
62 DUK_MEMZERO(res, sizeof(duk_hobject));
63
64 duk__init_object_parts(heap, res, hobject_flags);
65
66 return res;
67 }
68
69 DUK_INTERNAL duk_hcompiledfunction *duk_hcompiledfunction_alloc(duk_heap *heap, duk_uint_t hobject_flags) {
70 duk_hcompiledfunction *res;
71
72 res = (duk_hcompiledfunction *) DUK_ALLOC(heap, sizeof(duk_hcompiledfunction));
73 if (!res) {
74 return NULL;
75 }
76 DUK_MEMZERO(res, sizeof(duk_hcompiledfunction));
77
78 duk__init_object_parts(heap, &res->obj, hobject_flags);
79
80 #ifdef DUK_USE_EXPLICIT_NULL_INIT
81 #ifdef DUK_USE_HEAPPTR16
82 /* NULL pointer is required to encode to zero, so memset is enough. */
83 #else
84 res->data = NULL;
85 res->funcs = NULL;
86 res->bytecode = NULL;
87 #endif
88 #endif
89
90 return res;
91 }
92
93 DUK_INTERNAL duk_hnativefunction *duk_hnativefunction_alloc(duk_heap *heap, duk_uint_t hobject_flags) {
94 duk_hnativefunction *res;
95
96 res = (duk_hnativefunction *) DUK_ALLOC(heap, sizeof(duk_hnativefunction));
97 if (!res) {
98 return NULL;
99 }
100 DUK_MEMZERO(res, sizeof(duk_hnativefunction));
101
102 duk__init_object_parts(heap, &res->obj, hobject_flags);
103
104 #ifdef DUK_USE_EXPLICIT_NULL_INIT
105 res->func = NULL;
106 #endif
107
108 return res;
109 }
110
111 DUK_INTERNAL duk_hbufferobject *duk_hbufferobject_alloc(duk_heap *heap, duk_uint_t hobject_flags) {
112 duk_hbufferobject *res;
113
114 res = (duk_hbufferobject *) DUK_ALLOC(heap, sizeof(duk_hbufferobject));
115 if (!res) {
116 return NULL;
117 }
118 DUK_MEMZERO(res, sizeof(duk_hbufferobject));
119
120 duk__init_object_parts(heap, &res->obj, hobject_flags);
121
122 #ifdef DUK_USE_EXPLICIT_NULL_INIT
123 res->buf = NULL;
124 #endif
125
126 DUK_ASSERT_HBUFFEROBJECT_VALID(res);
127 return res;
128 }
129
130 /*
131 * Allocate a new thread.
132 *
133 * Leaves the built-ins array uninitialized. The caller must either
134 * initialize a new global context or share existing built-ins from
135 * another thread.
136 */
137
138 DUK_INTERNAL duk_hthread *duk_hthread_alloc(duk_heap *heap, duk_uint_t hobject_flags) {
139 duk_hthread *res;
140
141 res = (duk_hthread *) DUK_ALLOC(heap, sizeof(duk_hthread));
142 if (!res) {
143 return NULL;
144 }
145 DUK_MEMZERO(res, sizeof(duk_hthread));
146
147 duk__init_object_parts(heap, &res->obj, hobject_flags);
148
149 #ifdef DUK_USE_EXPLICIT_NULL_INIT
150 res->ptr_curr_pc = NULL;
151 res->heap = NULL;
152 res->valstack = NULL;
153 res->valstack_end = NULL;
154 res->valstack_bottom = NULL;
155 res->valstack_top = NULL;
156 res->callstack = NULL;
157 res->catchstack = NULL;
158 res->resumer = NULL;
159 res->compile_ctx = NULL,
160 #ifdef DUK_USE_HEAPPTR16
161 res->strs16 = NULL;
162 #else
163 res->strs = NULL;
164 #endif
165 {
166 int i;
167 for (i = 0; i < DUK_NUM_BUILTINS; i++) {
168 res->builtins[i] = NULL;
169 }
170 }
171 #endif
172 /* when nothing is running, API calls are in non-strict mode */
173 DUK_ASSERT(res->strict == 0);
174
175 res->heap = heap;
176 res->valstack_max = DUK_VALSTACK_DEFAULT_MAX;
177 res->callstack_max = DUK_CALLSTACK_DEFAULT_MAX;
178 res->catchstack_max = DUK_CATCHSTACK_DEFAULT_MAX;
179
180 return res;
181 }
182
183 #if 0 /* unused now */
184 DUK_INTERNAL duk_hobject *duk_hobject_alloc_checked(duk_hthread *thr, duk_uint_t hobject_flags) {
185 duk_hobject *res = duk_hobject_alloc(thr->heap, hobject_flags);
186 if (!res) {
187 DUK_ERROR(thr, DUK_ERR_ALLOC_ERROR, "failed to allocate an object");
188 }
189 return res;
190 }
191 #endif