//============================================================================= // Copyright (C) 2002 Radical Entertainment Ltd. All rights reserved. // // File: .h // // Description: Blahblahblah // // History: 23/05/2002 + Created -- NAME // //============================================================================= #ifndef ALLOCPOOL_H #define ALLOCPOOL_H //======================================== // Nested Includes //======================================== #include //======================================== // Forward References //======================================== //============================================================================= // // Synopsis: Blahblahblah // //============================================================================= template class AllocPool { public: AllocPool( GameMemoryAllocator heap, unsigned int iPoolSize ); virtual ~AllocPool(); T* AllocateFromPool(); void ReturnToPool( unsigned int pItem ); protected: // override initialize if you have a different constructor virtual void Initialize(); void Finalize(); GameMemoryAllocator GetHeap() { return( mHeap ); } private: //Prevent wasteful constructor creation. AllocPool( const AllocPool& pAllocPool ); AllocPool& operator=( const AllocPool& pAllocPool ); GameMemoryAllocator mHeap; unsigned int mPoolIndex; unsigned int miPoolSize; T* mPool; bool* mUsed; }; template AllocPool::AllocPool( GameMemoryAllocator heap, unsigned int iPoolSize ) { mHeap = heap; miPoolSize = iPoolSize; mPoolIndex = 0; mPool = new(mHeap) T[ miPoolSize ]; mUsed = new(mHeap) bool[ miPoolSize ]; Initialize(); } template AllocPool::~AllocPool() { Finalize(); delete[] ( mHeap, mPool ); delete[] ( mHeap, mUsed ); } template T* AllocPool::AllocateFromPool() { // if you hit this assert then your pool is too small rAssert( mPoolIndex < miPoolSize ); T* pItem = &mPool[ mPoolIndex ]; mUsed[ mPoolIndex ] = true; while(( mPoolIndex < miPoolSize ) && ( mUsed[ mPoolIndex ] )) { mPoolIndex++; } return( pItem ); } template void AllocPool::ReturnToPool( unsigned int pItem ) { for( unsigned int i = 0; i < miPoolSize; i++ ) { if( (unsigned int)&mPool[ i ] == pItem ) { mUsed[ i ] = false; if( i < mPoolIndex ) { mPoolIndex = i; return; } } } } template void AllocPool::Initialize() { for( unsigned int i = 0; i < miPoolSize; i++ ) { mUsed[ i ] = false; } } template void AllocPool::Finalize() { /* for( unsigned int i = 0; i < miPoolSize; i++ ) { if( mPool[ i ] == NULL ) { delete( mHeap, mPool[ i ] ); mPool[ i ] = NULL; } }*/ } #endif // ALLOCPOOL_H