This library implements libc memory allocation functions in WebAssembly. The implementation assumes a single page of memory, that is, a 64 KiB buffer.
malloccallocfreereallocmemsetmemcpymemmove
Initialize memory with cmem_init(start), where start is the 16 bit address of the first byte of the memory region to use. Proceed to use memory allocation functions as usual. Exceeding available memory will trap.
cmem_end() returns a past-the-end pointer to the last buffer in memory, effectively retrieving the current length of memory. Intended use is backing up memory states at minimal size.
build.sh runs the source code through the C language preprocessor, then translates it with wat2wasm from the wabt software package.
The result is an object file that can be linked using wasm-ld, as well as a WebAssembly module.
The script also produces an optimized module using wasm-opt -O3 from the binaryen package.
EXPORT_ALLexports the memory and all functions rather than justcmem_initandcmem_end, useful for testing.PREFIX_LIBCprependscmem_to libc function names.FIRST_FITemploys a first-fit instead of a best-fit strategy for allocation.BULK_MEMORYenables use of bulk memory instructions, decreasing embedder compatibility.
Best-fit allocation. Doubly linked list to navigate buffers. 16-bit addresses constrict memory length to 64 KiB.