Portable not-so-smart Heap

This commit is contained in:
dcodeIO
2017-12-11 03:31:35 +01:00
parent d0b189b437
commit b5ffaf36cd
4 changed files with 54 additions and 13 deletions

View File

@ -9,9 +9,13 @@ let HEAP_OFFSET: usize = HEAP_START; // HEAP_START is a constant generated by th
class Heap {
static allocate(size: usize): usize {
if (!size) return 0;
const len: i32 = current_memory();
if (HEAP_OFFSET + size > <usize>len << 16)
if(grow_memory(max<i32>(<i32>ceil<f64>(<f64>size / 65536), len * 2 - len)) < 0)
unreachable();
const ptr: usize = HEAP_OFFSET;
assert(ptr + size <= (<usize>current_memory() << 16));
if (((HEAP_OFFSET += size) & ALIGN_MASK) != 0) // align next offset
if ((HEAP_OFFSET += size) & ALIGN_MASK) // align next offset
HEAP_OFFSET = (HEAP_OFFSET | ALIGN_MASK) + 1;
return ptr;
}