-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathil2cppGC.cpp
More file actions
154 lines (128 loc) · 2.73 KB
/
il2cppGC.cpp
File metadata and controls
154 lines (128 loc) · 2.73 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
#include "il2cpp.h"
#include <gc.h>
#if defined(GC_THREADS) && defined(IL2CPP_ENABLE_FINALIZER_THREAD)
#include <condition_variable>
static class FinalizerThread
{
public:
~FinalizerThread()
{
IsExit_ = true;
Notify();
if (Thread_.joinable())
Thread_.join();
}
void Start()
{
Thread_ = std::thread(&FinalizerThread::WorkingThread, this);
}
void Notify()
{
IsNotify_ = true;
CondVar_.notify_one();
}
private:
void WorkingThread()
{
il2cpp_GC_RegisterThread();
while (!IsExit_)
{
std::unique_lock<std::mutex> lk(Mutex_);
CondVar_.wait(lk);
while (IsNotify_)
{
IsNotify_ = false;
GC_invoke_finalizers();
}
}
il2cpp_GC_UnregisterThread();
}
private:
std::thread Thread_;
std::mutex Mutex_;
std::condition_variable CondVar_;
volatile bool IsNotify_ = false;
bool IsExit_ = false;
} g_FinalizerThread;
static void GC_CALLBACK FinalizerNotifier()
{
g_FinalizerThread.Notify();
}
#endif
void il2cpp_GC_Init()
{
GC_set_no_dls(1);
GC_INIT();
#if defined(GC_THREADS)
GC_allow_register_threads();
#if defined(IL2CPP_ENABLE_FINALIZER_THREAD)
GC_set_finalize_on_demand(1);
GC_set_finalizer_notifier(&FinalizerNotifier);
g_FinalizerThread.Start();
#endif
#endif
}
void* il2cpp_GC_Alloc(uintptr_t sz)
{
return GC_MALLOC(sz);
}
void* il2cpp_GC_AllocAtomic(uintptr_t sz)
{
void* ptr = GC_MALLOC_ATOMIC(sz);
memset(ptr, 0, sz);
return ptr;
}
void il2cpp_GC_AddRoots(void* low, void* high)
{
GC_add_roots(low, high);
}
bool il2cpp_GC_RegisterThread()
{
#if defined(GC_THREADS)
int temp = 0;
GC_stack_base sb;
int res = GC_get_stack_base(&sb);
if (res != GC_SUCCESS)
sb.mem_base = &temp;
res = GC_register_my_thread(&sb);
if ((res != GC_SUCCESS) && (res != GC_DUPLICATE))
return false;
return true;
#else
return false;
#endif
}
bool il2cpp_GC_UnregisterThread()
{
#if defined(GC_THREADS)
int res = GC_unregister_my_thread();
return res == GC_SUCCESS;
#else
return false;
#endif
}
static void GC_CALLBACK FinalizerCallback(void* obj, void* cdata)
{
((IL2CPP_FINALIZER_FUNC)cdata)((cls_Object*)obj);
}
void il2cpp_GC_RegisterFinalizer(cls_Object* obj, IL2CPP_FINALIZER_FUNC finalizer)
{
IL2CPP_ASSERT(finalizer != nullptr);
GC_REGISTER_FINALIZER_NO_ORDER(obj, &FinalizerCallback, (void*)finalizer, nullptr, nullptr);
}
void il2cpp_GC_Collect()
{
GC_gcollect();
}
#if defined(IL2CPP_PATCH_LLVM)
extern "C" void* _il2cpp_GC_PatchCalloc(uintptr_t nelem, uintptr_t sz)
{
if (nelem != 1 && sz == 1)
return il2cpp_GC_AllocAtomic(nelem);
else if (nelem == 1 && sz != 1)
return il2cpp_GC_Alloc(sz);
else
IL2CPP_TRAP;
return nullptr;
}
#endif