Skip to content

Commit 219ee81

Browse files
committed
Rename heap to memory; Allocator strategies
1 parent 1b0ed61 commit 219ee81

15 files changed

+4049
-2590
lines changed

bin/asc.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ function checkDiagnostics(parser) {
105105
}
106106

107107
// Include standard library
108+
var stdlibDir = path.join(__dirname, "..", "std", "assembly");
108109
if (!args.noLib) {
109-
var stdlibDir = path.join(__dirname, "..", "std", "assembly");
110110
var notIoTime = 0;
111111
readTime += measure(() => {
112112
glob.sync("*.ts", { cwd: stdlibDir }).forEach(file => {
@@ -156,7 +156,10 @@ args._.forEach(filename => {
156156
while ((nextPath = parser.nextFile()) != null) {
157157
try {
158158
readTime += measure(() => {
159-
nextText = fs.readFileSync(nextPath + ".ts", { encoding: "utf8" });
159+
if (nextPath.starsWith("std:"))
160+
nextText = fs.readFileSync(stdlibDir + "/" + nextPath.substring(4) + ".ts", { encoding: "utf8" });
161+
else
162+
nextText = fs.readFileSync(nextPath + ".ts", { encoding: "utf8" });
160163
});
161164
++readCount;
162165
} catch (e) {

std/assembly/heap.ts renamed to std/assembly/memory.ts

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,4 @@
1-
const ALIGN_LOG2: usize = 3;
2-
const ALIGN_SIZE: usize = 1 << ALIGN_LOG2;
3-
const ALIGN_MASK: usize = ALIGN_SIZE - 1;
4-
5-
var HEAP_OFFSET: usize = HEAP_BASE; // HEAP_BASE is a constant generated by the compiler
6-
7-
export function allocate_memory(size: usize): usize {
8-
if (!size) return 0;
9-
var len: i32 = current_memory();
10-
if (HEAP_OFFSET + size > <usize>len << 16)
11-
if(grow_memory(max<i32>(<i32>ceil<f64>(<f64>size / 65536), len * 2 - len)) < 0)
12-
unreachable();
13-
var ptr: usize = HEAP_OFFSET;
14-
if ((HEAP_OFFSET += size) & ALIGN_MASK) // align next offset
15-
HEAP_OFFSET = (HEAP_OFFSET | ALIGN_MASK) + 1;
16-
return ptr;
17-
}
18-
19-
export function free_memory(ptr: usize): void {
20-
// just a big chunk of non-disposable memory for now
21-
}
1+
import "std:memory/arena";
222

233
function copy_memory(dest: usize, src: usize, n: usize): void {
244
// based on musl's implementation of memcpy

std/assembly/memory/arena.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// An simple arena allocator that provides a `clear_memory` function to reset
2+
// everything to the initial state. A user must have to make sure that there
3+
// a no more references to cleared memory.
4+
5+
const ALIGN_LOG2: usize = 3;
6+
const ALIGN_SIZE: usize = 1 << ALIGN_LOG2;
7+
const ALIGN_MASK: usize = ALIGN_SIZE - 1;
8+
9+
var HEAP_OFFSET: usize = HEAP_BASE; // generated by the compiler
10+
11+
export function allocate_memory(size: usize): usize {
12+
if (!size) return 0;
13+
var len: i32 = current_memory();
14+
if (HEAP_OFFSET + size > <usize>len << 16)
15+
if(grow_memory(max<i32>(<i32>ceil<f64>(<f64>size / 65536), len * 2 - len)) < 0)
16+
unreachable();
17+
var ptr: usize = HEAP_OFFSET;
18+
if ((HEAP_OFFSET += size) & ALIGN_MASK) // align next offset
19+
HEAP_OFFSET = (HEAP_OFFSET | ALIGN_MASK) + 1;
20+
set_memory(ptr, 0, size);
21+
return ptr;
22+
}
23+
24+
export function free_memory(ptr: usize): void {
25+
// nop
26+
}
27+
28+
export function clear_memory(): void {
29+
HEAP_OFFSET = HEAP_BASE;
30+
}

tests/compiler.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ var ElementKind = require("../src/program").ElementKind;
1515
var isCreate = process.argv[2] === "--create";
1616
var filter = process.argv.length > 2 && !isCreate ? "**/" + process.argv[2] + ".ts" : "**/*.ts";
1717

18-
var stdFiles = glob.sync("*.ts", { cwd: __dirname + "/../std/assembly" });
18+
var stdDir = __dirname + "/../std/assembly";
19+
var stdFiles = glob.sync("*.ts", { cwd: stdDir });
1920

2021
var success = 0;
2122
var failures = 0;
@@ -42,7 +43,10 @@ glob.sync(filter, { cwd: __dirname + "/compiler" }).forEach(filename => {
4243
while ((nextFile = parser.nextFile()) !== null) {
4344
var nextSourceText;
4445
try {
45-
nextSourceText = fs.readFileSync(path.join(__dirname, "compiler", nextFile + ".ts"), { encoding: "utf8" });
46+
if (nextFile.startsWith("std:"))
47+
nextSourceText = fs.readFileSync(path.join(stdDir, nextFile.substring(4) + ".ts"), { encoding: "utf8" });
48+
else
49+
nextSourceText = fs.readFileSync(path.join(__dirname, "compiler", nextFile + ".ts"), { encoding: "utf8" });
4650
} catch (e) {
4751
nextSourceText = fs.readFileSync(path.join(__dirname, "compiler", nextFile, "index.ts"), { encoding: "utf8" });
4852
}

0 commit comments

Comments
 (0)