Skip to content

Commit 8968108

Browse files
committed
Make memory allocators pluggable
1 parent 78debee commit 8968108

File tree

13 files changed

+16
-41
lines changed

13 files changed

+16
-41
lines changed

bin/asc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ args._.forEach(filename => {
156156
while ((nextPath = parser.nextFile()) != null) {
157157
try {
158158
readTime += measure(() => {
159-
if (nextPath.starsWith("std:"))
159+
if (nextPath.startsWith("std:"))
160160
nextText = fs.readFileSync(stdlibDir + "/" + nextPath.substring(4) + ".ts", { encoding: "utf8" });
161161
else
162162
nextText = fs.readFileSync(nextPath + ".ts", { encoding: "utf8" });

src/util/path.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ export function normalize(path: string, trimExtension: bool = false, separator:
8282

8383
/** Resolves the specified path to a normalized path relative to the specified origin. */
8484
export function resolve(normalizedPath: string, normalizedOrigin: string, separator: CharCode = CharCode.SLASH): string {
85+
if (normalizedPath.startsWith("std:"))
86+
return normalizedPath;
8587
return normalize(dirname(normalizedOrigin, separator) + String.fromCharCode(separator) + normalizedPath);
8688
}
8789

std/assembly/memory.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import "std:memory/arena";
2-
31
function copy_memory(dest: usize, src: usize, n: usize): void {
42
// based on musl's implementation of memcpy
53
// not a future instruction and sufficiently covered by the upcoming move_memory intrinsic

tests/compiler/std/array.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import "std:memory/arena";
2+
13
var arr = changetype<i32[]>(allocate_memory(sizeof<usize>() + 2 * sizeof<i32>()));
24

35
assert(arr.length == 0);

tests/compiler/std/array.wast

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
(type $iii (func (param i32 i32) (result i32)))
88
(type $iiii (func (param i32 i32 i32) (result i32)))
99
(type $v (func))
10-
(global $std:memory/arena/HEAP_OFFSET (mut i32) (i32.const 0))
1110
(global $std:memory/arena/ALIGN_LOG2 i32 (i32.const 3))
1211
(global $std:memory/arena/ALIGN_SIZE i32 (i32.const 8))
1312
(global $std:memory/arena/ALIGN_MASK i32 (i32.const 7))
13+
(global $std:memory/arena/HEAP_OFFSET (mut i32) (i32.const 0))
1414
(global $std/array/arr (mut i32) (i32.const 0))
1515
(global $std/array/i (mut i32) (i32.const 0))
1616
(global $HEAP_BASE i32 (i32.const 4))

tests/compiler/std/carray.wast

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -285,16 +285,6 @@
285285
FUNCTION_PROTOTYPE: std:string/parseFloat
286286
FUNCTION_PROTOTYPE: parseFloat
287287
GLOBAL: std/carray/arr
288-
GLOBAL: std:memory/arena/ALIGN_LOG2
289-
GLOBAL: std:memory/arena/ALIGN_SIZE
290-
GLOBAL: std:memory/arena/ALIGN_MASK
291-
GLOBAL: std:memory/arena/HEAP_OFFSET
292-
FUNCTION_PROTOTYPE: std:memory/arena/allocate_memory
293-
FUNCTION_PROTOTYPE: allocate_memory
294-
FUNCTION_PROTOTYPE: std:memory/arena/free_memory
295-
FUNCTION_PROTOTYPE: free_memory
296-
FUNCTION_PROTOTYPE: std:memory/arena/clear_memory
297-
FUNCTION_PROTOTYPE: clear_memory
298288
[program.exports]
299289
CLASS_PROTOTYPE: std:array/Array
300290
CLASS_PROTOTYPE: Array
@@ -322,10 +312,4 @@
322312
FUNCTION_PROTOTYPE: std:string/parseInt
323313
FUNCTION_PROTOTYPE: parseFloat
324314
FUNCTION_PROTOTYPE: std:string/parseFloat
325-
FUNCTION_PROTOTYPE: allocate_memory
326-
FUNCTION_PROTOTYPE: std:memory/arena/allocate_memory
327-
FUNCTION_PROTOTYPE: free_memory
328-
FUNCTION_PROTOTYPE: std:memory/arena/free_memory
329-
FUNCTION_PROTOTYPE: clear_memory
330-
FUNCTION_PROTOTYPE: std:memory/arena/clear_memory
331315
;)

tests/compiler/std/heap.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import "std:memory/arena";
2+
13
const size: usize = 42;
24
let ptr1: usize = allocate_memory(size);
35
let ptr2: usize = allocate_memory(size);
@@ -20,7 +22,6 @@ assert(compare_memory(ptr1, ptr2, size) == 0);
2022
free_memory(ptr1);
2123
free_memory(ptr2);
2224

23-
// arena
2425
clear_memory();
2526
ptr1 = allocate_memory(size);
2627
assert(ptr1 == HEAP_BASE);

tests/compiler/std/heap.wast

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
(type $iiii (func (param i32 i32 i32) (result i32)))
66
(type $iv (func (param i32)))
77
(type $v (func))
8-
(global $std/heap/size i32 (i32.const 42))
9-
(global $std:memory/arena/HEAP_OFFSET (mut i32) (i32.const 0))
108
(global $std:memory/arena/ALIGN_LOG2 i32 (i32.const 3))
119
(global $std:memory/arena/ALIGN_SIZE i32 (i32.const 8))
1210
(global $std:memory/arena/ALIGN_MASK i32 (i32.const 7))
11+
(global $std:memory/arena/HEAP_OFFSET (mut i32) (i32.const 0))
12+
(global $std/heap/size i32 (i32.const 42))
1313
(global $std/heap/ptr1 (mut i32) (i32.const 0))
1414
(global $std/heap/ptr2 (mut i32) (i32.const 0))
1515
(global $std/heap/i (mut i32) (i32.const 0))

tests/compiler/std/new.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import "std:memory/arena";
2+
13
class AClass {
24
static aStaticField: i32 = 0;
35
aField: i32 = 1;

tests/compiler/std/new.wast

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
(type $ii (func (param i32) (result i32)))
44
(type $ifv (func (param i32 f32)))
55
(type $v (func))
6-
(global $std:memory/arena/HEAP_OFFSET (mut i32) (i32.const 0))
76
(global $std:memory/arena/ALIGN_LOG2 i32 (i32.const 3))
87
(global $std:memory/arena/ALIGN_SIZE i32 (i32.const 8))
98
(global $std:memory/arena/ALIGN_MASK i32 (i32.const 7))
9+
(global $std:memory/arena/HEAP_OFFSET (mut i32) (i32.const 0))
1010
(global $std/new/aClass (mut i32) (i32.const 0))
1111
(global $HEAP_BASE i32 (i32.const 4))
1212
(memory $0 1)

tests/compiler/std/set.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import "std:memory/arena";
2+
13
// note that this doesn't test a real set implementation yet, see std/assembly/set.ts
24

35
var set = changetype<Set<i32>>(allocate_memory(sizeof<usize>() + 2 * sizeof<i32>()));

tests/compiler/std/set.wast

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
(type $iv (func (param i32)))
66
(type $iii (func (param i32 i32) (result i32)))
77
(type $v (func))
8-
(global $std:memory/arena/HEAP_OFFSET (mut i32) (i32.const 0))
98
(global $std:memory/arena/ALIGN_LOG2 i32 (i32.const 3))
109
(global $std:memory/arena/ALIGN_SIZE i32 (i32.const 8))
1110
(global $std:memory/arena/ALIGN_MASK i32 (i32.const 7))
11+
(global $std:memory/arena/HEAP_OFFSET (mut i32) (i32.const 0))
1212
(global $std/set/set (mut i32) (i32.const 0))
1313
(global $HEAP_BASE i32 (i32.const 4))
1414
(memory $0 1)

tests/compiler/std/string.wast

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -636,16 +636,6 @@
636636
FUNCTION_PROTOTYPE: parseFloat
637637
GLOBAL: std/string/str
638638
FUNCTION_PROTOTYPE: std/string/getString
639-
GLOBAL: std:memory/arena/ALIGN_LOG2
640-
GLOBAL: std:memory/arena/ALIGN_SIZE
641-
GLOBAL: std:memory/arena/ALIGN_MASK
642-
GLOBAL: std:memory/arena/HEAP_OFFSET
643-
FUNCTION_PROTOTYPE: std:memory/arena/allocate_memory
644-
FUNCTION_PROTOTYPE: allocate_memory
645-
FUNCTION_PROTOTYPE: std:memory/arena/free_memory
646-
FUNCTION_PROTOTYPE: free_memory
647-
FUNCTION_PROTOTYPE: std:memory/arena/clear_memory
648-
FUNCTION_PROTOTYPE: clear_memory
649639
[program.exports]
650640
CLASS_PROTOTYPE: std:array/Array
651641
CLASS_PROTOTYPE: Array
@@ -674,10 +664,4 @@
674664
FUNCTION_PROTOTYPE: parseFloat
675665
FUNCTION_PROTOTYPE: std:string/parseFloat
676666
FUNCTION_PROTOTYPE: std/string/getString
677-
FUNCTION_PROTOTYPE: allocate_memory
678-
FUNCTION_PROTOTYPE: std:memory/arena/allocate_memory
679-
FUNCTION_PROTOTYPE: free_memory
680-
FUNCTION_PROTOTYPE: std:memory/arena/free_memory
681-
FUNCTION_PROTOTYPE: clear_memory
682-
FUNCTION_PROTOTYPE: std:memory/arena/clear_memory
683667
;)

0 commit comments

Comments
 (0)