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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
#include "UnityPrefix.h"
#include "StackAllocator.h"
#include "Runtime/Allocator/MemoryManager.h"
#include "Runtime/Profiler/MemoryProfiler.h"
StackAllocator::StackAllocator(int blockSize, const char* name)
: BaseAllocator(name)
, m_Block(NULL)
, m_LastAlloc(NULL)
, m_BlockSize(blockSize)
{
#if ENABLE_MEMORY_MANAGER
m_Block = (char*)MemoryManager::LowLevelAllocate(m_BlockSize);
#else
m_Block = (char*)malloc(m_BlockSize);
#endif
m_TotalReservedMemory = blockSize;
}
StackAllocator::~StackAllocator()
{
while(m_LastAlloc)
Deallocate(m_LastAlloc);
#if ENABLE_MEMORY_MANAGER
MemoryManager::LowLevelFree(m_Block);
#else
free(m_Block);
#endif
}
void* StackAllocator::Allocate (size_t size, int align)
{
//1 byte alignment doesn't work for webgl; this is a fix(ish)....
#if UNITY_WEBGL || UNITY_BB10
if(align % 8 != 0)
align = 8;
#endif
size_t alignmask = align - 1;
// make header size a multiple
int alignedHeaderSize = (GetHeaderSize() + alignmask) & ~alignmask;
int paddedSize = (size + alignedHeaderSize + alignmask) & ~alignmask;
char* realPtr;
char* freePtr = (char*)AlignPtr(GetBufferFreePtr(), align);
size_t freeSize = m_BlockSize - (int)(freePtr - m_Block);
if ( InBlock(freePtr) && paddedSize < freeSize )
{
realPtr = freePtr;
}
else
{
// Spilled over. We have to allocate the memory default alloc
realPtr = (char*)UNITY_MALLOC_ALIGNED(kMemTempOverflow, paddedSize, align);
}
if(realPtr == NULL)
return NULL;
char* ptr = realPtr + alignedHeaderSize;
Header* h = ( (Header*)ptr )-1;
h->prevPtr = m_LastAlloc;
h->size = size;
h->deleted = 0;
h->realPtr = realPtr;
m_LastAlloc = ptr;
return ptr;
}
void* StackAllocator::Reallocate (void* p, size_t size, int align)
{
if (p == NULL)
return Allocate(size, align);
char* freePtr = (char*)AlignPtr(GetBufferFreePtr(), align);
size_t freeSize = m_BlockSize - (int)(freePtr - m_Block);
size_t oldSize = GetPtrSize(p);
if ((p == m_LastAlloc || oldSize >= size) && InBlock(p)
&& AlignPtr(p,align) == p
&& oldSize + freeSize > size)
{
// just expand the top allocation of the stack to the realloc amount
Header* h = ( (Header*)p )-1;
h->size = size;
return p;
}
void* newPtr = NULL;
if (!InBlock(p))
{
size_t alignmask = align - 1;
int alignedHeaderSize = (GetHeaderSize() + alignmask) & ~alignmask;
int paddedSize = (size + alignedHeaderSize + alignmask) & ~alignmask;
char* realPtr = (char*)UNITY_REALLOC_ALIGNED(kMemTempOverflow, GetRealPtr(p), paddedSize, align);
if(realPtr == NULL)
return NULL;
newPtr = realPtr + alignedHeaderSize;
Header* h = ( (Header*)newPtr ) - 1;
h->size = size;
h->deleted = 0;
h->realPtr = realPtr;
if(m_LastAlloc == p)
m_LastAlloc = (char*)newPtr;
else
UpdateNextHeader(p, newPtr);
}
else
{
newPtr = Allocate(size, align);
if(newPtr != NULL)
memcpy(newPtr, p, std::min(size, oldSize));
Deallocate(p);
}
return newPtr;
}
void StackAllocator::Deallocate (void* p)
{
if (p == m_LastAlloc){
m_LastAlloc = GetPrevAlloc(p);
if ( !InBlock(p) )
{
UNITY_FREE(kMemTempOverflow, GetRealPtr(p));
}
if (IsDeleted(m_LastAlloc))
Deallocate(m_LastAlloc);
}
else
{
SetDeleted(p);
}
}
bool StackAllocator::ContainsInternal (const void* p)
{
// if no temp allocations (should hit here most often)
if (m_LastAlloc == NULL)
return false;
// test inblock
if (InBlock(p))
return true;
// test overflow allocations (should almost never happen)
void* ptr = m_LastAlloc;
while (ptr != NULL && !InBlock(ptr))
{
if (p == ptr)
return true;
ptr = GetPrevAlloc(ptr);
}
return false;
}
void StackAllocator::UpdateNextHeader(void* before, void* after)
{
if (before == m_LastAlloc)
return;
void* ptr = m_LastAlloc;
while (ptr != NULL && !InBlock(ptr))
{
void* prevAlloc = GetPrevAlloc(ptr);
if (before == prevAlloc)
{
Header* h = ((Header*)ptr)-1;
h->prevPtr = (char*)after;
return;
}
ptr = prevAlloc;
}
FatalErrorString("Allocation no found in temp allocation list");
}
size_t StackAllocator::GetAllocatedMemorySize() const
{
int total = 0;
void* ptr = m_LastAlloc;
while (ptr != NULL)
{
total += GetPtrSize(ptr);
ptr = GetPrevAlloc(ptr);
}
return total;
}
size_t StackAllocator::GetAllocatorSizeTotalUsed() const
{
int total = 0;
void* ptr = m_LastAlloc;
while (ptr != NULL)
{
total += GetPtrSize(ptr)+GetHeaderSize();
ptr = GetPrevAlloc(ptr);
}
return total;
}
size_t StackAllocator::GetReservedSizeTotal() const
{
return m_TotalReservedMemory;
}
|