nlib
heap/gameheap/gameheap.h

Refer to the heap/gameheap/gameheap_appcode.cpp documentation.

#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 : 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* 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 : 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_;
}
// 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 : 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 handle_; }
protected:
virtual ~IDisposer() NLIB_NOEXCEPT {
// Multpiple inheritance is not available.
handle_.free(this);
}
private:
HeapHandle handle_;
};
#endif // SAMPLES_SOURCES_HEAP_GAMEHEAP_GAMEHEAP_H_