Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix GC interface and collect automatically #848

Merged
merged 3 commits into from
Sep 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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