MetadataAlloc.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include "il2cpp-config.h"
  2. #include "MetadataAlloc.h"
  3. #include "il2cpp-class-internals.h"
  4. #include "utils/MemoryPool.h"
  5. #include "il2cpp-runtime-stats.h"
  6. #if IL2CPP_SANITIZE_ADDRESS
  7. #include "utils/MemoryPoolAddressSanitizer.h"
  8. #endif
  9. namespace il2cpp
  10. {
  11. namespace vm
  12. {
  13. #if IL2CPP_SANITIZE_ADDRESS
  14. typedef utils::MemoryPoolAddressSanitizer MemoryPoolType;
  15. #else
  16. typedef utils::MemoryPool MemoryPoolType;
  17. #endif
  18. // we allocate these dynamically on runtime initialization
  19. // because the pool uses standard allocators, and we want to give embedding
  20. // client the chance to install their own allocator callbacks
  21. static MemoryPoolType* s_MetadataMemoryPool;
  22. static MemoryPoolType* s_GenericClassMemoryPool;
  23. static MemoryPoolType* s_GenericMethodMemoryPool;
  24. // This initial size (256k/512k) allows us enough room to initialize metadata
  25. // an empty Unity project and have a bit of room leftover.
  26. const size_t kInitialRegionSize = IL2CPP_SIZEOF_VOID_P * 64 * 1024;
  27. void MetadataAllocInitialize()
  28. {
  29. #if IL2CPP_SANITIZE_ADDRESS
  30. s_MetadataMemoryPool = new utils::MemoryPoolAddressSanitizer(kInitialRegionSize);
  31. s_GenericClassMemoryPool = new utils::MemoryPoolAddressSanitizer();
  32. s_GenericMethodMemoryPool = new utils::MemoryPoolAddressSanitizer();
  33. #else
  34. s_MetadataMemoryPool = new utils::MemoryPool(kInitialRegionSize);
  35. // these can use the default smaller initial pool size
  36. s_GenericClassMemoryPool = new utils::MemoryPool();
  37. s_GenericMethodMemoryPool = new utils::MemoryPool();
  38. #endif
  39. }
  40. void MetadataAllocCleanup()
  41. {
  42. delete s_MetadataMemoryPool;
  43. s_MetadataMemoryPool = NULL;
  44. delete s_GenericClassMemoryPool;
  45. s_GenericClassMemoryPool = NULL;
  46. delete s_GenericMethodMemoryPool;
  47. s_GenericMethodMemoryPool = NULL;
  48. }
  49. void* MetadataMalloc(size_t size, Il2CppMemStat label)
  50. {
  51. #if IL2CPP_ENABLE_MEM_STATS
  52. mem_stats_add_on_label(label, size);
  53. #endif
  54. return s_MetadataMemoryPool->Malloc(size);
  55. }
  56. void* MetadataCalloc(size_t count, size_t size, Il2CppMemStat label)
  57. {
  58. #if IL2CPP_ENABLE_MEM_STATS
  59. mem_stats_add_on_label(label, size * count);
  60. #endif
  61. return s_MetadataMemoryPool->Calloc(count, size);
  62. }
  63. Il2CppGenericClass* MetadataAllocGenericClass()
  64. {
  65. #if IL2CPP_ENABLE_MEM_STATS
  66. size_t size = sizeof(Il2CppGenericClass);
  67. il2cpp_mem_stats.meta.generic_class_size += size;
  68. ++il2cpp_mem_stats.meta.generic_class_count;
  69. #endif
  70. return (Il2CppGenericClass*)s_GenericClassMemoryPool->Calloc(1, sizeof(Il2CppGenericClass));
  71. }
  72. Il2CppGenericMethod* MetadataAllocGenericMethod()
  73. {
  74. #if IL2CPP_ENABLE_MEM_STATS
  75. size_t size = sizeof(Il2CppGenericMethod);
  76. il2cpp_mem_stats.meta.generic_method_size += size;
  77. ++il2cpp_mem_stats.meta.generic_method_count;
  78. #endif
  79. return (Il2CppGenericMethod*)s_GenericMethodMemoryPool->Calloc(1, sizeof(Il2CppGenericMethod));
  80. }
  81. #if IL2CPP_ENABLE_MEM_STATS
  82. std::size_t mem_stats_get_metadata_free() {
  83. return s_MetadataMemoryPool->FreeSize() + s_GenericClassMemoryPool->FreeSize() + s_GenericMethodMemoryPool->FreeSize();
  84. }
  85. #endif
  86. std::size_t MetadataTotalMemSize() {
  87. std::size_t total = s_MetadataMemoryPool->TotalSize() + s_GenericClassMemoryPool->TotalSize() + s_GenericMethodMemoryPool->TotalSize();
  88. return total;
  89. }
  90. }
  91. }