#ifndef __ALLOCATOR_H__
#define __ALLOCATOR_H__
#include <vector>
/*===============================================*/
/* MemoryInfo */
/*===============================================*/
//! メモリ情報
struct MemoryInfo;
/*===============================================*/
/* IAllocator */
/*===============================================*/
//! メモリ生成者インターフェース
class IAllocator
{
public :
//! デストラクタ
virtual ~IAllocator() {}
//! メモリ作成
virtual void* Allocate( size_t, const char*, int ) = 0;
//! メモリ破棄
virtual void Deallocate( void* ) = 0;
};
/*===============================================*/
/* DefaultAllocator */
/*===============================================*/
//! デフォルトメモリ割り当て
/*!
* メモリ生成:new メモリ破棄:delete をするだけのクラス
*
*/
class DefaultAllocator : public IAllocator
{
//! コンストラクタ
DefaultAllocator();
public :
//! デストラクタ
~DefaultAllocator();
//! 実体取得
//! @return 実体
static DefaultAllocator* Get();
//! メモリ生成
//! @param [in] s サイズ
//! @param [in] f ファイル
//! @param [in] l ライン
//! @return メモリ
void* Allocate( size_t s, const char* f, int l );
//! メモリ破棄
//! @param [out] p メモリ
void Deallocate( void* p );
};
/*===============================================*/
/* AllocateManager */
/*===============================================*/
//! メモリ割り当て管理
class AllocateManager
{
IAllocator* m_alloc; //! メモリ管理
public :
//! コンストラクタ
AllocateManager();
//! デストラクタ
~AllocateManager();
//! メモリ生成
//! @param [in] s サイズ
//! @param [in] f ファイル
//! @param [in] l ライン
void* Allocate( size_t s, const char* f, int l );
//! メモリ破棄
//! @param [out] p メモリ
void Deallocate( void* p );
//! メモリ作成者設定
//! NULLを代入するとデフォルトのメモリ生成状態となるので @n
//! 破棄する場合はNULLを設定すること
//! @param [in,out] alloc メモリ生成者
void Set( IAllocator* alloc );
};
/*===============================================*/
/* Alloc */
/*===============================================*/
//! メモリ割り当て
class Alloc
{
AllocateManager m_manager; //! メモリ管理
//! コンストラクタ
Alloc();
public :
//! 実体取得
//! @return 実体
static Alloc* Get();
//! メモリ生成
//! @param [in] s サイズ
//! @param [in] f ファイル名
//! @param [in] l ライン
//! @return メモリ
void* Allocate( size_t s, const char* f, int l );
//! メモリ破棄
//! @param [out] p メモリ
void Deallocate( void* p );
//! メモリ生成者設定
//! @param [in,out] alloc メモリ生成者
void Set( IAllocator* alloc );
};
/*===============================================*/
/* BaseAllocator */
/*===============================================*/
//! メモリ割り当てベース
/*!
* メモリ割り当てに必要な制約を受け持つクラス @n
* 通常はこのクラスを継承して使用する
*
*/
class BaseAllocator : public IAllocator
{
public :
//! コンストラクタ
BaseAllocator();
//! デストラクタ
virtual ~BaseAllocator();
//! メモリ生成
virtual void* Allocate( size_t, const char*, int ) = 0;
//! メモリ破棄
virtual void Deallocate( void* ) = 0;
};
/*===============================================*/
/* Allocator */
/*===============================================*/
//! メモリ割り当て
/*!
* メモリリーク検知を行う
*
*/
class Allocator : public BaseAllocator
{
private :
//! コンテナ
typedef std::vector<MemoryInfo*> container;
//! イテレータ
typedef container::iterator iterator;
//! イテレータ
typedef container::const_iterator const_iterator;
container m_container; //!< メモリ情報コンテナ
public :
//! コンストラクタ
Allocator();
//! デストラクタ
~Allocator();
//! メモリ生成
//! @param [in] s サイズ
//! @param [in] f ファイル
//! @param [in] l ライン
//! @return メモリ
void* Allocate( size_t s, const char* f, int l );
//! メモリ破棄
//! @param [out] p メモリ
void Deallocate( void* p );
//! リークチェック
//! @retval true メモリリークしていない
//! @retval fales メモリリークしている
bool Check() const;
};
#endif//__ALLOCATOR_H__
最終更新:2009年02月14日 15:51