forked from focus-creative-games/il2cpp_plus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryPool.cpp
More file actions
122 lines (101 loc) · 3.04 KB
/
MemoryPool.cpp
File metadata and controls
122 lines (101 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include "il2cpp-config.h"
#include "utils/MemoryPool.h"
#include "utils/Memory.h"
#include <algorithm>
#include <limits>
namespace il2cpp
{
namespace utils
{
// 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 size_t s_RegionSize = 64 * 1024;
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;
};
void MemoryPool::SetRegionSize(size_t size)
{
s_RegionSize = size;
}
size_t MemoryPool::GetRegionSize()
{
return s_RegionSize;
}
MemoryPool::MemoryPool()
{
AddRegion(s_RegionSize);
}
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_FREE(*iter);
}
m_Regions.clear();
}
void* MemoryPool::Malloc(size_t size)
{
size = MakeMultipleOf(size, kMemoryAlignment);
Region* region = m_Regions.back();
if (region->free < size)
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;
}
bool MemoryPool::Contains(const void* data) const
{
for (RegionList::const_iterator iter = m_Regions.begin(); iter != m_Regions.end(); ++iter)
{
Region* region = *iter;
if (data >= region->start && data < region->start + region->size)
return true;
}
return false;
}
MemoryPool::Region* MemoryPool::AddRegion(size_t size)
{
Region* newRegion = (Region*)IL2CPP_MALLOC(sizeof(Region));
Region* lastFreeRegion = m_Regions.size() > 0 ? m_Regions.back() : NULL;
size_t allocationSize;
// If we have more than 1/16th (4k by default) of the current region remaining free,
// perform a one off allocation rather than losing that space to fragmentation.
if (lastFreeRegion != NULL && lastFreeRegion->free >= (s_RegionSize / 16))
{
allocationSize = size;
m_Regions.pop_back();
m_Regions.push_back(newRegion);
m_Regions.push_back(lastFreeRegion);
}
else
{
allocationSize = std::max(s_RegionSize, size);
m_Regions.push_back(newRegion);
}
newRegion->start = newRegion->current = (char*)IL2CPP_MALLOC_ZERO(allocationSize);
newRegion->size = newRegion->free = allocationSize;
return newRegion;
}
}
}