内存池程序中的问题

caoyiwen19830726 2012-10-08 12:30:59
// 内存池程序如下
#include "stdafx.h"
#include <iostream>
#include <windows.h>

using namespace std;

template<typename T>
class CMemoryPool
{
public:
enum { EXPANSION_SIZE = 32};

CMemoryPool(unsigned int nItemCount = EXPANSION_SIZE)
{
ExpandFreeList(nItemCount);
}

~CMemoryPool()
{
//free all memory in the list
CMemoryPool<T>* pNext = NULL;
for(pNext = m_pFreeList; pNext != NULL; pNext = m_pFreeList)
{
m_pFreeList = m_pFreeList->m_pFreeList;
delete [](char*)pNext;
}
}

void* Alloc(unsigned int /*size*/)
{
if(m_pFreeList == NULL)
{
ExpandFreeList();
}

//get free memory from head
CMemoryPool<T>* pHead = m_pFreeList;
m_pFreeList = m_pFreeList->m_pFreeList;
return pHead;
}

void Free(void* p)
{
//push the free memory back to list
CMemoryPool<T>* pHead = static_cast<CMemoryPool<T>*>(p);
pHead->m_pFreeList = m_pFreeList;
m_pFreeList = pHead;
}

protected:
//allocate memory and push to the list
void ExpandFreeList(unsigned nItemCount = EXPANSION_SIZE)
{
unsigned int nSize = sizeof(T) > sizeof(CMemoryPool<T>*) ? sizeof(T) : sizeof(CMemoryPool<T>*);
CMemoryPool<T>* pLastItem = static_cast<CMemoryPool<T>*>(static_cast<void*>(new char[nSize]));
m_pFreeList = pLastItem;
for(int i=0; i<nItemCount-1; ++i)
{
pLastItem->m_pFreeList = static_cast<CMemoryPool<T>*>(static_cast<void*>(new char[nSize]));
pLastItem = pLastItem->m_pFreeList;
}

pLastItem->m_pFreeList = NULL;
}

private:
CMemoryPool<T>* m_pFreeList;
};

class CCriticalSection
{
public:
CCriticalSection()
{
InitializeCriticalSection(&m_cs);
}

~CCriticalSection()
{
DeleteCriticalSection(&m_cs);
}

void Lock()
{
EnterCriticalSection(&m_cs);
}

void Unlock()
{
LeaveCriticalSection(&m_cs);
}

protected:
CRITICAL_SECTION m_cs;
};

template<typename POOLTYPE, typename LOCKTYPE>
class CMTMemoryPool
{
public:
void* Alloc(unsigned int size)
{
void* p = NULL;
m_lock.Lock();
p = m_pool.Alloc(size);
m_lock.Unlock();

return p;
}

void Free(void* p)
{
m_lock.Lock();
m_pool.Free(p);
m_lock.Unlock();
}

private:
POOLTYPE m_pool;
LOCKTYPE m_lock;
};

class CTest
{
public:
int m_n;
int m_n1;

void* operator new(size_t size)
{
void* p = s_pool->Alloc(size);
return p;
}

void operator delete(void* p, size_t size)
{
s_pool->Free(p);
}

static void NewPool()
{
s_pool = new CMTMemoryPool<CMemoryPool<CTest>, CCriticalSection>;
}

static void DeletePool()
{
delete s_pool;
s_pool = NULL;
}

static CMTMemoryPool<CMemoryPool<CTest>, CCriticalSection>* s_pool;
};
CMTMemoryPool<CMemoryPool<CTest>, CCriticalSection>* CTest::s_pool = NULL;

void testFun()
{
int i;
const int nLoop = 10;
const int nCount = 200000;

for(int j = 0; j<nLoop; ++j)
{
typedef CTest* LPTest;
LPTest arData[nCount];
for(i=0;i <nCount; ++i)
{
arData[i] = new CTest;
}

for(i=0;i <nCount; ++i)
{
delete arData[i]; }
}
}

int main(int argc, char* argv[])
{
{
unsigned int dwStartTickCount = GetTickCount();

CTest::NewPool();

testFun();

CTest::DeletePool();

cout << "total cost:" << GetTickCount() - dwStartTickCount << endl;
}

return 0;
}

根据测试表明new CTest 和 delete arData[i]调用的是CTest类的 void* operator new(size_t size)函数 和 void operator delete(void* p, size_t size)函数。 但是new Test语句和delete arData[i]语句是怎么体现new和delete中size_t参数的呢? 希望高手给予解答,谢谢!
...全文
150 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
zzhays 2012-10-08
  • 打赏
  • 举报
回复
new Test
equals
new (sizeof(Test))
sizeof(Test)=8;

new Test -----> new (8)

...
zzhays 2012-10-08
  • 打赏
  • 举报
回复
m_pFreeList = m_pFreeList->m_pFreeList;
先膜拜下这行逆天的代码
caoyiwen19830726 2012-10-08
  • 打赏
  • 举报
回复
巧的是这里size_t类型的参数size值为CTest类中两个int型数m_n和m_n1的大小和,即为8,如果再加一个整数则参数size变成12,难道系统能自动识别吗? 不清楚这中间究竟发生了什么,请高手帮忙看看啊!
冷月清晖 2012-10-08
  • 打赏
  • 举报
回复
size_t size 编译器会为其初始化一个非负的值,一般情况是zero.
caoyiwen19830726 2012-10-08
  • 打赏
  • 举报
回复
希望大家能帮我看看啊 不甚感激!

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧