MetadataAlloc.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include "il2cpp-config.h"
  2. #include "MetadataAlloc.h"
  3. #include "il2cpp-class-internals.h"
  4. #include "utils/MemoryPool.h"
  5. #if IL2CPP_SANITIZE_ADDRESS
  6. #include "utils/MemoryPoolAddressSanitizer.h"
  7. #endif
  8. namespace il2cpp
  9. {
  10. namespace vm
  11. {
  12. #if IL2CPP_SANITIZE_ADDRESS
  13. typedef utils::MemoryPoolAddressSanitizer MemoryPoolType;
  14. #else
  15. typedef utils::MemoryPool MemoryPoolType;
  16. #endif
  17. // we allocate these dynamically on runtime initialization
  18. // because the pool uses standard allocators, and we want to give embedding
  19. // client the chance to install their own allocator callbacks
  20. static MemoryPoolType* s_MetadataMemoryPool;
  21. static MemoryPoolType* s_GenericClassMemoryPool;
  22. static MemoryPoolType* s_GenericMethodMemoryPool;
  23. // This initial size (256k/512k) allows us enough room to initialize metadata
  24. // an empty Unity project and have a bit of room leftover.
  25. const size_t kInitialRegionSize = IL2CPP_SIZEOF_VOID_P * 64 * 1024;
  26. void MetadataAllocInitialize()
  27. {
  28. #if IL2CPP_SANITIZE_ADDRESS
  29. s_MetadataMemoryPool = new utils::MemoryPoolAddressSanitizer(kInitialRegionSize);
  30. s_GenericClassMemoryPool = new utils::MemoryPoolAddressSanitizer();
  31. s_GenericMethodMemoryPool = new utils::MemoryPoolAddressSanitizer();
  32. #else
  33. s_MetadataMemoryPool = new utils::MemoryPool(kInitialRegionSize);
  34. // these can use the default smaller initial pool size
  35. s_GenericClassMemoryPool = new utils::MemoryPool();
  36. s_GenericMethodMemoryPool = new utils::MemoryPool();
  37. #endif
  38. }
  39. void MetadataAllocCleanup()
  40. {
  41. delete s_MetadataMemoryPool;
  42. s_MetadataMemoryPool = NULL;
  43. delete s_GenericClassMemoryPool;
  44. s_GenericClassMemoryPool = NULL;
  45. delete s_GenericMethodMemoryPool;
  46. s_GenericMethodMemoryPool = NULL;
  47. }
  48. void* MetadataMalloc(size_t size)
  49. {
  50. return s_MetadataMemoryPool->Malloc(size);
  51. }
  52. void* MetadataCalloc(size_t count, size_t size)
  53. {
  54. return s_MetadataMemoryPool->Calloc(count, size);
  55. }
  56. Il2CppGenericClass* MetadataAllocGenericClass()
  57. {
  58. return (Il2CppGenericClass*)s_GenericClassMemoryPool->Calloc(1, sizeof(Il2CppGenericClass));
  59. }
  60. Il2CppGenericMethod* MetadataAllocGenericMethod()
  61. {
  62. return (Il2CppGenericMethod*)s_GenericMethodMemoryPool->Calloc(1, sizeof(Il2CppGenericMethod));
  63. }
  64. }
  65. }