简明分析C/C++内存分配的解决方案(6)
SMemoryChunk *CMemoryPool::SkipChunks(SMemoryChunk *ptrStartChunk, unsigned int uiChunksToSkip)
{
SMemoryChunk *ptrCurrentChunk = ptrStartChunk;
for(unsigned int i = 0; i < uiChunksToSkip; i++)
{
if(ptrCurrentChunk)
{
ptrCurrentChunk = ptrCurrentChunk->Next;
}
else
{
assert(false && "Error : Chunk == NULL was not expected.");
break ;
}
}
return ptrCurrentChunk;
}
测试方法:
// 111.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "CMemoryPool.h"
CMemoryPool* g_pMemPool = NULL;
class testMemoryPool
{
public:
testMemoryPool(){
}
void *operator new(std::size_t ObjectSize)
{
return g_pMemPool->GetMemory(ObjectSize) ;
}
private:
char a[25];
bool b;
long c;
};//sizeof(32);
int _tmain(int argc, _TCHAR* argv[])
{
g_pMemPool = new CMemoryPool();
testMemoryPool* test = new testMemoryPool();
return 0;
}