diff --git a/examples/tlsf/assembly/tlsf.ts b/examples/tlsf/assembly/tlsf.ts index b2b0613d..2ca47d6c 100644 --- a/examples/tlsf/assembly/tlsf.ts +++ b/examples/tlsf/assembly/tlsf.ts @@ -323,9 +323,9 @@ class Root { /** * Uses the specified free block, removing it from internal maps and - * splitting it if possible. + * splitting it if possible, and returns its data pointer. */ - use(block: Block, size: usize): void { + use(block: Block, size: usize): usize { assert(block.info & FREE); // must be free so we can use it assert(size >= Block.MIN_SIZE && size < Block.MAX_SIZE); // must be valid assert(!(size & AL_MASK)); // size must be aligned so the new block is @@ -349,6 +349,8 @@ class Root { var right: Block = assert(block.right); // can't be null (tail) right.info &= ~LEFT_FREE; } + + return changetype(block) + Block.INFO; } /** Adds more memory to the pool. */ @@ -409,9 +411,9 @@ export function allocate_memory(size: usize): usize { var rootOffset = (HEAP_BASE + AL_MASK) & ~AL_MASK; ROOT = root = changetype(rootOffset); root.flMap = 0; - for (var fl = 0; fl < FL_BITS; ++fl) { + for (var fl: usize = 0; fl < FL_BITS; ++fl) { root.setSLMap(fl, 0); - for (var sl = 0; sl < SL_SIZE; ++sl) + for (var sl: u32 = 0; sl < SL_SIZE; ++sl) root.setHead(fl, sl, null); } root.addMemory(rootOffset + Root.SIZE, current_memory() << 16); @@ -436,8 +438,7 @@ export function allocate_memory(size: usize): usize { } assert((block.info & ~TAGS) >= size); - root.use(block, size); - data = changetype(block) + Block.INFO; + data = root.use(block, size); } return data; @@ -454,4 +455,5 @@ export function free_memory(data: usize): void { } } -export { set_memory }; +// For stand-alone usage, e.g., in tests +export { move_memory, set_memory, compare_memory }; diff --git a/std/assembly/allocator/emscripten.ts b/std/assembly/allocator/emscripten.ts new file mode 100644 index 00000000..021755f9 --- /dev/null +++ b/std/assembly/allocator/emscripten.ts @@ -0,0 +1,20 @@ +///////////////////////// Emscripten Memory Allocator ////////////////////////// + +// Uses Emscripten's exported _malloc and _free implementations, i.e., when +// linking with Emscripten-compiled programs that already provide these. +// Differs from 'system' in that their names are prefixed with an underscore. + +declare function _malloc(size: usize): usize; +declare function _free(ptr: usize): void; + +export function allocate_memory(size: usize): usize { + return _malloc(size); +} + +export function free_memory(ptr: usize): void { + _free(ptr); +} + +export function reset_memory(): void { + throw new Error("not supported"); +} diff --git a/std/assembly/allocator/system.ts b/std/assembly/allocator/system.ts new file mode 100644 index 00000000..70ac59f9 --- /dev/null +++ b/std/assembly/allocator/system.ts @@ -0,0 +1,19 @@ +/////////////////////////// System Memory Allocator //////////////////////////// + +// Uses the environment's malloc and free implementations, i.e., when linking +// with other C-like programs that already provide these. + +declare function malloc(size: usize): usize; +declare function free(ptr: usize): void; + +export function allocate_memory(size: usize): usize { + return malloc(size); +} + +export function free_memory(ptr: usize): void { + free(ptr); +} + +export function reset_memory(): void { + throw new Error("not supported"); +} diff --git a/std/assembly/allocator/tlsf.ts b/std/assembly/allocator/tlsf.ts index 560aa90f..bfae246a 100644 --- a/std/assembly/allocator/tlsf.ts +++ b/std/assembly/allocator/tlsf.ts @@ -1,3 +1,5 @@ +////////////// TLSF (Two-Level Segregate Fit) Memory Allocator //////////////// + // Re-export for now, so there's just one source file being worked on export {