1 /*---------------------------------------------------------------------------* 2 Project: NintendoWare 3 File: snd_InstancePool.h 4 5 Copyright (C)2009-2010 Nintendo Co., Ltd./HAL Laboratory, Inc. All rights reserved. 6 7 These coded instructions, statements, and computer programs contain 8 proprietary information of Nintendo of America Inc. and/or Nintendo 9 Company Ltd., and are protected by Federal copyright law. They may 10 not be disclosed to third parties or copied or duplicated in any form, 11 in whole or in part, without the prior written consent of Nintendo. 12 13 $Revision: 20266 $ 14 *---------------------------------------------------------------------------*/ 15 16 #ifndef NW_SND_INSTANCE_POOL_H_ 17 #define NW_SND_INSTANCE_POOL_H_ 18 19 #include <new> 20 #include <nn/types.h> 21 22 namespace nw { 23 namespace snd { 24 namespace internal { 25 26 /* ======================================================================== 27 PoolImpl class 28 ======================================================================== */ 29 30 class PoolImpl 31 { 32 public: PoolImpl()33 PoolImpl() : m_pNext( NULL ) {} 34 protected: 35 u32 CreateImpl( void* buffer, size_t size, u32 objSize ); 36 void DestroyImpl( void* buffer, unsigned long size ); 37 int CountImpl() const; 38 void* AllocImpl(); 39 void FreeImpl( void* ptr ); 40 41 private: 42 PoolImpl* m_pNext; 43 }; 44 45 46 /* ======================================================================== 47 MemoryPool class 48 ======================================================================== */ 49 50 template< typename T > 51 class MemoryPool : private internal::PoolImpl 52 { 53 public: 54 /*---------------------------------------------------------------------------* 55 Name: Create 56 57 Description: 指定したメモリ領域をプールに割り当てます 58 59 Arguments: buffer - メモリ領域の開始アドレス 60 size - メモリ領域のサイズ 61 62 Returns: 作成したプール中のインスタンス数 63 *---------------------------------------------------------------------------*/ Create(void * buffer,size_t size)64 u32 Create( void* buffer, size_t size ) 65 { 66 u32 objSize = 67 ( sizeof( T ) > sizeof( MemoryPool<T>* ) ) ? 68 sizeof( T ): 69 sizeof( MemoryPool<T>* ) 70 ; 71 return CreateImpl( buffer, size, objSize ); 72 } 73 74 /*---------------------------------------------------------------------------* 75 Name: Destroy 76 77 Description: 指定したメモリ領域をプールから解放します 78 解放するためにはメモリ領域が Free されてプールに格納されている 79 必要があります。 80 81 Arguments: buffer - メモリ領域の開始アドレス 82 size - メモリ領域のサイズ 83 84 Returns: 無し 85 *---------------------------------------------------------------------------*/ Destroy(void * buffer,unsigned long size)86 void Destroy( void* buffer, unsigned long size ) { DestroyImpl( buffer, size ); } 87 88 /*---------------------------------------------------------------------------* 89 Name: Count 90 91 Description: プール中のインスタンス数を取得します。 92 93 Arguments: 無し 94 95 Returns: インスタンス数 96 *---------------------------------------------------------------------------*/ Count()97 int Count() const { return CountImpl(); } 98 99 /*---------------------------------------------------------------------------* 100 Name: Alloc 101 102 Description: インスタンスを確保する。 103 104 Arguments: 無し 105 106 Returns: 確保したインスタンス、確保できなければNULL 107 *---------------------------------------------------------------------------*/ Alloc()108 T* Alloc() { return static_cast<T*>( AllocImpl() ); } 109 110 /*---------------------------------------------------------------------------* 111 Name: Free 112 113 Description: インスタンスをプールに解放する 114 115 Arguments: obj - インスタンス 116 117 Returns: 無し 118 *---------------------------------------------------------------------------*/ Free(T * obj)119 void Free( T* obj ) { FreeImpl( obj ); } 120 }; 121 122 123 /* ======================================================================== 124 InstancePool class 125 ======================================================================== */ 126 127 template< typename T > 128 class InstancePool : private internal::PoolImpl 129 { 130 public: 131 /*---------------------------------------------------------------------------* 132 Name: Create 133 134 Description: 指定したメモリ領域をプールに割り当てます 135 136 Arguments: buffer - メモリ領域の開始アドレス 137 size - メモリ領域のサイズ 138 139 Returns: 作成したプール中のインスタンス数 140 *---------------------------------------------------------------------------*/ Create(void * buffer,unsigned long size)141 u32 Create( void* buffer, unsigned long size ) 142 { 143 u32 objSize = 144 ( sizeof( T ) > sizeof( InstancePool<T>* ) ) ? 145 sizeof( T ): 146 sizeof( InstancePool<T>* ) 147 ; 148 return CreateImpl( buffer, size, objSize ); 149 } 150 151 /*---------------------------------------------------------------------------* 152 Name: Destroy 153 154 Description: 指定したメモリ領域をプールから解放します 155 解放するためにはメモリ領域が Free されてプールに格納されている 156 必要があります。 157 158 Arguments: buffer - メモリ領域の開始アドレス 159 size - メモリ領域のサイズ 160 161 Returns: 無し 162 *---------------------------------------------------------------------------*/ Destroy(void * buffer,unsigned long size)163 void Destroy( void* buffer, unsigned long size ) { DestroyImpl( buffer, size ); } 164 165 /*---------------------------------------------------------------------------* 166 Name: Count 167 168 Description: プール中のインスタンス数を取得します。 169 170 Arguments: 無し 171 172 Returns: インスタンス数 173 *---------------------------------------------------------------------------*/ Count()174 int Count() const { return CountImpl(); } 175 176 /*---------------------------------------------------------------------------* 177 Name: Alloc 178 179 Description: インスタンスを確保する。 180 181 Arguments: 無し 182 183 Returns: 確保したインスタンス、確保できなければNULL 184 *---------------------------------------------------------------------------*/ Alloc()185 T* Alloc() 186 { 187 void *ptr = AllocImpl(); 188 if ( ptr == NULL ) return NULL; 189 return new( ptr ) T(); // コンストラクタ呼びだし 190 } 191 192 /*---------------------------------------------------------------------------* 193 Name: Free 194 195 Description: インスタンスをプールに解放する 196 197 Arguments: obj - インスタンス 198 199 Returns: 無し 200 *---------------------------------------------------------------------------*/ Free(T * obj)201 void Free( T* obj ) 202 { 203 if ( obj == NULL ) return; 204 obj->~T(); // デストラクタ呼びだし 205 FreeImpl( obj ); 206 } 207 }; 208 209 } // namespace nw::snd::internal 210 } // namespace nw::snd 211 } // namespace nw 212 213 214 #endif /* NW_SND_INSTANCE_POOL_H_ */ 215 216