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