/*---------------------------------------------------------------------------* Project: NintendoWare File: snd_InstancePool.h Copyright (C)2009-2010 Nintendo Co., Ltd./HAL Laboratory, Inc. All rights reserved. These coded instructions, statements, and computer programs contain proprietary information of Nintendo of America Inc. and/or Nintendo Company Ltd., and are protected by Federal copyright law. 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. $Revision: 20266 $ *---------------------------------------------------------------------------*/ #ifndef NW_SND_INSTANCE_POOL_H_ #define NW_SND_INSTANCE_POOL_H_ #include #include namespace nw { namespace snd { namespace internal { /* ======================================================================== PoolImpl class ======================================================================== */ class PoolImpl { public: PoolImpl() : m_pNext( NULL ) {} protected: u32 CreateImpl( void* buffer, size_t size, u32 objSize ); void DestroyImpl( void* buffer, unsigned long size ); int CountImpl() const; void* AllocImpl(); void FreeImpl( void* ptr ); private: PoolImpl* m_pNext; }; /* ======================================================================== MemoryPool class ======================================================================== */ template< typename T > class MemoryPool : private internal::PoolImpl { public: /*---------------------------------------------------------------------------* Name: Create Description: 指定したメモリ領域をプールに割り当てます Arguments: buffer - メモリ領域の開始アドレス size - メモリ領域のサイズ Returns: 作成したプール中のインスタンス数 *---------------------------------------------------------------------------*/ u32 Create( void* buffer, size_t size ) { u32 objSize = ( sizeof( T ) > sizeof( MemoryPool* ) ) ? sizeof( T ): sizeof( MemoryPool* ) ; return CreateImpl( buffer, size, objSize ); } /*---------------------------------------------------------------------------* Name: Destroy Description: 指定したメモリ領域をプールから解放します 解放するためにはメモリ領域が Free されてプールに格納されている 必要があります。 Arguments: buffer - メモリ領域の開始アドレス size - メモリ領域のサイズ Returns: 無し *---------------------------------------------------------------------------*/ void Destroy( void* buffer, unsigned long size ) { DestroyImpl( buffer, size ); } /*---------------------------------------------------------------------------* Name: Count Description: プール中のインスタンス数を取得します。 Arguments: 無し Returns: インスタンス数 *---------------------------------------------------------------------------*/ int Count() const { return CountImpl(); } /*---------------------------------------------------------------------------* Name: Alloc Description: インスタンスを確保する。 Arguments: 無し Returns: 確保したインスタンス、確保できなければNULL *---------------------------------------------------------------------------*/ T* Alloc() { return static_cast( AllocImpl() ); } /*---------------------------------------------------------------------------* Name: Free Description: インスタンスをプールに解放する Arguments: obj - インスタンス Returns: 無し *---------------------------------------------------------------------------*/ void Free( T* obj ) { FreeImpl( obj ); } }; /* ======================================================================== InstancePool class ======================================================================== */ template< typename T > class InstancePool : private internal::PoolImpl { public: /*---------------------------------------------------------------------------* Name: Create Description: 指定したメモリ領域をプールに割り当てます Arguments: buffer - メモリ領域の開始アドレス size - メモリ領域のサイズ Returns: 作成したプール中のインスタンス数 *---------------------------------------------------------------------------*/ u32 Create( void* buffer, unsigned long size ) { u32 objSize = ( sizeof( T ) > sizeof( InstancePool* ) ) ? sizeof( T ): sizeof( InstancePool* ) ; return CreateImpl( buffer, size, objSize ); } /*---------------------------------------------------------------------------* Name: Destroy Description: 指定したメモリ領域をプールから解放します 解放するためにはメモリ領域が Free されてプールに格納されている 必要があります。 Arguments: buffer - メモリ領域の開始アドレス size - メモリ領域のサイズ Returns: 無し *---------------------------------------------------------------------------*/ void Destroy( void* buffer, unsigned long size ) { DestroyImpl( buffer, size ); } /*---------------------------------------------------------------------------* Name: Count Description: プール中のインスタンス数を取得します。 Arguments: 無し Returns: インスタンス数 *---------------------------------------------------------------------------*/ int Count() const { return CountImpl(); } /*---------------------------------------------------------------------------* Name: Alloc Description: インスタンスを確保する。 Arguments: 無し Returns: 確保したインスタンス、確保できなければNULL *---------------------------------------------------------------------------*/ T* Alloc() { void *ptr = AllocImpl(); if ( ptr == NULL ) return NULL; return new( ptr ) T(); // コンストラクタ呼びだし } /*---------------------------------------------------------------------------* Name: Free Description: インスタンスをプールに解放する Arguments: obj - インスタンス Returns: 無し *---------------------------------------------------------------------------*/ void Free( T* obj ) { if ( obj == NULL ) return; obj->~T(); // デストラクタ呼びだし FreeImpl( obj ); } }; } // namespace nw::snd::internal } // namespace nw::snd } // namespace nw #endif /* NW_SND_INSTANCE_POOL_H_ */