summaryrefslogtreecommitdiff
path: root/Runtime/Allocator/DualThreadAllocator.cpp
blob: 921ecf982910e5fc03a7cad4c99dd8500c6817fc (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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#include "UnityPrefix.h"
#include "DualThreadAllocator.h"
#include "Runtime/Threads/Thread.h"
#include "Runtime/Allocator/DynamicHeapAllocator.h"

#if ENABLE_MEMORY_MANAGER

class DelayedPointerDeletionManager
{
public:
	DelayedPointerDeletionManager(BaseAllocator* mainAlloc, BaseAllocator* threadAlloc) : 
		m_HasPendingDeletes (0), 
		m_MainAlloctor (mainAlloc),
		m_ThreadAlloctor (threadAlloc),
		m_MainThreadPendingPointers(NULL),
		m_MainThreadPendingCount (0),
		m_MainThreadPendingReserved (0) {}
	~DelayedPointerDeletionManager(){ CleanupPendingMainThreadPointers(); }

	bool HasPending() {return m_HasPendingDeletes != 0;}
	void AddPointerToMainThreadDealloc( void* ptr) ;
	void CleanupPendingMainThreadPointers() ;
	void DeallocateLocalMemory();
private:
	void GrowPendingBuffer();
	void** m_MainThreadPendingPointers;
	UInt32 m_MainThreadPendingCount;
	UInt32 m_MainThreadPendingReserved;
	
	volatile int m_HasPendingDeletes;
	BaseAllocator* m_MainAlloctor;
	BaseAllocator* m_ThreadAlloctor;
	Mutex m_MainThreadPendingPointersMutex;
};

void DelayedPointerDeletionManager::AddPointerToMainThreadDealloc( void* ptr) 
{
	Mutex::AutoLock autolock(m_MainThreadPendingPointersMutex);
	if(++m_MainThreadPendingCount > m_MainThreadPendingReserved)
		GrowPendingBuffer();
	m_MainThreadPendingPointers[m_MainThreadPendingCount-1] = ptr;
	m_HasPendingDeletes = 1;
}

void DelayedPointerDeletionManager::CleanupPendingMainThreadPointers() 
{
	Mutex::AutoLock autolock(m_MainThreadPendingPointersMutex);
	Assert(Thread::CurrentThreadIsMainThread());
	m_HasPendingDeletes = 0;

	for(UInt32 i = 0; i < m_MainThreadPendingCount; ++i)
		m_MainAlloctor->Deallocate(m_MainThreadPendingPointers[i]);
	m_MainThreadPendingCount = 0;
}

void DelayedPointerDeletionManager::DeallocateLocalMemory() 
{ 
	Assert(!m_HasPendingDeletes && m_MainThreadPendingCount == 0);
	m_ThreadAlloctor->Deallocate(m_MainThreadPendingPointers);
	m_MainThreadPendingPointers = NULL;
	m_MainThreadPendingReserved = 0;
};

void DelayedPointerDeletionManager::GrowPendingBuffer()
{
	const UInt32 kInitialBufferSize = 128;
	m_MainThreadPendingReserved = std::max(m_MainThreadPendingReserved*2, kInitialBufferSize);
	m_MainThreadPendingPointers = (void**) m_ThreadAlloctor->Reallocate(m_MainThreadPendingPointers, m_MainThreadPendingReserved*sizeof(void*), kDefaultMemoryAlignment);
}


template <class UnderlyingAllocator>
DualThreadAllocator<UnderlyingAllocator>::DualThreadAllocator(const char* name, BaseAllocator* mainAllocator, BaseAllocator* threadAllocator)
: BaseAllocator(name)
{
	m_MainAllocator = (UnderlyingAllocator*)mainAllocator;
	m_ThreadAllocator = (UnderlyingAllocator*)threadAllocator;
	m_DelayedDeletion = NULL;
}

template <class UnderlyingAllocator>
void DualThreadAllocator<UnderlyingAllocator>::ThreadCleanup()
{
	if(Thread::CurrentThreadIsMainThread())
		UNITY_DELETE(m_DelayedDeletion, kMemManager);
}

template <class UnderlyingAllocator>
void DualThreadAllocator<UnderlyingAllocator>::FrameMaintenance(bool cleanup)
{
	if(m_DelayedDeletion)
	{
		m_DelayedDeletion->CleanupPendingMainThreadPointers();
		if(cleanup)
			m_DelayedDeletion->DeallocateLocalMemory();
	}
}

template <class UnderlyingAllocator>
UnderlyingAllocator* DualThreadAllocator<UnderlyingAllocator>::GetCurrentAllocator()
{
	if(Thread::CurrentThreadIsMainThread())
		return m_MainAllocator;
	else
		return m_ThreadAllocator;
}


template <class UnderlyingAllocator>
void* DualThreadAllocator<UnderlyingAllocator>::Allocate( size_t size, int align )
{
	UnderlyingAllocator* alloc = GetCurrentAllocator();
	bool isMainThread = alloc == m_MainAllocator;
	if(isMainThread && m_DelayedDeletion && m_DelayedDeletion->HasPending())
		m_DelayedDeletion->CleanupPendingMainThreadPointers();

	return alloc->UnderlyingAllocator::Allocate(size, align);
}

template <class UnderlyingAllocator>
void* DualThreadAllocator<UnderlyingAllocator>::Reallocate( void* p, size_t size, int align )
{ 
	UnderlyingAllocator* alloc = GetCurrentAllocator();
	
	if(alloc->UnderlyingAllocator::Contains(p))
		return alloc->UnderlyingAllocator::Reallocate(p, size, align);
	
	UnderlyingAllocator* containingAlloc = NULL;
	if (alloc == m_MainAllocator)
	{
		Assert(m_ThreadAllocator->UnderlyingAllocator::Contains(p));
		containingAlloc = m_ThreadAllocator;
	}
	else
	{
		Assert(m_MainAllocator->UnderlyingAllocator::Contains(p));
		containingAlloc = m_MainAllocator;
	}

	size_t oldSize = containingAlloc->UnderlyingAllocator::GetPtrSize(p);
	void* ptr = alloc->UnderlyingAllocator::Allocate(size, align);
	memcpy(ptr, p, std::min(size,oldSize));
	Deallocate(p);
	return ptr;
}

template <class UnderlyingAllocator>
void DualThreadAllocator<UnderlyingAllocator>::Deallocate( void* p )
{
	UnderlyingAllocator* alloc = GetCurrentAllocator();
	
	if(alloc->UnderlyingAllocator::Contains(p))
		return alloc->UnderlyingAllocator::Deallocate(p);

	if (alloc == m_MainAllocator)
	{
		DebugAssert(m_ThreadAllocator->UnderlyingAllocator::Contains(p));
		m_ThreadAllocator->UnderlyingAllocator::Deallocate(p);
	}
	else
	{
		DebugAssert(m_MainAllocator->UnderlyingAllocator::Contains(p));
		if(!m_DelayedDeletion)
		{
			SET_ALLOC_OWNER(NULL);
			m_DelayedDeletion = UNITY_NEW(DelayedPointerDeletionManager(m_MainAllocator, m_ThreadAllocator), kMemManager);
		}
		m_DelayedDeletion->AddPointerToMainThreadDealloc(p);
	}
}

template <class UnderlyingAllocator>
bool DualThreadAllocator<UnderlyingAllocator>::TryDeallocate( void* p )
{
	UnderlyingAllocator* alloc = GetCurrentAllocator();

	if(alloc->UnderlyingAllocator::Contains(p))
	{
		alloc->UnderlyingAllocator::Deallocate(p);
		return true;
	}

	if (m_ThreadAllocator->UnderlyingAllocator::Contains(p))
	{
		m_ThreadAllocator->UnderlyingAllocator::Deallocate(p);
		return true;
	}
	if(m_MainAllocator->UnderlyingAllocator::Contains(p))
	{
		if(!m_DelayedDeletion)
			m_DelayedDeletion = UNITY_NEW(DelayedPointerDeletionManager(m_MainAllocator, m_ThreadAllocator), kMemManager);
		m_DelayedDeletion->AddPointerToMainThreadDealloc(p);
		return true;
	}
	return false;
}


template <class UnderlyingAllocator>
bool DualThreadAllocator<UnderlyingAllocator>::Contains( const void* p )
{
	UnderlyingAllocator* alloc = GetCurrentAllocator();
	if(alloc->UnderlyingAllocator::Contains(p))
		return true;
	if(m_ThreadAllocator->UnderlyingAllocator::Contains(p))
		return true;
	if(m_MainAllocator->UnderlyingAllocator::Contains(p))
		return true;
	return false;
}

template <class UnderlyingAllocator>
size_t DualThreadAllocator<UnderlyingAllocator>::GetAllocatedMemorySize( ) const
{
	return m_MainAllocator->GetAllocatedMemorySize() + (m_ThreadAllocator?m_ThreadAllocator->GetAllocatedMemorySize():0);
}

template <class UnderlyingAllocator>
size_t DualThreadAllocator<UnderlyingAllocator>::GetAllocatorSizeTotalUsed() const
{
	return m_MainAllocator->GetAllocatorSizeTotalUsed() + (m_ThreadAllocator?m_ThreadAllocator->GetAllocatorSizeTotalUsed():0);
}

template <class UnderlyingAllocator>
size_t DualThreadAllocator<UnderlyingAllocator>::GetReservedSizeTotal() const
{
	return m_MainAllocator->GetReservedSizeTotal() + (m_ThreadAllocator?m_ThreadAllocator->GetReservedSizeTotal():0);
}

template <class UnderlyingAllocator>
size_t DualThreadAllocator<UnderlyingAllocator>::GetPtrSize( const void* ptr ) const
{
	// all allocators have the same allocation header
	return m_MainAllocator->UnderlyingAllocator::GetPtrSize(ptr);
}

template <class UnderlyingAllocator>
ProfilerAllocationHeader* DualThreadAllocator<UnderlyingAllocator>::GetProfilerHeader( const void* ptr ) const
{
	return m_MainAllocator->UnderlyingAllocator::GetProfilerHeader(ptr);
}


template <class UnderlyingAllocator>
bool DualThreadAllocator<UnderlyingAllocator>::CheckIntegrity()
{
	bool valid = m_ThreadAllocator->UnderlyingAllocator::CheckIntegrity();
	if(Thread::CurrentThreadIsMainThread())
		valid &= m_MainAllocator->UnderlyingAllocator::CheckIntegrity();
	Assert(valid);
	return valid;
}

template <class UnderlyingAllocator>
bool DualThreadAllocator<UnderlyingAllocator>::ValidatePointer(void* ptr)
{
	UnderlyingAllocator* alloc = GetCurrentAllocator();
	return alloc->UnderlyingAllocator::ValidatePointer(ptr);
}

template class DualThreadAllocator< DynamicHeapAllocator< LowLevelAllocator > >;

#endif // #if ENABLE_MEMORY_MANAGER