- #ifndef _INORDPOOL_H
- #define _INORDPOOL_H
- #define DEFPOOLSIZE 32
- #define DEFRECYCLESIZE DEFPOOLSIZE
- #define DEFRECORDSIZE 4
- //-----------------------------------------------------------------------------
- // Name: Template class CInordPool
- // Desc: Class to create a pool for memory used and recycle
- //-----------------------------------------------------------------------------
- template <typename elemType>
- class CInordPool {
- elemType *m_pPoolArray;
- int m_nPoolUsed, m_nPoolTotal;
- int m_nEnlargeSize;
- elemType **m_pRecycleArray;
- int m_nRecycleSize, m_nRecycleRemain;
- elemType **m_pRecord;
- int m_nRecordSize, m_nRecordUsed;
- bool m_bInitialized;
- public:
- CInordPool();
- ~CInordPool();
- // Access functions
- void SetEnlargeSize( int size ) { if ( size > 0 ) m_nEnlargeSize = size; }
- // Creation/destruction methods
- bool InitPool( int poolsize );
- void DestroyPool();
- // Methods
- elemType *GetUsable();
- void Recycle( elemType *pvalid );
- protected:
- bool CreatePool();
- // Access functions
- void SetValid( bool flag ) { m_bInitialized = flag; }
- // Status functions
- bool IsValid() { return m_bInitialized; }
- bool IsPoorRemain() { return m_nPoolUsed<m_nPoolTotal; }
- bool IsRecycleRemain() { return m_nRecycleRemain>0; }
- bool IsRecycleFull() { return m_nRecycleRemain==m_nRecycleSize; }
- };
- //-----------------------------------------------------------------------------
- // Name: CDisplay()
- // Desc:
- //-----------------------------------------------------------------------------
- template <typename elemType>
- CInordPool<elemType>::CInordPool() {
- SetValid( false );
- }
- //-----------------------------------------------------------------------------
- // Name: CDisplay()
- // Desc:
- //-----------------------------------------------------------------------------
- template <typename elemType>
- CInordPool<elemType>::~CInordPool() {
- DestroyPool();
- }
- //-----------------------------------------------------------------------------
- // Name: CDisplay()
- // Desc:
- //-----------------------------------------------------------------------------
- template <typename elemType>
- bool CInordPool<elemType>::InitPool( int poolsize ) {
- if ( IsValid() )
- return false;
- // set enlarge size;
- if ( poolsize > 0 )
- m_nEnlargeSize = poolsize;
- else
- m_nEnlargeSize = DEFPOOLSIZE;
- // create a new recycle stack
- m_nRecycleSize = m_nEnlargeSize;
- m_pRecycleArray =( elemType ** )new LPVOID[m_nRecycleSize];
- if ( !m_pRecycleArray )
- return false;
- m_nRecycleRemain = 0;
- // clear record array
- m_nRecordSize = 0;
- m_nRecordUsed = 0;
- m_pRecord = NULL;
- // create a new pool
- if ( CreatePool() ) {
- // set valid
- SetValid( true );
- return true;
- } else
- return false;
- }
- //-----------------------------------------------------------------------------
- // Name: CDisplay()
- // Desc:
- //-----------------------------------------------------------------------------
- template <typename elemType>
- void CInordPool<elemType>::DestroyPool() {
- if ( !IsValid() )
- return ;
- SAFE_DELETE_ARRAY( m_pRecycleArray );
- if ( m_pRecord )
- for( int i=0; i<m_nRecordUsed; i++ )
- SAFE_DELETE_ARRAY( m_pRecord[i] );
- SAFE_DELETE_ARRAY( m_pRecord );
- SetValid( false );
- }
- //-----------------------------------------------------------------------------
- // Name: CDisplay()
- // Desc:
- //-----------------------------------------------------------------------------
- template <typename elemType>
- bool CInordPool<elemType>::CreatePool() {
- // create a new pool
- m_pPoolArray = new elemType[m_nEnlargeSize];
- if ( !m_pPoolArray )
- return false;
- m_nPoolUsed = 0;
- m_nPoolTotal = m_nEnlargeSize;
- // add record
- if ( m_nRecordSize == m_nRecordUsed ) {
- m_nRecordSize += DEFRECORDSIZE;
- elemType **ptr = m_pRecord;
- m_pRecord =(elemType **)new LPVOID[m_nRecordSize];
- if ( m_pRecord ) {
- if ( ptr ) {
- memcpy( m_pRecord, ptr, sizeof(elemType *)*m_nRecordUsed );
- SAFE_DELETE_ARRAY( ptr );
- }
- } else {
- m_pRecord = ptr;
- m_nRecordSize = m_nRecordUsed;
- return false;
- }
- }
- m_pRecord[m_nRecordUsed] = m_pPoolArray;
- m_nRecordUsed++;
- return true;
- }
- //-----------------------------------------------------------------------------
- // Name: CDisplay()
- // Desc:
- //-----------------------------------------------------------------------------
- template <typename elemType>
- elemType *CInordPool<elemType>::GetUsable() {
- if ( !IsValid() )
- return NULL;
- if( IsPoorRemain() ) {
- m_nPoolUsed++;
- return &m_pPoolArray[m_nPoolUsed-1];
- }
- else if ( IsRecycleRemain() ) {
- m_nRecycleRemain--;
- return m_pRecycleArray[m_nRecycleRemain];
- }
- else if ( CreatePool() ) {
- m_nPoolUsed++;
- return &m_pPoolArray[m_nPoolUsed-1];
- }
- else
- return NULL;
- }
- //-----------------------------------------------------------------------------
- // Name: CDisplay()
- // Desc:
- //-----------------------------------------------------------------------------
- template <typename elemType>
- void CInordPool<elemType>::Recycle( elemType *pvalid ) {
- if ( !pvalid )
- return ;
- if ( !IsValid() ) {
- SAFE_DELETE( pvalid );
- return ;
- }
- if ( IsRecycleFull() ) {
- // create a new recycle stack
- elemType **ptr = m_pRecycleArray;
- m_pRecycleArray =(elemType **)new LPVOID[m_nRecycleSize+m_nEnlargeSize];
- if ( !m_pRecycleArray ) {
- // restore
- m_pRecycleArray = ptr;
- SAFE_DELETE( pvalid );
- return ;
- }
- else {
- memcpy( m_pRecycleArray, ptr, sizeof(elemType *)*m_nRecycleSize );
- SAFE_DELETE_ARRAY( ptr );
- m_nRecycleSize += m_nEnlargeSize;
- }
- }
- m_pRecycleArray[m_nRecycleRemain] = pvalid;
- m_nRecycleRemain++;
- }
- #endif // _INORDPOOL_H