From 15740faf9fe9fe4be08965098bbf2947e096aeeb Mon Sep 17 00:00:00 2001 From: chai Date: Wed, 14 Aug 2019 22:50:43 +0800 Subject: +Unity Runtime code --- Runtime/Allocator/TLSAllocator.cpp | 198 +++++++++++++++++++++++++++++++++++++ 1 file changed, 198 insertions(+) create mode 100644 Runtime/Allocator/TLSAllocator.cpp (limited to 'Runtime/Allocator/TLSAllocator.cpp') diff --git a/Runtime/Allocator/TLSAllocator.cpp b/Runtime/Allocator/TLSAllocator.cpp new file mode 100644 index 0000000..00bf88b --- /dev/null +++ b/Runtime/Allocator/TLSAllocator.cpp @@ -0,0 +1,198 @@ +#include "UnityPrefix.h" +#include "TLSAllocator.h" +#include "Runtime/Threads/Thread.h" +#include "Runtime/Allocator/StackAllocator.h" + +#if ENABLE_MEMORY_MANAGER + +template +int TLSAllocator::s_NumberOfInstances = 0; + +template +UNITY_TLS_VALUE(UnderlyingAllocator*) TLSAllocator::m_UniqueThreadAllocator; + +template +TLSAllocator::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 +TLSAllocator::~TLSAllocator() +{ + s_NumberOfInstances--; +} + +template +void TLSAllocator::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 +void TLSAllocator::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 +void TLSAllocator::FrameMaintenance(bool cleanup) +{ + Assert(m_UniqueThreadAllocator->GetAllocatedMemorySize() == 0); +} + +template +bool TLSAllocator::IsAssigned() const +{ + return m_UniqueThreadAllocator != NULL; +} + +template +UnderlyingAllocator* TLSAllocator::GetCurrentAllocator() +{ + return m_UniqueThreadAllocator; +} + + +template +void* TLSAllocator::Allocate( size_t size, int align ) +{ + UnderlyingAllocator* alloc = GetCurrentAllocator(); + return alloc ? alloc->UnderlyingAllocator::Allocate(size, align) : NULL; +} + +template +void* TLSAllocator::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 +void TLSAllocator::Deallocate( void* p ) +{ + UnderlyingAllocator* alloc = GetCurrentAllocator(); + DebugAssert(alloc); + DebugAssert(alloc->UnderlyingAllocator::Contains(p)); + return alloc->UnderlyingAllocator::Deallocate(p); +} + +template +bool TLSAllocator::TryDeallocate( void* p ) +{ + UnderlyingAllocator* alloc = GetCurrentAllocator(); + if(!alloc) + return false; + + if(!alloc->UnderlyingAllocator::Contains(p)) + return false; + + alloc->UnderlyingAllocator::Deallocate(p); + return true; +} + + +template +bool TLSAllocator::Contains( const void* p ) +{ + UnderlyingAllocator* alloc = GetCurrentAllocator(); + if(alloc && alloc->UnderlyingAllocator::Contains(p)) + return true; + return false; +} + +template +size_t TLSAllocator::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 +size_t TLSAllocator::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 +size_t TLSAllocator::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 +size_t TLSAllocator::GetPtrSize( const void* ptr ) const +{ + // all allocators have the same allocation header + return m_ThreadTempAllocators[0]->UnderlyingAllocator::GetPtrSize(ptr); +} + +template +ProfilerAllocationHeader* TLSAllocator::GetProfilerHeader( const void* ptr ) const +{ + return m_ThreadTempAllocators[0]->UnderlyingAllocator::GetProfilerHeader(ptr); +} + + +template +bool TLSAllocator::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 -- cgit v1.1-26-g67d0