Skip to content

Commit

Permalink
Add an optional handler for detecting memory.grow() use in the runtime
Browse files Browse the repository at this point in the history
This handler can be used with an import for users not using ESM bindings
to recreate views into memory only when necessary.
  • Loading branch information
CountBleck committed Sep 30, 2023
1 parent 1126ef1 commit a94f551
Show file tree
Hide file tree
Showing 8 changed files with 4,681 additions and 1 deletion.
3 changes: 3 additions & 0 deletions std/assembly/rt/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ declare function __finalize(ptr: usize): void;
declare const ASC_RTRACE: bool;
declare const ASC_PROFILE: bool;

// Memory growth handling
declare function ASC_RT_GROW_HANDLER(): void;

// Incremental GC constants
declare const ASC_GC_GRANULARITY: i32;
declare const ASC_GC_SWEEPFACTOR: i32;
Expand Down
1 change: 1 addition & 0 deletions std/assembly/rt/stub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ function maybeGrowMemory(newOffset: usize): void {
if (memory.grow(pagesWanted) < 0) {
if (memory.grow(pagesNeeded) < 0) unreachable(); // out of memory
}
if (isDefined(ASC_RT_GROW_HANDLER)) ASC_RT_GROW_HANDLER();
}
offset = newOffset;
}
Expand Down
6 changes: 5 additions & 1 deletion std/assembly/rt/tlsf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,7 @@ function growMemory(root: Root, size: usize): void {
if (memory.grow(pagesWanted) < 0) {
if (memory.grow(pagesNeeded) < 0) unreachable();
}
if (isDefined(ASC_RT_GROW_HANDLER)) ASC_RT_GROW_HANDLER();
let pagesAfter = memory.size();
addMemory(root, <usize>pagesBefore << 16, <u64>pagesAfter << 16);
}
Expand All @@ -468,7 +469,10 @@ function initialize(): void {
let rootOffset = (__heap_base + AL_MASK) & ~AL_MASK;
let pagesBefore = memory.size();
let pagesNeeded = <i32>((((rootOffset + ROOT_SIZE) + 0xffff) & ~0xffff) >>> 16);
if (pagesNeeded > pagesBefore && memory.grow(pagesNeeded - pagesBefore) < 0) unreachable();
if (pagesNeeded > pagesBefore) {
if (memory.grow(pagesNeeded - pagesBefore) < 0) unreachable();
if (isDefined(ASC_RT_GROW_HANDLER)) ASC_RT_GROW_HANDLER();
}
let root = changetype<Root>(rootOffset);
root.flMap = 0;
SETTAIL(root, changetype<Block>(0));
Expand Down
Loading

0 comments on commit a94f551

Please sign in to comment.