Skip to content

Commit

Permalink
Fix GC interface and collect automatically (#848)
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO authored Sep 20, 2019
1 parent 5b51057 commit bc6a944
Show file tree
Hide file tree
Showing 39 changed files with 15,152 additions and 10,146 deletions.
13 changes: 13 additions & 0 deletions std/assembly/gc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/// <reference path="./rt/index.d.ts" />

/** Garbage collector interface. */
export namespace gc {

/** Can be set to `false` to disable automatic collection. Defaults to `true`. */
export var auto: bool = true;

/** Performs a full garbage collection cycle. */
export function collect(): void {
__collect();
}
}
10 changes: 3 additions & 7 deletions std/assembly/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1032,16 +1032,12 @@ declare namespace memory {
export function compare(vl: usize, vr: usize, n: usize): i32;
}

/** Garbage collector operations. */
/** Garbage collector interface. */
declare namespace gc {
/** Whether the garbage collector interface is implemented. */
export const implemented: bool;
/** Can be set to `false` to disable automatic collection. Defaults to `true`. */
export var auto: bool;
/** Performs a full garbage collection cycle. */
export function collect(): void;
/** Retains a reference, making sure that it doesn't become collected. */
export function retain(ref: usize): void;
/** Releases a reference, allowing it to become collected. */
export function release(ref: usize): void;
}

/** Table operations. */
Expand Down
23 changes: 20 additions & 3 deletions std/assembly/rt/tlsf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -477,14 +477,31 @@ export function initializeRoot(): void {
ROOT = root;
}

// @ts-ignore: decorator
@lazy
var collectLock: bool = false;

/** Allocates a block of the specified size. */
export function allocateBlock(root: Root, size: usize): Block {
if (DEBUG) assert(!collectLock); // must not allocate while collecting
var payloadSize = prepareSize(size);
var block = searchBlock(root, payloadSize);
if (!block) {
growMemory(root, payloadSize);
block = <Block>searchBlock(root, payloadSize);
if (DEBUG) assert(block); // must be found now
if (gc.auto) {
if (DEBUG) collectLock = true;
__collect();
if (DEBUG) collectLock = false;
block = searchBlock(root, payloadSize);
if (!block) {
growMemory(root, payloadSize);
block = <Block>searchBlock(root, payloadSize);
if (DEBUG) assert(block); // must be found now
}
} else {
growMemory(root, payloadSize);
block = <Block>searchBlock(root, payloadSize);
if (DEBUG) assert(block); // must be found now
}
}
if (DEBUG) assert((block.mmInfo & ~TAGS_MASK) >= payloadSize); // must fit
block.gcInfo = 0; // RC=0
Expand Down
Loading

0 comments on commit bc6a944

Please sign in to comment.