#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 : idx_(0) {}
HeapHandle(const HeapHandle& h) NLIB_NOEXCEPT : idx_(h.idx_) {}
HeapHandle& operator=(const HeapHandle& rhs) NLIB_NOEXCEPT {
idx_ = rhs.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 : idx_(h) {}
int32_t 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.idx_ == rhs.idx_;
}
inline bool operator!=(
const HeapHandle& lhs,
const HeapHandle& rhs) NLIB_NOEXCEPT {
return lhs.idx_ != rhs.idx_;
}
HeapHandle GetThreadDefaultHeapHandle() NLIB_NOEXCEPT;
void SetThreadDefaultHeapHandle(HeapHandle h) NLIB_NOEXCEPT;
class IDisposer {
public:
IDisposer(HeapHandle h) NLIB_NOEXCEPT : handle_(h) {}
void Destroy() NLIB_NOEXCEPT {
this->~IDisposer();
}
HeapHandle GetHeapHandle() const NLIB_NOEXCEPT { return handle_; }
protected:
virtual ~IDisposer() NLIB_NOEXCEPT {
handle_.free(this);
}
private:
HeapHandle handle_;
};
#endif // SAMPLES_SOURCES_HEAP_GAMEHEAP_GAMEHEAP_H_