nlib
heap/gameheap/gameheap.h

heap/gameheap/gameheap_appcode.cpp の説明を御覧ください

#pragma once
#ifndef SAMPLES_SOURCES_HEAP_GAMEHEAP_GAMEHEAP_H_
#define SAMPLES_SOURCES_HEAP_GAMEHEAP_GAMEHEAP_H_
#include "nn/nlib/Config.h"
class HeapMgr;
class HeapHandle {
public:
static HeapHandle createHeap(size_t size) NLIB_NOEXCEPT;
static HeapHandle createHeap(void* mem, size_t size) NLIB_NOEXCEPT;
static void destroyHeap(HeapHandle h) 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* realloc(void* ptr, size_t new_size) NLIB_NOEXCEPT;
void freeAll() NLIB_NOEXCEPT;
// const void* getStartAddress() const NLIB_NOEXCEPT;
// const void* getEndAddress() const NLIB_NOEXCEPT;
// size_t getSize() const NLIB_NOEXCEPT;
// size_t getFreeSize() const NLIB_NOEXCEPT;
// size_t getMaxAllocatableSize(size_t alignment = 8) NLIB_NOEXCEPT;
// bool isInclude(const void* ptr) const NLIB_NOEXCEPT;
// void dump() const 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;
}
// You can register default heap handle per a thread.
// To make a different thread has a different heap handle,
// you can avoid the overhead of locking in memory allocation.
// Also, freeing memory from different thread is possible,
// and you can move the ownership of memory from a thread to another thread.
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 {
nlib_printf("IDisposer::Destroy() begin\n");
this->~IDisposer();
nlib_printf("IDisposer::Destroy() end\n");
}
HeapHandle GetHeapHandle() const NLIB_NOEXCEPT { return m_Handle; }
protected:
virtual ~IDisposer() NLIB_NOEXCEPT {
// Multpiple inheritance is not available.
m_Handle.free(this);
}
private:
HeapHandle m_Handle;
};
#endif // SAMPLES_SOURCES_HEAP_GAMEHEAP_GAMEHEAP_H_