mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-06-20 02:11:31 +00:00
changetype builtin; some namespace parsing; more stdlib ideas; compiler options for asc
This commit is contained in:
38
std/impl/heap.ts
Normal file
38
std/impl/heap.ts
Normal file
@ -0,0 +1,38 @@
|
||||
/// <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() {}
|
||||
}
|
Reference in New Issue
Block a user