123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- #include "il2cpp-config.h"
- #include "utils/MemoryPool.h"
- #include "utils/Memory.h"
- #include <algorithm>
- #include <limits>
- namespace il2cpp
- {
- namespace utils
- {
- const size_t kPageSize = IL2CPP_PAGE_SIZE;
- const size_t kDefaultRegionSize = 64 * 1024;
- // by making all allocations a multiple of this value, we ensure the next
- // allocation will always be aligned to this value
- const size_t kMemoryAlignment = 8;
- static inline size_t MakeMultipleOf(size_t size, size_t alignment)
- {
- return (size + alignment - 1) & ~(alignment - 1);
- }
- struct MemoryPool::Region
- {
- char* start;
- char* current;
- size_t size;
- size_t free;
- };
- MemoryPool::MemoryPool()
- {
- AddRegion(kDefaultRegionSize);
- }
- MemoryPool::MemoryPool(size_t initialSize)
- {
- AddRegion(initialSize);
- }
- MemoryPool::~MemoryPool()
- {
- for (RegionList::iterator iter = m_Regions.begin(); iter != m_Regions.end(); ++iter)
- {
- IL2CPP_FREE((*iter)->start, IL2CPP_MEM_META_POOL);
- IL2CPP_FREE(*iter, IL2CPP_MEM_META_POOL);
- }
- m_Regions.clear();
- }
- void* MemoryPool::Malloc(size_t size)
- {
- size = MakeMultipleOf(size, kMemoryAlignment);
- Region* region = m_Regions.back();
- if (region->free < size)
- {
- #if IL2CPP_ENABLE_MEM_STATS
- il2cpp_mem_stats.meta.meta_wasted += region->free;
- #endif //IL2CPP_ENABLE_MEM_STATS
- region = AddRegion(size);
- }
- IL2CPP_ASSERT(region->free >= size);
- void* value = region->current;
- region->current += size;
- region->free -= size;
- return value;
- }
- void* MemoryPool::Calloc(size_t count, size_t size)
- {
- void* ret = Malloc(count * size);
- return ret;
- }
- MemoryPool::Region* MemoryPool::AddRegion(size_t size)
- {
- Region* newRegion = (Region*)IL2CPP_MALLOC(sizeof(Region), IL2CPP_MEM_META_POOL);
- Region* lastFreeRegion = m_Regions.size() > 0 ? m_Regions.back() : NULL;
- size_t allocationSize;
- if (lastFreeRegion != NULL && lastFreeRegion->free >= kPageSize)
- {
- allocationSize = MakeMultipleOf(size, kPageSize);
- m_Regions.pop_back();
- m_Regions.push_back(newRegion);
- m_Regions.push_back(lastFreeRegion);
- }
- else
- {
- allocationSize = std::max(kDefaultRegionSize, MakeMultipleOf(size, kPageSize));
- m_Regions.push_back(newRegion);
- }
- newRegion->start = newRegion->current = (char*)IL2CPP_MALLOC_ZERO(allocationSize, IL2CPP_MEM_META_POOL);
- newRegion->size = newRegion->free = allocationSize;
-
- #if IL2CPP_ENABLE_MEM_STATS
- il2cpp_mem_stats.meta.meta_total += (allocationSize + sizeof(Region));
- ++il2cpp_mem_stats.meta.meta_region_count;
- #endif //IL2CPP_ENABLE_MEM_STATS
- return newRegion;
- }
-
- size_t MemoryPool::RegionSize() {
- return sizeof(Region);
- }
- size_t MemoryPool::FreeSize(){
- return m_Regions.back()->free;
- }
-
- size_t MemoryPool::TotalSize() {
- size_t total = 0;
- for (RegionList::iterator iter = m_Regions.begin(); iter != m_Regions.end(); ++iter)
- {
- total += (*iter)->size;
- }
- return total + sizeof(Region) * m_Regions.size();
- }
- }
- }
|