2018-03-04 01:30:16 +01:00
|
|
|
/**
|
|
|
|
* @file Arena Memory Allocator
|
|
|
|
*
|
|
|
|
* Provides a `reset_memory` 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.
|
|
|
|
*/
|
2018-01-28 15:30:49 +01:00
|
|
|
|
|
|
|
const ALIGN_LOG2: usize = 3;
|
|
|
|
const ALIGN_SIZE: usize = 1 << ALIGN_LOG2;
|
|
|
|
const ALIGN_MASK: usize = ALIGN_SIZE - 1;
|
|
|
|
|
2018-01-28 19:14:20 +01:00
|
|
|
var HEAP_OFFSET: usize = HEAP_BASE;
|
2018-01-28 15:30:49 +01:00
|
|
|
|
2018-02-17 11:09:22 +01:00
|
|
|
@global
|
2018-01-28 15:30:49 +01:00
|
|
|
export function allocate_memory(size: usize): usize {
|
|
|
|
if (!size) return 0;
|
2018-01-28 19:14:20 +01:00
|
|
|
var ptr = HEAP_OFFSET;
|
|
|
|
var off = (ptr + size + ALIGN_MASK) & ~ALIGN_MASK;
|
|
|
|
var avail = <usize>current_memory() << 16;
|
|
|
|
if (off > avail && grow_memory(
|
2018-02-28 01:48:01 +01:00
|
|
|
<i32>max(
|
2018-01-28 19:14:20 +01:00
|
|
|
(((off + 0xffff) & ~0xffff) - avail) >> 16, // minimum required pages
|
|
|
|
avail >> 16 // at least double memory
|
|
|
|
)
|
|
|
|
) < 0) unreachable(); // out of memory
|
|
|
|
HEAP_OFFSET = off;
|
2018-01-28 15:30:49 +01:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2018-02-17 11:09:22 +01:00
|
|
|
@global
|
2018-01-28 15:30:49 +01:00
|
|
|
export function free_memory(ptr: usize): void {
|
|
|
|
// nop
|
|
|
|
}
|
|
|
|
|
2018-02-17 11:09:22 +01:00
|
|
|
@global
|
2018-02-02 03:07:54 +01:00
|
|
|
export function reset_memory(): void {
|
2018-01-28 15:30:49 +01:00
|
|
|
HEAP_OFFSET = HEAP_BASE;
|
|
|
|
}
|