summaryrefslogtreecommitdiff
path: root/Runtime/Allocator/DynamicHeapAllocator.h
blob: 29aed9d08f4082492e9c055efc35b50c2e1b1196 (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
#ifndef DYNAMIC_HEAP_ALLOCATOR_H_
#define DYNAMIC_HEAP_ALLOCATOR_H_

#if ENABLE_MEMORY_MANAGER

#include "BaseAllocator.h"
#include "Runtime/Threads/Mutex.h"
#include "Runtime/Allocator/LowLevelDefaultAllocator.h"
#include "Runtime/Utilities/LinkedList.h"

template<class LLAllocator>
class DynamicHeapAllocator : public BaseAllocator
{
public:

	DynamicHeapAllocator( UInt32 poolIncrementSize, size_t splitLimit, bool useLocking, const char* name);
	~DynamicHeapAllocator();

	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 bool CheckIntegrity();
	virtual bool ValidatePointer(void* ptr);

	virtual size_t GetPtrSize(const void* ptr) const;

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

	// return the free block count for each pow2
	virtual void GetFreeBlockCount(int* freeCount, int size);
	// return the used block count for each pow2
	virtual void GetUsedBlockCount(int* usedCount, int size);

private:
	struct PoolElement : public ListElement
	{
		bool Contains(const void* ptr) const
		{
			return ptr >= memoryBase && ptr < memoryBase + memorySize;
		}
		void* tlsfPool;
		char* memoryBase;
		UInt32 memorySize;
		UInt32 allocationCount;
		UInt32 allocationSize;
	};

	typedef List<PoolElement> PoolList;

	size_t m_SplitLimit; 
	PoolElement& GetActivePool(size_t size);
	PoolList& GetPoolList(size_t size);

	PoolList m_SmallTLSFPools;
	PoolList m_LargeTLSFPools;

	Mutex m_DHAMutex;
	bool m_UseLocking;
	size_t m_RequestedPoolSize;

	struct LargeAllocations
	{
		LargeAllocations* next;
		char* allocation;
		void* returnedPtr;
		size_t size;
	};
	LargeAllocations* m_FirstLargeAllocation;

	PoolElement* FindPoolFromPtr(const void* ptr);

	void RegisterAllocation(const void* p);
	void RegisterDeallocation(const void* p);

	void* AddHeaderAndFooter( void* ptr, size_t size, int align ) const;
};

#endif
#endif