Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
duk_heap_alloc.c File Reference
#include "duk_internal.h"

Go to the source code of this file.

Macros

#define DUK__BITPACK_LETTER_LIMIT   26
 
#define DUK__BITPACK_UNDERSCORE   26
 
#define DUK__BITPACK_FF   27
 
#define DUK__BITPACK_SWITCH1   29
 
#define DUK__BITPACK_SWITCH   30
 
#define DUK__BITPACK_SEVENBIT   31
 

Functions

DUK_INTERNAL void duk_free_hobject_inner (duk_heap *heap, duk_hobject *h)
 
DUK_INTERNAL void duk_free_hbuffer_inner (duk_heap *heap, duk_hbuffer *h)
 
DUK_INTERNAL void duk_free_hstring_inner (duk_heap *heap, duk_hstring *h)
 
DUK_INTERNAL void duk_heap_free_heaphdr_raw (duk_heap *heap, duk_heaphdr *hdr)
 
DUK_LOCAL void duk__free_allocated (duk_heap *heap)
 
DUK_LOCAL void duk__free_refzero_list (duk_heap *heap)
 
DUK_LOCAL void duk__free_markandsweep_finalize_list (duk_heap *heap)
 
DUK_LOCAL void duk__free_stringtable (duk_heap *heap)
 
DUK_LOCAL void duk__free_run_finalizers (duk_heap *heap)
 
DUK_INTERNAL void duk_heap_free (duk_heap *heap)
 
DUK_LOCAL duk_bool_t duk__init_heap_strings (duk_heap *heap)
 
DUK_LOCAL duk_bool_t duk__init_heap_thread (duk_heap *heap)
 
DUK_INTERNAL duk_heapduk_heap_alloc (duk_alloc_function alloc_func, duk_realloc_function realloc_func, duk_free_function free_func, void *heap_udata, duk_fatal_function fatal_func)
 

Macro Definition Documentation

◆ DUK__BITPACK_FF

#define DUK__BITPACK_FF   27

Definition at line 10 of file duktape-1.5.2/src-separate/duk_heap_alloc.c.

Referenced by duk__init_heap_strings().

◆ DUK__BITPACK_LETTER_LIMIT

#define DUK__BITPACK_LETTER_LIMIT   26

Definition at line 8 of file duktape-1.5.2/src-separate/duk_heap_alloc.c.

Referenced by duk__init_heap_strings().

◆ DUK__BITPACK_SEVENBIT

#define DUK__BITPACK_SEVENBIT   31

Definition at line 13 of file duktape-1.5.2/src-separate/duk_heap_alloc.c.

Referenced by duk__init_heap_strings().

◆ DUK__BITPACK_SWITCH

#define DUK__BITPACK_SWITCH   30

Definition at line 12 of file duktape-1.5.2/src-separate/duk_heap_alloc.c.

Referenced by duk__init_heap_strings().

◆ DUK__BITPACK_SWITCH1

#define DUK__BITPACK_SWITCH1   29

Definition at line 11 of file duktape-1.5.2/src-separate/duk_heap_alloc.c.

Referenced by duk__init_heap_strings().

◆ DUK__BITPACK_UNDERSCORE

#define DUK__BITPACK_UNDERSCORE   26

Definition at line 9 of file duktape-1.5.2/src-separate/duk_heap_alloc.c.

Referenced by duk__init_heap_strings().

Function Documentation

◆ duk__free_allocated()

DUK_LOCAL void duk__free_allocated ( duk_heap * heap)

Definition at line 123 of file duktape-1.5.2/src-separate/duk_heap_alloc.c.

123 {
124 duk_heaphdr *curr;
126
127 curr = heap->heap_allocated;
128 while (curr) {
129 /* We don't log or warn about freeing zero refcount objects
130 * because they may happen with finalizer processing.
131 */
132
133 DUK_DDD(DUK_DDDPRINT("FINALFREE (allocated): %!iO",
134 (duk_heaphdr *) curr));
135 next = DUK_HEAPHDR_GET_NEXT(heap, curr);
136 duk_heap_free_heaphdr_raw(heap, curr);
137 curr = next;
138 }
139}
#define DUK_HEAPHDR_GET_NEXT(heap, h)
DUK_INTERNAL void duk_heap_free_heaphdr_raw(duk_heap *heap, duk_heaphdr *hdr)
#define next(ls)

References DUK_DDD, DUK_DDDPRINT, duk_heap_free_heaphdr_raw(), DUK_HEAPHDR_GET_NEXT, duk_heap::heap_allocated, and next.

Referenced by duk_heap_free().

◆ duk__free_markandsweep_finalize_list()

DUK_LOCAL void duk__free_markandsweep_finalize_list ( duk_heap * heap)

Definition at line 158 of file duktape-1.5.2/src-separate/duk_heap_alloc.c.

158 {
159 duk_heaphdr *curr;
161
162 curr = heap->finalize_list;
163 while (curr) {
164 DUK_DDD(DUK_DDDPRINT("FINALFREE (finalize_list): %!iO",
165 (duk_heaphdr *) curr));
166 next = DUK_HEAPHDR_GET_NEXT(heap, curr);
167 duk_heap_free_heaphdr_raw(heap, curr);
168 curr = next;
169 }
170}

References DUK_DDD, DUK_DDDPRINT, duk_heap_free_heaphdr_raw(), DUK_HEAPHDR_GET_NEXT, duk_heap::finalize_list, and next.

Referenced by duk_heap_free().

◆ duk__free_refzero_list()

DUK_LOCAL void duk__free_refzero_list ( duk_heap * heap)

Definition at line 142 of file duktape-1.5.2/src-separate/duk_heap_alloc.c.

142 {
143 duk_heaphdr *curr;
145
146 curr = heap->refzero_list;
147 while (curr) {
148 DUK_DDD(DUK_DDDPRINT("FINALFREE (refzero_list): %!iO",
149 (duk_heaphdr *) curr));
150 next = DUK_HEAPHDR_GET_NEXT(heap, curr);
151 duk_heap_free_heaphdr_raw(heap, curr);
152 curr = next;
153 }
154}

References DUK_DDD, DUK_DDDPRINT, duk_heap_free_heaphdr_raw(), DUK_HEAPHDR_GET_NEXT, next, and duk_heap::refzero_list.

Referenced by duk_heap_free().

◆ duk__free_run_finalizers()

DUK_LOCAL void duk__free_run_finalizers ( duk_heap * heap)

Definition at line 178 of file duktape-1.5.2/src-separate/duk_heap_alloc.c.

178 {
179 duk_hthread *thr;
180 duk_heaphdr *curr;
181 duk_uint_t round_no;
182 duk_size_t count_all;
183 duk_size_t count_finalized;
184 duk_size_t curr_limit;
185
186 DUK_ASSERT(heap != NULL);
187 DUK_ASSERT(heap->heap_thread != NULL);
188
189#if defined(DUK_USE_REFERENCE_COUNTING)
190 DUK_ASSERT(heap->refzero_list == NULL); /* refzero not running -> must be empty */
191#endif
192#if defined(DUK_USE_MARK_AND_SWEEP)
193 DUK_ASSERT(heap->finalize_list == NULL); /* mark-and-sweep not running -> must be empty */
194#endif
195
196 /* XXX: here again finalizer thread is the heap_thread which needs
197 * to be coordinated with finalizer thread fixes.
198 */
199 thr = heap->heap_thread;
200 DUK_ASSERT(thr != NULL);
201
202 /* Prevent mark-and-sweep for the pending finalizers, also prevents
203 * refzero handling from moving objects away from the heap_allocated
204 * list. (The flag meaning is slightly abused here.)
205 */
208
209 curr_limit = 0; /* suppress warning, not used */
210 for (round_no = 0; ; round_no++) {
211 curr = heap->heap_allocated;
212 count_all = 0;
213 count_finalized = 0;
214 while (curr) {
215 count_all++;
217 /* Only objects in heap_allocated may have finalizers. Check that
218 * the object itself has a _Finalizer property (own or inherited)
219 * so that we don't execute finalizers for e.g. Proxy objects.
220 */
221 DUK_ASSERT(thr != NULL);
222 DUK_ASSERT(curr != NULL);
223
225 if (!DUK_HEAPHDR_HAS_FINALIZED((duk_heaphdr *) curr)) {
226 DUK_ASSERT(DUK_HEAP_HAS_FINALIZER_NORESCUE(heap)); /* maps to finalizer 2nd argument */
228 count_finalized++;
229 }
230 }
231 }
232 curr = DUK_HEAPHDR_GET_NEXT(heap, curr);
233 }
234
235 /* Each round of finalizer execution may spawn new finalizable objects
236 * which is normal behavior for some applications. Allow multiple
237 * rounds of finalization, but use a shrinking limit based on the
238 * first round to detect the case where a runaway finalizer creates
239 * an unbounded amount of new finalizable objects. Finalizer rescue
240 * is not supported: the semantics are unclear because most of the
241 * objects being finalized here are already reachable. The finalizer
242 * is given a boolean to indicate that rescue is not possible.
243 *
244 * See discussion in: https://github.com/svaarala/duktape/pull/473
245 */
246
247 if (round_no == 0) {
248 /* Cannot wrap: each object is at least 8 bytes so count is
249 * at most 1/8 of that.
250 */
251 curr_limit = count_all * 2;
252 } else {
253 curr_limit = (curr_limit * 3) / 4; /* Decrease by 25% every round */
254 }
255 DUK_D(DUK_DPRINT("finalizer round %ld complete, %ld objects, tried to execute %ld finalizers, current limit is %ld",
256 (long) round_no, (long) count_all, (long) count_finalized, (long) curr_limit));
257
258 if (count_finalized == 0) {
259 DUK_D(DUK_DPRINT("no more finalizable objects, forced finalization finished"));
260 break;
261 }
262 if (count_finalized >= curr_limit) {
263 DUK_D(DUK_DPRINT("finalizer count above limit, potentially runaway finalizer; skip remaining finalizers"));
264 break;
265 }
266 }
267
270}
duk_uint_fast32_t duk_uint_t
#define DUK_HEAP_HAS_FINALIZER_NORESCUE(heap)
#define DUK_HEAPHDR_GET_TYPE(h)
#define DUK_HEAP_SET_MARKANDSWEEP_RUNNING(heap)
#define DUK_HEAP_CLEAR_MARKANDSWEEP_RUNNING(heap)
DUK_INTERNAL_DECL duk_bool_t duk_hobject_hasprop_raw(duk_hthread *thr, duk_hobject *obj, duk_hstring *key)
#define DUK_HEAP_HAS_MARKANDSWEEP_RUNNING(heap)
#define DUK_HEAPHDR_HAS_FINALIZED(h)
#define DUK_HTHREAD_STRING_INT_FINALIZER(thr)
DUK_INTERNAL_DECL void duk_hobject_run_finalizer(duk_hthread *thr, duk_hobject *obj)
#define NULL
Definition gmacros.h:924

References DUK_ASSERT, DUK_D, DUK_DPRINT, DUK_HEAP_CLEAR_MARKANDSWEEP_RUNNING, DUK_HEAP_HAS_FINALIZER_NORESCUE, DUK_HEAP_HAS_MARKANDSWEEP_RUNNING, DUK_HEAP_SET_MARKANDSWEEP_RUNNING, DUK_HEAPHDR_GET_NEXT, DUK_HEAPHDR_GET_TYPE, DUK_HEAPHDR_HAS_FINALIZED, duk_hobject_hasprop_raw(), duk_hobject_run_finalizer(), DUK_HTHREAD_STRING_INT_FINALIZER, DUK_HTYPE_OBJECT, duk_heap::finalize_list, duk_heap::heap_allocated, duk_heap::heap_thread, NULL, and duk_heap::refzero_list.

Referenced by duk_heap_free().

◆ duk__free_stringtable()

DUK_LOCAL void duk__free_stringtable ( duk_heap * heap)

Definition at line 173 of file duktape-1.5.2/src-separate/duk_heap_alloc.c.

173 {
174 /* strings are only tracked by stringtable */
176}
DUK_INTERNAL void duk_heap_free_strtab(duk_heap *heap)

References duk_heap_free_strtab().

Referenced by duk_heap_free().

◆ duk__init_heap_strings()

DUK_LOCAL duk_bool_t duk__init_heap_strings ( duk_heap * heap)

Definition at line 375 of file duktape-1.5.2/src-separate/duk_heap_alloc.c.

375 {
376 duk_bitdecoder_ctx bd_ctx;
377 duk_bitdecoder_ctx *bd = &bd_ctx; /* convenience */
378 duk_small_uint_t i, j;
379
380 DUK_MEMZERO(&bd_ctx, sizeof(bd_ctx));
381 bd->data = (const duk_uint8_t *) duk_strings_data;
383
384 for (i = 0; i < DUK_HEAP_NUM_STRINGS; i++) {
385 duk_uint8_t tmp[DUK_STRDATA_MAX_STRLEN];
386 duk_hstring *h;
388 duk_small_uint_t mode;
390
391 len = duk_bd_decode(bd, 5);
392 mode = 32; /* 0 = uppercase, 32 = lowercase (= 'a' - 'A') */
393 for (j = 0; j < len; j++) {
394 t = duk_bd_decode(bd, 5);
396 t = t + DUK_ASC_UC_A + mode;
397 } else if (t == DUK__BITPACK_UNDERSCORE) {
399 } else if (t == DUK__BITPACK_FF) {
400 /* Internal keys are prefixed with 0xFF in the stringtable
401 * (which makes them invalid UTF-8 on purpose).
402 */
403 t = 0xff;
404 } else if (t == DUK__BITPACK_SWITCH1) {
405 t = duk_bd_decode(bd, 5);
406 DUK_ASSERT_DISABLE(t >= 0); /* unsigned */
407 DUK_ASSERT(t <= 25);
408 t = t + DUK_ASC_UC_A + (mode ^ 32);
409 } else if (t == DUK__BITPACK_SWITCH) {
410 mode = mode ^ 32;
411 t = duk_bd_decode(bd, 5);
412 DUK_ASSERT_DISABLE(t >= 0);
413 DUK_ASSERT(t <= 25);
414 t = t + DUK_ASC_UC_A + mode;
415 } else if (t == DUK__BITPACK_SEVENBIT) {
416 t = duk_bd_decode(bd, 7);
417 }
418 tmp[j] = (duk_uint8_t) t;
419 }
420
421 /* No need to length check string: it will never exceed even
422 * the 16-bit length maximum.
423 */
424 DUK_ASSERT(len <= 0xffffUL);
425 DUK_DDD(DUK_DDDPRINT("intern built-in string %ld", (long) i));
426 h = duk_heap_string_intern(heap, tmp, len);
427 if (!h) {
428 goto error;
429 }
431
432 /* Special flags checks. Since these strings are always
433 * reachable and a string cannot appear twice in the string
434 * table, there's no need to check/set these flags elsewhere.
435 * The 'internal' flag is set by string intern code.
436 */
437 if (i == DUK_STRIDX_EVAL || i == DUK_STRIDX_LC_ARGUMENTS) {
439 }
444 }
445 }
446
447 DUK_DDD(DUK_DDDPRINT("interned: %!O", (duk_heaphdr *) h));
448
449 /* XXX: The incref macro takes a thread pointer but doesn't
450 * use it right now.
451 */
452 DUK_HSTRING_INCREF(_never_referenced_, h);
453
454#if defined(DUK_USE_HEAPPTR16)
455 heap->strs16[i] = DUK_USE_HEAPPTR_ENC16(heap->heap_udata, (void *) h);
456#else
457 heap->strs[i] = h;
458#endif
459 }
460
461 return 1;
462
463 error:
464 return 0;
465}
unsigned int duk_small_uint_t
#define DUK_MEMZERO(p, n)
#define DUK_STRIDX_LC_ARGUMENTS
#define DUK_STRDATA_MAX_STRLEN
DUK_INTERNAL const duk_uint8_t duk_strings_data[1049]
#define DUK_STRIDX_START_RESERVED
#define DUK_STRDATA_DATA_LENGTH
#define DUK_ASSERT_DISABLE(x)
DUK_INTERNAL_DECL duk_hstring * duk_heap_string_intern(duk_heap *heap, const duk_uint8_t *str, duk_uint32_t blen)
#define DUK_HSTRING_SET_EVAL_OR_ARGUMENTS(x)
#define DUK_HSTRING_INCREF(thr, h)
#define DUK_HEAPHDR_HAS_READONLY(h)
#define DUK_STRIDX_START_STRICT_RESERVED
#define DUK_HSTRING_SET_STRICT_RESERVED_WORD(x)
#define DUK_HSTRING_SET_RESERVED_WORD(x)
#define DUK_STRIDX_END_RESERVED
DUK_INTERNAL_DECL duk_int32_t duk_bd_decode(duk_bitdecoder_ctx *ctx, duk_small_int_t bits)
static void error(LoadState *S, const char *why)
duk_hstring * strs[DUK_HEAP_NUM_STRINGS]

References duk_bitdecoder_ctx::data, DUK__BITPACK_FF, DUK__BITPACK_LETTER_LIMIT, DUK__BITPACK_SEVENBIT, DUK__BITPACK_SWITCH, DUK__BITPACK_SWITCH1, DUK__BITPACK_UNDERSCORE, DUK_ASC_UC_A, DUK_ASC_UNDERSCORE, DUK_ASSERT, DUK_ASSERT_DISABLE, duk_bd_decode(), DUK_DDD, DUK_DDDPRINT, DUK_HEAP_NUM_STRINGS, duk_heap_string_intern(), DUK_HEAPHDR_HAS_READONLY, DUK_HSTRING_INCREF, DUK_HSTRING_SET_EVAL_OR_ARGUMENTS, DUK_HSTRING_SET_RESERVED_WORD, DUK_HSTRING_SET_STRICT_RESERVED_WORD, DUK_MEMZERO, DUK_STRDATA_DATA_LENGTH, DUK_STRDATA_MAX_STRLEN, DUK_STRIDX_END_RESERVED, DUK_STRIDX_EVAL, DUK_STRIDX_LC_ARGUMENTS, DUK_STRIDX_START_RESERVED, DUK_STRIDX_START_STRICT_RESERVED, duk_strings_data, error(), duk_heap::heap_udata, duk_bitdecoder_ctx::length, and duk_heap::strs.

Referenced by duk_heap_alloc().

◆ duk__init_heap_thread()

DUK_LOCAL duk_bool_t duk__init_heap_thread ( duk_heap * heap)

Definition at line 468 of file duktape-1.5.2/src-separate/duk_heap_alloc.c.

468 {
469 duk_hthread *thr;
470
471 DUK_DD(DUK_DDPRINT("heap init: alloc heap thread"));
472 thr = duk_hthread_alloc(heap,
476 if (!thr) {
477 DUK_D(DUK_DPRINT("failed to alloc heap_thread"));
478 return 0;
479 }
481#if defined(DUK_USE_ROM_STRINGS)
482 /* No strs[] pointer. */
483#else /* DUK_USE_ROM_STRINGS */
484#if defined(DUK_USE_HEAPPTR16)
485 thr->strs16 = heap->strs16;
486#else
487 thr->strs = heap->strs;
488#endif
489#endif /* DUK_USE_ROM_STRINGS */
490
491 heap->heap_thread = thr;
492 DUK_HTHREAD_INCREF(thr, thr); /* Note: first argument not really used */
493
494 /* 'thr' is now reachable */
495
496 if (!duk_hthread_init_stacks(heap, thr)) {
497 return 0;
498 }
499
500 /* XXX: this may now fail, and is not handled correctly */
502
503 /* default prototype (Note: 'thr' must be reachable) */
505
506 return 1;
507}
DUK_INTERNAL_DECL void duk_hthread_create_builtin_objects(duk_hthread *thr)
#define DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, h, p)
DUK_INTERNAL_DECL duk_hthread * duk_hthread_alloc(duk_heap *heap, duk_uint_t hobject_flags)
#define DUK_HOBJECT_CLASS_AS_FLAGS(v)
#define DUK_HTHREAD_STATE_INACTIVE
#define DUK_HTHREAD_INCREF(thr, h)
DUK_INTERNAL_DECL duk_bool_t duk_hthread_init_stacks(duk_heap *heap, duk_hthread *thr)
#define DUK_HOBJECT_CLASS_THREAD
#define DUK_HOBJECT_FLAG_THREAD
#define DUK_BIDX_THREAD_PROTOTYPE
#define DUK_HOBJECT_FLAG_EXTENSIBLE

References duk_hthread::builtins, DUK_BIDX_THREAD_PROTOTYPE, DUK_D, DUK_DD, DUK_DDPRINT, DUK_DPRINT, DUK_HOBJECT_CLASS_AS_FLAGS, DUK_HOBJECT_CLASS_THREAD, DUK_HOBJECT_FLAG_EXTENSIBLE, DUK_HOBJECT_FLAG_THREAD, DUK_HOBJECT_SET_PROTOTYPE_UPDREF, duk_hthread_alloc(), duk_hthread_create_builtin_objects(), DUK_HTHREAD_INCREF, duk_hthread_init_stacks(), DUK_HTHREAD_STATE_INACTIVE, duk_heap::heap_thread, duk_hthread::state, duk_hthread::strs, and duk_heap::strs.

Referenced by duk_heap_alloc().

◆ duk_free_hbuffer_inner()

DUK_INTERNAL void duk_free_hbuffer_inner ( duk_heap * heap,
duk_hbuffer * h )

◆ duk_free_hobject_inner()

DUK_INTERNAL void duk_free_hobject_inner ( duk_heap * heap,
duk_hobject * h )

Definition at line 29 of file duktape-1.5.2/src-separate/duk_heap_alloc.c.

29 {
30 DUK_ASSERT(heap != NULL);
31 DUK_ASSERT(h != NULL);
32
33 DUK_FREE(heap, DUK_HOBJECT_GET_PROPS(heap, h));
34
37 DUK_UNREF(f);
38 /* Currently nothing to free; 'data' is a heap object */
39 } else if (DUK_HOBJECT_IS_NATIVEFUNCTION(h)) {
41 DUK_UNREF(f);
42 /* Currently nothing to free */
43 } else if (DUK_HOBJECT_IS_THREAD(h)) {
44 duk_hthread *t = (duk_hthread *) h;
45 DUK_FREE(heap, t->valstack);
46 DUK_FREE(heap, t->callstack);
47 DUK_FREE(heap, t->catchstack);
48 /* Don't free h->resumer because it exists in the heap.
49 * Callstack entries also contain function pointers which
50 * are not freed for the same reason.
51 */
52
53 /* XXX: with 'caller' property the callstack would need
54 * to be unwound to update the 'caller' properties of
55 * functions in the callstack.
56 */
57 }
58}
#define DUK_HOBJECT_GET_PROPS(heap, h)
#define DUK_HOBJECT_IS_COMPILEDFUNCTION(h)
#define DUK_HOBJECT_IS_THREAD(h)
#define DUK_HOBJECT_IS_NATIVEFUNCTION(h)

References duk_hthread::callstack, duk_hthread::catchstack, DUK_ASSERT, DUK_FREE, DUK_HOBJECT_GET_PROPS, DUK_HOBJECT_IS_COMPILEDFUNCTION, DUK_HOBJECT_IS_NATIVEFUNCTION, DUK_HOBJECT_IS_THREAD, DUK_UNREF, NULL, and duk_hthread::valstack.

Referenced by duk_heap_free_heaphdr_raw().

◆ duk_free_hstring_inner()

DUK_INTERNAL void duk_free_hstring_inner ( duk_heap * heap,
duk_hstring * h )

Definition at line 71 of file duktape-1.5.2/src-separate/duk_heap_alloc.c.

71 {
72 DUK_ASSERT(heap != NULL);
73 DUK_ASSERT(h != NULL);
74
75 DUK_UNREF(heap);
76 DUK_UNREF(h);
77
78#if defined(DUK_USE_HSTRING_EXTDATA) && defined(DUK_USE_EXTSTR_FREE)
80 DUK_DDD(DUK_DDDPRINT("free extstr: hstring %!O, extdata: %p",
81 h, DUK_HSTRING_GET_EXTDATA((duk_hstring_external *) h)));
82 DUK_USE_EXTSTR_FREE(heap->heap_udata, (const void *) DUK_HSTRING_GET_EXTDATA((duk_hstring_external *) h));
83 }
84#endif
85}
#define DUK_HSTRING_HAS_EXTDATA(x)

References DUK_ASSERT, DUK_DDD, DUK_DDDPRINT, DUK_HSTRING_HAS_EXTDATA, DUK_UNREF, duk_heap::heap_udata, and NULL.

Referenced by duk_heap_free_heaphdr_raw().

◆ duk_heap_alloc()

DUK_INTERNAL duk_heap * duk_heap_alloc ( duk_alloc_function alloc_func,
duk_realloc_function realloc_func,
duk_free_function free_func,
void * heap_udata,
duk_fatal_function fatal_func )

Definition at line 731 of file duktape-1.5.2/src-separate/duk_heap_alloc.c.

735 {
736 duk_heap *res = NULL;
737
738 /* Silence a few global unused warnings here. */
740
741 DUK_D(DUK_DPRINT("allocate heap"));
742
743 /*
744 * Debug dump type sizes
745 */
746
747#if defined(DUK_USE_DEBUG)
748 duk__dump_misc_options();
749 duk__dump_type_sizes();
750 duk__dump_type_limits();
751#endif
752
753 /*
754 * If selftests enabled, run them as early as possible
755 */
756#if defined(DUK_USE_SELF_TESTS)
757 DUK_D(DUK_DPRINT("running self tests"));
758 duk_selftest_run_tests();
759 DUK_D(DUK_DPRINT("self tests passed"));
760#endif
761
762 /*
763 * Computed values (e.g. INFINITY)
764 */
765
766#if defined(DUK_USE_COMPUTED_NAN)
767 do {
768 /* Workaround for some exotic platforms where NAN is missing
769 * and the expression (0.0 / 0.0) does NOT result in a NaN.
770 * Such platforms use the global 'duk_computed_nan' which must
771 * be initialized at runtime. Use 'volatile' to ensure that
772 * the compiler will actually do the computation and not try
773 * to do constant folding which might result in the original
774 * problem.
775 */
776 volatile double dbl1 = 0.0;
777 volatile double dbl2 = 0.0;
778 duk_computed_nan = dbl1 / dbl2;
779 } while (0);
780#endif
781
782#if defined(DUK_USE_COMPUTED_INFINITY)
783 do {
784 /* Similar workaround for INFINITY. */
785 volatile double dbl1 = 1.0;
786 volatile double dbl2 = 0.0;
787 duk_computed_infinity = dbl1 / dbl2;
788 } while (0);
789#endif
790
791 /*
792 * Allocate heap struct
793 *
794 * Use a raw call, all macros expect the heap to be initialized
795 */
796
797 res = (duk_heap *) alloc_func(heap_udata, sizeof(duk_heap));
798 if (!res) {
799 goto error;
800 }
801
802 /*
803 * Zero the struct, and start initializing roughly in order
804 */
805
806 DUK_MEMZERO(res, sizeof(*res));
807
808 /* explicit NULL inits */
809#if defined(DUK_USE_EXPLICIT_NULL_INIT)
810 res->heap_udata = NULL;
811 res->heap_allocated = NULL;
812#if defined(DUK_USE_REFERENCE_COUNTING)
813 res->refzero_list = NULL;
814 res->refzero_list_tail = NULL;
815#endif
816#if defined(DUK_USE_MARK_AND_SWEEP)
817 res->finalize_list = NULL;
818#endif
819 res->heap_thread = NULL;
820 res->curr_thread = NULL;
821 res->heap_object = NULL;
822#if defined(DUK_USE_STRTAB_CHAIN)
823 /* nothing to NULL */
824#elif defined(DUK_USE_STRTAB_PROBE)
825#if defined(DUK_USE_HEAPPTR16)
826 res->strtable16 = (duk_uint16_t *) NULL;
827#else
828 res->strtable = (duk_hstring **) NULL;
829#endif
830#endif
831#if defined(DUK_USE_ROM_STRINGS)
832 /* no res->strs[] */
833#else /* DUK_USE_ROM_STRINGS */
834#if defined(DUK_USE_HEAPPTR16)
835 /* res->strs16[] is zeroed and zero decodes to NULL, so no NULL inits. */
836#else
837 {
839 for (i = 0; i < DUK_HEAP_NUM_STRINGS; i++) {
840 res->strs[i] = NULL;
841 }
842 }
843#endif
844#endif /* DUK_USE_ROM_STRINGS */
845#if defined(DUK_USE_DEBUGGER_SUPPORT)
846 res->dbg_read_cb = NULL;
847 res->dbg_write_cb = NULL;
848 res->dbg_peek_cb = NULL;
849 res->dbg_read_flush_cb = NULL;
850 res->dbg_write_flush_cb = NULL;
851 res->dbg_request_cb = NULL;
852 res->dbg_udata = NULL;
853 res->dbg_step_thread = NULL;
854#endif
855#endif /* DUK_USE_EXPLICIT_NULL_INIT */
856
857 res->alloc_func = alloc_func;
858 res->realloc_func = realloc_func;
859 res->free_func = free_func;
860 res->heap_udata = heap_udata;
861 res->fatal_func = fatal_func;
862
863#if defined(DUK_USE_HEAPPTR16)
864 /* XXX: zero assumption */
865 res->heapptr_null16 = DUK_USE_HEAPPTR_ENC16(res->heap_udata, (void *) NULL);
866 res->heapptr_deleted16 = DUK_USE_HEAPPTR_ENC16(res->heap_udata, (void *) DUK_STRTAB_DELETED_MARKER(res));
867#endif
868
869 /* res->mark_and_sweep_trigger_counter == 0 -> now causes immediate GC; which is OK */
870
871 res->call_recursion_depth = 0;
873
874 /* XXX: use the pointer as a seed for now: mix in time at least */
875
876 /* The casts through duk_intr_pt is to avoid the following GCC warning:
877 *
878 * warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
879 *
880 * This still generates a /Wp64 warning on VS2010 when compiling for x86.
881 */
882#if defined(DUK_USE_ROM_STRINGS)
883 /* XXX: make a common DUK_USE_ option, and allow custom fixed seed? */
884 DUK_D(DUK_DPRINT("using rom strings, force heap hash_seed to fixed value 0x%08lx", (long) DUK__FIXED_HASH_SEED));
885 res->hash_seed = (duk_uint32_t) DUK__FIXED_HASH_SEED;
886#else /* DUK_USE_ROM_STRINGS */
887 res->hash_seed = (duk_uint32_t) (duk_intptr_t) res;
888 res->rnd_state = (duk_uint32_t) (duk_intptr_t) res;
889#if !defined(DUK_USE_STRHASH_DENSE)
890 res->hash_seed ^= 5381; /* Bernstein hash init value is normally 5381; XOR it in in case pointer low bits are 0 */
891#endif
892#endif /* DUK_USE_ROM_STRINGS */
893
894#if defined(DUK_USE_EXPLICIT_NULL_INIT)
895 res->lj.jmpbuf_ptr = NULL;
896#endif
897 DUK_ASSERT(res->lj.type == DUK_LJ_TYPE_UNKNOWN); /* zero */
898
901
902#if (DUK_STRTAB_INITIAL_SIZE < DUK_UTIL_MIN_HASH_PRIME)
903#error initial heap stringtable size is defined incorrectly
904#endif
905
906 /*
907 * Init stringtable: fixed variant
908 */
909
910#if defined(DUK_USE_STRTAB_CHAIN)
912#if defined(DUK_USE_EXPLICIT_NULL_INIT)
913 {
915 for (i = 0; i < DUK_STRTAB_CHAIN_SIZE; i++) {
916#if defined(DUK_USE_HEAPPTR16)
917 res->strtable[i].u.str16 = res->heapptr_null16;
918#else
919 res->strtable[i].u.str = NULL;
920#endif
921 }
922 }
923#endif /* DUK_USE_EXPLICIT_NULL_INIT */
924#endif /* DUK_USE_STRTAB_CHAIN */
925
926 /*
927 * Init stringtable: probe variant
928 */
929
930#if defined(DUK_USE_STRTAB_PROBE)
931#if defined(DUK_USE_HEAPPTR16)
932 res->strtable16 = (duk_uint16_t *) alloc_func(heap_udata, sizeof(duk_uint16_t) * DUK_STRTAB_INITIAL_SIZE);
933 if (!res->strtable16) {
934 goto error;
935 }
936#else /* DUK_USE_HEAPPTR16 */
937 res->strtable = (duk_hstring **) alloc_func(heap_udata, sizeof(duk_hstring *) * DUK_STRTAB_INITIAL_SIZE);
938 if (!res->strtable) {
939 goto error;
940 }
941#endif /* DUK_USE_HEAPPTR16 */
943#if defined(DUK_USE_EXPLICIT_NULL_INIT)
944 {
947 for (i = 0; i < DUK_STRTAB_INITIAL_SIZE; i++) {
948#if defined(DUK_USE_HEAPPTR16)
949 res->strtable16[i] = res->heapptr_null16;
950#else
951 res->strtable[i] = NULL;
952#endif
953 }
954 }
955#else /* DUK_USE_EXPLICIT_NULL_INIT */
956#if defined(DUK_USE_HEAPPTR16)
957 DUK_MEMZERO(res->strtable16, sizeof(duk_uint16_t) * DUK_STRTAB_INITIAL_SIZE);
958#else
960#endif
961#endif /* DUK_USE_EXPLICIT_NULL_INIT */
962#endif /* DUK_USE_STRTAB_PROBE */
963
964 /*
965 * Init stringcache
966 */
967
968#if defined(DUK_USE_EXPLICIT_NULL_INIT)
969 {
971 for (i = 0; i < DUK_HEAP_STRCACHE_SIZE; i++) {
972 res->strcache[i].h = NULL;
973 }
974 }
975#endif
976
977 /* XXX: error handling is incomplete. It would be cleanest if
978 * there was a setjmp catchpoint, so that all init code could
979 * freely throw errors. If that were the case, the return code
980 * passing here could be removed.
981 */
982
983 /*
984 * Init built-in strings
985 */
986
987 DUK_DD(DUK_DDPRINT("HEAP: INIT STRINGS"));
988 if (!duk__init_heap_strings(res)) {
989 goto error;
990 }
991
992 /*
993 * Init the heap thread
994 */
995
996 DUK_DD(DUK_DDPRINT("HEAP: INIT HEAP THREAD"));
997 if (!duk__init_heap_thread(res)) {
998 goto error;
999 }
1000
1001 /*
1002 * Init the heap object
1003 */
1004
1005 DUK_DD(DUK_DDPRINT("HEAP: INIT HEAP OBJECT"));
1006 DUK_ASSERT(res->heap_thread != NULL);
1009 if (!res->heap_object) {
1010 goto error;
1011 }
1013
1014 /*
1015 * All done
1016 */
1017
1018 DUK_D(DUK_DPRINT("allocated heap: %p", (void *) res));
1019 return res;
1020
1021 error:
1022 DUK_D(DUK_DPRINT("heap allocation failed"));
1023
1024 if (res) {
1025 /* assumes that allocated pointers and alloc funcs are valid
1026 * if res exists
1027 */
1028 DUK_ASSERT(res->alloc_func != NULL);
1029 DUK_ASSERT(res->realloc_func != NULL);
1030 DUK_ASSERT(res->free_func != NULL);
1031 duk_heap_free(res);
1032 }
1033 return NULL;
1034}
#define DUK_USE_NATIVE_CALL_RECLIMIT
DUK_INTERNAL const char * duk_str_unsupported
#define DUK_STRTAB_DELETED_MARKER(heap)
#define DUK_HOBJECT_CLASS_OBJECT
#define DUK_STRTAB_CHAIN_SIZE
#define DUK_STRTAB_INITIAL_SIZE
#define DUK_HEAP_STRCACHE_SIZE
#define DUK_HOBJECT_INCREF(thr, h)
#define DUK_TVAL_SET_UNDEFINED(tv)
DUK_INTERNAL_DECL duk_hobject * duk_hobject_alloc(duk_heap *heap, duk_uint_t hobject_flags)
DUK_INTERNAL void duk_heap_free(duk_heap *heap)
DUK_LOCAL duk_bool_t duk__init_heap_thread(duk_heap *heap)
DUK_LOCAL duk_bool_t duk__init_heap_strings(duk_heap *heap)
duk_strcache strcache[DUK_HEAP_STRCACHE_SIZE]
duk_fatal_function fatal_func
duk_realloc_function realloc_func
duk_alloc_function alloc_func

References duk_heap::alloc_func, duk_heap::call_recursion_depth, duk_heap::call_recursion_limit, duk_heap::curr_thread, duk__init_heap_strings(), duk__init_heap_thread(), DUK_ASSERT, DUK_D, DUK_DD, DUK_DDPRINT, DUK_DPRINT, duk_heap_free(), DUK_HEAP_NUM_STRINGS, DUK_HEAP_STRCACHE_SIZE, duk_hobject_alloc(), DUK_HOBJECT_CLASS_AS_FLAGS, DUK_HOBJECT_CLASS_OBJECT, DUK_HOBJECT_FLAG_EXTENSIBLE, DUK_HOBJECT_INCREF, DUK_LJ_TYPE_UNKNOWN, DUK_MEMZERO, duk_str_unsupported, DUK_STRTAB_CHAIN_SIZE, DUK_STRTAB_DELETED_MARKER, DUK_STRTAB_INITIAL_SIZE, DUK_TVAL_SET_UNDEFINED, DUK_UNREF, DUK_USE_NATIVE_CALL_RECLIMIT, error(), duk_heap::fatal_func, duk_heap::finalize_list, duk_heap::free_func, duk_strcache::h, duk_heap::hash_seed, duk_heap::heap_allocated, duk_heap::heap_object, duk_heap::heap_thread, duk_heap::heap_udata, duk_ljstate::jmpbuf_ptr, duk_heap::lj, NULL, duk_heap::realloc_func, duk_heap::refzero_list, duk_heap::refzero_list_tail, duk_heap::rnd_state, duk_heap::st_size, duk_heap::strcache, duk_heap::strs, duk_heap::strtable, duk_ljstate::type, duk_ljstate::value1, and duk_ljstate::value2.

◆ duk_heap_free()

DUK_INTERNAL void duk_heap_free ( duk_heap * heap)

Definition at line 272 of file duktape-1.5.2/src-separate/duk_heap_alloc.c.

272 {
273 DUK_D(DUK_DPRINT("free heap: %p", (void *) heap));
274
275#if defined(DUK_USE_DEBUG)
276 duk_heap_dump_strtab(heap);
277#endif
278
279#if defined(DUK_USE_DEBUGGER_SUPPORT)
280 /* Detach a debugger if attached (can be called multiple times)
281 * safely.
282 */
283 /* XXX: Add a flag to reject an attempt to re-attach? Otherwise
284 * the detached callback may immediately reattach.
285 */
286 duk_debug_do_detach(heap);
287#endif
288
289 /* Execute finalizers before freeing the heap, even for reachable
290 * objects, and regardless of whether or not mark-and-sweep is
291 * enabled. This gives finalizers the chance to free any native
292 * resources like file handles, allocations made outside Duktape,
293 * etc. This is quite tricky to get right, so that all finalizer
294 * guarantees are honored.
295 *
296 * XXX: this perhaps requires an execution time limit.
297 */
298 DUK_D(DUK_DPRINT("execute finalizers before freeing heap"));
299#if defined(DUK_USE_MARK_AND_SWEEP)
300 /* Run mark-and-sweep a few times just in case (unreachable object
301 * finalizers run already here). The last round must rescue objects
302 * from the previous round without running any more finalizers. This
303 * ensures rescued objects get their FINALIZED flag cleared so that
304 * their finalizer is called once more in forced finalization to
305 * satisfy finalizer guarantees. However, we don't want to run any
306 * more finalizer because that'd required one more loop, and so on.
307 */
308 DUK_D(DUK_DPRINT("forced gc #1 in heap destruction"));
310 DUK_D(DUK_DPRINT("forced gc #2 in heap destruction"));
312 DUK_D(DUK_DPRINT("forced gc #3 in heap destruction (don't run finalizers)"));
313 duk_heap_mark_and_sweep(heap, DUK_MS_FLAG_SKIP_FINALIZERS); /* skip finalizers; queue finalizable objects to heap_allocated */
314#endif
315
316 DUK_HEAP_SET_FINALIZER_NORESCUE(heap); /* rescue no longer supported */
318
319 /* Note: heap->heap_thread, heap->curr_thread, and heap->heap_object
320 * are on the heap allocated list.
321 */
322
323 DUK_D(DUK_DPRINT("freeing heap objects of heap: %p", (void *) heap));
325
326#if defined(DUK_USE_REFERENCE_COUNTING)
327 DUK_D(DUK_DPRINT("freeing refzero list of heap: %p", (void *) heap));
329#endif
330
331#if defined(DUK_USE_MARK_AND_SWEEP)
332 DUK_D(DUK_DPRINT("freeing mark-and-sweep finalize list of heap: %p", (void *) heap));
334#endif
335
336 DUK_D(DUK_DPRINT("freeing string table of heap: %p", (void *) heap));
338
339 DUK_D(DUK_DPRINT("freeing heap structure: %p", (void *) heap));
340 heap->free_func(heap->heap_udata, heap);
341}
#define DUK_HEAP_SET_FINALIZER_NORESCUE(heap)
DUK_INTERNAL_DECL duk_bool_t duk_heap_mark_and_sweep(duk_heap *heap, duk_small_uint_t flags)
#define DUK_MS_FLAG_SKIP_FINALIZERS
DUK_LOCAL void duk__free_run_finalizers(duk_heap *heap)
DUK_LOCAL void duk__free_stringtable(duk_heap *heap)
DUK_LOCAL void duk__free_markandsweep_finalize_list(duk_heap *heap)
DUK_LOCAL void duk__free_refzero_list(duk_heap *heap)
DUK_LOCAL void duk__free_allocated(duk_heap *heap)

References duk__free_allocated(), duk__free_markandsweep_finalize_list(), duk__free_refzero_list(), duk__free_run_finalizers(), duk__free_stringtable(), DUK_D, DUK_DPRINT, duk_heap_mark_and_sweep(), DUK_HEAP_SET_FINALIZER_NORESCUE, DUK_MS_FLAG_SKIP_FINALIZERS, duk_heap::free_func, and duk_heap::heap_udata.

Referenced by duk_heap_alloc().

◆ duk_heap_free_heaphdr_raw()

DUK_INTERNAL void duk_heap_free_heaphdr_raw ( duk_heap * heap,
duk_heaphdr * hdr )

Definition at line 87 of file duktape-1.5.2/src-separate/duk_heap_alloc.c.

87 {
88 DUK_ASSERT(heap);
89 DUK_ASSERT(hdr);
90
91 DUK_DDD(DUK_DDDPRINT("free heaphdr %p, htype %ld", (void *) hdr, (long) DUK_HEAPHDR_GET_TYPE(hdr)));
92
93 switch ((int) DUK_HEAPHDR_GET_TYPE(hdr)) {
96 break;
99 break;
100 case DUK_HTYPE_BUFFER:
101 duk_free_hbuffer_inner(heap, (duk_hbuffer *) hdr);
102 break;
103 default:
105 }
106
107 DUK_FREE(heap, hdr);
108}
DUK_INTERNAL void duk_free_hobject_inner(duk_heap *heap, duk_hobject *h)
DUK_INTERNAL void duk_free_hstring_inner(duk_heap *heap, duk_hstring *h)
DUK_INTERNAL void duk_free_hbuffer_inner(duk_heap *heap, duk_hbuffer *h)

References DUK_ASSERT, DUK_DDD, DUK_DDDPRINT, DUK_FREE, duk_free_hbuffer_inner(), duk_free_hobject_inner(), duk_free_hstring_inner(), DUK_HEAPHDR_GET_TYPE, DUK_HTYPE_BUFFER, DUK_HTYPE_OBJECT, DUK_HTYPE_STRING, and DUK_UNREACHABLE.

Referenced by duk__free_allocated(), duk__free_markandsweep_finalize_list(), and duk__free_refzero_list().