Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
duk_heap.h File Reference

Go to the source code of this file.

Data Structures

struct  duk_breakpoint
 
struct  duk_strcache
 
struct  duk_ljstate
 
struct  duk_strtab_entry
 
struct  duk_heap
 

Macros

#define DUK_HEAP_FLAG_MARKANDSWEEP_RUNNING   (1 << 0) /* mark-and-sweep is currently running */
 
#define DUK_HEAP_FLAG_MARKANDSWEEP_RECLIMIT_REACHED   (1 << 1) /* mark-and-sweep marking reached a recursion limit and must use multi-pass marking */
 
#define DUK_HEAP_FLAG_REFZERO_FREE_RUNNING   (1 << 2) /* refcount code is processing refzero list */
 
#define DUK_HEAP_FLAG_ERRHANDLER_RUNNING   (1 << 3) /* an error handler (user callback to augment/replace error) is running */
 
#define DUK_HEAP_FLAG_INTERRUPT_RUNNING   (1 << 4) /* executor interrupt running (used to avoid nested interrupts) */
 
#define DUK_HEAP_FLAG_FINALIZER_NORESCUE   (1 << 5) /* heap destruction ongoing, finalizer rescue no longer possible */
 
#define DUK__HEAP_HAS_FLAGS(heap, bits)   ((heap)->flags & (bits))
 
#define DUK__HEAP_SET_FLAGS(heap, bits)
 
#define DUK__HEAP_CLEAR_FLAGS(heap, bits)
 
#define DUK_HEAP_HAS_MARKANDSWEEP_RUNNING(heap)   DUK__HEAP_HAS_FLAGS((heap), DUK_HEAP_FLAG_MARKANDSWEEP_RUNNING)
 
#define DUK_HEAP_HAS_MARKANDSWEEP_RECLIMIT_REACHED(heap)   DUK__HEAP_HAS_FLAGS((heap), DUK_HEAP_FLAG_MARKANDSWEEP_RECLIMIT_REACHED)
 
#define DUK_HEAP_HAS_REFZERO_FREE_RUNNING(heap)   DUK__HEAP_HAS_FLAGS((heap), DUK_HEAP_FLAG_REFZERO_FREE_RUNNING)
 
#define DUK_HEAP_HAS_ERRHANDLER_RUNNING(heap)   DUK__HEAP_HAS_FLAGS((heap), DUK_HEAP_FLAG_ERRHANDLER_RUNNING)
 
#define DUK_HEAP_HAS_INTERRUPT_RUNNING(heap)   DUK__HEAP_HAS_FLAGS((heap), DUK_HEAP_FLAG_INTERRUPT_RUNNING)
 
#define DUK_HEAP_HAS_FINALIZER_NORESCUE(heap)   DUK__HEAP_HAS_FLAGS((heap), DUK_HEAP_FLAG_FINALIZER_NORESCUE)
 
#define DUK_HEAP_SET_MARKANDSWEEP_RUNNING(heap)   DUK__HEAP_SET_FLAGS((heap), DUK_HEAP_FLAG_MARKANDSWEEP_RUNNING)
 
#define DUK_HEAP_SET_MARKANDSWEEP_RECLIMIT_REACHED(heap)   DUK__HEAP_SET_FLAGS((heap), DUK_HEAP_FLAG_MARKANDSWEEP_RECLIMIT_REACHED)
 
#define DUK_HEAP_SET_REFZERO_FREE_RUNNING(heap)   DUK__HEAP_SET_FLAGS((heap), DUK_HEAP_FLAG_REFZERO_FREE_RUNNING)
 
#define DUK_HEAP_SET_ERRHANDLER_RUNNING(heap)   DUK__HEAP_SET_FLAGS((heap), DUK_HEAP_FLAG_ERRHANDLER_RUNNING)
 
#define DUK_HEAP_SET_INTERRUPT_RUNNING(heap)   DUK__HEAP_SET_FLAGS((heap), DUK_HEAP_FLAG_INTERRUPT_RUNNING)
 
#define DUK_HEAP_SET_FINALIZER_NORESCUE(heap)   DUK__HEAP_SET_FLAGS((heap), DUK_HEAP_FLAG_FINALIZER_NORESCUE)
 
#define DUK_HEAP_CLEAR_MARKANDSWEEP_RUNNING(heap)   DUK__HEAP_CLEAR_FLAGS((heap), DUK_HEAP_FLAG_MARKANDSWEEP_RUNNING)
 
#define DUK_HEAP_CLEAR_MARKANDSWEEP_RECLIMIT_REACHED(heap)   DUK__HEAP_CLEAR_FLAGS((heap), DUK_HEAP_FLAG_MARKANDSWEEP_RECLIMIT_REACHED)
 
#define DUK_HEAP_CLEAR_REFZERO_FREE_RUNNING(heap)   DUK__HEAP_CLEAR_FLAGS((heap), DUK_HEAP_FLAG_REFZERO_FREE_RUNNING)
 
#define DUK_HEAP_CLEAR_ERRHANDLER_RUNNING(heap)   DUK__HEAP_CLEAR_FLAGS((heap), DUK_HEAP_FLAG_ERRHANDLER_RUNNING)
 
#define DUK_HEAP_CLEAR_INTERRUPT_RUNNING(heap)   DUK__HEAP_CLEAR_FLAGS((heap), DUK_HEAP_FLAG_INTERRUPT_RUNNING)
 
#define DUK_HEAP_CLEAR_FINALIZER_NORESCUE(heap)   DUK__HEAP_CLEAR_FLAGS((heap), DUK_HEAP_FLAG_FINALIZER_NORESCUE)
 
#define DUK_LJ_TYPE_UNKNOWN   0 /* unused */
 
#define DUK_LJ_TYPE_THROW   1 /* value1 -> error object */
 
#define DUK_LJ_TYPE_YIELD   2 /* value1 -> yield value, iserror -> error / normal */
 
#define DUK_LJ_TYPE_RESUME   3 /* value1 -> resume value, value2 -> resumee thread, iserror -> error/normal */
 
#define DUK_LJ_TYPE_BREAK   4 /* value1 -> label number, pseudo-type to indicate a break continuation (for ENDFIN) */
 
#define DUK_LJ_TYPE_CONTINUE   5 /* value1 -> label number, pseudo-type to indicate a continue continuation (for ENDFIN) */
 
#define DUK_LJ_TYPE_RETURN   6 /* value1 -> return value, pseudo-type to indicate a return continuation (for ENDFIN) */
 
#define DUK_LJ_TYPE_NORMAL   7 /* no value, pseudo-type to indicate a normal continuation (for ENDFIN) */
 
#define DUK_MS_FLAG_EMERGENCY   (1 << 0) /* emergency mode: try extra hard */
 
#define DUK_MS_FLAG_NO_STRINGTABLE_RESIZE   (1 << 1) /* don't resize stringtable (but may sweep it); needed during stringtable resize */
 
#define DUK_MS_FLAG_NO_OBJECT_COMPACTION   (1 << 2) /* don't compact objects; needed during object property allocation resize */
 
#define DUK_MS_FLAG_NO_FINALIZERS   (1 << 3) /* don't run finalizers; leave finalizable objects in finalize_list for next round */
 
#define DUK_MS_FLAG_SKIP_FINALIZERS   (1 << 4) /* don't run finalizers; queue finalizable objects back to heap_allocated */
 
#define DUK_HEAP_SWITCH_THREAD(heap, newthr)
 
#define DUK_HEAP_STRCACHE_SIZE   4
 
#define DUK_HEAP_STRINGCACHE_NOCACHE_LIMIT   16 /* strings up to the this length are not cached */
 
#define DUK_HEAP_INSERT_INTO_HEAP_ALLOCATED(heap, hdr)   duk_heap_insert_into_heap_allocated((heap),(hdr))
 
#define DUK_STRTAB_INITIAL_SIZE   17
 
#define DUK_STRTAB_DELETED_MARKER(heap)   ((duk_hstring *) heap)
 
#define DUK_STRTAB_MIN_FREE_DIVISOR   4 /* load factor max 75% */
 
#define DUK_STRTAB_MIN_USED_DIVISOR   4 /* load factor min 25% */
 
#define DUK_STRTAB_GROW_ST_SIZE(n)   ((n) + (n)) /* used entries + approx 100% -> reset load to 50% */
 
#define DUK_STRTAB_U32_MAX_STRLEN   10 /* 4'294'967'295 */
 
#define DUK_STRTAB_HIGHEST_32BIT_PRIME   0xfffffffbUL
 
#define DUK_STRTAB_HASH_INITIAL(hash, h_size)   ((hash) % (h_size))
 
#define DUK_STRTAB_HASH_PROBE_STEP(hash)   DUK_UTIL_GET_HASH_PROBE_STEP((hash))
 
#define DUK_STRTAB_CHAIN_SIZE   DUK_USE_STRTAB_CHAIN_SIZE
 
#define DUK_HEAP_GET_STRING(heap, idx)    ((heap)->strs[(idx)])
 
#define DUK_ALLOC_RAW(heap, size)    ((heap)->alloc_func((heap)->heap_udata, (size)))
 
#define DUK_REALLOC_RAW(heap, ptr, newsize)    ((heap)->realloc_func((heap)->heap_udata, (void *) (ptr), (newsize)))
 
#define DUK_FREE_RAW(heap, ptr)    ((heap)->free_func((heap)->heap_udata, (void *) (ptr)))
 
#define DUK_ALLOC(heap, size)   duk_heap_mem_alloc((heap), (size))
 
#define DUK_ALLOC_ZEROED(heap, size)   duk_heap_mem_alloc_zeroed((heap), (size))
 
#define DUK_REALLOC(heap, ptr, newsize)   duk_heap_mem_realloc((heap), (ptr), (newsize))
 
#define DUK_REALLOC_INDIRECT(heap, cb, ud, newsize)   duk_heap_mem_realloc_indirect((heap), (cb), (ud), (newsize))
 
#define DUK_FREE(heap, ptr)   duk_heap_mem_free((heap), (ptr))
 
#define DUK_HEAP_ALLOC_FAIL_MARKANDSWEEP_LIMIT
 
#define DUK_HEAP_ALLOC_FAIL_MARKANDSWEEP_EMERGENCY_LIMIT
 
#define DUK_HEAP_MAX_BREAKPOINTS   16
 
#define DUK_HEAP_DBG_RATELIMIT_OPCODES   4000
 
#define DUK_HEAP_DBG_RATELIMIT_MILLISECS   200
 
#define DUK_STEP_TYPE_NONE   0
 
#define DUK_STEP_TYPE_INTO   1
 
#define DUK_STEP_TYPE_OVER   2
 
#define DUK_STEP_TYPE_OUT   3
 

Typedefs

typedef void *(* duk_mem_getptr) (duk_heap *heap, void *ud)
 

Functions

DUK_INTERNAL_DECL 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)
 
DUK_INTERNAL_DECL void duk_heap_free (duk_heap *heap)
 
DUK_INTERNAL_DECL void duk_free_hobject_inner (duk_heap *heap, duk_hobject *h)
 
DUK_INTERNAL_DECL void duk_free_hbuffer_inner (duk_heap *heap, duk_hbuffer *h)
 
DUK_INTERNAL_DECL void duk_free_hstring_inner (duk_heap *heap, duk_hstring *h)
 
DUK_INTERNAL_DECL void duk_heap_free_heaphdr_raw (duk_heap *heap, duk_heaphdr *hdr)
 
DUK_INTERNAL_DECL void duk_heap_insert_into_heap_allocated (duk_heap *heap, duk_heaphdr *hdr)
 
DUK_INTERNAL_DECL duk_hstringduk_heap_string_intern (duk_heap *heap, const duk_uint8_t *str, duk_uint32_t blen)
 
DUK_INTERNAL_DECL duk_hstringduk_heap_string_intern_checked (duk_hthread *thr, const duk_uint8_t *str, duk_uint32_t len)
 
DUK_INTERNAL_DECL duk_hstringduk_heap_string_intern_u32 (duk_heap *heap, duk_uint32_t val)
 
DUK_INTERNAL_DECL duk_hstringduk_heap_string_intern_u32_checked (duk_hthread *thr, duk_uint32_t val)
 
DUK_INTERNAL void duk_heap_free_strtab (duk_heap *heap)
 
DUK_INTERNAL_DECL void duk_heap_strcache_string_remove (duk_heap *heap, duk_hstring *h)
 
DUK_INTERNAL_DECL duk_uint_fast32_t duk_heap_strcache_offset_char2byte (duk_hthread *thr, duk_hstring *h, duk_uint_fast32_t char_offset)
 
DUK_INTERNAL_DECL void * duk_heap_mem_alloc (duk_heap *heap, duk_size_t size)
 
DUK_INTERNAL_DECL void * duk_heap_mem_alloc_zeroed (duk_heap *heap, duk_size_t size)
 
DUK_INTERNAL_DECL void * duk_heap_mem_realloc (duk_heap *heap, void *ptr, duk_size_t newsize)
 
DUK_INTERNAL_DECL void * duk_heap_mem_realloc_indirect (duk_heap *heap, duk_mem_getptr cb, void *ud, duk_size_t newsize)
 
DUK_INTERNAL_DECL void duk_heap_mem_free (duk_heap *heap, void *ptr)
 
DUK_INTERNAL_DECL duk_uint32_t duk_heap_hashstring (duk_heap *heap, const duk_uint8_t *str, duk_size_t len)
 

Macro Definition Documentation

◆ DUK__HEAP_CLEAR_FLAGS

#define DUK__HEAP_CLEAR_FLAGS ( heap,
bits )
Value:
do { \
(heap)->flags &= ~(bits); \
} while (0)

Definition at line 28 of file duktape-1.8.0/src-separate/duk_heap.h.

28#define DUK__HEAP_CLEAR_FLAGS(heap,bits) do { \
29 (heap)->flags &= ~(bits); \
30 } while (0)

◆ DUK__HEAP_HAS_FLAGS

#define DUK__HEAP_HAS_FLAGS ( heap,
bits )   ((heap)->flags & (bits))

Definition at line 24 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK__HEAP_SET_FLAGS

#define DUK__HEAP_SET_FLAGS ( heap,
bits )
Value:
do { \
(heap)->flags |= (bits); \
} while (0)

Definition at line 25 of file duktape-1.8.0/src-separate/duk_heap.h.

25#define DUK__HEAP_SET_FLAGS(heap,bits) do { \
26 (heap)->flags |= (bits); \
27 } while (0)

◆ DUK_ALLOC

#define DUK_ALLOC ( heap,
size )   duk_heap_mem_alloc((heap), (size))

Definition at line 218 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_ALLOC_RAW

#define DUK_ALLOC_RAW ( heap,
size )    ((heap)->alloc_func((heap)->heap_udata, (size)))

Definition at line 178 of file duktape-1.8.0/src-separate/duk_heap.h.

178#define DUK_ALLOC_RAW(heap,size) \
179 ((heap)->alloc_func((heap)->heap_udata, (size)))

◆ DUK_ALLOC_ZEROED

#define DUK_ALLOC_ZEROED ( heap,
size )   duk_heap_mem_alloc_zeroed((heap), (size))

Definition at line 219 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_FREE

#define DUK_FREE ( heap,
ptr )   duk_heap_mem_free((heap), (ptr))

Definition at line 222 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_FREE_RAW

#define DUK_FREE_RAW ( heap,
ptr )    ((heap)->free_func((heap)->heap_udata, (void *) (ptr)))

Definition at line 184 of file duktape-1.8.0/src-separate/duk_heap.h.

184#define DUK_FREE_RAW(heap,ptr) \
185 ((heap)->free_func((heap)->heap_udata, (void *) (ptr)))

◆ DUK_HEAP_ALLOC_FAIL_MARKANDSWEEP_EMERGENCY_LIMIT

#define DUK_HEAP_ALLOC_FAIL_MARKANDSWEEP_EMERGENCY_LIMIT
Value:
3 /* Starting from this round, use emergency mode
* for mark-and-sweep.
*/

Definition at line 230 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_HEAP_ALLOC_FAIL_MARKANDSWEEP_LIMIT

#define DUK_HEAP_ALLOC_FAIL_MARKANDSWEEP_LIMIT
Value:
5 /* Retry allocation after mark-and-sweep for this
* many times. A single mark-and-sweep round is
* not guaranteed to free all unreferenced memory
* because of finalization (in fact, ANY number of
* rounds is strictly not enough).
*/

Definition at line 228 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_HEAP_CLEAR_ERRHANDLER_RUNNING

#define DUK_HEAP_CLEAR_ERRHANDLER_RUNNING ( heap)    DUK__HEAP_CLEAR_FLAGS((heap), DUK_HEAP_FLAG_ERRHANDLER_RUNNING)

Definition at line 49 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_HEAP_CLEAR_FINALIZER_NORESCUE

#define DUK_HEAP_CLEAR_FINALIZER_NORESCUE ( heap)    DUK__HEAP_CLEAR_FLAGS((heap), DUK_HEAP_FLAG_FINALIZER_NORESCUE)

Definition at line 51 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_HEAP_CLEAR_INTERRUPT_RUNNING

#define DUK_HEAP_CLEAR_INTERRUPT_RUNNING ( heap)    DUK__HEAP_CLEAR_FLAGS((heap), DUK_HEAP_FLAG_INTERRUPT_RUNNING)

Definition at line 50 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_HEAP_CLEAR_MARKANDSWEEP_RECLIMIT_REACHED

#define DUK_HEAP_CLEAR_MARKANDSWEEP_RECLIMIT_REACHED ( heap)    DUK__HEAP_CLEAR_FLAGS((heap), DUK_HEAP_FLAG_MARKANDSWEEP_RECLIMIT_REACHED)

Definition at line 47 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_HEAP_CLEAR_MARKANDSWEEP_RUNNING

#define DUK_HEAP_CLEAR_MARKANDSWEEP_RUNNING ( heap)    DUK__HEAP_CLEAR_FLAGS((heap), DUK_HEAP_FLAG_MARKANDSWEEP_RUNNING)

Definition at line 46 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_HEAP_CLEAR_REFZERO_FREE_RUNNING

#define DUK_HEAP_CLEAR_REFZERO_FREE_RUNNING ( heap)    DUK__HEAP_CLEAR_FLAGS((heap), DUK_HEAP_FLAG_REFZERO_FREE_RUNNING)

Definition at line 48 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_HEAP_DBG_RATELIMIT_MILLISECS

#define DUK_HEAP_DBG_RATELIMIT_MILLISECS   200

Definition at line 249 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_HEAP_DBG_RATELIMIT_OPCODES

#define DUK_HEAP_DBG_RATELIMIT_OPCODES   4000

Definition at line 246 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_HEAP_FLAG_ERRHANDLER_RUNNING

#define DUK_HEAP_FLAG_ERRHANDLER_RUNNING   (1 << 3) /* an error handler (user callback to augment/replace error) is running */

Definition at line 20 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_HEAP_FLAG_FINALIZER_NORESCUE

#define DUK_HEAP_FLAG_FINALIZER_NORESCUE   (1 << 5) /* heap destruction ongoing, finalizer rescue no longer possible */

Definition at line 22 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_HEAP_FLAG_INTERRUPT_RUNNING

#define DUK_HEAP_FLAG_INTERRUPT_RUNNING   (1 << 4) /* executor interrupt running (used to avoid nested interrupts) */

Definition at line 21 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_HEAP_FLAG_MARKANDSWEEP_RECLIMIT_REACHED

#define DUK_HEAP_FLAG_MARKANDSWEEP_RECLIMIT_REACHED   (1 << 1) /* mark-and-sweep marking reached a recursion limit and must use multi-pass marking */

Definition at line 18 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_HEAP_FLAG_MARKANDSWEEP_RUNNING

#define DUK_HEAP_FLAG_MARKANDSWEEP_RUNNING   (1 << 0) /* mark-and-sweep is currently running */

Definition at line 17 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_HEAP_FLAG_REFZERO_FREE_RUNNING

#define DUK_HEAP_FLAG_REFZERO_FREE_RUNNING   (1 << 2) /* refcount code is processing refzero list */

Definition at line 19 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_HEAP_GET_STRING

#define DUK_HEAP_GET_STRING ( heap,
idx )    ((heap)->strs[(idx)])

Definition at line 169 of file duktape-1.8.0/src-separate/duk_heap.h.

169#define DUK_HEAP_GET_STRING(heap,idx) \
170 ((heap)->strs[(idx)])

◆ DUK_HEAP_HAS_ERRHANDLER_RUNNING

#define DUK_HEAP_HAS_ERRHANDLER_RUNNING ( heap)    DUK__HEAP_HAS_FLAGS((heap), DUK_HEAP_FLAG_ERRHANDLER_RUNNING)

Definition at line 35 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_HEAP_HAS_FINALIZER_NORESCUE

#define DUK_HEAP_HAS_FINALIZER_NORESCUE ( heap)    DUK__HEAP_HAS_FLAGS((heap), DUK_HEAP_FLAG_FINALIZER_NORESCUE)

Definition at line 37 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_HEAP_HAS_INTERRUPT_RUNNING

#define DUK_HEAP_HAS_INTERRUPT_RUNNING ( heap)    DUK__HEAP_HAS_FLAGS((heap), DUK_HEAP_FLAG_INTERRUPT_RUNNING)

Definition at line 36 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_HEAP_HAS_MARKANDSWEEP_RECLIMIT_REACHED

#define DUK_HEAP_HAS_MARKANDSWEEP_RECLIMIT_REACHED ( heap)    DUK__HEAP_HAS_FLAGS((heap), DUK_HEAP_FLAG_MARKANDSWEEP_RECLIMIT_REACHED)

Definition at line 33 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_HEAP_HAS_MARKANDSWEEP_RUNNING

#define DUK_HEAP_HAS_MARKANDSWEEP_RUNNING ( heap)    DUK__HEAP_HAS_FLAGS((heap), DUK_HEAP_FLAG_MARKANDSWEEP_RUNNING)

Definition at line 32 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_HEAP_HAS_REFZERO_FREE_RUNNING

#define DUK_HEAP_HAS_REFZERO_FREE_RUNNING ( heap)    DUK__HEAP_HAS_FLAGS((heap), DUK_HEAP_FLAG_REFZERO_FREE_RUNNING)

Definition at line 34 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_HEAP_INSERT_INTO_HEAP_ALLOCATED

#define DUK_HEAP_INSERT_INTO_HEAP_ALLOCATED ( heap,
hdr )   duk_heap_insert_into_heap_allocated((heap),(hdr))

Definition at line 129 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_HEAP_MAX_BREAKPOINTS

#define DUK_HEAP_MAX_BREAKPOINTS   16

Definition at line 239 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_HEAP_SET_ERRHANDLER_RUNNING

#define DUK_HEAP_SET_ERRHANDLER_RUNNING ( heap)    DUK__HEAP_SET_FLAGS((heap), DUK_HEAP_FLAG_ERRHANDLER_RUNNING)

Definition at line 42 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_HEAP_SET_FINALIZER_NORESCUE

#define DUK_HEAP_SET_FINALIZER_NORESCUE ( heap)    DUK__HEAP_SET_FLAGS((heap), DUK_HEAP_FLAG_FINALIZER_NORESCUE)

Definition at line 44 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_HEAP_SET_INTERRUPT_RUNNING

#define DUK_HEAP_SET_INTERRUPT_RUNNING ( heap)    DUK__HEAP_SET_FLAGS((heap), DUK_HEAP_FLAG_INTERRUPT_RUNNING)

Definition at line 43 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_HEAP_SET_MARKANDSWEEP_RECLIMIT_REACHED

#define DUK_HEAP_SET_MARKANDSWEEP_RECLIMIT_REACHED ( heap)    DUK__HEAP_SET_FLAGS((heap), DUK_HEAP_FLAG_MARKANDSWEEP_RECLIMIT_REACHED)

Definition at line 40 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_HEAP_SET_MARKANDSWEEP_RUNNING

#define DUK_HEAP_SET_MARKANDSWEEP_RUNNING ( heap)    DUK__HEAP_SET_FLAGS((heap), DUK_HEAP_FLAG_MARKANDSWEEP_RUNNING)

Definition at line 39 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_HEAP_SET_REFZERO_FREE_RUNNING

#define DUK_HEAP_SET_REFZERO_FREE_RUNNING ( heap)    DUK__HEAP_SET_FLAGS((heap), DUK_HEAP_FLAG_REFZERO_FREE_RUNNING)

Definition at line 41 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_HEAP_STRCACHE_SIZE

#define DUK_HEAP_STRCACHE_SIZE   4

Definition at line 125 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_HEAP_STRINGCACHE_NOCACHE_LIMIT

#define DUK_HEAP_STRINGCACHE_NOCACHE_LIMIT   16 /* strings up to the this length are not cached */

Definition at line 126 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_HEAP_SWITCH_THREAD

#define DUK_HEAP_SWITCH_THREAD ( heap,
newthr )
Value:
do { \
(heap)->curr_thread = (newthr); \
} while (0)

Definition at line 91 of file duktape-1.8.0/src-separate/duk_heap.h.

91#define DUK_HEAP_SWITCH_THREAD(heap,newthr) do { \
92 (heap)->curr_thread = (newthr); \
93 } while (0)

◆ DUK_LJ_TYPE_BREAK

#define DUK_LJ_TYPE_BREAK   4 /* value1 -> label number, pseudo-type to indicate a break continuation (for ENDFIN) */

Definition at line 61 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_LJ_TYPE_CONTINUE

#define DUK_LJ_TYPE_CONTINUE   5 /* value1 -> label number, pseudo-type to indicate a continue continuation (for ENDFIN) */

Definition at line 62 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_LJ_TYPE_NORMAL

#define DUK_LJ_TYPE_NORMAL   7 /* no value, pseudo-type to indicate a normal continuation (for ENDFIN) */

Definition at line 64 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_LJ_TYPE_RESUME

#define DUK_LJ_TYPE_RESUME   3 /* value1 -> resume value, value2 -> resumee thread, iserror -> error/normal */

Definition at line 60 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_LJ_TYPE_RETURN

#define DUK_LJ_TYPE_RETURN   6 /* value1 -> return value, pseudo-type to indicate a return continuation (for ENDFIN) */

Definition at line 63 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_LJ_TYPE_THROW

#define DUK_LJ_TYPE_THROW   1 /* value1 -> error object */

Definition at line 58 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_LJ_TYPE_UNKNOWN

#define DUK_LJ_TYPE_UNKNOWN   0 /* unused */

Definition at line 57 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_LJ_TYPE_YIELD

#define DUK_LJ_TYPE_YIELD   2 /* value1 -> yield value, iserror -> error / normal */

Definition at line 59 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_MS_FLAG_EMERGENCY

#define DUK_MS_FLAG_EMERGENCY   (1 << 0) /* emergency mode: try extra hard */

Definition at line 74 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_MS_FLAG_NO_FINALIZERS

#define DUK_MS_FLAG_NO_FINALIZERS   (1 << 3) /* don't run finalizers; leave finalizable objects in finalize_list for next round */

Definition at line 77 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_MS_FLAG_NO_OBJECT_COMPACTION

#define DUK_MS_FLAG_NO_OBJECT_COMPACTION   (1 << 2) /* don't compact objects; needed during object property allocation resize */

Definition at line 76 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_MS_FLAG_NO_STRINGTABLE_RESIZE

#define DUK_MS_FLAG_NO_STRINGTABLE_RESIZE   (1 << 1) /* don't resize stringtable (but may sweep it); needed during stringtable resize */

Definition at line 75 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_MS_FLAG_SKIP_FINALIZERS

#define DUK_MS_FLAG_SKIP_FINALIZERS   (1 << 4) /* don't run finalizers; queue finalizable objects back to heap_allocated */

Definition at line 78 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_REALLOC

#define DUK_REALLOC ( heap,
ptr,
newsize )   duk_heap_mem_realloc((heap), (ptr), (newsize))

Definition at line 220 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_REALLOC_INDIRECT

#define DUK_REALLOC_INDIRECT ( heap,
cb,
ud,
newsize )   duk_heap_mem_realloc_indirect((heap), (cb), (ud), (newsize))

Definition at line 221 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_REALLOC_RAW

#define DUK_REALLOC_RAW ( heap,
ptr,
newsize )    ((heap)->realloc_func((heap)->heap_udata, (void *) (ptr), (newsize)))

Definition at line 181 of file duktape-1.8.0/src-separate/duk_heap.h.

181#define DUK_REALLOC_RAW(heap,ptr,newsize) \
182 ((heap)->realloc_func((heap)->heap_udata, (void *) (ptr), (newsize)))

◆ DUK_STEP_TYPE_INTO

#define DUK_STEP_TYPE_INTO   1

Definition at line 253 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_STEP_TYPE_NONE

#define DUK_STEP_TYPE_NONE   0

Definition at line 252 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_STEP_TYPE_OUT

#define DUK_STEP_TYPE_OUT   3

Definition at line 255 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_STEP_TYPE_OVER

#define DUK_STEP_TYPE_OVER   2

Definition at line 254 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_STRTAB_CHAIN_SIZE

#define DUK_STRTAB_CHAIN_SIZE   DUK_USE_STRTAB_CHAIN_SIZE

Definition at line 154 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_STRTAB_DELETED_MARKER

#define DUK_STRTAB_DELETED_MARKER ( heap)    ((duk_hstring *) heap)

Definition at line 139 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_STRTAB_GROW_ST_SIZE

#define DUK_STRTAB_GROW_ST_SIZE ( n)    ((n) + (n)) /* used entries + approx 100% -> reset load to 50% */

Definition at line 144 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_STRTAB_HASH_INITIAL

#define DUK_STRTAB_HASH_INITIAL ( hash,
h_size )   ((hash) % (h_size))

Definition at line 150 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_STRTAB_HASH_PROBE_STEP

#define DUK_STRTAB_HASH_PROBE_STEP ( hash)    DUK_UTIL_GET_HASH_PROBE_STEP((hash))

Definition at line 151 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_STRTAB_HIGHEST_32BIT_PRIME

#define DUK_STRTAB_HIGHEST_32BIT_PRIME   0xfffffffbUL

Definition at line 147 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_STRTAB_INITIAL_SIZE

#define DUK_STRTAB_INITIAL_SIZE   17

Definition at line 136 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_STRTAB_MIN_FREE_DIVISOR

#define DUK_STRTAB_MIN_FREE_DIVISOR   4 /* load factor max 75% */

Definition at line 142 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_STRTAB_MIN_USED_DIVISOR

#define DUK_STRTAB_MIN_USED_DIVISOR   4 /* load factor min 25% */

Definition at line 143 of file duktape-1.8.0/src-separate/duk_heap.h.

◆ DUK_STRTAB_U32_MAX_STRLEN

#define DUK_STRTAB_U32_MAX_STRLEN   10 /* 4'294'967'295 */

Definition at line 146 of file duktape-1.8.0/src-separate/duk_heap.h.

Typedef Documentation

◆ duk_mem_getptr

typedef void *(* duk_mem_getptr) (duk_heap *heap, void *ud)

Definition at line 216 of file duktape-1.8.0/src-separate/duk_heap.h.

Function Documentation

◆ duk_free_hbuffer_inner()

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

Definition at line 40759 of file duktape-1.5.2/src-noline/duktape.c.

40759 {
40760 duk_hthread *t = (duk_hthread *) h;
40761 DUK_FREE(heap, t->valstack);
40762 DUK_FREE(heap, t->callstack);
40763 DUK_FREE(heap, t->catchstack);
40764 /* Don't free h->resumer because it exists in the heap.
40765 * Callstack entries also contain function pointers which
40766 * are not freed for the same reason.
40767 */
40768
#define DUK_FREE(heap, ptr)

References DUK_ASSERT, DUK_DDD, DUK_DDDPRINT, DUK_FREE, DUK_HBUFFER_DYNAMIC_GET_DATA_PTR, DUK_HBUFFER_HAS_DYNAMIC, DUK_HBUFFER_HAS_EXTERNAL, and NULL.

Referenced by duk_heap_free_heaphdr_raw().

◆ duk_free_hobject_inner()

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

Definition at line 40728 of file duktape-1.5.2/src-noline/duktape.c.

40745 {
40746 DUK_ASSERT(heap != NULL);
40747 DUK_ASSERT(h != NULL);
40748
40749 DUK_FREE(heap, DUK_HOBJECT_GET_PROPS(heap, h));
40750
40753 DUK_UNREF(f);
40754 /* Currently nothing to free; 'data' is a heap object */
40755 } else if (DUK_HOBJECT_IS_NATIVEFUNCTION(h)) {
40757 DUK_UNREF(f);
#define DUK_HOBJECT_GET_PROPS(heap, h)
#define DUK_HOBJECT_IS_COMPILEDFUNCTION(h)
#define DUK_HOBJECT_IS_NATIVEFUNCTION(h)
#define NULL
Definition gmacros.h:924

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_DECL void duk_free_hstring_inner ( duk_heap * heap,
duk_hstring * h )

◆ duk_heap_alloc()

DUK_INTERNAL_DECL 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 41430 of file duktape-1.5.2/src-noline/duktape.c.

41430 : big"));
41431#else
41432 DUK_D(DUK_DPRINT("integer endianness: ???"));
41433#endif
41434#if defined(DUK_USE_DOUBLE_LE)
41435 DUK_D(DUK_DPRINT("IEEE double endianness: little"));
41436#elif defined(DUK_USE_DOUBLE_ME)
41437 DUK_D(DUK_DPRINT("IEEE double endianness: mixed"));
41438#elif defined(DUK_USE_DOUBLE_BE)
41439 DUK_D(DUK_DPRINT("IEEE double endianness: big"));
41440#else
41441 DUK_D(DUK_DPRINT("IEEE double endianness: ???"));
41442#endif
41443}
41444#endif /* DUK_USE_DEBUG */
41445
41446DUK_INTERNAL
41447duk_heap *duk_heap_alloc(duk_alloc_function alloc_func,
41448 duk_realloc_function realloc_func,
41449 duk_free_function free_func,
41450 void *heap_udata,
41451 duk_fatal_function fatal_func) {
41452 duk_heap *res = NULL;
41453
41454 /* Silence a few global unused warnings here. */
41455 DUK_UNREF(duk_str_unsupported);
41456
41457 DUK_D(DUK_DPRINT("allocate heap"));
41458
41459 /*
41460 * Debug dump type sizes
41461 */
41462
41463#if defined(DUK_USE_DEBUG)
41464 duk__dump_misc_options();
41465 duk__dump_type_sizes();
41466 duk__dump_type_limits();
41467#endif
41468
41469 /*
41470 * If selftests enabled, run them as early as possible
41471 */
41472#if defined(DUK_USE_SELF_TESTS)
41473 DUK_D(DUK_DPRINT("running self tests"));
41474 duk_selftest_run_tests();
41475 DUK_D(DUK_DPRINT("self tests passed"));
41476#endif
41477
41478 /*
41479 * Computed values (e.g. INFINITY)
41480 */
41481
41482#if defined(DUK_USE_COMPUTED_NAN)
41483 do {
41484 /* Workaround for some exotic platforms where NAN is missing
41485 * and the expression (0.0 / 0.0) does NOT result in a NaN.
41486 * Such platforms use the global 'duk_computed_nan' which must
41487 * be initialized at runtime. Use 'volatile' to ensure that
41488 * the compiler will actually do the computation and not try
41489 * to do constant folding which might result in the original
41490 * problem.
41491 */
41492 volatile double dbl1 = 0.0;
41493 volatile double dbl2 = 0.0;
41494 duk_computed_nan = dbl1 / dbl2;
41495 } while (0);
41496#endif
41497
41498#if defined(DUK_USE_COMPUTED_INFINITY)
41499 do {
41500 /* Similar workaround for INFINITY. */
41501 volatile double dbl1 = 1.0;
41502 volatile double dbl2 = 0.0;
41503 duk_computed_infinity = dbl1 / dbl2;
41504 } while (0);
41505#endif
41506
41507 /*
41508 * Allocate heap struct
41509 *
41510 * Use a raw call, all macros expect the heap to be initialized
41511 */
41512
41513 res = (duk_heap *) alloc_func(heap_udata, sizeof(duk_heap));
41514 if (!res) {
41515 goto error;
41516 }
41517
41518 /*
41519 * Zero the struct, and start initializing roughly in order
41520 */
41521
41522 DUK_MEMZERO(res, sizeof(*res));
41523
41524 /* explicit NULL inits */
41525#if defined(DUK_USE_EXPLICIT_NULL_INIT)
41526 res->heap_udata = NULL;
41527 res->heap_allocated = NULL;
41528#if defined(DUK_USE_REFERENCE_COUNTING)
41529 res->refzero_list = NULL;
41530 res->refzero_list_tail = NULL;
41531#endif
41532#if defined(DUK_USE_MARK_AND_SWEEP)
41533 res->finalize_list = NULL;
41534#endif
41535 res->heap_thread = NULL;
41536 res->curr_thread = NULL;
41537 res->heap_object = NULL;
41538#if defined(DUK_USE_STRTAB_CHAIN)
41539 /* nothing to NULL */
41540#elif defined(DUK_USE_STRTAB_PROBE)
41541#if defined(DUK_USE_HEAPPTR16)
41542 res->strtable16 = (duk_uint16_t *) NULL;
41543#else
41544 res->strtable = (duk_hstring **) NULL;
41545#endif
41546#endif
41547#if defined(DUK_USE_ROM_STRINGS)
41548 /* no res->strs[] */
41549#else /* DUK_USE_ROM_STRINGS */
41550#if defined(DUK_USE_HEAPPTR16)
41551 /* res->strs16[] is zeroed and zero decodes to NULL, so no NULL inits. */
41552#else
41553 {
41554 duk_small_uint_t i;
41555 for (i = 0; i < DUK_HEAP_NUM_STRINGS; i++) {
41556 res->strs[i] = NULL;
41557 }
41558 }
41559#endif
41560#endif /* DUK_USE_ROM_STRINGS */
41561#if defined(DUK_USE_DEBUGGER_SUPPORT)
41562 res->dbg_read_cb = NULL;
41563 res->dbg_write_cb = NULL;
41564 res->dbg_peek_cb = NULL;
41565 res->dbg_read_flush_cb = NULL;
41566 res->dbg_write_flush_cb = NULL;
41567 res->dbg_request_cb = NULL;
41568 res->dbg_udata = NULL;
41569 res->dbg_step_thread = NULL;
41570#endif
41571#endif /* DUK_USE_EXPLICIT_NULL_INIT */
41572
41573 res->alloc_func = alloc_func;
41574 res->realloc_func = realloc_func;
41575 res->free_func = free_func;
41576 res->heap_udata = heap_udata;
41577 res->fatal_func = fatal_func;
41578
41579#if defined(DUK_USE_HEAPPTR16)
41580 /* XXX: zero assumption */
41581 res->heapptr_null16 = DUK_USE_HEAPPTR_ENC16(res->heap_udata, (void *) NULL);
41582 res->heapptr_deleted16 = DUK_USE_HEAPPTR_ENC16(res->heap_udata, (void *) DUK_STRTAB_DELETED_MARKER(res));
41583#endif
41584
41585 /* res->mark_and_sweep_trigger_counter == 0 -> now causes immediate GC; which is OK */
41586
41587 res->call_recursion_depth = 0;
41588 res->call_recursion_limit = DUK_USE_NATIVE_CALL_RECLIMIT;
41589
41590 /* XXX: use the pointer as a seed for now: mix in time at least */
41591
41592 /* The casts through duk_intr_pt is to avoid the following GCC warning:
41593 *
41594 * warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
41595 *
41596 * This still generates a /Wp64 warning on VS2010 when compiling for x86.
41597 */
41598#if defined(DUK_USE_ROM_STRINGS)
41599 /* XXX: make a common DUK_USE_ option, and allow custom fixed seed? */
41600 DUK_D(DUK_DPRINT("using rom strings, force heap hash_seed to fixed value 0x%08lx", (long) DUK__FIXED_HASH_SEED));
41601 res->hash_seed = (duk_uint32_t) DUK__FIXED_HASH_SEED;
41602#else /* DUK_USE_ROM_STRINGS */
41603 res->hash_seed = (duk_uint32_t) (duk_intptr_t) res;
41604 res->rnd_state = (duk_uint32_t) (duk_intptr_t) res;
41605#if !defined(DUK_USE_STRHASH_DENSE)
41606 res->hash_seed ^= 5381; /* Bernstein hash init value is normally 5381; XOR it in in case pointer low bits are 0 */
41607#endif
41608#endif /* DUK_USE_ROM_STRINGS */
41609
41610#if defined(DUK_USE_EXPLICIT_NULL_INIT)
41611 res->lj.jmpbuf_ptr = NULL;
41612#endif
41613 DUK_ASSERT(res->lj.type == DUK_LJ_TYPE_UNKNOWN); /* zero */
41614
41615 DUK_TVAL_SET_UNDEFINED(&res->lj.value1);
41616 DUK_TVAL_SET_UNDEFINED(&res->lj.value2);
41617
41618#if (DUK_STRTAB_INITIAL_SIZE < DUK_UTIL_MIN_HASH_PRIME)
41619#error initial heap stringtable size is defined incorrectly
41620#endif
41621
41622 /*
41623 * Init stringtable: fixed variant
41624 */
41625
41626#if defined(DUK_USE_STRTAB_CHAIN)
41627 DUK_MEMZERO(res->strtable, sizeof(duk_strtab_entry) * DUK_STRTAB_CHAIN_SIZE);
41628#if defined(DUK_USE_EXPLICIT_NULL_INIT)
41629 {
41630 duk_small_uint_t i;
41631 for (i = 0; i < DUK_STRTAB_CHAIN_SIZE; i++) {
41632#if defined(DUK_USE_HEAPPTR16)
41633 res->strtable[i].u.str16 = res->heapptr_null16;
41634#else
41635 res->strtable[i].u.str = NULL;
41636#endif
41637 }
41638 }
41639#endif /* DUK_USE_EXPLICIT_NULL_INIT */
41640#endif /* DUK_USE_STRTAB_CHAIN */
41641
41642 /*
41643 * Init stringtable: probe variant
41644 */
41645
41646#if defined(DUK_USE_STRTAB_PROBE)
41647#if defined(DUK_USE_HEAPPTR16)
41648 res->strtable16 = (duk_uint16_t *) alloc_func(heap_udata, sizeof(duk_uint16_t) * DUK_STRTAB_INITIAL_SIZE);
41649 if (!res->strtable16) {
41650 goto error;
41651 }
41652#else /* DUK_USE_HEAPPTR16 */
41653 res->strtable = (duk_hstring **) alloc_func(heap_udata, sizeof(duk_hstring *) * DUK_STRTAB_INITIAL_SIZE);
41654 if (!res->strtable) {
41655 goto error;
41656 }
41657#endif /* DUK_USE_HEAPPTR16 */
41658 res->st_size = DUK_STRTAB_INITIAL_SIZE;
41659#if defined(DUK_USE_EXPLICIT_NULL_INIT)
41660 {
41661 duk_small_uint_t i;
41662 DUK_ASSERT(res->st_size == DUK_STRTAB_INITIAL_SIZE);
41663 for (i = 0; i < DUK_STRTAB_INITIAL_SIZE; i++) {
41664#if defined(DUK_USE_HEAPPTR16)
41665 res->strtable16[i] = res->heapptr_null16;
41666#else
41667 res->strtable[i] = NULL;
41668#endif
41669 }
41670 }
41671#else /* DUK_USE_EXPLICIT_NULL_INIT */
41672#if defined(DUK_USE_HEAPPTR16)
41673 DUK_MEMZERO(res->strtable16, sizeof(duk_uint16_t) * DUK_STRTAB_INITIAL_SIZE);
41674#else
41675 DUK_MEMZERO(res->strtable, sizeof(duk_hstring *) * DUK_STRTAB_INITIAL_SIZE);
41676#endif
41677#endif /* DUK_USE_EXPLICIT_NULL_INIT */
41678#endif /* DUK_USE_STRTAB_PROBE */
41679
41680 /*
41681 * Init stringcache
41682 */
41683
41684#if defined(DUK_USE_EXPLICIT_NULL_INIT)
41685 {
41686 duk_small_uint_t i;
41687 for (i = 0; i < DUK_HEAP_STRCACHE_SIZE; i++) {
41688 res->strcache[i].h = NULL;
41689 }
41690 }
41691#endif
41692
41693 /* XXX: error handling is incomplete. It would be cleanest if
41694 * there was a setjmp catchpoint, so that all init code could
41695 * freely throw errors. If that were the case, the return code
41696 * passing here could be removed.
41697 */
41698
41699 /*
41700 * Init built-in strings
41701 */
41702
41703 DUK_DD(DUK_DDPRINT("HEAP: INIT STRINGS"));
41704 if (!duk__init_heap_strings(res)) {
41705 goto error;
41706 }
41707
41708 /*
41709 * Init the heap thread
41710 */
41711
41712 DUK_DD(DUK_DDPRINT("HEAP: INIT HEAP THREAD"));
41713 if (!duk__init_heap_thread(res)) {
41714 goto error;
41715 }
41716
41717 /*
41718 * Init the heap object
41719 */
41720
41721 DUK_DD(DUK_DDPRINT("HEAP: INIT HEAP OBJECT"));
41722 DUK_ASSERT(res->heap_thread != NULL);
41723 res->heap_object = duk_hobject_alloc(res, DUK_HOBJECT_FLAG_EXTENSIBLE |
41724 DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_OBJECT));
41725 if (!res->heap_object) {
41726 goto error;
41727 }
41728 DUK_HOBJECT_INCREF(res->heap_thread, res->heap_object);
41729
41730 /*
41731 * All done
41732 */
41733
int value
Definition lsqlite3.c:2155

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.

Referenced by duk_create_heap().

◆ duk_heap_free()

DUK_INTERNAL_DECL void duk_heap_free ( duk_heap * heap)

Definition at line 40971 of file duktape-1.5.2/src-noline/duktape.c.

40974 {
40975 DUK_D(DUK_DPRINT("no more finalizable objects, forced finalization finished"));
40976 break;
40977 }
40978 if (count_finalized >= curr_limit) {
40979 DUK_D(DUK_DPRINT("finalizer count above limit, potentially runaway finalizer; skip remaining finalizers"));
40980 break;
40981 }
40982 }
40983
40986}
40987
40989 DUK_D(DUK_DPRINT("free heap: %p", (void *) heap));
40990
40991#if defined(DUK_USE_DEBUG)
40992 duk_heap_dump_strtab(heap);
40993#endif
40994
40995#if defined(DUK_USE_DEBUGGER_SUPPORT)
40996 /* Detach a debugger if attached (can be called multiple times)
40997 * safely.
40998 */
40999 /* XXX: Add a flag to reject an attempt to re-attach? Otherwise
41000 * the detached callback may immediately reattach.
41001 */
41002 duk_debug_do_detach(heap);
41003#endif
41004
41005 /* Execute finalizers before freeing the heap, even for reachable
41006 * objects, and regardless of whether or not mark-and-sweep is
41007 * enabled. This gives finalizers the chance to free any native
41008 * resources like file handles, allocations made outside Duktape,
41009 * etc. This is quite tricky to get right, so that all finalizer
41010 * guarantees are honored.
41011 *
41012 * XXX: this perhaps requires an execution time limit.
41013 */
41014 DUK_D(DUK_DPRINT("execute finalizers before freeing heap"));
41015#if defined(DUK_USE_MARK_AND_SWEEP)
41016 /* Run mark-and-sweep a few times just in case (unreachable object
41017 * finalizers run already here). The last round must rescue objects
41018 * from the previous round without running any more finalizers. This
41019 * ensures rescued objects get their FINALIZED flag cleared so that
41020 * their finalizer is called once more in forced finalization to
41021 * satisfy finalizer guarantees. However, we don't want to run any
41022 * more finalizer because that'd required one more loop, and so on.
41023 */
41024 DUK_D(DUK_DPRINT("forced gc #1 in heap destruction"));
41025 duk_heap_mark_and_sweep(heap, 0);
41026 DUK_D(DUK_DPRINT("forced gc #2 in heap destruction"));
41027 duk_heap_mark_and_sweep(heap, 0);
41028 DUK_D(DUK_DPRINT("forced gc #3 in heap destruction (don't run finalizers)"));
41029 duk_heap_mark_and_sweep(heap, DUK_MS_FLAG_SKIP_FINALIZERS); /* skip finalizers; queue finalizable objects to heap_allocated */
41030#endif
41031
41032 DUK_HEAP_SET_FINALIZER_NORESCUE(heap); /* rescue no longer supported */
41034
41035 /* Note: heap->heap_thread, heap->curr_thread, and heap->heap_object
41036 * are on the heap allocated list.
41037 */
41038
41039 DUK_D(DUK_DPRINT("freeing heap objects of heap: %p", (void *) heap));
41040 duk__free_allocated(heap);
DUK_INTERNAL_DECL void duk_heap_free(duk_heap *heap)
DUK_LOCAL void duk__free_run_finalizers(duk_heap *heap)
#define DUK_HEAP_SET_FINALIZER_NORESCUE(heap)
#define DUK_HEAP_CLEAR_MARKANDSWEEP_RUNNING(heap)
DUK_INTERNAL_DECL duk_bool_t duk_heap_mark_and_sweep(duk_heap *heap, duk_small_uint_t flags)
#define DUK_HEAP_HAS_MARKANDSWEEP_RUNNING(heap)
DUK_LOCAL void duk__free_allocated(duk_heap *heap)
#define DUK_MS_FLAG_SKIP_FINALIZERS

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_destroy_heap(), and duk_heap_alloc().

◆ duk_heap_free_heaphdr_raw()

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

Definition at line 40786 of file duktape-1.5.2/src-noline/duktape.c.

40787 {
40788 DUK_ASSERT(heap != NULL);
40789 DUK_ASSERT(h != NULL);
40790
40791 DUK_UNREF(heap);
40792 DUK_UNREF(h);
40793
40794#if defined(DUK_USE_HSTRING_EXTDATA) && defined(DUK_USE_EXTSTR_FREE)
40795 if (DUK_HSTRING_HAS_EXTDATA(h)) {
40796 DUK_DDD(DUK_DDDPRINT("free extstr: hstring %!O, extdata: %p",
40797 h, DUK_HSTRING_GET_EXTDATA((duk_hstring_external *) h)));
40798 DUK_USE_EXTSTR_FREE(heap->heap_udata, (const void *) DUK_HSTRING_GET_EXTDATA((duk_hstring_external *) h));
40799 }
40800#endif
40801}
40802
40804 DUK_ASSERT(heap);
40805 DUK_ASSERT(hdr);
40806
40807 DUK_DDD(DUK_DDDPRINT("free heaphdr %p, htype %ld", (void *) hdr, (long) DUK_HEAPHDR_GET_TYPE(hdr)));
#define DUK_HEAPHDR_GET_TYPE(h)
#define DUK_HSTRING_HAS_EXTDATA(x)
DUK_INTERNAL_DECL void duk_heap_free_heaphdr_raw(duk_heap *heap, duk_heaphdr *hdr)

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_HSTRING_HAS_EXTDATA, DUK_HTYPE_BUFFER, DUK_HTYPE_OBJECT, DUK_HTYPE_STRING, DUK_UNREACHABLE, DUK_UNREF, duk_heap::heap_udata, and NULL.

Referenced by duk__free_allocated(), duk__free_markandsweep_finalize_list(), duk__free_refzero_list(), duk__free_run_finalizers(), duk__refzero_free_pending(), duk__sweep_heap(), and duk_heaphdr_refzero().

◆ duk_heap_free_strtab()

DUK_INTERNAL void duk_heap_free_strtab ( duk_heap * heap)

Definition at line 45796 of file duktape-1.5.2/src-noline/duktape.c.

45802 {
45803 duk_free_hstring_inner(heap, h);
45804 DUK_FREE(heap, h);
45805 }
45806 }
45807 e->listlen = 0;
45808 }
45809}
45810#endif /* DUK_USE_STRTAB_CHAIN */
45811
45812#if defined(DUK_USE_STRTAB_PROBE)
45815 duk_hstring *h;
45816
45817#if defined(DUK_USE_HEAPPTR16)
45818 if (heap->strtable16) {
45819#else
45820 if (heap->strtable) {
45821#endif
45822 for (i = 0; i < (duk_uint_fast32_t) heap->st_size; i++) {
45823#if defined(DUK_USE_HEAPPTR16)
45824 h = (duk_hstring *) DUK_USE_HEAPPTR_DEC16(heap->heap_udata, heap->strtable16[i]);
45825#else
45826 h = heap->strtable[i];
45827#endif
45828 if (h == NULL || h == DUK_STRTAB_DELETED_MARKER(heap)) {
45829 continue;
45830 }
45831 DUK_ASSERT(h != NULL);
45832
duk_uint32_t duk_uint_fast32_t
#define DUK_STRTAB_DELETED_MARKER(heap)
DUK_INTERNAL_DECL void duk_free_hstring_inner(duk_heap *heap, duk_hstring *h)
DUK_INTERNAL void duk_heap_free_strtab(duk_heap *heap)

References DUK_ASSERT, DUK_FREE, duk_free_hstring_inner(), DUK_STRTAB_DELETED_MARKER, duk_heap::heap_udata, NULL, duk_heap::st_size, and duk_heap::strtable.

Referenced by duk__free_stringtable().

◆ duk_heap_hashstring()

DUK_INTERNAL_DECL duk_uint32_t duk_heap_hashstring ( duk_heap * heap,
const duk_uint8_t * str,
duk_size_t len )

Definition at line 41829 of file duktape-1.5.2/src-noline/duktape.c.

41846 {
41847 duk_uint32_t hash;
41848 duk_size_t step;
41849 duk_size_t off;
41850
41851 /* Slightly modified "Bernstein hash" from:
41852 *
41853 * http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx
41854 *
41855 * Modifications: string skipping and reverse direction similar to
41856 * Lua 5.1.5, and different hash initializer.
41857 *
41858 * The reverse direction ensures last byte it always included in the
41859 * hash which is a good default as changing parts of the string are
41860 * more often in the suffix than in the prefix.

References DUK_ASSERT, DUK_USE_STRHASH_SKIP_SHIFT, and duk_heap::hash_seed.

Referenced by duk__do_lookup().

◆ duk_heap_insert_into_heap_allocated()

DUK_INTERNAL_DECL void duk_heap_insert_into_heap_allocated ( duk_heap * heap,
duk_heaphdr * hdr )

Definition at line 43698 of file duktape-1.5.2/src-noline/duktape.c.

43699 {
43700 heap->heap_allocated = DUK_HEAPHDR_GET_NEXT(heap, hdr);
43701 }
43702 if (DUK_HEAPHDR_GET_NEXT(heap, hdr)) {
43704 } else {
43705 ;
43706 }
43707
43708 /* The prev/next pointers of the removed duk_heaphdr are left as garbage.
43709 * It's up to the caller to ensure they're written before inserting the
43710 * object back.
#define DUK_HEAPHDR_GET_PREV(heap, h)
#define DUK_HEAPHDR_SET_PREV(heap, h, val)
#define DUK_HEAPHDR_GET_NEXT(heap, h)

References DUK_ASSERT, DUK_HEAPHDR_GET_PREV, DUK_HEAPHDR_GET_TYPE, DUK_HEAPHDR_SET_NEXT, DUK_HEAPHDR_SET_PREV, DUK_HTYPE_STRING, duk_heap::heap_allocated, and NULL.

◆ duk_heap_mem_alloc()

DUK_INTERNAL_DECL void * duk_heap_mem_alloc ( duk_heap * heap,
duk_size_t size )

Definition at line 43327 of file duktape-1.5.2/src-noline/duktape.c.

43344 {
43345 void *res;
43346 duk_bool_t rc;
43348
43349 DUK_ASSERT(heap != NULL);
43350 DUK_ASSERT_DISABLE(size >= 0);
43351
43352 /*
43353 * Voluntary periodic GC (if enabled)
43354 */
43355
43357
43358 /*
43359 * First attempt
43360 */
43361
43362#ifdef DUK_USE_GC_TORTURE
43363 /* simulate alloc failure on every alloc (except when mark-and-sweep is running) */
43365 DUK_DDD(DUK_DDDPRINT("gc torture enabled, pretend that first alloc attempt fails"));
43366 res = NULL;
43367 DUK_UNREF(res);
43368 goto skip_attempt;
43369 }
43370#endif
43371 res = heap->alloc_func(heap->heap_udata, size);
43372 if (res || size == 0) {
43373 /* for zero size allocations NULL is allowed */
43374 return res;
43375 }
43376#ifdef DUK_USE_GC_TORTURE
43377 skip_attempt:
43378#endif
43379
43380 DUK_D(DUK_DPRINT("first alloc attempt failed, attempt to gc and retry"));
43381
43382 /*
43383 * Avoid a GC if GC is already running. This can happen at a late
43384 * stage in a GC when we try to e.g. resize the stringtable
43385 * or compact objects.
43386 */
43387
43389 DUK_D(DUK_DPRINT("duk_heap_mem_alloc() failed, gc in progress (gc skipped), alloc size %ld", (long) size));
43390 return NULL;
43391 }
43392
43393 /*
43394 * Retry with several GC attempts. Initial attempts are made without
43395 * emergency mode; later attempts use emergency mode which minimizes
43396 * memory allocations forcibly.
43397 */
43398
43399 for (i = 0; i < DUK_HEAP_ALLOC_FAIL_MARKANDSWEEP_LIMIT; i++) {
43400 duk_small_uint_t flags;
43401
43402 flags = 0;
unsigned int duk_small_uint_t
duk_small_int_t duk_bool_t
#define DUK_ASSERT_DISABLE(x)
#define DUK_HEAP_ALLOC_FAIL_MARKANDSWEEP_LIMIT
#define DUK__VOLUNTARY_PERIODIC_GC(heap)
#define DUK_HEAP_ALLOC_FAIL_MARKANDSWEEP_EMERGENCY_LIMIT
duk_alloc_function alloc_func

References duk_heap::alloc_func, DUK__VOLUNTARY_PERIODIC_GC, DUK_ASSERT, DUK_ASSERT_DISABLE, DUK_D, DUK_DDD, DUK_DDDPRINT, DUK_DPRINT, DUK_HEAP_ALLOC_FAIL_MARKANDSWEEP_EMERGENCY_LIMIT, DUK_HEAP_ALLOC_FAIL_MARKANDSWEEP_LIMIT, DUK_HEAP_HAS_MARKANDSWEEP_RUNNING, duk_heap_mark_and_sweep(), DUK_MS_FLAG_EMERGENCY, DUK_UNREF, duk_heap::heap_udata, and NULL.

◆ duk_heap_mem_alloc_zeroed()

DUK_INTERNAL_DECL void * duk_heap_mem_alloc_zeroed ( duk_heap * heap,
duk_size_t size )

Definition at line 43417 of file duktape-1.5.2/src-noline/duktape.c.

43426 {
43427 DUK_ASSERT(heap != NULL);
43428 DUK_ASSERT_DISABLE(size >= 0);
43429

References DUK_ALLOC, DUK_ASSERT, DUK_ASSERT_DISABLE, DUK_MEMZERO, and NULL.

◆ duk_heap_mem_free()

DUK_INTERNAL_DECL void duk_heap_mem_free ( duk_heap * heap,
void * ptr )

Definition at line 43637 of file duktape-1.5.2/src-noline/duktape.c.

43644 {
43645 return heap->realloc_func(heap->heap_udata, cb(heap, ud), newsize);
43646}
43647#endif /* DUK_USE_MARK_AND_SWEEP */
43648
43649/*
43650 * Free memory
43651 */
43652
43653#ifdef DUK_USE_MARK_AND_SWEEP
43654DUK_INTERNAL void duk_heap_mem_free(duk_heap *heap, void *ptr) {
DUK_INTERNAL_DECL void duk_heap_mem_free(duk_heap *heap, void *ptr)
duk_realloc_function realloc_func

References DUK_ASSERT, duk_heap::free_func, duk_heap::heap_udata, duk_heap::mark_and_sweep_trigger_counter, and NULL.

◆ duk_heap_mem_realloc()

DUK_INTERNAL_DECL void * duk_heap_mem_realloc ( duk_heap * heap,
void * ptr,
duk_size_t newsize )

Definition at line 43436 of file duktape-1.5.2/src-noline/duktape.c.

43441 {
43442 /* assume memset with zero size is OK */
43443 DUK_MEMZERO(res, size);
43444 }
43445 return res;
43446}
43447
43448/*
43449 * Reallocate memory with garbage collection
43450 */
43451
43452#ifdef DUK_USE_MARK_AND_SWEEP
43453DUK_INTERNAL void *duk_heap_mem_realloc(duk_heap *heap, void *ptr, duk_size_t newsize) {
43454 void *res;
43455 duk_bool_t rc;
43457
43458 DUK_ASSERT(heap != NULL);
43459 /* ptr may be NULL */
43460 DUK_ASSERT_DISABLE(newsize >= 0);
43461
43462 /*
43463 * Voluntary periodic GC (if enabled)
43464 */
43465
43467
43468 /*
43469 * First attempt
43470 */
43471
43472#ifdef DUK_USE_GC_TORTURE
43473 /* simulate alloc failure on every realloc (except when mark-and-sweep is running) */
43475 DUK_DDD(DUK_DDDPRINT("gc torture enabled, pretend that first realloc attempt fails"));
43476 res = NULL;
43477 DUK_UNREF(res);
43478 goto skip_attempt;
43479 }
43480#endif
43481 res = heap->realloc_func(heap->heap_udata, ptr, newsize);
43482 if (res || newsize == 0) {
43483 /* for zero size allocations NULL is allowed */
43484 return res;
43485 }
43486#ifdef DUK_USE_GC_TORTURE
43487 skip_attempt:
43488#endif
43489
43490 DUK_D(DUK_DPRINT("first realloc attempt failed, attempt to gc and retry"));
43491
43492 /*
43493 * Avoid a GC if GC is already running. See duk_heap_mem_alloc().
43494 */
43495
43497 DUK_D(DUK_DPRINT("duk_heap_mem_realloc() failed, gc in progress (gc skipped), alloc size %ld", (long) newsize));
43498 return NULL;
43499 }
43500
43501 /*
43502 * Retry with several GC attempts. Initial attempts are made without
43503 * emergency mode; later attempts use emergency mode which minimizes
43504 * memory allocations forcibly.
43505 */
43506
43507 for (i = 0; i < DUK_HEAP_ALLOC_FAIL_MARKANDSWEEP_LIMIT; i++) {
43508 duk_small_uint_t flags;
43509
43510 flags = 0;
#define DUK_MEMZERO(p, n)
DUK_INTERNAL_DECL void * duk_heap_mem_realloc(duk_heap *heap, void *ptr, duk_size_t newsize)

References DUK__VOLUNTARY_PERIODIC_GC, DUK_ASSERT, DUK_ASSERT_DISABLE, DUK_D, DUK_DDD, DUK_DDDPRINT, DUK_DPRINT, DUK_HEAP_ALLOC_FAIL_MARKANDSWEEP_EMERGENCY_LIMIT, DUK_HEAP_ALLOC_FAIL_MARKANDSWEEP_LIMIT, DUK_HEAP_HAS_MARKANDSWEEP_RUNNING, duk_heap_mark_and_sweep(), DUK_MS_FLAG_EMERGENCY, DUK_UNREF, duk_heap::heap_udata, NULL, and duk_heap::realloc_func.

◆ duk_heap_mem_realloc_indirect()

DUK_INTERNAL_DECL void * duk_heap_mem_realloc_indirect ( duk_heap * heap,
duk_mem_getptr cb,
void * ud,
duk_size_t newsize )

Definition at line 43530 of file duktape-1.5.2/src-noline/duktape.c.

43531 {
43532 DUK_ASSERT(heap != NULL);
43533 /* ptr may be NULL */
43534 DUK_ASSERT_DISABLE(newsize >= 0);
43535
43536 return heap->realloc_func(heap->heap_udata, ptr, newsize);
43537}
43538#endif /* DUK_USE_MARK_AND_SWEEP */
43539
43540/*
43541 * Reallocate memory with garbage collection, using a callback to provide
43542 * the current allocated pointer. This variant is used when a mark-and-sweep
43543 * (e.g. finalizers) might change the original pointer.
43544 */
43545
43546#ifdef DUK_USE_MARK_AND_SWEEP
43548 void *res;
43549 duk_bool_t rc;
43551
43552 DUK_ASSERT(heap != NULL);
43553 DUK_ASSERT_DISABLE(newsize >= 0);
43554
43555 /*
43556 * Voluntary periodic GC (if enabled)
43557 */
43558
43560
43561 /*
43562 * First attempt
43563 */
43564
43565#ifdef DUK_USE_GC_TORTURE
43566 /* simulate alloc failure on every realloc (except when mark-and-sweep is running) */
43568 DUK_DDD(DUK_DDDPRINT("gc torture enabled, pretend that first indirect realloc attempt fails"));
43569 res = NULL;
43570 DUK_UNREF(res);
43571 goto skip_attempt;
43572 }
43573#endif
43574 res = heap->realloc_func(heap->heap_udata, cb(heap, ud), newsize);
43575 if (res || newsize == 0) {
43576 /* for zero size allocations NULL is allowed */
43577 return res;
43578 }
43579#ifdef DUK_USE_GC_TORTURE
43580 skip_attempt:
43581#endif
43582
43583 DUK_D(DUK_DPRINT("first indirect realloc attempt failed, attempt to gc and retry"));
43584
43585 /*
43586 * Avoid a GC if GC is already running. See duk_heap_mem_alloc().
43587 */
43588
43590 DUK_D(DUK_DPRINT("duk_heap_mem_realloc_indirect() failed, gc in progress (gc skipped), alloc size %ld", (long) newsize));
43591 return NULL;
43592 }
43593
43594 /*
43595 * Retry with several GC attempts. Initial attempts are made without
43596 * emergency mode; later attempts use emergency mode which minimizes
43597 * memory allocations forcibly.
43598 */
43599
43600 for (i = 0; i < DUK_HEAP_ALLOC_FAIL_MARKANDSWEEP_LIMIT; i++) {
43601 duk_small_uint_t flags;
43602
43603#ifdef DUK_USE_ASSERTIONS
43604 void *ptr_pre; /* ptr before mark-and-sweep */
43605 void *ptr_post;
43606#endif
43607
43608#ifdef DUK_USE_ASSERTIONS
43609 ptr_pre = cb(heap, ud);
43610#endif
43611 flags = 0;
43613 flags |= DUK_MS_FLAG_EMERGENCY;
43614 }
43615
43616 rc = duk_heap_mark_and_sweep(heap, flags);
43617 DUK_UNREF(rc);
43618#ifdef DUK_USE_ASSERTIONS
43619 ptr_post = cb(heap, ud);
43620 if (ptr_pre != ptr_post) {
43621 /* useful for debugging */
43622 DUK_DD(DUK_DDPRINT("note: base pointer changed by mark-and-sweep: %p -> %p",
43623 (void *) ptr_pre, (void *) ptr_post));
43624 }
#define DUK_MS_FLAG_EMERGENCY
DUK_INTERNAL_DECL void * duk_heap_mem_realloc_indirect(duk_heap *heap, duk_mem_getptr cb, void *ud, duk_size_t newsize)
void *(* duk_mem_getptr)(duk_heap *heap, void *ud)

References DUK__VOLUNTARY_PERIODIC_GC, DUK_ASSERT, DUK_ASSERT_DISABLE, DUK_D, DUK_DD, DUK_DDD, DUK_DDDPRINT, DUK_DDPRINT, DUK_DPRINT, DUK_HEAP_ALLOC_FAIL_MARKANDSWEEP_EMERGENCY_LIMIT, DUK_HEAP_ALLOC_FAIL_MARKANDSWEEP_LIMIT, DUK_HEAP_HAS_MARKANDSWEEP_RUNNING, duk_heap_mark_and_sweep(), DUK_MS_FLAG_EMERGENCY, DUK_UNREF, duk_heap::heap_udata, NULL, and duk_heap::realloc_func.

◆ duk_heap_strcache_offset_char2byte()

DUK_INTERNAL_DECL duk_uint_fast32_t duk_heap_strcache_offset_char2byte ( duk_hthread * thr,
duk_hstring * h,
duk_uint_fast32_t char_offset )

Definition at line 44448 of file duktape-1.5.2/src-noline/duktape.c.

44465 {
44466 duk_heap *heap;
44467 duk_strcache *sce;
44468 duk_uint_fast32_t byte_offset;
44470 duk_bool_t use_cache;
44471 duk_uint_fast32_t dist_start, dist_end, dist_sce;
44472 const duk_uint8_t *p_start;
44473 const duk_uint8_t *p_end;
44474 const duk_uint8_t *p_found;
44475
44476 if (char_offset > DUK_HSTRING_GET_CHARLEN(h)) {
44477 goto error;
44478 }
44479
44480 /*
44481 * For ASCII strings, the answer is simple.
44482 */
44483
44484 if (DUK_HSTRING_IS_ASCII(h)) {
44485 /* clen == blen -> pure ascii */
44486 return char_offset;
44487 }
44488
44489 /*
44490 * For non-ASCII strings, we need to scan forwards or backwards
44491 * from some starting point. The starting point may be the start
44492 * or end of the string, or some cached midpoint in the string
44493 * cache.
44494 *
44495 * For "short" strings we simply scan without checking or updating
44496 * the cache. For longer strings we check and update the cache as
44497 * necessary, inserting a new cache entry if none exists.
44498 */
44499
44500 DUK_DDD(DUK_DDDPRINT("non-ascii string %p, char_offset=%ld, clen=%ld, blen=%ld",
44501 (void *) h, (long) char_offset,
44502 (long) DUK_HSTRING_GET_CHARLEN(h),
44503 (long) DUK_HSTRING_GET_BYTELEN(h)));
44504
44505 heap = thr->heap;
44506 sce = NULL;
44508
44509 if (use_cache) {
44510#ifdef DUK_USE_DDDPRINT
44511 DUK_DDD(DUK_DDDPRINT("stringcache before char2byte (using cache):"));
44512 for (i = 0; i < DUK_HEAP_STRCACHE_SIZE; i++) {
44513 duk_strcache *c = heap->strcache + i;
44514 DUK_DDD(DUK_DDDPRINT(" [%ld] -> h=%p, cidx=%ld, bidx=%ld",
44515 (long) i, (void *) c->h, (long) c->cidx, (long) c->bidx));
44516 }
44517#endif
44518
44519 for (i = 0; i < DUK_HEAP_STRCACHE_SIZE; i++) {
44520 duk_strcache *c = heap->strcache + i;
44521
44522 if (c->h == h) {
44523 sce = c;
44524 break;
44525 }
44526 }
44527 }
44528
44529 /*
44530 * Scan from shortest distance:
44531 * - start of string
44532 * - end of string
44533 * - cache entry (if exists)
44534 */
44535
44536 DUK_ASSERT(DUK_HSTRING_GET_CHARLEN(h) >= char_offset);
44537 dist_start = char_offset;
44538 dist_end = DUK_HSTRING_GET_CHARLEN(h) - char_offset;
44539 dist_sce = 0; DUK_UNREF(dist_sce); /* initialize for debug prints, needed if sce==NULL */
44540
44541 p_start = (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h);
44542 p_end = (const duk_uint8_t *) (p_start + DUK_HSTRING_GET_BYTELEN(h));
44543 p_found = NULL;
44544
44545 if (sce) {
44546 if (char_offset >= sce->cidx) {
44547 dist_sce = char_offset - sce->cidx;
44548 if ((dist_sce <= dist_start) && (dist_sce <= dist_end)) {
44549 DUK_DDD(DUK_DDDPRINT("non-ascii string, use_cache=%ld, sce=%p:%ld:%ld, "
44550 "dist_start=%ld, dist_end=%ld, dist_sce=%ld => "
44551 "scan forwards from sce",
44552 (long) use_cache, (void *) (sce ? sce->h : NULL),
44553 (sce ? (long) sce->cidx : (long) -1),
44554 (sce ? (long) sce->bidx : (long) -1),
44555 (long) dist_start, (long) dist_end, (long) dist_sce));
44556
44557 p_found = duk__scan_forwards(p_start + sce->bidx,
44558 p_end,
44559 dist_sce);
44560 goto scan_done;
44561 }
44562 } else {
44563 dist_sce = sce->cidx - char_offset;
44564 if ((dist_sce <= dist_start) && (dist_sce <= dist_end)) {
44565 DUK_DDD(DUK_DDDPRINT("non-ascii string, use_cache=%ld, sce=%p:%ld:%ld, "
44566 "dist_start=%ld, dist_end=%ld, dist_sce=%ld => "
44567 "scan backwards from sce",
44568 (long) use_cache, (void *) (sce ? sce->h : NULL),
44569 (sce ? (long) sce->cidx : (long) -1),
44570 (sce ? (long) sce->bidx : (long) -1),
44571 (long) dist_start, (long) dist_end, (long) dist_sce));
44572
44573 p_found = duk__scan_backwards(p_start + sce->bidx,
44574 p_start,
44575 dist_sce);
44576 goto scan_done;
44577 }
44578 }
44579 }
44580
44581 /* no sce, or sce scan not best */
44582
44583 if (dist_start <= dist_end) {
44584 DUK_DDD(DUK_DDDPRINT("non-ascii string, use_cache=%ld, sce=%p:%ld:%ld, "
44585 "dist_start=%ld, dist_end=%ld, dist_sce=%ld => "
44586 "scan forwards from string start",
44587 (long) use_cache, (void *) (sce ? sce->h : NULL),
44588 (sce ? (long) sce->cidx : (long) -1),
44589 (sce ? (long) sce->bidx : (long) -1),
44590 (long) dist_start, (long) dist_end, (long) dist_sce));
44591
44592 p_found = duk__scan_forwards(p_start,
44593 p_end,
44594 dist_start);
44595 } else {
44596 DUK_DDD(DUK_DDDPRINT("non-ascii string, use_cache=%ld, sce=%p:%ld:%ld, "
44597 "dist_start=%ld, dist_end=%ld, dist_sce=%ld => "
44598 "scan backwards from string end",
44599 (long) use_cache, (void *) (sce ? sce->h : NULL),
44600 (sce ? (long) sce->cidx : (long) -1),
44601 (sce ? (long) sce->bidx : (long) -1),
44602 (long) dist_start, (long) dist_end, (long) dist_sce));
44603
44604 p_found = duk__scan_backwards(p_end,
44605 p_start,
44606 dist_end);
44607 }
44608
44609 scan_done:
44610
44611 if (!p_found) {
44612 /* Scan error: this shouldn't normally happen; it could happen if
44613 * string is not valid UTF-8 data, and clen/blen are not consistent
44614 * with the scanning algorithm.
44615 */
44616 goto error;
44617 }
44618
44619 DUK_ASSERT(p_found >= p_start);
44620 DUK_ASSERT(p_found <= p_end); /* may be equal */
44621 byte_offset = (duk_uint32_t) (p_found - p_start);
44622
44623 DUK_DDD(DUK_DDDPRINT("-> string %p, cidx %ld -> bidx %ld",
44624 (void *) h, (long) char_offset, (long) byte_offset));
44625
44626 /*
44627 * Update cache entry (allocating if necessary), and move the
44628 * cache entry to the first place (in an "LRU" policy).
44629 */
44630
44631 if (use_cache) {
44632 /* update entry, allocating if necessary */
44633 if (!sce) {
44634 sce = heap->strcache + DUK_HEAP_STRCACHE_SIZE - 1; /* take last entry */
44635 sce->h = h;
44636 }
44637 DUK_ASSERT(sce != NULL);
44638 sce->bidx = (duk_uint32_t) (p_found - p_start);
44639 sce->cidx = (duk_uint32_t) char_offset;
44640
44641 /* LRU: move our entry to first */
44642 if (sce > &heap->strcache[0]) {
44643 /*
44644 * A C
44645 * B A
44646 * C <- sce ==> B
44647 * D D
44648 */
44649 duk_strcache tmp;
44650
44651 tmp = *sce;
44652 DUK_MEMMOVE((void *) (&heap->strcache[1]),
44653 (const void *) (&heap->strcache[0]),
44654 (size_t) (((char *) sce) - ((char *) &heap->strcache[0])));
44655 heap->strcache[0] = tmp;
44656
44657 /* 'sce' points to the wrong entry here, but is no longer used */
#define DUK_HSTRING_GET_DATA(x)
#define DUK_HSTRING_IS_ASCII(x)
#define DUK_HSTRING_GET_CHARLEN(x)
#define DUK_HSTRING_GET_BYTELEN(x)
DUK_LOCAL const duk_uint8_t * duk__scan_forwards(const duk_uint8_t *p, const duk_uint8_t *q, duk_uint_fast32_t n)
#define DUK_HEAP_STRINGCACHE_NOCACHE_LIMIT
#define DUK_HEAP_STRCACHE_SIZE
DUK_LOCAL const duk_uint8_t * duk__scan_backwards(const duk_uint8_t *p, const duk_uint8_t *q, duk_uint_fast32_t n)
static void error(LoadState *S, const char *why)
duk_strcache strcache[DUK_HEAP_STRCACHE_SIZE]

References duk_strcache::bidx, duk_strcache::cidx, duk__scan_backwards(), duk__scan_forwards(), DUK_ASSERT, DUK_DDD, DUK_DDDPRINT, DUK_ERROR_INTERNAL_DEFMSG, DUK_HEAP_STRCACHE_SIZE, DUK_HEAP_STRINGCACHE_NOCACHE_LIMIT, DUK_HSTRING_GET_BYTELEN, DUK_HSTRING_GET_CHARLEN, DUK_HSTRING_GET_DATA, DUK_HSTRING_IS_ASCII, DUK_MEMMOVE, DUK_UNREF, error(), duk_strcache::h, duk_hthread::heap, NULL, and duk_heap::strcache.

Referenced by duk__regexp_match_helper(), duk_bi_string_prototype_indexof_shared(), duk_bi_string_prototype_replace(), duk_bi_string_prototype_split(), duk_hstring_char_code_at_raw(), and duk_substring().

◆ duk_heap_strcache_string_remove()

DUK_INTERNAL_DECL void duk_heap_strcache_string_remove ( duk_heap * heap,
duk_hstring * h )

◆ duk_heap_string_intern()

DUK_INTERNAL_DECL duk_hstring * duk_heap_string_intern ( duk_heap * heap,
const duk_uint8_t * str,
duk_uint32_t blen )

Definition at line 45637 of file duktape-1.5.2/src-noline/duktape.c.

45648 {
45649 duk_uint32_t strhash; /* dummy */
45650 return duk__do_lookup(heap, str, blen, &strhash);
45651}
DUK_LOCAL duk_hstring * duk__do_lookup(duk_heap *heap, const duk_uint8_t *str, duk_uint32_t blen, duk_uint32_t *out_strhash)

References duk__do_intern(), duk__do_lookup(), DUK_ASSERT, and DUK_HSTRING_MAX_BYTELEN.

Referenced by duk__init_heap_strings(), duk_heap_string_intern_checked(), and duk_heap_string_intern_u32().

◆ duk_heap_string_intern_checked()

DUK_INTERNAL_DECL duk_hstring * duk_heap_string_intern_checked ( duk_hthread * thr,
const duk_uint8_t * str,
duk_uint32_t len )

Definition at line 45653 of file duktape-1.5.2/src-noline/duktape.c.

45654 {
45655 duk_hstring *res;
45656 duk_uint32_t strhash;
45657
45658 /* caller is responsible for ensuring this */
#define DUK_HSTRING_MAX_BYTELEN

References duk__do_intern(), duk__do_lookup(), DUK_ASSERT, DUK_ERROR_ALLOC_DEFMSG, duk_heap_string_intern(), DUK_HSTRING_MAX_BYTELEN, and duk_hthread::heap.

Referenced by duk_push_lstring(), and duk_substring().

◆ duk_heap_string_intern_u32()

DUK_INTERNAL_DECL duk_hstring * duk_heap_string_intern_u32 ( duk_heap * heap,
duk_uint32_t val )

Definition at line 45671 of file duktape-1.5.2/src-noline/duktape.c.

45672 {
45674 }
45675 return res;
45676}
45677
#define DUK_ERROR_ALLOC_DEFMSG(thr)

References DUK_ASSERT, duk_heap_string_intern(), DUK_SNPRINTF, DUK_STRLEN, DUK_STRTAB_U32_MAX_STRLEN, and DUK_UINT32_MAX.

Referenced by duk__realloc_props(), and duk_heap_string_intern_u32_checked().

◆ duk_heap_string_intern_u32_checked()

DUK_INTERNAL_DECL duk_hstring * duk_heap_string_intern_u32_checked ( duk_hthread * thr,
duk_uint32_t val )

Definition at line 45679 of file duktape-1.5.2/src-noline/duktape.c.

45679 {
45680 char buf[DUK_STRTAB_U32_MAX_STRLEN+1];
45681 DUK_SNPRINTF(buf, sizeof(buf), "%lu", (unsigned long) val);
45682 buf[sizeof(buf) - 1] = (char) 0;
45683 DUK_ASSERT(DUK_STRLEN(buf) <= DUK_UINT32_MAX); /* formatted result limited */
45684 return duk_heap_string_lookup(heap, (const duk_uint8_t *) buf, (duk_uint32_t) DUK_STRLEN(buf));
45685}
#define DUK_STRTAB_U32_MAX_STRLEN

References DUK_ASSERT, DUK_ERROR_ALLOC_DEFMSG, duk_heap_string_intern_u32(), DUK_SNPRINTF, DUK_STRLEN, DUK_STRTAB_U32_MAX_STRLEN, DUK_UINT32_MAX, and duk_hthread::heap.

Referenced by duk_hobject_enumerator_create().