nlib
heap/gameheap/gameheap.h

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

/*--------------------------------------------------------------------------------*
Project: CrossRoad
Copyright (C)Nintendo All rights reserved.
These coded instructions, statements, and computer programs contain proprietary
information of Nintendo and/or its licensed developers and are protected by
national and international copyright laws. They may not be disclosed to third
parties or copied or duplicated in any form, in whole or in part, without the
prior written consent of Nintendo.
The content herein is highly confidential and should be handled accordingly.
*--------------------------------------------------------------------------------*/
#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_