forked from AssemblyScript/assemblyscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcms.ts
254 lines (222 loc) Β· 9.24 KB
/
tcms.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import { BLOCK, BLOCK_OVERHEAD, OBJECT_OVERHEAD, OBJECT_MAXSIZE, TOTAL_OVERHEAD, DEBUG, TRACE, RTRACE } from "./common";
import { onvisit, oncollect } from "./rtrace";
import { E_ALLOCATION_TOO_LARGE, E_ALREADY_PINNED, E_NOT_PINNED } from "../util/error";
// === TCMS: A Two-Color Mark & Sweep garbage collector ===
// βββββββββββββββ€ββββββββββββββ Colors ββββββββββββββββββββββββββββ
// β Color β Meaning β
// βββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββββββββ€
// β WHITE* β Unreachable β
// β BLACK* β Reachable β
// β TRANSPARENT β Manually pinned (always reachable) β
// βββββββββββββββ΄ββββββββββββββββββββββββββββββββββββββββββββββββββ
// * flipped between cycles
// @ts-ignore: decorator
@lazy let white = 0;
// @ts-ignore: decorator
@inline const transparent = 3;
// @ts-ignore: decorator
@inline const COLOR_MASK = 3;
/** Size in memory of all objects currently managed by the GC. */
// @ts-ignore: decorator
@lazy let total: usize = 0;
// @ts-ignore: decorator
@lazy let fromSpace = initLazy(changetype<Object>(memory.data(offsetof<Object>())));
// @ts-ignore: decorator
@lazy let toSpace = initLazy(changetype<Object>(memory.data(offsetof<Object>())));
// @ts-ignore: decorator
@lazy let pinSpace = initLazy(changetype<Object>(memory.data(offsetof<Object>())));
function initLazy(space: Object): Object {
space.nextWithColor = changetype<usize>(space);
space.prev = space;
return space;
}
/** Visit cookie indicating scanning of an object. */
// @ts-ignore: decorator
@inline const VISIT_SCAN = 0;
// ββββββββββββββββ Managed object layout (32-bit) βββββββββββββββββ
// 3 2 1
// 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 bits
// βββ΄ββ΄ββ΄ββ΄ββ΄ββ΄ββ΄ββ΄ββ΄ββ΄ββ΄ββ΄ββ΄ββ΄ββ΄ββ΄ββ΄ββ΄ββ΄ββ΄ββ΄ββ΄ββ΄ββ΄ββ΄ββ΄ββ΄ββ΄ββ΄ββ΄ββ΄ββ€
// β Memory manager block β
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€ββββ‘
// β next β C β = nextWithColor
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ΄ββββ€
// β prev β
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
// β rtId β
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
// β rtSize β
// β>ptrββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ‘
// β ... β
// C: color
/** Represents a managed object in memory, consisting of a header followed by the object's data. */
@unmanaged class Object extends BLOCK {
/** Pointer to the next object with color flags stored in the alignment bits. */
nextWithColor: usize; // *u32
/** Pointer to the previous object. */
prev: Object; // *u32
/** Runtime id. */
rtId: u32;
/** Runtime size. */
rtSize: u32;
/** Gets the pointer to the next object. */
get next(): Object {
return changetype<Object>(this.nextWithColor & ~COLOR_MASK);
}
/** Sets the pointer to the next object. */
set next(obj: Object) {
this.nextWithColor = changetype<usize>(obj) | (this.nextWithColor & COLOR_MASK);
}
/** Gets this object's color. */
get color(): i32 {
return i32(this.nextWithColor & COLOR_MASK);
}
/** Sets this object's color. */
set color(color: i32) {
this.nextWithColor = (this.nextWithColor & ~COLOR_MASK) | color;
}
/** Gets the size of this object in memory. */
get size(): usize {
return BLOCK_OVERHEAD + (this.mmInfo & ~3);
}
/** Unlinks this object from its list. */
unlink(): void {
let next = this.next;
if (next == null) {
if (DEBUG) assert(this.prev == null && changetype<usize>(this) < __heap_base);
return; // static data not yet linked
}
let prev = this.prev;
if (DEBUG) assert(prev);
next.prev = prev;
prev.next = next;
}
/** Links this object to the specified list, with the given color. */
linkTo(list: Object, withColor: i32): void {
let prev = list.prev;
this.nextWithColor = changetype<usize>(list) | withColor;
this.prev = prev;
prev.next = this;
list.prev = this;
}
}
// Garbage collector interface
// @ts-ignore: decorator
@global @unsafe
export function __new(size: usize, id: i32): usize {
if (size > OBJECT_MAXSIZE) throw new Error(E_ALLOCATION_TOO_LARGE);
let obj = changetype<Object>(__alloc(OBJECT_OVERHEAD + size) - BLOCK_OVERHEAD);
obj.rtId = id;
obj.rtSize = <u32>size;
obj.linkTo(fromSpace, white);
total += obj.size;
return changetype<usize>(obj) + TOTAL_OVERHEAD;
}
// @ts-ignore: decorator
@global @unsafe
export function __renew(oldPtr: usize, size: usize): usize {
let oldObj = changetype<Object>(oldPtr - TOTAL_OVERHEAD);
if (oldPtr < __heap_base) { // move to heap for simplicity
let newPtr = __new(size, oldObj.rtId);
memory.copy(newPtr, oldPtr, min(size, oldObj.rtSize));
return newPtr;
}
if (size > OBJECT_MAXSIZE) throw new Error(E_ALLOCATION_TOO_LARGE);
total -= oldObj.size;
let newPtr = __realloc(oldPtr - OBJECT_OVERHEAD, OBJECT_OVERHEAD + size) + OBJECT_OVERHEAD;
let newObj = changetype<Object>(newPtr - TOTAL_OVERHEAD);
newObj.rtSize = <u32>size;
// Replace with new object
newObj.next.prev = newObj;
newObj.prev.next = newObj;
total += newObj.size;
return newPtr;
}
// @ts-ignore: decorator
@global @unsafe
export function __link(parentPtr: usize, childPtr: usize, expectMultiple: bool): void {
// nop
}
// @ts-ignore: decorator
@global @unsafe
export function __visit(ptr: usize, cookie: i32): void {
if (!ptr) return;
let obj = changetype<Object>(ptr - TOTAL_OVERHEAD);
if (RTRACE) if (!onvisit(obj)) return;
if (obj.color == white) {
obj.unlink(); // from fromSpace
obj.linkTo(toSpace, i32(!white));
}
}
// @ts-ignore: decorator
@global @unsafe
export function __pin(ptr: usize): usize {
if (ptr) {
let obj = changetype<Object>(ptr - TOTAL_OVERHEAD);
if (obj.color == transparent) {
throw new Error(E_ALREADY_PINNED);
}
obj.unlink(); // from fromSpace
obj.linkTo(pinSpace, transparent);
}
return ptr;
}
// @ts-ignore: decorator
@global @unsafe
export function __unpin(ptr: usize): void {
if (!ptr) return;
let obj = changetype<Object>(ptr - TOTAL_OVERHEAD);
if (obj.color != transparent) {
throw new Error(E_NOT_PINNED);
}
obj.unlink(); // from pinSpace
obj.linkTo(fromSpace, white);
}
// @ts-ignore: decorator
@global @unsafe
export function __collect(): void {
if (TRACE) trace("GC at", 1, total);
// Mark roots (add to toSpace)
__visit_globals(VISIT_SCAN);
// Mark direct members of pinned objects (add to toSpace)
let pn = pinSpace;
let iter = pn.next;
while (iter != pn) {
if (DEBUG) assert(iter.color == transparent);
__visit_members(changetype<usize>(iter) + TOTAL_OVERHEAD, VISIT_SCAN);
iter = iter.next;
}
// Mark what's reachable from toSpace
let black = i32(!white);
let to = toSpace;
iter = to.next;
while (iter != to) {
if (DEBUG) assert(iter.color == black);
__visit_members(changetype<usize>(iter) + TOTAL_OVERHEAD, VISIT_SCAN);
iter = iter.next;
}
// Sweep what's left in fromSpace
let from = fromSpace;
iter = from.next;
while (iter != from) {
if (DEBUG) assert(iter.color == white);
let newNext = iter.next;
if (changetype<usize>(iter) < __heap_base) {
iter.nextWithColor = 0; // may become linked again
iter.prev = changetype<Object>(0);
} else {
total -= iter.size;
if (isDefined(__finalize)) __finalize(changetype<usize>(iter) + TOTAL_OVERHEAD);
__free(changetype<usize>(iter) + BLOCK_OVERHEAD);
}
iter = newNext;
}
from.nextWithColor = changetype<usize>(from);
from.prev = from;
// Flip spaces and colors
fromSpace = to;
toSpace = from;
white = black;
if (TRACE) trace("GC done at", 1, total);
if (RTRACE) oncollect(total);
}