mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-04-25 23:12:19 +00:00
39 lines
949 B
TypeScript
39 lines
949 B
TypeScript
/// <reference path="../../assembly.d.ts" />
|
|
|
|
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 <= (<usize>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 (<usize>current_memory() << 16) - HEAP_OFFSET;
|
|
}
|
|
|
|
static get size(): usize {
|
|
return (<usize>current_memory() << 16) - HEAP_START;
|
|
}
|
|
|
|
private constructor() {}
|
|
}
|