Game Development Reference
In-Depth Information
else
{
m_pHead = ppNewMemArray[m_memArraySize];
}
// destroy the old memory array
if (m_ppRawMemoryArray)
free(m_ppRawMemoryArray);
// assign the new memory array and increment the size count
m_ppRawMemoryArray = ppNewMemArray;
++m_memArraySize;
return true;
}
unsigned char* MemoryPool::AllocateNewMemoryBlock(void)
{
// calculate the size of each block and the size of the
// actual memory allocation
size_t blockSize = m_chunkSize + CHUNK_HEADER_SIZE; // chunk + linked list
// overhead
size_t trueSize = blockSize * m_numChunks;
// allocate the memory
unsigned char* pNewMem = (unsigned char*)malloc(trueSize);
if (!pNewMem)
return NULL;
// turn the memory into a linked list of chunks
unsigned char* pEnd = pNewMem + trueSize;
unsigned char* pCurr = pNewMem;
while (pCurr < pEnd)
{
// calculate the next pointer position
unsigned char* pNext = pCurr + blockSize;
// set the next pointer
unsigned char** ppChunkHeader = (unsigned char**)pCurr;
ppChunkHeader[0] = (pNext < pEnd ? pNext : NULL);
// move to the next block
pCurr += blockSize;
}
return pNewMem;
}
Search WWH ::




Custom Search