Skip to main content

Module alloc

Module alloc 

Source
Expand description

The allocator the C side shares with us, and the boxes that hold engine values.

§Why there is an allocator here at all

quickjs.h decrements a reference count inline, in the caller’s translation unit:

static inline void JS_FreeValue(JSContext *ctx, JSValue v) {
    if (JS_VALUE_HAS_REF_COUNT(v)) {
        JSRefCountHeader *p = __js_rc(JS_VALUE_GET_PTR(v));  /* (uint32_t *)ptr - 1 */
        if (--p->ref_count <= 0) __JS_FreeValue(ctx, v);
    }
}

__js_rc is (uint32_t *)ptr - 1, so the four bytes immediately before every reference-counted payload must be a writable int. No Rust container can promise that, so the payloads C sees are allocated here, with the reference’s own header shape:

block + 0 : u32  kind and size   (see Kind::slot)
block + 4 : i32  ref_count       <- what __js_rc(payload) points at
block + 8 : ...  payload         <- JS_VALUE_GET_PTR

That is also why js_malloc / js_free live in the same allocator as the boxes: bjson.c hands us a buffer from JS_WriteObject and then calls js_free on it, and two allocators would mean freeing a pointer we never allocated.

§Two families, one header

  • alloc_raw / free_raw — untyped bytes, for js_malloc and the buffers behind JS_WriteObject.
  • box_value / free_value — a header plus one engine Value, for every reference-counted JSValue that crosses the boundary.

Both put ref_count at +4 and the payload at +8, so C’s --p->ref_count is correct for either, and free_value is the one place an engine value is released on the C side’s say-so — which is where a host class’s finalizer runs.

§Alignment and size limits

Payloads are 8-byte aligned, matching the reference’s JS_MALLOC_ALIGN. That is enough for a double, a pointer, and the uint64_t arrays tests/bjson.c builds.

The size of a byte block is recorded in the same 32-bit word as its kind, so a single block tops out at 1 GiB − 8 bytes. Nothing in the C API asks for more: the one large buffer it produces is JS_WriteObject’s output, and a 1 GiB serialisation is already beyond what test_bjson.js exercises. A block that big is answered with a null pointer, which every caller already has to handle.

Structs§

ValueBox
One engine Value in a block whose payload pointer is also a JSValue.

Enums§

Kind
What a block holds, as stored in the header word.

Constants§

ALIGN
Alignment every block is allocated with.
HEADER_SIZE
Size of the header in front of every payload: two 32-bit words.
MAX_PAYLOAD
The largest payload a block may hold.
PAYLOAD_OFFSET
Offset of the first payload byte inside a block.
REF_COUNT_OFFSET
Offset of ref_count inside a block. __js_rc(payload) is payload - 4, so this is the number that must not move.

Functions§

alloc_raw⚠
js_malloc: size bytes, uninitialised, with a header in front.
alloc_raw_zeroed⚠
js_mallocz: alloc_raw, zeroed.
box_value
Box value so that JS_VALUE_GET_PTR on the answer can be --’d from C.
free_raw⚠
js_free: release a block from alloc_raw, or do nothing for a null pointer.
free_value⚠
Reclaim a value box, dropping the engine value it held.
payload_size⚠
The payload size recorded in a block’s header.
realloc_raw⚠
js_realloc: resize a block from alloc_raw, keeping its contents.
value_of⚠
The engine value behind a payload pointer.