/* ========================================================================== * ÀÛ ¼º ÀÚ : À̼ø±Ô * ÀÛ ¼º ÀÏ : 2006.09.17 * ³» ¿ë : tPool¸¦ ±¸Çö * ÁÖÀÇ»çÇ× : *===========================================================================*/ template inline tPoolNode::tPoolNode( unsigned int capacity ) : mCapacity( capacity ) , mpNext( 0 ) { if( capacity > 0 ) { mpObjectArray = ALLOC::Allocate( capacity ); assert( mpObjectArray ); } else { assert( 0 && "size of pool node is zero" ); mpObjectArray = 0; } } template inline tPoolNode::~tPoolNode() { ALLOC::Deallocate( mpObjectArray ); delete mpNext; } template inline T* tPoolNode::GetObject( unsigned int i ) { if( i >= mCapacity ) { assert( 0 && "index is invalid" ); return 0; } return &mpObjectArray[i]; } template inline tPool::tPool( unsigned int initialCapacity, unsigned int stepCapacity, unsigned int hashHint ) : mFreeObjectSet( hashHint ) , mpRootNode( 0 ) , mSize( 0 ) , mCapacity( 0 ) , mInitialCapacity( initialCapacity ) , mStepCapacity( stepCapacity ) { if( initialCapacity > 0 ) { CreateNode( initialCapacity ); } } template inline tPool::~tPool() { mFreeObjectSet.Clear(); delete mpRootNode; } template inline void tPool::Clear() { mFreeObjectSet.Clear(); delete mpRootNode; mpRootNode = 0; mSize = 0; mCapacity = 0; } template inline void tPool::Reserve( unsigned int capacity, unsigned int stepCapacity ) { if( stepCapacity ) { mStepCapacity = stepCapacity; } int c = (int)(capacity - mCapacity); if( c > 0 ) { CreateNode( (unsigned int)c >= mStepCapacity ? (unsigned int)c : mStepCapacity ); } } template inline T* tPool::Alloc() { if( mFreeObjectSet.GetSize() == 0 ) { if( mStepCapacity == 0 ) { CreateNode( mCapacity ); } else { CreateNode( mStepCapacity ); } } cObjectSet::cIterator i = mFreeObjectSet.Begin(); T* p = *i; mFreeObjectSet.Erase( i ); /// ÇÒ´çµÈ ¿ÀºêÁ§Æ® ¼ö Áõ°¡ ++mSize; return p; } template inline void tPool::Free( T* p ) { if( mFreeObjectSet.Insert( p ) == true ) { /// ÇÒ´çµÈ ¿ÀºêÁ§Æ® ¼ö °¨¼Ò --mSize; } } template inline unsigned int tPool::GetSize() const { return mSize; } template inline unsigned int tPool::GetCapacity() const { return mCapacity; } template inline bool tPool::CreateNode( unsigned int capacity ) { if( capacity == 0 ) { assert( 0 && "zero capacity" ); return false; } else { cNode* n = new cNode( capacity ); for( unsigned int i = 0; i < capacity; ++i ) { mFreeObjectSet.Insert( n->GetObject(i) ); } /// ³ëµå ¿¬°á n->mpNext = mpRootNode; mpRootNode = n; /// ¿ë·® Áõ°¡ mCapacity += capacity; return true; } }