summaryrefslogtreecommitdiff
path: root/Runtime/Allocator/TLSAllocator.h
blob: a7520ffa45e597fb794efac73a784a429d99bc27 (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
#ifndef TLS_ALLOCATOR_H_
#define TLS_ALLOCATOR_H_

#if ENABLE_MEMORY_MANAGER

#include "Runtime/Allocator/BaseAllocator.h"
#include "Runtime/Threads/ThreadSpecificValue.h"


// TLS Allocator is an indirection to a real allocator
// Has a tls value pointing to the threadspecific allocator if unique per thread.

template <class UnderlyingAllocator>
class TLSAllocator : public BaseAllocator
{
public:
	// when constructing it will be from the main thread
	TLSAllocator(const char* name);
	virtual ~TLSAllocator(); 

	virtual void* Allocate(size_t size, int align); 
	virtual void* Reallocate (void* p, size_t size, int align);
	virtual void  Deallocate (void* p);
	virtual bool  Contains (const void* p);

	virtual size_t GetAllocatedMemorySize() const;
	virtual size_t GetAllocatorSizeTotalUsed() const;
	virtual size_t GetReservedSizeTotal() const;

	virtual size_t GetPtrSize(const void* ptr) const;
	virtual ProfilerAllocationHeader* GetProfilerHeader(const void* ptr) const;

	virtual void ThreadInitialize(BaseAllocator* allocator);
	virtual void ThreadCleanup();

	virtual bool CheckIntegrity();

	virtual bool IsAssigned() const;
	bool TryDeallocate (void* p);

	UnderlyingAllocator* GetCurrentAllocator();
	virtual void FrameMaintenance(bool cleanup);

private:
	// because TLS values have to be static on some platforms, this is made static
	// and only one instance of the TLS is allowed 
	static UNITY_TLS_VALUE(UnderlyingAllocator*) m_UniqueThreadAllocator; // the memorymanager holds the list of allocators
	static int s_NumberOfInstances;

	static const int      kMaxThreadTempAllocators = 128;
	UnderlyingAllocator*  m_ThreadTempAllocators[kMaxThreadTempAllocators];
};

#endif
#endif