59 lines
1.7 KiB
TypeScript
Raw Normal View History

2019-04-18 11:51:07 +02:00
import { AL_MASK, DEBUG } from "./common";
//////////////////////////////////// Memory manager interface /////////////////////////////////////
2019-04-18 12:53:48 +02:00
import { ROOT, Block, BLOCK_OVERHEAD, initializeRoot, allocateBlock, reallocateBlock, freeBlock } from "./tlsf";
2019-04-18 11:51:07 +02:00
// @ts-ignore: decorator
@global @unsafe
export function __rt_allocate(size: usize, id: u32): usize {
2019-04-18 11:51:07 +02:00
var root = ROOT;
if (!root) {
initializeRoot();
root = ROOT;
}
2019-04-18 12:53:48 +02:00
var block = allocateBlock(root, size);
block.rtId = id;
return changetype<usize>(block) + BLOCK_OVERHEAD;
2019-04-18 11:51:07 +02:00
}
// @ts-ignore: decorator
@global @unsafe
export function __rt_reallocate(ref: usize, size: usize): usize {
2019-04-18 11:51:07 +02:00
if (DEBUG) assert(ROOT); // must be initialized
2019-04-18 12:53:48 +02:00
assert(ref != 0 && !(ref & AL_MASK)); // must exist and be aligned
return changetype<usize>(reallocateBlock(ROOT, changetype<Block>(ref - BLOCK_OVERHEAD), size)) + BLOCK_OVERHEAD;
2019-04-18 11:51:07 +02:00
}
// @ts-ignore: decorator
@global @unsafe
export function __rt_free(ref: usize): void {
2019-04-18 11:51:07 +02:00
if (DEBUG) assert(ROOT); // must be initialized
2019-04-18 12:53:48 +02:00
assert(ref != 0 && !(ref & AL_MASK)); // must exist and be aligned
freeBlock(ROOT, changetype<Block>(ref - BLOCK_OVERHEAD));
2019-04-18 11:51:07 +02:00
}
/////////////////////////////////// Garbage collector interface ///////////////////////////////////
2019-04-18 12:53:48 +02:00
import { increment, decrement, collectCycles } from "./pure";
2019-04-18 11:51:07 +02:00
// @ts-ignore: decorator
@global @unsafe
export function __rt_retain(ref: usize): void {
2019-04-18 11:51:07 +02:00
if (ref) increment(changetype<Block>(ref - BLOCK_OVERHEAD));
}
// @ts-ignore: decorator
@global @unsafe
export function __rt_release(ref: usize): void {
2019-04-18 11:51:07 +02:00
if (ref) decrement(changetype<Block>(ref - BLOCK_OVERHEAD));
}
// @ts-ignore: decorator
@global @unsafe
export function __rt_collect(): void {
2019-04-18 11:51:07 +02:00
collectCycles();
}
export { __rt_typeinfo };