summaryrefslogtreecommitdiff
path: root/Runtime/Allocator/FixedHeapAllocator.cpp
blob: 1cc3c712f9a6cca3ae11477a3a50f70316b1306d (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
#include "UnityPrefix.h"
#include "FixedHeapAllocator.h"
#include "tlsf/tlsf.h"

#if FIXED_HEAP_ALLOC_COUNT_USED
#define ALLOC_USED_MEM_INC(ptr) if (ptr) { m_nSizeUsed += tlsf_block_size(ptr); }
#define ALLOC_USED_MEM_DEC(ptr) if (ptr) { m_nSizeUsed -= tlsf_block_size(ptr); }
#else
#define ALLOC_USED_MEM_INC(ptr)
#define ALLOC_USED_MEM_DEC(ptr)
#endif

FixedHeapAllocator::FixedHeapAllocator(void* pMemoryBase, UInt32 nMemorySize, const char* name)
: BaseAllocator(name)
, m_TlsfPool(0)
, m_pMemoryBase(pMemoryBase)
, m_nMemorySize(nMemorySize)
#if FIXED_HEAP_ALLOC_COUNT_USED
, m_nSizeUsed(0)
#endif
{
	m_TlsfPool = tlsf_create(pMemoryBase, nMemorySize);
}


FixedHeapAllocator::~FixedHeapAllocator()
{
	tlsf_destroy(m_TlsfPool);
}

void* FixedHeapAllocator::Allocate (size_t size, int align)
{
	void* addr = tlsf_memalign(m_TlsfPool, align, size);
	ALLOC_USED_MEM_INC(addr);
	return addr;
}

void* FixedHeapAllocator::Reallocate(void* p, size_t size, size_t align)
{
	ALLOC_USED_MEM_DEC(p);
	void* addr = tlsf_realloc(m_TlsfPool, p, size);
	ALLOC_USED_MEM_INC(addr);
	return addr;
}

void FixedHeapAllocator::Deallocate(void* p)
{
	ALLOC_USED_MEM_DEC(p);
	return tlsf_free(m_TlsfPool, p);
}

UInt32 FixedHeapAllocator::GetPtrSize(void* p)
{
	return (UInt32)tlsf_block_size(p);
}

bool FixedHeapAllocator::Contains(const void* p)
{
	return (p >= m_pMemoryBase && p < (char*)m_pMemoryBase + m_nMemorySize);
}

bool FixedHeapAllocator::CheckIntegrity()
{
	return tlsf_check_heap(m_TlsfPool);
}

#undef ALLOC_USED_MEM_INC
#undef ALLOC_USED_MEM_DEC