#pragma once
#ifndef SAMPLES_SOURCES_HEAP_GAMEHEAP_GAMEHEAP_H_
#define SAMPLES_SOURCES_HEAP_GAMEHEAP_GAMEHEAP_H_
class HeapMgr;
class HeapHandle {
public:
static HeapHandle createHeap(
void* mem,
size_t size)
NLIB_NOEXCEPT;
HeapHandle() NLIB_NOEXCEPT : m_Idx(0) {}
HeapHandle(const HeapHandle& h) NLIB_NOEXCEPT : m_Idx(h.m_Idx) {}
HeapHandle& operator=(const HeapHandle& rhs) NLIB_NOEXCEPT {
m_Idx = rhs.m_Idx;
return *this;
}
void* alloc(size_t size, size_t alignment = 8) NLIB_NOEXCEPT;
void free(void* ptr) NLIB_NOEXCEPT;
void freeAll() NLIB_NOEXCEPT;
bool isValid() NLIB_NOEXCEPT;
bool isClean() NLIB_NOEXCEPT;
private:
explicit HeapHandle(int h) NLIB_NOEXCEPT : m_Idx(h) {}
int32_t m_Idx;
friend class HeapMgr;
friend HeapHandle GetThreadDefaultHeapHandle() NLIB_NOEXCEPT;
friend void SetThreadDefaultHeapHandle(HeapHandle h) NLIB_NOEXCEPT;
friend bool operator==(const HeapHandle& lhs, const HeapHandle& rhs) NLIB_NOEXCEPT;
friend bool operator!=(const HeapHandle& lhs, const HeapHandle& rhs) NLIB_NOEXCEPT;
};
inline bool operator==(const HeapHandle& lhs, const HeapHandle& rhs) NLIB_NOEXCEPT {
return lhs.m_Idx == rhs.m_Idx;
}
inline bool operator!=(
const HeapHandle& lhs,
const HeapHandle& rhs) NLIB_NOEXCEPT {
return lhs.m_Idx != rhs.m_Idx;
}
HeapHandle GetThreadDefaultHeapHandle() NLIB_NOEXCEPT;
void SetThreadDefaultHeapHandle(HeapHandle h) NLIB_NOEXCEPT;
class IDisposer {
public:
IDisposer(HeapHandle h) NLIB_NOEXCEPT : m_Handle(h) {}
void Destroy() NLIB_NOEXCEPT {
this->~IDisposer();
}
HeapHandle GetHeapHandle() const NLIB_NOEXCEPT { return m_Handle; }
protected:
virtual ~IDisposer() NLIB_NOEXCEPT {
m_Handle.free(this);
}
private:
HeapHandle m_Handle;
};
#endif // SAMPLES_SOURCES_HEAP_GAMEHEAP_GAMEHEAP_H_