///
const ALIGN_LOG2: usize = 3;
const ALIGN_SIZE: usize = 1 << ALIGN_LOG2;
const ALIGN_MASK: usize = ALIGN_SIZE - 1;
let HEAP_OFFSET: usize = HEAP_START; // HEAP_START is a constant generated by the compiler
@global()
@struct()
class Heap {
static allocate(size: usize): usize {
const ptr: usize = HEAP_OFFSET;
assert(ptr + size <= (current_memory() << 16));
if (((HEAP_OFFSET += size) & ALIGN_MASK) != 0) // align next offset
HEAP_OFFSET = (HEAP_OFFSET | ALIGN_MASK) + 1;
return ptr;
}
static dispose(ptr: usize): void {
// just a big chunk of non-disposable memory for now
}
static get used(): usize {
return HEAP_OFFSET - HEAP_START;
}
static get free(): usize {
return (current_memory() << 16) - HEAP_OFFSET;
}
static get size(): usize {
return (current_memory() << 16) - HEAP_START;
}
private constructor() {}
}