blob: 00bf88b507e8fa6153160ae3d1af4dd5cbb743f3 (
plain)
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
|
#include "UnityPrefix.h"
#include "TLSAllocator.h"
#include "Runtime/Threads/Thread.h"
#include "Runtime/Allocator/StackAllocator.h"
#if ENABLE_MEMORY_MANAGER
template <class UnderlyingAllocator>
int TLSAllocator<UnderlyingAllocator>::s_NumberOfInstances = 0;
template <class UnderlyingAllocator>
UNITY_TLS_VALUE(UnderlyingAllocator*) TLSAllocator<UnderlyingAllocator>::m_UniqueThreadAllocator;
template <class UnderlyingAllocator>
TLSAllocator<UnderlyingAllocator>::TLSAllocator(const char* name)
: BaseAllocator(name)
{
if(s_NumberOfInstances != 0)
ErrorString("Only one instance of the TLS allocator is allowed because of TLS implementation");
s_NumberOfInstances++;
memset (m_ThreadTempAllocators, 0, sizeof(m_ThreadTempAllocators));
}
template <class UnderlyingAllocator>
TLSAllocator<UnderlyingAllocator>::~TLSAllocator()
{
s_NumberOfInstances--;
}
template <class UnderlyingAllocator>
void TLSAllocator<UnderlyingAllocator>::ThreadInitialize(BaseAllocator *allocator)
{
m_UniqueThreadAllocator = (UnderlyingAllocator*)allocator;
for(int i = 0; i < kMaxThreadTempAllocators; i++)
{
if(m_ThreadTempAllocators[i] == NULL)
{
m_ThreadTempAllocators[i] = (UnderlyingAllocator*) allocator;
break;
}
}
}
template <class UnderlyingAllocator>
void TLSAllocator<UnderlyingAllocator>::ThreadCleanup()
{
UnderlyingAllocator* allocator = m_UniqueThreadAllocator;
m_UniqueThreadAllocator = NULL;
for(int i = 0; i < kMaxThreadTempAllocators; i++)
{
if(m_ThreadTempAllocators[i] == allocator)
{
m_ThreadTempAllocators[i] = NULL;
break;
}
}
UNITY_DELETE(allocator, kMemManager);
}
template <class UnderlyingAllocator>
void TLSAllocator<UnderlyingAllocator>::FrameMaintenance(bool cleanup)
{
Assert(m_UniqueThreadAllocator->GetAllocatedMemorySize() == 0);
}
template <class UnderlyingAllocator>
bool TLSAllocator<UnderlyingAllocator>::IsAssigned() const
{
return m_UniqueThreadAllocator != NULL;
}
template <class UnderlyingAllocator>
UnderlyingAllocator* TLSAllocator<UnderlyingAllocator>::GetCurrentAllocator()
{
return m_UniqueThreadAllocator;
}
template <class UnderlyingAllocator>
void* TLSAllocator<UnderlyingAllocator>::Allocate( size_t size, int align )
{
UnderlyingAllocator* alloc = GetCurrentAllocator();
return alloc ? alloc->UnderlyingAllocator::Allocate(size, align) : NULL;
}
template <class UnderlyingAllocator>
void* TLSAllocator<UnderlyingAllocator>::Reallocate( void* p, size_t size, int align )
{
UnderlyingAllocator* alloc = GetCurrentAllocator();
if(!alloc)
return NULL;
if(alloc->UnderlyingAllocator::Contains(p))
return alloc->UnderlyingAllocator::Reallocate(p, size, align);
return NULL;
}
template <class UnderlyingAllocator>
void TLSAllocator<UnderlyingAllocator>::Deallocate( void* p )
{
UnderlyingAllocator* alloc = GetCurrentAllocator();
DebugAssert(alloc);
DebugAssert(alloc->UnderlyingAllocator::Contains(p));
return alloc->UnderlyingAllocator::Deallocate(p);
}
template <class UnderlyingAllocator>
bool TLSAllocator<UnderlyingAllocator>::TryDeallocate( void* p )
{
UnderlyingAllocator* alloc = GetCurrentAllocator();
if(!alloc)
return false;
if(!alloc->UnderlyingAllocator::Contains(p))
return false;
alloc->UnderlyingAllocator::Deallocate(p);
return true;
}
template <class UnderlyingAllocator>
bool TLSAllocator<UnderlyingAllocator>::Contains( const void* p )
{
UnderlyingAllocator* alloc = GetCurrentAllocator();
if(alloc && alloc->UnderlyingAllocator::Contains(p))
return true;
return false;
}
template <class UnderlyingAllocator>
size_t TLSAllocator<UnderlyingAllocator>::GetAllocatedMemorySize( ) const
{
size_t allocated = 0;
for(int i = 0; i < kMaxThreadTempAllocators; i++)
{
if(m_ThreadTempAllocators[i] != NULL)
allocated += m_ThreadTempAllocators[i]->UnderlyingAllocator::GetAllocatedMemorySize();
}
return allocated;
}
template <class UnderlyingAllocator>
size_t TLSAllocator<UnderlyingAllocator>::GetAllocatorSizeTotalUsed() const
{
size_t total = 0;
for(int i = 0; i < kMaxThreadTempAllocators; i++)
{
if(m_ThreadTempAllocators[i] != NULL)
total += m_ThreadTempAllocators[i]->UnderlyingAllocator::GetAllocatorSizeTotalUsed();
}
return total;
}
template <class UnderlyingAllocator>
size_t TLSAllocator<UnderlyingAllocator>::GetReservedSizeTotal() const
{
size_t total = 0;
for(int i = 0; i < kMaxThreadTempAllocators; i++)
{
if(m_ThreadTempAllocators[i] != NULL)
total += m_ThreadTempAllocators[i]->UnderlyingAllocator::GetReservedSizeTotal();
}
return total;
}
template <class UnderlyingAllocator>
size_t TLSAllocator<UnderlyingAllocator>::GetPtrSize( const void* ptr ) const
{
// all allocators have the same allocation header
return m_ThreadTempAllocators[0]->UnderlyingAllocator::GetPtrSize(ptr);
}
template <class UnderlyingAllocator>
ProfilerAllocationHeader* TLSAllocator<UnderlyingAllocator>::GetProfilerHeader( const void* ptr ) const
{
return m_ThreadTempAllocators[0]->UnderlyingAllocator::GetProfilerHeader(ptr);
}
template <class UnderlyingAllocator>
bool TLSAllocator<UnderlyingAllocator>::CheckIntegrity()
{
bool succes = true;
for(int i = 0; i < kMaxThreadTempAllocators; i++)
{
if(m_ThreadTempAllocators[i] != NULL)
succes &= m_ThreadTempAllocators[i]->UnderlyingAllocator::CheckIntegrity();
}
return succes;
}
template class TLSAllocator< StackAllocator >;
#endif // #if ENABLE_MEMORY_MANAGER
|