MemoryPool.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include "il2cpp-config.h"
  2. #include "utils/MemoryPool.h"
  3. #include "utils/Memory.h"
  4. #include <algorithm>
  5. #include <limits>
  6. namespace il2cpp
  7. {
  8. namespace utils
  9. {
  10. // by making all allocations a multiple of this value, we ensure the next
  11. // allocation will always be aligned to this value
  12. const size_t kMemoryAlignment = 8;
  13. static size_t s_RegionSize = 64 * 1024;
  14. static inline size_t MakeMultipleOf(size_t size, size_t alignment)
  15. {
  16. return (size + alignment - 1) & ~(alignment - 1);
  17. }
  18. struct MemoryPool::Region
  19. {
  20. char* start;
  21. char* current;
  22. size_t size;
  23. size_t free;
  24. };
  25. void MemoryPool::SetRegionSize(size_t size)
  26. {
  27. s_RegionSize = size;
  28. }
  29. size_t MemoryPool::GetRegionSize()
  30. {
  31. return s_RegionSize;
  32. }
  33. MemoryPool::MemoryPool()
  34. {
  35. AddRegion(s_RegionSize);
  36. }
  37. MemoryPool::MemoryPool(size_t initialSize)
  38. {
  39. AddRegion(initialSize);
  40. }
  41. MemoryPool::~MemoryPool()
  42. {
  43. for (RegionList::iterator iter = m_Regions.begin(); iter != m_Regions.end(); ++iter)
  44. {
  45. IL2CPP_FREE((*iter)->start);
  46. IL2CPP_FREE(*iter);
  47. }
  48. m_Regions.clear();
  49. }
  50. void* MemoryPool::Malloc(size_t size)
  51. {
  52. size = MakeMultipleOf(size, kMemoryAlignment);
  53. Region* region = m_Regions.back();
  54. if (region->free < size)
  55. region = AddRegion(size);
  56. IL2CPP_ASSERT(region->free >= size);
  57. void* value = region->current;
  58. region->current += size;
  59. region->free -= size;
  60. return value;
  61. }
  62. void* MemoryPool::Calloc(size_t count, size_t size)
  63. {
  64. void* ret = Malloc(count * size);
  65. return memset(ret, 0, count * size);
  66. }
  67. MemoryPool::Region* MemoryPool::AddRegion(size_t size)
  68. {
  69. Region* newRegion = (Region*)IL2CPP_MALLOC(sizeof(Region));
  70. Region* lastFreeRegion = m_Regions.size() > 0 ? m_Regions.back() : NULL;
  71. size_t allocationSize;
  72. // If we have more than 1/16th (4k by default) of the current region remaining free,
  73. // perform a one off allocation rather than losing that space to fragmentation.
  74. if (lastFreeRegion != NULL && lastFreeRegion->free >= (s_RegionSize / 16))
  75. {
  76. allocationSize = size;
  77. m_Regions.pop_back();
  78. m_Regions.push_back(newRegion);
  79. m_Regions.push_back(lastFreeRegion);
  80. }
  81. else
  82. {
  83. allocationSize = std::max(s_RegionSize, size);
  84. m_Regions.push_back(newRegion);
  85. }
  86. newRegion->start = newRegion->current = (char*)IL2CPP_MALLOC(allocationSize);
  87. newRegion->size = newRegion->free = allocationSize;
  88. return newRegion;
  89. }
  90. }
  91. }