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_PTRThat 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, forjs_mallocand the buffers behindJS_WriteObject.box_value/free_value— a header plus one engineValue, for every reference-countedJSValuethat 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§
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_countinside a block.__js_rc(payload)ispayload - 4, so this is the number that must not move.
Functions§
- alloc_
raw ⚠ js_malloc:sizebytes, uninitialised, with a header in front.- alloc_
raw_ ⚠zeroed js_mallocz:alloc_raw, zeroed.- box_
value - Box
valueso thatJS_VALUE_GET_PTRon the answer can be--’d from C. - free_
raw ⚠ js_free: release a block fromalloc_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 fromalloc_raw, keeping its contents.- value_
of ⚠ - The engine value behind a payload pointer.