improve rtrace and move it to lib

This commit is contained in:
dcode
2019-05-23 16:41:26 +02:00
parent fbba76ef2c
commit a49ab7a706
35 changed files with 3435 additions and 3307 deletions

View File

@ -1,6 +1,7 @@
import { DEBUG, BLOCK_OVERHEAD } from "rt/common";
import { Block, freeBlock, ROOT } from "rt/tlsf";
import { TypeinfoFlags } from "shared/typeinfo";
import { onincrement, ondecrement } from "./rtrace";
/////////////////////////// A Pure Reference Counting Garbage Collector ///////////////////////////
// see: https://researcher.watson.ibm.com/researcher/files/us-bacon/Bacon03Pure.pdf
@ -102,7 +103,7 @@ function increment(s: Block): void {
var info = s.gcInfo;
assert((info & ~REFCOUNT_MASK) == ((info + 1) & ~REFCOUNT_MASK)); // overflow
s.gcInfo = info + 1;
if (isDefined(ASC_RTRACE)) onIncrement(s);
if (isDefined(ASC_RTRACE)) onincrement(s);
if (DEBUG) assert(!(s.mmInfo & 1)); // used
}
@ -110,7 +111,7 @@ function increment(s: Block): void {
function decrement(s: Block): void {
var info = s.gcInfo;
var rc = info & REFCOUNT_MASK;
if (isDefined(ASC_RTRACE)) onDecrement(s);
if (isDefined(ASC_RTRACE)) ondecrement(s);
if (DEBUG) assert(!(s.mmInfo & 1)); // used
if (rc == 1) {
__visit_members(changetype<usize>(s) + BLOCK_OVERHEAD, VISIT_DECREMENT);
@ -272,11 +273,3 @@ export function __skippedRelease(oldRef: usize, newRef: usize): usize {
if (oldRef > HEAP_BASE) decrement(changetype<Block>(oldRef - BLOCK_OVERHEAD));
return newRef;
}
// @ts-ignore: decorator
@external("rtrace", "retain")
declare function onIncrement(s: Block): void;
// @ts-ignore: decorator
@external("rtrace", "release")
declare function onDecrement(s: Block): void;

View File

@ -0,0 +1,6 @@
import { BLOCK } from "./common";
export declare function onalloc(s: BLOCK): void;
export declare function onincrement(s: BLOCK): void;
export declare function ondecrement(s: BLOCK): void;
export declare function onfree(s: BLOCK): void;

View File

@ -1,4 +1,5 @@
import { AL_BITS, AL_MASK, DEBUG, BLOCK, BLOCK_OVERHEAD, BLOCK_MAXSIZE } from "rt/common";
import { onfree, onalloc } from "./rtrace";
/////////////////////// The TLSF (Two-Level Segregate Fit) memory allocator ///////////////////////
// see: http://www.gii.upv.es/tlsf/
@ -481,6 +482,7 @@ export function allocateBlock(root: Root, size: usize): Block {
block.rtSize = size;
removeBlock(root, <Block>block);
prepareBlock(root, <Block>block, payloadSize);
if (isDefined(ASC_RTRACE)) onalloc(<Block>block);
return <Block>block;
}
@ -520,7 +522,6 @@ export function reallocateBlock(root: Root, block: Block, size: usize): Block {
memory.copy(changetype<usize>(newBlock) + BLOCK_OVERHEAD, changetype<usize>(block) + BLOCK_OVERHEAD, size);
block.mmInfo = blockInfo | FREE;
insertBlock(root, block);
if (isDefined(ASC_RTRACE)) onFree(block);
return newBlock;
}
@ -530,7 +531,7 @@ export function freeBlock(root: Root, block: Block): void {
assert(!(blockInfo & FREE)); // must be used (user might call through to this)
block.mmInfo = blockInfo | FREE;
insertBlock(root, block);
if (isDefined(ASC_RTRACE)) onFree(block);
if (isDefined(ASC_RTRACE)) onfree(block);
}
// @ts-ignore: decorator
@ -561,7 +562,3 @@ export function __free(ref: usize): void {
assert(ref != 0 && !(ref & AL_MASK)); // must exist and be aligned
freeBlock(ROOT, changetype<Block>(ref - BLOCK_OVERHEAD));
}
// @ts-ignore: decorator
@external("rtrace", "free")
declare function onFree(s: Block): void;