// wzmemorypool.h #ifndef _PROGRAMCOMMON_WZMEMORYPOOL_H_ #define _PROGRAMCOMMON_WZMEMORYPOOL_H_ #include "wzmemoryband.h" //------------------------------------------------------------------------------ /** @class CWzMemoryPool */ template class CWzMemoryPool { public: // »ý¼ºÀÚ CWzMemoryPool( void ) : m_head( NULL ) , m_freeBand( NULL ) , m_extSize( 0 ) { // empty } // ¼Ò¸êÀÚ ~CWzMemoryPool( void ) { Destroy(); } // ÃʱâÈ­ bool Create( int size, int extSize = 0 ) { WzAssert( size > 0 ); WzAssert( !m_head ); if( m_head ) { WZLOG( WZWAR, "CWzMemoryPool::Create() - ÀÌ¹Ì ÀÖÀ½" ); return false; } CWzMemoryBand* newBand = AllocBand( size ); WzAssert( newBand ); if( !newBand ) { WZLOG( WZWAR, "CWzMemoryPool::Create() - »õ ¹êµå »ý¼º ½ÇÆÐ" ); return false; } SetHeadBand(newBand); m_freeBand = newBand; m_extSize = ( extSize > 0 ? extSize : size / 2 + 1 ); return true; } // ÇØÁ¦ void Destroy( void ) { if( m_head ) { CWzMemoryBand* cur = m_head; while( cur ) { CWzMemoryBand* del = cur; cur = cur->GetNext(); delete del; } m_head = NULL; m_freeBand = NULL; } } // Ãà¼Ò (ÀÏÁ¾ÀÇ Á¤¸®) void Shrink( void ) { if( m_head ) { CWzMemoryBand* next = m_head->GetNext(); while( next ) { delete m_head; m_head = next; next = next->GetNext(); } m_head->SetPrev( NULL ); m_head->SetNext( NULL ); m_head->Initialize(); m_freeBand = m_head; } } // °´Ã¼ ÇÒ´ç T* Alloc( void ) { WzAssert( m_freeBand ); T* newObj = m_freeBand->AllocObject(); if( newObj != NULL ) { // ÇöÀçÀÇ freeBand¿¡¼­ »ý¼ºÇß´Ù¸é ¸®ÅÏ return newObj; } // ÇöÀçÀÇ freeBand¿¡ ¿©À¯°¡ ¾ø´Ù¸é Çì´õºÎÅÍ ´Ù½Ã ã´Â´Ù CWzMemoryBand* band = m_head; while( band ) { newObj = band->AllocObject(); if( newObj != NULL ) { // ÀÌ ¹êµå¸¦ freeBand·Î ¼³Á¤ÇÏ°í ¸®ÅÏ m_freeBand = band; return newObj; } band = band->GetNext(); } // ¸ðµç ¹êµå¿¡ ¿©À¯°¡ ¾ø´Ù¸é »õ·Î¿î ¹êµå¸¦ »ý¼ºÇÑ´Ù CWzMemoryBand* newBand = AllocBand( m_extSize ); WzAssert( newBand ); newObj = newBand->AllocObject(); WzAssert(newObj); SetHeadBand( newBand ); m_freeBand = newBand; return newObj; } // °´Ã¼ ÇØÁ¦ void Free( T* obj ) { WzAssert( obj ); if( obj == NULL ) return; WzAssert( m_freeBand ); // ÇöÀçÀÇ freeBand¿¡ Ãß°¡ÇÑ´Ù if( m_freeBand->FreeObject( obj ) == false ) { // ²Ë Âù°æ¿ì ´Ù¸¥¹êµå¿¡ Ãß°¡ÇÑ´Ù CWzMemoryBand* band = m_head; while( band ) { if( band->FreeObject( obj ) == true ) { return; } band = band->GetNext(); } WzAssert( FALSE || "CWzMemoryPool::Free() - ¹ÝȯÀ» ¸¹ÀÌ Çß´Ù." ); } } #ifdef _DEBUG int GetSize() { int total_size = 0; CWzMemoryBand* band = m_head; while( band ) { total_size += band->GetSize(); band = band->GetNext(); } return total_size; } int GetFreeSize() { int total_free_size = 0; CWzMemoryBand* band = m_head; while( band ) { total_free_size += band->GetFreeSize(); band = band->GetNext(); } return total_free_size; } #endif private: void SetHeadBand( CWzMemoryBand* band ) { band->SetNext( m_head ); if( m_head ) { m_head->SetPrev( band ); } m_head = band; } CWzMemoryBand* AllocBand( int size ) { CWzMemoryBand* newBand = new CWzMemoryBand; WzAssert( newBand ); if( !newBand ) { WZLOG( WZWAR, "CWzMemoryPool::AllocBand() - newBand »ý¼º ½ÇÆÐ" ); goto _error; } if( !newBand->Create( size ) ) { WZLOG( WZWAR, "CWzMemoryPool::AllocBand() - newBand ÃʱâÈ­ ½ÇÆÐ" ); goto _error; } return newBand; _error: if( newBand ) { delete newBand; } return NULL; } private: CWzMemoryBand* m_head; CWzMemoryBand* m_freeBand; int m_extSize; }; ////////////////////////////////////////////////////////////////////////// //!~ //#define _USE_ENGINE_MEMORY_POOL_ namespace CMemoryPool { #ifdef _USE_ENGINE_MEMORY_POOL_ template class SingletonPool { SingletonPool() { pool_.Create(200); }; ~SingletonPool() { pool_.Destroy(); }; public: static SingletonPool* Instance() { static SingletonPool instance; return &instance; } CWzMemoryPool pool_; }; template static T* AllocNew() { return SingletonPool::Instance()->pool_.Alloc(); } template static void AllocDelete(T* ptr) { SingletonPool::Instance()->pool_.Free(ptr); } #else // template static T* AllocNew() { return new T; } template static void AllocDelete(T* ptr) { delete ptr; } #endif //_USE_ENGINE_MEMORY_POOL_ //~! } #endif // _PROGRAMCOMMON_WZMEMORYPOOL_H_