- Notifications
You must be signed in to change notification settings - Fork 670
/
Copy pathmemory.ts
123 lines (102 loc) · 3.46 KB
/
memory.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import{memcmp,memmove,memset}from"./util/memory";
import{E_NOTIMPLEMENTED}from"./util/error";
/** Memory manager interface. */
exportnamespacememory{
/** Gets the size of the memory in pages. */
// @ts-ignore: decorator
@builtin
exportdeclarefunctionsize(): i32;
/** Grows the memory by the given size in pages and returns the previous size in pages. */
// @ts-ignore: decorator
@unsafe @builtin
exportdeclarefunctiongrow(pages: i32): i32;
/** Fills a section in memory with the specified byte value. */
// @ts-ignore: decorator
@unsafe @builtin
exportfunctionfill(dst: usize,c: u8,n: usize): void{
memset(dst,c,n);// fallback if "bulk-memory" isn't enabled
}
/** Copies a section of memory to another. Has move semantics. */
// @ts-ignore: decorator
@unsafe @builtin
exportfunctioncopy(dst: usize,src: usize,n: usize): void{
memmove(dst,src,n);// fallback if "bulk-memory" isn't enabled
}
exportnamespaceatomic{
// @ts-ignore: decorator
@unsafe @builtin
exportdeclarefunctionwait32(ptr: usize,expected: i32,timeout: i64): AtomicWaitResult;
// @ts-ignore: decorator
@unsafe @builtin
exportdeclarefunctionwait64(ptr: usize,expected: i64,timeout: i64): AtomicWaitResult;
}
/** Initializes a memory segment. */
// @ts-ignore: decorator
@unsafe
exportfunctioninit(segmentIndex: u32,srcOffset: usize,dstOffset: usize,n: usize): void{
thrownewError(E_NOTIMPLEMENTED);
}
/** Drops a memory segment. */
// @ts-ignore: decorator
@unsafe
exportfunctiondrop(segmentIndex: u32): void{
thrownewError(E_NOTIMPLEMENTED);
}
/** Repeats a section of memory at a specific address. */
// @ts-ignore: decorator
@unsafe
exportfunctionrepeat(dst: usize,src: usize,srcLength: usize,count: usize): void{
letindex: usize=0;
lettotal=srcLength*count;
while(index<total){
memory.copy(dst+index,src,srcLength);
index+=srcLength;
}
}
/** Compares a section of memory to another. */
// @ts-ignore: decorator
@inline
exportfunctioncompare(vl: usize,vr: usize,n: usize): i32{
returnmemcmp(vl,vr,n);
}
/** Gets a pointer to a static chunk of memory of the given size. */
// @ts-ignore: decorator
@builtin
exportdeclarefunctiondata<T>(size: T,align?: i32): usize;
}
// @ts-ignore: decorator
@builtin
exportdeclareconst__data_end: usize;
// @ts-ignore: decorator
@builtin
exportdeclarelet__stack_pointer: usize;
// @ts-ignore: decorator
@builtin
exportdeclareconst__heap_base: usize;
/** Heap memory interface. */
exportnamespaceheap{
/** Allocates a chunk of memory of at least the specified size. */
// @ts-ignore: decorator
@unsafeexportfunctionalloc(size: usize): usize{
return__alloc(size);
}
/** Reallocates a chunk of memory to have at least the specified size. */
// @ts-ignore: decorator
@unsafeexportfunctionrealloc(ptr: usize,size: usize): usize{
return__realloc(ptr,size);
}
/** Frees a chunk of memory. Does hardly anything (most recent block only) with the stub runtime. */
// @ts-ignore: decorator
@unsafeexportfunctionfree(ptr: usize): void{
__free(ptr);
}
/** Dangerously resets the entire heap. Specific to the stub runtime. */
// @ts-ignore: decorator
@unsafeexportfunctionreset(): void{
if(isDefined(__reset)){
__reset();
}else{
thrownewError(E_NOTIMPLEMENTED);
}
}
}