Skip to content

Commit 65844c5

Browse files
committed
Replace std::vector to plain C array for register roots
1 parent cf73519 commit 65844c5

3 files changed

Lines changed: 34 additions & 39 deletions

File tree

il2cpp/GeneratorContext.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -301,20 +301,25 @@ private CompileUnit GenInitUnit(Dictionary<string, string> transMap)
301301
{
302302
if (item.Item2)
303303
{
304-
prtGC.AppendFormatLine("IL2CPP_ADD_ROOT({0});", item.Item1);
304+
prtGC.AppendFormatLine("IL2CPP_ADD_ROOT({0}),", item.Item1);
305305
addedRoots = true;
306306
}
307307
prtInit.AppendFormatLine("{0} = {{}};", item.Item1);
308308
}
309309
}
310310

311-
if (addedRoots)
312-
prtGC.AppendLine("il2cpp_CommitRoots();");
313-
314311
CodePrinter prtFunc = new CodePrinter();
315312
prtFunc.AppendLine("void il2cpp_InitVariables()\n{");
316313
++prtFunc.Indents;
317-
prtFunc.Append(prtGC.ToString());
314+
if (addedRoots)
315+
{
316+
prtFunc.AppendLine("il2cppRootItem roots[] =\n{");
317+
++prtFunc.Indents;
318+
prtFunc.Append(prtGC.ToString());
319+
--prtFunc.Indents;
320+
prtFunc.AppendLine("};");
321+
prtFunc.AppendLine("il2cpp_CommitRoots(roots, sizeof(roots) / sizeof(roots[0]));");
322+
}
318323
prtFunc.Append(prtInit.ToString());
319324
--prtFunc.Indents;
320325
prtFunc.AppendLine("}");

runtime/il2cpp.cpp

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#include "il2cppGC.h"
33
#include "il2cppBridge.h"
44
#include <math.h>
5-
#include <vector>
65
#include <algorithm>
76

87
#if defined(_WIN32)
@@ -59,55 +58,36 @@ void* il2cpp_New(uint32_t sz, uint32_t typeID, uint8_t isNoRef, IL2CPP_FINALIZER
5958
return obj;
6059
}
6160

62-
struct GCRootItem
61+
void il2cpp_CommitRoots(il2cppRootItem* roots, uint32_t num)
6362
{
64-
uint8_t* Start;
65-
uint32_t Length;
66-
67-
GCRootItem(uint8_t* start, uint32_t len)
68-
: Start(start)
69-
, Length(len)
63+
std::sort(roots, roots + num,
64+
[](const il2cppRootItem& lhs, const il2cppRootItem& rhs)
7065
{
71-
}
72-
};
73-
74-
static std::vector<GCRootItem> g_Roots;
75-
void il2cpp_AddRoot(void* ptr, uint32_t len)
76-
{
77-
g_Roots.emplace_back(static_cast<uint8_t*>(ptr), len);
78-
}
79-
80-
void il2cpp_CommitRoots()
81-
{
82-
std::sort(g_Roots.begin(), g_Roots.end(),
83-
[](const GCRootItem& lhs, const GCRootItem& rhs)
84-
{
85-
return lhs.Start < rhs.Start;
66+
return lhs.Ptr < rhs.Ptr;
8667
});
8768

8869
uint8_t* start = nullptr;
8970
uint8_t* end = nullptr;
90-
for (const auto& item : g_Roots)
71+
for (const il2cppRootItem* itEnd = roots + num;
72+
roots < itEnd;
73+
++roots)
9174
{
75+
const il2cppRootItem& item = *roots;
9276
if (start)
9377
{
94-
if (item.Start <= end)
78+
if (item.Ptr <= end)
9579
{
9680
end += item.Length;
9781
continue;
9882
}
9983
il2cpp_GC_AddRoots(start, end);
10084
}
10185

102-
start = item.Start;
103-
end = item.Start + item.Length;
86+
start = item.Ptr;
87+
end = item.Ptr + item.Length;
10488
}
10589
if (start)
10690
il2cpp_GC_AddRoots(start, end);
107-
108-
g_Roots.clear();
109-
g_Roots.reserve(0);
110-
auto tmp = std::move(g_Roots);
11191
}
11292

11393
void il2cpp_Yield()

runtime/il2cpp.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
#define IL2CPP_MEMCMP memcmp
4343
#define IL2CPP_ALLOCA alloca
4444
#define IL2CPP_NEW il2cpp_New
45-
#define IL2CPP_ADD_ROOT(_x) il2cpp_AddRoot(&(_x), sizeof(_x))
45+
#define IL2CPP_ADD_ROOT(_x) il2cppRootItem(&(_x), sizeof(_x))
4646
#define IL2CPP_THROW(_ex) throw il2cppException(_ex)
4747
#define IL2CPP_THROW_INVALIDCAST do { il2cpp_ThrowInvalidCast(); IL2CPP_UNREACHABLE; } while(0)
4848

@@ -178,13 +178,23 @@ struct il2cppFieldInfo
178178
uint32_t Offset;
179179
};
180180

181+
struct il2cppRootItem
182+
{
183+
uint8_t* Ptr;
184+
uint32_t Length;
185+
186+
il2cppRootItem(const void* ptr, uint32_t len)
187+
: Ptr((uint8_t*)ptr)
188+
, Length(len)
189+
{}
190+
};
191+
181192
using IL2CPP_FINALIZER_FUNC = void(*)(cls_Object*);
182193

183194
void il2cpp_Init();
184195
void* il2cpp_New(uint32_t sz, uint32_t typeID, uint8_t isNoRef);
185196
void* il2cpp_New(uint32_t sz, uint32_t typeID, uint8_t isNoRef, IL2CPP_FINALIZER_FUNC finalizer);
186-
void il2cpp_AddRoot(void* ptr, uint32_t len);
187-
void il2cpp_CommitRoots();
197+
void il2cpp_CommitRoots(il2cppRootItem* roots, uint32_t num);
188198
void il2cpp_Yield();
189199
void il2cpp_SleepMS(uint32_t ms);
190200
uintptr_t il2cpp_ThreadID();

0 commit comments

Comments
 (0)