A WebAssembly slab allocator for external references.
  • Rust 76.2%
  • WebAssembly 23.8%
Find a file
2025-10-13 12:59:52 +02:00
tests Allow freeing null and added take function 2025-10-13 12:59:52 +02:00
.gitignore Initial release 2025-10-10 16:16:23 +02:00
Cargo.lock Initial release 2025-10-10 16:16:23 +02:00
Cargo.toml Initial release 2025-10-10 16:16:23 +02:00
README.md Allow freeing null and added take function 2025-10-13 12:59:52 +02:00
table-slab.wat Allow freeing null and added take function 2025-10-13 12:59:52 +02:00

WASM Table Slab

A slab allocator for external references. It can be used to convert from externref to i32 such that it can be used by languages which don't support externref.

Assembling

Use wasm-tools to assemble.

wasm-tools parse -o table-slab.wasm table-slab.wat

How it works

The library uses an anyref table, which initially only contains the null reference at index 0. A the $next global is the index of a free index or after the last element if the table is full. When an item is removed, it is replaced by an i31 pointing to the current $next, and $next is updated to point to the removed item.

This way, the i31 "tombstones" form a linked list of vacant items enabling O(1) alloc and free.