-
-
Notifications
You must be signed in to change notification settings - Fork 696
Expand file tree
/
Copy pathpure.ts
More file actions
305 lines (280 loc) · 10.6 KB
/
Copy pathpure.ts
File metadata and controls
305 lines (280 loc) · 10.6 KB
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import { BLOCK_OVERHEAD, DEBUG, OBJECT, OBJECT_MAXSIZE, OBJECT_OVERHEAD, TOTAL_OVERHEAD } from "rt/common";
import { Block, freeBlock, ROOT } from "rt/tlsf";
import { TypeinfoFlags } from "shared/typeinfo";
import { onincrement, ondecrement } from "./rtrace";
// === A Pure Reference Counting Garbage Collector ===
// see: https://researcher.watson.ibm.com/researcher/files/us-bacon/Bacon03Pure.pdf
// ╒══════════════════════ GC Info structure ══════════════════════╕
// │ 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│
// ├─┼─┴─┴─┼─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┤
// │B│color│ refCount │
// └─┴─────┴───────────────────────────────────────────────────────┘
// B: buffered
// @ts-ignore: decorator
@inline const BUFFERED_MASK: u32 = 1 << ((sizeof<u32>() * 8) - 1);
// @ts-ignore: decorator
@inline const COLOR_BITS = 3;
// @ts-ignore: decorator
@inline const COLOR_SHIFT: u32 = ctz(BUFFERED_MASK) - COLOR_BITS;
// @ts-ignore: decorator
@inline const COLOR_MASK: u32 = ((1 << COLOR_BITS) - 1) << COLOR_SHIFT;
// @ts-ignore: decorator
@inline export const REFCOUNT_MASK: u32 = (1 << COLOR_SHIFT) - 1;
// ╒════════╤═══════════════════ Colors ═══════════════════════════╕
// │ Color │ Meaning │
// ├────────┼──────────────────────────────────────────────────────┤
// │ BLACK │ In use or free │
// │ GRAY │ Possible member of cycle │
// │ WHITE │ Member of garbage cycle │
// │ PURPLE │ Possible root of cycle │
// │ RED │ Candidate cycle undergoing Σ-computation *concurrent │
// │ ORANGE │ Candidate cycle awaiting epoch boundary *concurrent │
// └────────┴──────────────────────────────────────────────────────┘
// Acyclic detection has been decoupled, hence no GREEN.
// @ts-ignore: decorator
@inline const COLOR_BLACK: u32 = 0 << COLOR_SHIFT;
// @ts-ignore: decorator
@inline const COLOR_GRAY: u32 = 1 << COLOR_SHIFT;
// @ts-ignore: decorator
@inline const COLOR_WHITE: u32 = 2 << COLOR_SHIFT;
// @ts-ignore: decorator
@inline const COLOR_PURPLE: u32 = 3 << COLOR_SHIFT;
// @ts-ignore: decorator
// @inline const COLOR_RED: u32 = 4 << COLOR_SHIFT;
// @ts-ignore: decorator
// @inline const COLOR_ORANGE: u32 = 5 << COLOR_SHIFT;
// @ts-ignore: decorator
@inline const VISIT_DECREMENT = 1; // guard 0
// @ts-ignore: decorator
@inline const VISIT_MARKGRAY = 2;
// @ts-ignore: decorator
@inline const VISIT_SCAN = 3;
// @ts-ignore: decorator
@inline const VISIT_SCANBLACK = 4;
// @ts-ignore: decorator
@inline const VISIT_COLLECTWHITE = 5;
// @ts-ignore: decorator
@global @unsafe @lazy
function __visit(ref: usize, cookie: i32): void { // eslint-disable-line @typescript-eslint/no-unused-vars
if (ref < __heap_base) return;
if (isDefined(__GC_ALL_ACYCLIC)) {
if (DEBUG) assert(cookie == VISIT_DECREMENT);
decrement(changetype<OBJECT>(ref - TOTAL_OVERHEAD));
} else {
let s = changetype<OBJECT>(ref - TOTAL_OVERHEAD);
switch (cookie) {
case VISIT_DECREMENT: {
decrement(s);
break;
}
case VISIT_MARKGRAY: {
if (DEBUG) assert((s.gcInfo & REFCOUNT_MASK) > 0);
s.gcInfo = s.gcInfo - 1;
markGray(s);
break;
}
case VISIT_SCAN: {
scan(s);
break;
}
case VISIT_SCANBLACK: {
let info = s.gcInfo;
assert((info & ~REFCOUNT_MASK) == ((info + 1) & ~REFCOUNT_MASK)); // overflow
s.gcInfo = info + 1;
if ((info & COLOR_MASK) != COLOR_BLACK) {
scanBlack(s);
}
break;
}
case VISIT_COLLECTWHITE: {
collectWhite(s);
break;
}
default: if (DEBUG) assert(false);
}
}
}
/** Increments the reference count of the specified block by one.*/
function increment(s: OBJECT): void {
var info = s.gcInfo;
assert((info & ~REFCOUNT_MASK) == ((info + 1) & ~REFCOUNT_MASK)); // overflow
s.gcInfo = info + 1;
if (isDefined(ASC_RTRACE)) onincrement(s);
if (DEBUG) assert(!(s.mmInfo & 1)); // used
}
/** Decrements the reference count of the specified block by one, possibly freeing it. */
// @ts-ignore: decorator
@lazy
function decrement(s: OBJECT): void {
var info = s.gcInfo;
var rc = info & REFCOUNT_MASK;
if (isDefined(ASC_RTRACE)) ondecrement(s);
if (DEBUG) assert(!(s.mmInfo & 1)); // used
if (rc == 1) {
__visit_members(changetype<usize>(s) + TOTAL_OVERHEAD, VISIT_DECREMENT);
if (isDefined(__GC_ALL_ACYCLIC)) {
if (DEBUG) assert(!(info & BUFFERED_MASK));
finalize(s);
} else {
if (!(info & BUFFERED_MASK)) {
finalize(s);
} else {
s.gcInfo = BUFFERED_MASK | COLOR_BLACK | 0;
}
}
} else {
if (DEBUG) assert(rc > 0);
if (isDefined(__GC_ALL_ACYCLIC)) {
s.gcInfo = (info & ~REFCOUNT_MASK) | (rc - 1);
} else {
if (!(__typeinfo(s.rtId) & TypeinfoFlags.ACYCLIC)) {
s.gcInfo = BUFFERED_MASK | COLOR_PURPLE | (rc - 1);
if (!(info & BUFFERED_MASK)) {
appendRoot(s);
}
} else {
s.gcInfo = (info & ~REFCOUNT_MASK) | (rc - 1);
}
}
}
}
/** Finalizes the specified block, giving it back to the memory manager. */
function finalize(s: OBJECT): void {
if (isDefined(__finalize)) {
__finalize(changetype<usize>(s) + TOTAL_OVERHEAD);
}
freeBlock(ROOT, changetype<Block>(s));
}
/** Buffer of possible roots. */
// @ts-ignore: decorator
@lazy var ROOTS: usize;
/** Current absolute offset into the `ROOTS` buffer. */
// @ts-ignore: decorator
@lazy var CUR: usize = 0;
/** Current absolute end offset into the `ROOTS` buffer. */
// @ts-ignore: decorator
@lazy var END: usize = 0;
/** Appends a block to possible roots. */
function appendRoot(s: OBJECT): void {
var cur = CUR;
if (cur >= END) {
growRoots(); // TBD: either that or pick a default and force collection on overflow
cur = CUR;
}
store<OBJECT>(cur, s);
CUR = cur + sizeof<usize>();
}
/** Grows the roots buffer if it ran full. */
function growRoots(): void {
var oldRoots = ROOTS;
var oldSize = CUR - oldRoots;
var newSize = max(oldSize * 2, 64 << alignof<usize>());
var newRoots = __alloc(newSize);
memory.copy(newRoots, oldRoots, oldSize);
if (oldRoots) __free(oldRoots);
ROOTS = newRoots;
CUR = newRoots + oldSize;
END = newRoots + newSize;
}
/** Collects cyclic garbage. */
// @ts-ignore: decorator
@global @unsafe @lazy
export function __collect(): void {
if (isDefined(__GC_ALL_ACYCLIC)) return;
// markRoots
var roots = ROOTS;
var cur = roots;
for (let pos = cur, end = CUR; pos < end; pos += sizeof<usize>()) {
let s = load<OBJECT>(pos);
let info = s.gcInfo;
if ((info & COLOR_MASK) == COLOR_PURPLE && (info & REFCOUNT_MASK) > 0) {
markGray(s);
store<OBJECT>(cur, s);
cur += sizeof<usize>();
} else {
if ((info & COLOR_MASK) == COLOR_BLACK && !(info & REFCOUNT_MASK)) {
finalize(s);
} else {
s.gcInfo = info & ~BUFFERED_MASK;
}
}
}
CUR = cur;
// scanRoots
for (let pos = roots; pos < cur; pos += sizeof<usize>()) {
scan(load<OBJECT>(pos));
}
// collectRoots
for (let pos = roots; pos < cur; pos += sizeof<usize>()) {
let s = load<OBJECT>(pos);
s.gcInfo = s.gcInfo & ~BUFFERED_MASK;
collectWhite(s);
}
CUR = roots;
}
/** Marks a block as gray (possible member of cycle) during the collection phase. */
function markGray(s: OBJECT): void {
var info = s.gcInfo;
if ((info & COLOR_MASK) != COLOR_GRAY) {
s.gcInfo = (info & ~COLOR_MASK) | COLOR_GRAY;
__visit_members(changetype<usize>(s) + TOTAL_OVERHEAD, VISIT_MARKGRAY);
}
}
/** Scans a block during the collection phase, determining whether it is garbage or not. */
function scan(s: OBJECT): void {
var info = s.gcInfo;
if ((info & COLOR_MASK) == COLOR_GRAY) {
if ((info & REFCOUNT_MASK) > 0) {
scanBlack(s);
} else {
s.gcInfo = (info & ~COLOR_MASK) | COLOR_WHITE;
__visit_members(changetype<usize>(s) + TOTAL_OVERHEAD, VISIT_SCAN);
}
}
}
/** Marks a block as black (in use) if it was found to be reachable during the collection phase. */
function scanBlack(s: OBJECT): void {
s.gcInfo = (s.gcInfo & ~COLOR_MASK) | COLOR_BLACK;
__visit_members(changetype<usize>(s) + TOTAL_OVERHEAD, VISIT_SCANBLACK);
}
/** Collects all white (member of a garbage cycle) nodes when completing the collection phase. */
function collectWhite(s: OBJECT): void {
var info = s.gcInfo;
if ((info & COLOR_MASK) == COLOR_WHITE && !(info & BUFFERED_MASK)) {
s.gcInfo = (info & ~COLOR_MASK) | COLOR_BLACK;
__visit_members(changetype<usize>(s) + TOTAL_OVERHEAD, VISIT_COLLECTWHITE);
finalize(s);
}
}
// @ts-ignore: decorator
@global @unsafe
export function __new(size: usize, id: u32): usize {
if (size > OBJECT_MAXSIZE) throw new Error("allocation too large");
var ptr = __alloc(OBJECT_OVERHEAD + size);
var object = changetype<OBJECT>(ptr - BLOCK_OVERHEAD);
object.gcInfo = 0; // RC=0
object.gcInfo2 = 0;
object.rtId = id;
object.rtSize = <u32>size;
return ptr + OBJECT_OVERHEAD;
}
// @ts-ignore: decorator
@global @unsafe
export function __renew(oldPtr: usize, size: usize): usize {
if (size > OBJECT_MAXSIZE) throw new Error("allocation too large");
var newPtr = __realloc(oldPtr - OBJECT_OVERHEAD, OBJECT_OVERHEAD + size);
changetype<OBJECT>(newPtr - BLOCK_OVERHEAD).rtSize = <u32>size;
return newPtr + OBJECT_OVERHEAD;
}
// @ts-ignore: decorator
@global @unsafe
export function __retain(ptr: usize): usize {
if (ptr > __heap_base) increment(changetype<OBJECT>(ptr - TOTAL_OVERHEAD));
return ptr;
}
// @ts-ignore: decorator
@global @unsafe
export function __release(ptr: usize): void {
if (ptr > __heap_base) decrement(changetype<OBJECT>(ptr - TOTAL_OVERHEAD));
}