-
Notifications
You must be signed in to change notification settings - Fork 191
/
gc.ts
115 lines (91 loc) · 3.85 KB
/
gc.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
import Versioning from "versioning";
/** Garbage collector utility functions. */
class Il2CppGC {
protected constructor() {}
/** Gets the heap size in bytes. */
static get heapSize(): Int64 {
return Il2Cpp.Api._gcGetHeapSize();
}
/** Determines whether the garbage collector is disabled. */
static get isEnabled(): boolean {
return !Il2Cpp.Api._gcIsDisabled();
}
/** Determines whether the garbage collector is incremental. */
static get isIncremental(): boolean {
return !!Il2Cpp.Api._gcIsIncremental();
}
/** Gets the number of nanoseconds the garbage collector can spend in a collection step. */
static get maxTimeSlice(): Int64 {
return Il2Cpp.Api._gcGetMaxTimeSlice();
}
/** Gets the used heap size in bytes. */
static get usedHeapSize(): Int64 {
return Il2Cpp.Api._gcGetUsedSize();
}
/** Enables or disables the garbage collector. */
static set isEnabled(value: boolean) {
value ? Il2Cpp.Api._gcEnable() : Il2Cpp.Api._gcDisable();
}
/** Sets the number of nanoseconds the garbage collector can spend in a collection step. */
static set maxTimeSlice(nanoseconds: number | Int64) {
Il2Cpp.Api._gcSetMaxTimeSlice(nanoseconds);
}
/** Returns the heap allocated objects of the specified class. This variant reads GC descriptors. */
static choose(klass: Il2Cpp.Class): Il2Cpp.Object[] {
const matches: Il2Cpp.Object[] = [];
const callback = (objects: NativePointer, size: number, _: NativePointer) => {
for (let i = 0; i < size; i++) {
matches.push(new Il2Cpp.Object(objects.add(i * Process.pointerSize).readPointer()));
}
};
const chooseCallback = new NativeCallback(callback, "void", ["pointer", "int", "pointer"]);
if (Versioning.gte(Il2Cpp.unityVersion, "2021.2.0")) {
const realloc = (handle: NativePointer, size: UInt64) => {
if (!handle.isNull() && size.compare(0) == 0) {
Il2Cpp.free(handle);
return NULL;
} else {
return Il2Cpp.alloc(size);
}
};
const reallocCallback = new NativeCallback(realloc, "pointer", ["pointer", "size_t", "pointer"]);
const state = Il2Cpp.Api._livenessAllocateStruct(klass.handle, 0, chooseCallback, NULL, reallocCallback);
Il2Cpp.Api._livenessCalculationFromStatics(state);
Il2Cpp.Api._livenessFinalize(state);
Il2Cpp.Api._livenessFreeStruct(state);
} else {
const onWorld = new NativeCallback(() => {}, "void", []);
const state = Il2Cpp.Api._livenessCalculationBegin(klass.handle, 0, chooseCallback, NULL, onWorld, onWorld);
Il2Cpp.Api._livenessCalculationFromStatics(state);
Il2Cpp.Api._livenessCalculationEnd(state);
}
return matches;
}
/** Forces a garbage collection of the specified generation. */
static collect(generation: 0 | 1 | 2): void {
Il2Cpp.Api._gcCollect(generation < 0 ? 0 : generation > 2 ? 2 : generation);
}
/** Forces a garbage collection. */
static collectALittle(): void {
Il2Cpp.Api._gcCollectALittle();
}
/** Resumes all the previously stopped threads. */
static startWorld(): void {
return Il2Cpp.Api._gcStartWorld();
}
/** Performs an incremental garbage collection. */
static startIncrementalCollection(): void {
return Il2Cpp.Api._gcStartIncrementalCollection();
}
/** Stops all threads which may access the garbage collected heap, other than the caller. */
static stopWorld(): void {
return Il2Cpp.Api._gcStopWorld();
}
}
Reflect.set(Il2Cpp, "GC", Il2CppGC);
declare global {
namespace Il2Cpp {
class GC extends Il2CppGC {}
}
}
export {};