mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-06-13 06:51:34 +00:00
* Rename memory instructions as proposed by the bulk-memory-operations spec. * Rename memory manager functions to memory.* as well * Remove automatic inlining of constant globals (Binaryen does this now) * Improve 'const' enum compatibility * Improve module-level export generation * Enable the inline decorator for constant variables * Add ERROR, WARNING and INFO macros that emit a user-defined diagnostic * Reintroduce builtin decorator so these can appear anywhere in stdlib again * Inline isNaN and isFinite by default * Make an interface around gc.* similar to memory.* * Emit an error when trying to inline a mutable variable * Slim down CI stages * Add a more convenient tracing utility for debugging * Implement some prequesites for an eventual bundled GC
43 lines
1.2 KiB
TypeScript
43 lines
1.2 KiB
TypeScript
/**
|
|
* Arena Memory Allocator
|
|
*
|
|
* Provides a `memory.reset` function to reset the heap to its initial state. A user has to make
|
|
* sure that there are no more references to cleared memory afterwards. Always aligns to 8 bytes.
|
|
*
|
|
* @module std/assembly/allocator/arena
|
|
*//***/
|
|
|
|
import { AL_MASK, MAX_SIZE_32 } from "../internal/allocator";
|
|
|
|
var startOffset: usize = (HEAP_BASE + AL_MASK) & ~AL_MASK;
|
|
var offset: usize = startOffset;
|
|
|
|
// Memory allocator interface
|
|
|
|
@global export function __memory_allocate(size: usize): usize {
|
|
if (size) {
|
|
if (size > MAX_SIZE_32) unreachable();
|
|
let ptr = offset;
|
|
let newPtr = (ptr + size + AL_MASK) & ~AL_MASK;
|
|
let pagesBefore = memory.size();
|
|
if (newPtr > <usize>pagesBefore << 16) {
|
|
let pagesNeeded = ((newPtr - ptr + 0xffff) & ~0xffff) >>> 16;
|
|
let pagesWanted = max(pagesBefore, pagesNeeded); // double memory
|
|
if (memory.grow(pagesWanted) < 0) {
|
|
if (memory.grow(pagesNeeded) < 0) {
|
|
unreachable(); // out of memory
|
|
}
|
|
}
|
|
}
|
|
offset = newPtr;
|
|
return ptr;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
@global export function __memory_free(ptr: usize): void { /* nop */ }
|
|
|
|
@global export function __memory_reset(): void {
|
|
offset = startOffset;
|
|
}
|