summaryrefslogtreecommitdiff
path: root/Runtime/Allocator/MemoryManager.h
blob: b37292c849ad9fae63f5b074aff478042c4542ed (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
#ifndef _MEMORY_MANAGER_H_
#define _MEMORY_MANAGER_H_

#include "Runtime/Misc/AllocatorLabels.h"
#include "Runtime/Allocator/BaseAllocator.h"
#include "Runtime/Utilities/FileStripped.h"

#if UNITY_XENON
#include "PlatformDependent/Xbox360/Source/XenonMemory.h"
#endif

#if ENABLE_MEMORY_MANAGER

class MemoryManager
{
public:
	MemoryManager();
	~MemoryManager();
	static void StaticInitialize();
	static void StaticDestroy();

	void ThreadInitialize(size_t tempSize = 0);
	void ThreadCleanup();

	bool IsInitialized() {return m_IsInitialized;}
	bool IsActive() {return m_IsActive;}

	void* Allocate(size_t size, int align, MemLabelRef label, int allocateOptions = kAllocateOptionNone, const char* file = NULL, int line = 0);
	void* Reallocate(void* ptr, size_t size, int align, MemLabelRef label, int allocateOptions = kAllocateOptionNone, const char* file = NULL, int line = 0);
	void  Deallocate(void* ptr);
	void  Deallocate(void* ptr, MemLabelRef label);

	BaseAllocator* GetAllocator(MemLabelRef label);
	int GetAllocatorIndex(BaseAllocator* alloc);
	BaseAllocator* GetAllocatorAtIndex( int index );

	MemLabelId AddCustomAllocator(BaseAllocator* allocator);
	void RemoveCustomAllocator(BaseAllocator* allocator);

	void FrameMaintenance(bool cleanup = false);
	
	static void* LowLevelAllocate( size_t size );
	static void* LowLevelCAllocate( size_t count, size_t size );
	static void* LowLevelReallocate( void* p, size_t size );
	static void  LowLevelFree( void* p );

	const char* GetAllocatorName( int i );
	const char* GetMemcatName( MemLabelRef label );

	size_t GetTotalAllocatedMemory();
	size_t GetTotalUnusedReservedMemory();
	size_t GetTotalReservedMemory();

	size_t GetTotalProfilerMemory();

	int GetAllocatorCount( );

	size_t GetAllocatedMemory( MemLabelRef label );
	int GetAllocCount( MemLabelRef label );
	size_t GetLargestAlloc(MemLabelRef label);

#if UNITY_XENON
	size_t GetRegisteredGFXDriverMemory(){ return xenon::GetGfxMemoryAllocated();}
#else
	size_t GetRegisteredGFXDriverMemory(){ return m_RegisteredGfxDriverMemory;}
#endif

	void StartLoggingAllocations(size_t logAllocationsThreshold = 0);
	void StopLoggingAllocations();

	void DisallowAllocationsOnThisThread();
	void ReallowAllocationsOnThisThread();
	void CheckDisalowAllocation();

	BaseAllocator* GetAllocatorContainingPtr(const void* ptr);

	static inline bool IsTempAllocatorLabel( MemLabelRef label ) { return label.label == kMemTempAllocId; }

	volatile static long m_LowLevelAllocated;
	volatile static long m_RegisteredGfxDriverMemory;
	
private:
#if ENABLE_MEM_PROFILER
	void RegisterAllocation(void* ptr, size_t size, BaseAllocator* alloc, MemLabelRef label, const char* function, const char* file, int line);
	ProfilerAllocationHeader* RegisterDeallocation(void* ptr, BaseAllocator* alloc, MemLabelRef label, const char* function);
#endif
	void InitializeMainThreadAllocators();

	static const int kMaxAllocators = 16;

	BaseAllocator*   m_FrameTempAllocator;
	BaseAllocator*   m_InitialFallbackAllocator;

	BaseAllocator*   m_Allocators[kMaxAllocators];
	BaseAllocator*   m_MainAllocators[kMaxAllocators];
	BaseAllocator*   m_ThreadAllocators[kMaxAllocators];

	int              m_NumAllocators;
	bool             m_LogAllocations;
	bool             m_IsInitialized;
	bool             m_IsActive;


	size_t           m_LogAllocationsThreshold;

	struct LabelInfo
	{
		BaseAllocator* alloc;
		size_t allocatedMemory;
		int numAllocs;
		size_t largestAlloc;
	};
	LabelInfo   m_AllocatorMap[kMemLabelCount];
};
MemoryManager& GetMemoryManager();

#endif
#endif