void CMemPool::InitMemPool() { try { unsigned int blocksize = CalcBlockSize(m_nPoolSize); PMemoryBlock pMemBlock = new MemoryBlock[blocksize]; for (unsigned int i = 0; i < blocksize; i++) { m_MemoryUnitList.AddTail(&pMemBlock[i]); } m_pPoolEntry = (TByte*) new TByte[m_nPoolSize]; POSITION pos2, pos1 = m_MemoryUnitList.GetHeadPosition(); for (unsigned int i = 0; (pos2 = pos1) != NULL; i++) { m_MemoryUnitList.GetNext(pos1); m_MemoryUnitList.GetAt(pos2)->pData = (TByte*)(m_pPoolEntry + i * m_nBlockUnitSize); m_MemoryUnitList.GetAt(pos2)->DataSize = m_nPoolSize - i * m_nBlockUnitSize; } } catch (...) { // TODO : Catch the Exception of Memory allocation } m_CurUnitPos = m_MemoryUnitList.GetHeadPosition(); }
PMemoryBlock CMemPool::RequestBlock(unsigned int blocksize) { // Request from m_MemoryUnitList for Memory Block if (m_MemoryUnitList.GetAt(m_CurUnitPos)->DataSize >= blocksize) { PMemoryBlock pMemoryBlock = m_MemoryUnitList.GetAt(m_CurUnitPos); unsigned int unitsize = CalcBlockSize(blocksize); POSITION pos = m_CurUnitPos; for (unsigned int i = 0; i < unitsize; i++) { if (m_CurUnitPos == m_MemoryUnitList.GetTailPosition ()) { m_CurUnitPos = pos; // This value scroll back return NULL; } m_MemoryUnitList.GetNext(m_CurUnitPos); } m_nCurUsedSize += unitsize * m_nBlockUnitSize; // Increase the value of m_nCurUsedSize return pMemoryBlock; } // return NULL if Current Memory Size can not satisfy the Request return NULL; }
static uint_16 CalcNestSize( FullVerBlockNest * nest ) /****************************************************/ { FullVerBlock * block; uint_16 size; size = 0; for( block = nest->Head; block != NULL; block = block->Next ) { size += CalcBlockSize( block ); } return( size ); }