This commit is contained in:
dcode
2019-03-14 05:11:03 +01:00
parent 6163a73ab5
commit a5e14a0eaa
24 changed files with 739 additions and 102 deletions

View File

@ -1,9 +1,13 @@
import { HEAP_BASE, memory } from "../memory";
import { AL_MASK, MAX_SIZE_32 } from "../util/allocator";
// @ts-ignore: decorator
@lazy var startOffset: usize = (HEAP_BASE + AL_MASK) & ~AL_MASK;
// @ts-ignore: decorator
@lazy var offset: usize = startOffset;
// @ts-ignore: decorator
@unsafe @global function __memory_allocate(size: usize): usize {
if (size > MAX_SIZE_32) unreachable();
var ptr = offset;
@ -22,9 +26,11 @@ import { AL_MASK, MAX_SIZE_32 } from "../util/allocator";
return ptr;
}
// @ts-ignore: decorator
@unsafe @global function __memory_free(ptr: usize): void {
}
// @ts-ignore: decorator
@unsafe @global function __memory_reset(): void {
offset = startOffset;
}

View File

@ -1,10 +1,15 @@
// @ts-ignore: decorator
@unsafe declare function _malloc(size: usize): usize;
// @ts-ignore: decorator
@unsafe declare function _free(ptr: usize): void;
// @ts-ignore: decorator
@unsafe @global function __memory_allocate(size: usize): usize {
return _malloc(size);
}
// @ts-ignore: decorator
@unsafe @global function __memory_free(ptr: usize): void {
_free(ptr);
}

View File

@ -1,10 +1,15 @@
// @ts-ignore: decorator
@unsafe declare function malloc(size: usize): usize;
// @ts-ignore: decorator
@unsafe declare function free(ptr: usize): void;
// @ts-ignore: decorator
@unsafe @global function __memory_allocate(size: usize): usize {
return malloc(size);
}
// @ts-ignore: decorator
@unsafe @global function __memory_free(ptr: usize): void {
free(ptr);
}

View File

@ -44,8 +44,7 @@ const LEFT_FREE: usize = 1 << 1;
const TAGS: usize = FREE | LEFT_FREE;
/** Block structure. */
@unmanaged
class Block {
@unmanaged class Block {
/** Info field holding this block's size and tags. */
info: usize;
@ -110,8 +109,7 @@ class Block {
assert((1 << SL_BITS) <= 32); // second level must fit into 32 bits
/** Root structure. */
@unmanaged
class Root {
@unmanaged class Root {
/** First level bitmap. */
flMap: usize = 0;
@ -406,15 +404,17 @@ class Root {
}
/** Determines the first (LSB to MSB) set bit's index of a word. */
function ffs<T>(word: T): T {
function ffs<T extends number>(word: T): T {
assert(word != 0); // word cannot be 0
return ctz<T>(word); // differs from ffs only for 0
}
/** Determines the last (LSB to MSB) set bit's index of a word. */
function fls<T>(word: T): T {
function fls<T extends number>(word: T): T {
assert(word != 0); // word cannot be 0
// @ts-ignore: type
const inv: T = (sizeof<T>() << 3) - 1;
// @ts-ignore: type
return inv - clz<T>(word);
}
@ -422,6 +422,7 @@ function fls<T>(word: T): T {
var ROOT: Root = changetype<Root>(0);
/** Allocates a chunk of memory. */
// @ts-ignore: decorator
@unsafe @global function __memory_allocate(size: usize): usize {
// initialize if necessary
var root = ROOT;
@ -470,6 +471,7 @@ var ROOT: Root = changetype<Root>(0);
}
/** Frees the chunk of memory at the specified address. */
// @ts-ignore
@unsafe @global function __memory_free(data: usize): void {
if (data) {
let root = ROOT;