MetadataPool.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. #include "MetadataPool.h"
  2. #include "utils/MemoryPool.h"
  3. #include "vm/MetadataAlloc.h"
  4. #include "vm/MetadataLock.h"
  5. #include "vm/GlobalMetadata.h"
  6. #include "MetadataModule.h"
  7. namespace hybridclr
  8. {
  9. namespace metadata
  10. {
  11. using il2cpp::utils::HashUtils;
  12. constexpr int kMaxPrimitiveType = (int)IL2CPP_TYPE_OBJECT;
  13. static bool s_initializedMetadataPool = false;
  14. static Il2CppType s_primitiveTypes[(int)kMaxPrimitiveType + 1];
  15. static Il2CppType s_byRefPrimitiveTypes[(int)kMaxPrimitiveType + 1];
  16. class Il2CppArrayTypeHash
  17. {
  18. public:
  19. size_t operator()(const Il2CppArrayType* t1) const
  20. {
  21. return Hash(t1);
  22. }
  23. static size_t Hash(const Il2CppArrayType* t1);
  24. };
  25. class Il2CppTypeFullHash
  26. {
  27. public:
  28. size_t operator()(const Il2CppType* t1) const
  29. {
  30. return Hash(t1);
  31. }
  32. static size_t Hash(const Il2CppType* t1)
  33. {
  34. size_t hash = t1->type;
  35. hash = HashUtils::Combine(hash, t1->byref);
  36. hash = il2cpp::utils::HashUtils::Combine(hash, t1->attrs);
  37. //hash = il2cpp::utils::HashUtils::Combine(hash, t1->num_mods);
  38. hash = il2cpp::utils::HashUtils::Combine(hash, t1->pinned);
  39. //hash = il2cpp::utils::HashUtils::Combine(hash, t1->valuetype);
  40. switch (t1->type)
  41. {
  42. case IL2CPP_TYPE_VALUETYPE:
  43. case IL2CPP_TYPE_CLASS:
  44. {
  45. return HashUtils::Combine(hash, reinterpret_cast<size_t>(t1->data.typeHandle));
  46. }
  47. case IL2CPP_TYPE_SZARRAY:
  48. case IL2CPP_TYPE_PTR:
  49. {
  50. return HashUtils::Combine(hash, Hash(t1->data.type));
  51. }
  52. case IL2CPP_TYPE_ARRAY:
  53. {
  54. const Il2CppArrayType* a = t1->data.array;
  55. // dont' compute sizes and lobounds
  56. hash = HashUtils::Combine(hash, Il2CppArrayTypeHash::Hash(a));
  57. return hash;
  58. }
  59. case IL2CPP_TYPE_GENERICINST:
  60. {
  61. return HashUtils::Combine(hash, (size_t)t1->data.generic_class);
  62. }
  63. case IL2CPP_TYPE_VAR:
  64. case IL2CPP_TYPE_MVAR:
  65. return HashUtils::Combine(hash, reinterpret_cast<size_t>(t1->data.genericParameterHandle));
  66. default:
  67. return hash;
  68. }
  69. return hash;
  70. }
  71. };
  72. size_t Il2CppArrayTypeHash::Hash(const Il2CppArrayType* t1)
  73. {
  74. size_t hash = t1->rank;
  75. hash = HashUtils::Combine(hash, t1->numsizes);
  76. hash = HashUtils::Combine(hash, t1->numlobounds);
  77. hash = HashUtils::Combine(hash, Il2CppTypeFullHash::Hash(t1->etype));
  78. for (uint8_t i = 0; i < t1->numsizes; ++i)
  79. {
  80. hash = HashUtils::Combine(hash, t1->sizes[i]);
  81. }
  82. for (uint8_t i = 0; i < t1->numlobounds; ++i)
  83. {
  84. hash = HashUtils::Combine(hash, t1->lobounds[i]);
  85. }
  86. return hash;
  87. }
  88. class Il2CppArrayTypeEqualityComparer
  89. {
  90. public:
  91. bool operator()(const Il2CppArrayType* t1, const Il2CppArrayType* t2) const
  92. {
  93. return AreEqual(t1, t2);
  94. }
  95. static bool AreEqual(const Il2CppArrayType* t1, const Il2CppArrayType* t2);
  96. };
  97. class Il2CppTypeFullEqualityComparer
  98. {
  99. public:
  100. bool operator()(const Il2CppType* t1, const Il2CppType* t2) const
  101. {
  102. return AreEqual(t1, t2);
  103. }
  104. static bool AreEqual(const Il2CppType* t1, const Il2CppType* t2)
  105. {
  106. if (t1->type != t2->type)
  107. {
  108. return false;
  109. }
  110. if (t1->byref != t2->byref
  111. || t1->attrs != t2->attrs
  112. || t1->pinned != t2->pinned)
  113. {
  114. return false;
  115. }
  116. switch (t1->type)
  117. {
  118. case IL2CPP_TYPE_VALUETYPE:
  119. case IL2CPP_TYPE_CLASS:
  120. return t1->data.typeHandle == t2->data.typeHandle;
  121. case IL2CPP_TYPE_PTR:
  122. case IL2CPP_TYPE_SZARRAY:
  123. return AreEqual(t1->data.type, t2->data.type);
  124. case IL2CPP_TYPE_ARRAY:
  125. {
  126. const Il2CppArrayType* a1 = t1->data.array;
  127. const Il2CppArrayType* a2 = t2->data.array;
  128. return Il2CppArrayTypeEqualityComparer::AreEqual(a1, a2);
  129. }
  130. case IL2CPP_TYPE_GENERICINST:
  131. {
  132. return t1->data.generic_class == t2->data.generic_class;
  133. }
  134. case IL2CPP_TYPE_VAR:
  135. case IL2CPP_TYPE_MVAR:
  136. return t1->data.genericParameterHandle == t2->data.genericParameterHandle;
  137. default:
  138. return true;
  139. }
  140. return true;
  141. }
  142. };
  143. bool Il2CppArrayTypeEqualityComparer::AreEqual(const Il2CppArrayType* a1, const Il2CppArrayType* a2)
  144. {
  145. if (a1->rank != a2->rank || a1->numsizes != a2->numsizes || a1->numlobounds != a2->numlobounds)
  146. {
  147. return false;
  148. }
  149. for (uint8_t i = 0; i < a1->numsizes; ++i)
  150. {
  151. if (a1->sizes[i] != a2->sizes[i])
  152. return false;
  153. }
  154. for (uint8_t i = 0; i < a1->numlobounds; ++i)
  155. {
  156. if (a1->lobounds[i] != a2->lobounds[i])
  157. return false;
  158. }
  159. return Il2CppTypeFullEqualityComparer::AreEqual(a1->etype, a2->etype);
  160. }
  161. class Il2CppArrayTypeHash2
  162. {
  163. public:
  164. size_t operator()(const Il2CppArrayType* t1) const
  165. {
  166. return Hash(t1);
  167. }
  168. static size_t Hash(const Il2CppArrayType* t1)
  169. {
  170. size_t hash = t1->rank;
  171. hash = HashUtils::Combine(hash, Il2CppTypeFullHash::Hash(t1->etype));
  172. return hash;
  173. }
  174. };
  175. class Il2CppArrayTypeEqualityComparer2
  176. {
  177. public:
  178. bool operator()(const Il2CppArrayType* t1, const Il2CppArrayType* t2) const
  179. {
  180. return AreEqual(t1, t2);
  181. }
  182. static bool AreEqual(const Il2CppArrayType* a1, const Il2CppArrayType* a2)
  183. {
  184. if (a1->rank != a2->rank)
  185. {
  186. return false;
  187. }
  188. return Il2CppTypeFullEqualityComparer::AreEqual(a1->etype, a2->etype);
  189. }
  190. };
  191. typedef Il2CppHashSet<const Il2CppType*, Il2CppTypeFullHash, Il2CppTypeFullEqualityComparer> Il2CppTypeHashSet;
  192. static Il2CppTypeHashSet* s_Il2CppTypePool = nullptr;
  193. typedef Il2CppHashSet<const Il2CppArrayType*, Il2CppArrayTypeHash2, Il2CppArrayTypeEqualityComparer2> Il2CppArrayTypeHashSet;
  194. Il2CppArrayTypeHashSet* s_Il2CppArrayTypePool = nullptr;
  195. static void InitMetadataPool()
  196. {
  197. IL2CPP_ASSERT(!s_initializedMetadataPool);
  198. s_initializedMetadataPool = true;
  199. std::memset(s_primitiveTypes, 0, sizeof(s_primitiveTypes));
  200. std::memset(s_byRefPrimitiveTypes, 0, sizeof(s_byRefPrimitiveTypes));
  201. for (int i = 0; i <= kMaxPrimitiveType; i++)
  202. {
  203. Il2CppType& type = s_primitiveTypes[i];
  204. type.type = (Il2CppTypeEnum)i;
  205. bool isValueType = i != IL2CPP_TYPE_OBJECT && i != IL2CPP_TYPE_STRING && i != IL2CPP_TYPE_VOID;
  206. SET_IL2CPPTYPE_VALUE_TYPE(type, isValueType);
  207. Il2CppType& byRefType = s_byRefPrimitiveTypes[i];
  208. byRefType.type = (Il2CppTypeEnum)i;
  209. byRefType.byref = 1;
  210. }
  211. s_Il2CppTypePool = new Il2CppTypeHashSet();
  212. s_Il2CppArrayTypePool = new Il2CppArrayTypeHashSet();
  213. }
  214. static const Il2CppType* TryGetPrimitiveType(const Il2CppType& originalType)
  215. {
  216. if (!s_initializedMetadataPool)
  217. {
  218. InitMetadataPool();
  219. }
  220. if (originalType.attrs || originalType.num_mods || originalType.pinned)
  221. {
  222. return nullptr;
  223. }
  224. Il2CppTypeEnum type = originalType.type;
  225. switch (type)
  226. {
  227. case IL2CPP_TYPE_VOID:
  228. case IL2CPP_TYPE_BOOLEAN:
  229. case IL2CPP_TYPE_CHAR:
  230. case IL2CPP_TYPE_I1:
  231. case IL2CPP_TYPE_U1:
  232. case IL2CPP_TYPE_I2:
  233. case IL2CPP_TYPE_U2:
  234. case IL2CPP_TYPE_I4:
  235. case IL2CPP_TYPE_U4:
  236. case IL2CPP_TYPE_I8:
  237. case IL2CPP_TYPE_U8:
  238. case IL2CPP_TYPE_R4:
  239. case IL2CPP_TYPE_R8:
  240. case IL2CPP_TYPE_STRING:
  241. case IL2CPP_TYPE_I:
  242. case IL2CPP_TYPE_U:
  243. case IL2CPP_TYPE_OBJECT:
  244. return originalType.byref ? s_byRefPrimitiveTypes + (int)type : s_primitiveTypes + (int)type;
  245. case IL2CPP_TYPE_VALUETYPE:
  246. case IL2CPP_TYPE_CLASS:
  247. {
  248. const Il2CppTypeDefinition* typeDef = (const Il2CppTypeDefinition*)originalType.data.typeHandle;
  249. if (typeDef)
  250. {
  251. if (originalType.byref)
  252. {
  253. #if HYBRIDCLR_UNITY_2019
  254. return il2cpp::vm::GlobalMetadata::GetIl2CppTypeFromIndex(typeDef->byrefTypeIndex);
  255. #endif
  256. }
  257. else
  258. {
  259. return il2cpp::vm::GlobalMetadata::GetIl2CppTypeFromIndex(typeDef->byvalTypeIndex);
  260. }
  261. }
  262. return nullptr;
  263. }
  264. default:
  265. return nullptr;
  266. }
  267. }
  268. void MetadataPool::Initialize()
  269. {
  270. //InitMetadataPool();
  271. }
  272. static bool NeedCache(const Il2CppType& originalType)
  273. {
  274. Il2CppTypeEnum type = originalType.type;
  275. switch (type)
  276. {
  277. case IL2CPP_TYPE_VOID:
  278. case IL2CPP_TYPE_BOOLEAN:
  279. case IL2CPP_TYPE_CHAR:
  280. case IL2CPP_TYPE_I1:
  281. case IL2CPP_TYPE_U1:
  282. case IL2CPP_TYPE_I2:
  283. case IL2CPP_TYPE_U2:
  284. case IL2CPP_TYPE_I4:
  285. case IL2CPP_TYPE_U4:
  286. case IL2CPP_TYPE_I8:
  287. case IL2CPP_TYPE_U8:
  288. case IL2CPP_TYPE_R4:
  289. case IL2CPP_TYPE_R8:
  290. case IL2CPP_TYPE_STRING:
  291. case IL2CPP_TYPE_I:
  292. case IL2CPP_TYPE_U:
  293. case IL2CPP_TYPE_OBJECT:
  294. return true;
  295. case IL2CPP_TYPE_PTR:
  296. case IL2CPP_TYPE_SZARRAY:
  297. return NeedCache(*originalType.data.type);
  298. case IL2CPP_TYPE_VALUETYPE:
  299. case IL2CPP_TYPE_CLASS:
  300. {
  301. return originalType.data.typeHandle == nullptr;
  302. }
  303. default:
  304. return false;
  305. }
  306. }
  307. static Il2CppType* DeepCloneIl2CppType(const Il2CppType& type)
  308. {
  309. Il2CppType* newType = MetadataMallocT<Il2CppType>();
  310. *newType = type;
  311. return newType;
  312. }
  313. const Il2CppType* MetadataPool::GetPooledIl2CppType(const Il2CppType& type)
  314. {
  315. const Il2CppType* pooledType = TryGetPrimitiveType(type);
  316. if (pooledType)
  317. {
  318. return pooledType;
  319. }
  320. bool needCache = NeedCache(type);
  321. if (needCache)
  322. {
  323. auto it = s_Il2CppTypePool->find(&type);
  324. if (it != s_Il2CppTypePool->end())
  325. return *it;
  326. }
  327. Il2CppType* newType = DeepCloneIl2CppType(type);
  328. if (needCache)
  329. {
  330. auto ret = s_Il2CppTypePool->insert(newType);
  331. IL2CPP_ASSERT(ret.second);
  332. }
  333. return newType;
  334. }
  335. Il2CppType* MetadataPool::ShallowCloneIl2CppType(const Il2CppType* type)
  336. {
  337. Il2CppType* newType = MetadataMallocT<Il2CppType>();
  338. *newType = *type;
  339. return newType;
  340. }
  341. const Il2CppArrayType* MetadataPool::GetPooledIl2CppArrayType(const Il2CppType* elementType, uint32_t rank)
  342. {
  343. Il2CppArrayType type = {};
  344. type.etype = elementType;
  345. type.rank = rank;
  346. bool needCache = NeedCache(*elementType);
  347. if (needCache)
  348. {
  349. auto it = s_Il2CppArrayTypePool->find(&type);
  350. if (it != s_Il2CppArrayTypePool->end())
  351. return *it;
  352. }
  353. Il2CppArrayType* newType = MetadataMallocT<Il2CppArrayType>();
  354. *newType = type;
  355. if (needCache)
  356. {
  357. auto ret = s_Il2CppArrayTypePool->insert(newType);
  358. IL2CPP_ASSERT(ret.second);
  359. }
  360. return newType;
  361. }
  362. }
  363. }