#ifndef _MEM_POOL_TEST_
#define _MEM_POOL_TEST_
/********************************************************************
created: 2004/11/29
created: 29:11:2004 15:27
filename: e:\复用\memory pool\test\cmempooltest.h
file path: e:\复用\memory pool\test
file base: cmempooltest
file ext: h
author: David
purpose: 这是一个测试内存池类的类,主要用到的类为:
1.CMemPool--CMemPool.h主要用于对内存池的管理(Allocate and Release)的
template, 适用于每个节点的大小都相等的情况。
2.CLock--Lock.h主要被CMemPool内部所用,同步CMemPool内部inuse链表和free
链表。
3.CMemPoolTest CBuffer--用于测试CMemPool的类。一个CMemPoolTest实例模拟
一个网络session,因为该类实例有可能会频繁生成和释放,故需采用内存池,
而每个实例管理自己的缓冲区列表,而缓冲区列表也有可能会频繁生成和释放
掉,故也采用内存池,这样就有可能出现内存池嵌套现象,这两个类就是用来
模拟CMemPool嵌套的情况的,试验结果良好。
4.注意点:使用类CMemPool的类必须重装new和delete,在new中从内存池Allocate
内存,
而在delete中Release内存,归还给内存池。内存池应该属于类的资产,而不是
对象的资产,故为static类型的成员。
5.建议:对于同类对象频繁生成于释放的情况,建议使用内存池,这样可以防止内存
碎片和有助于程序的稳定。
*********************************************************************/
#include "CMemPool.h"
#include "global.h"
#include <list>
class CBuffer
{
public:
CBuffer();
virtual ~CBuffer();
static void* operator new(size_t);
static void operator delete(void*);
private:
int m_nBufLen;
char m_pBuffer[2048];
static CMemPool<CBuffer> m_MemPool;
};
class CMemPoolTest
{
public:
CMemPoolTest();
virtual ~CMemPoolTest();
static void* operator new(size_t);
static void operator delete(void*);
void SetBuffer();
private:
static CMemPool<CMemPoolTest> m_MemPool;
list<CBuffer *> m_BufferList;
};
#endif
//.cpp
#include "CMemPoolTest.h"
CBuffer::CBuffer()
{
}
CBuffer::~CBuffer()
{
cout << "~CBuffer" << endl;
}
void *CBuffer::operator new(size_t)
{
CBuffer *p = NULL;
p = static_cast<CBuffer*>(m_MemPool.Allocate());
int m_nInuse, m_nFree;
m_MemPool.GetInfo(m_nInuse, m_nFree);
cout << "CBuffer Allocate m_nInuse is " << m_nInuse << " m_nFree is "<< m_nFree << endl;
return p;
}
void CBuffer::operator delete(void *deadobject)
{
m_MemPool.Release((CBuffer *)deadobject);
int m_nInuse, m_nFree;
m_MemPool.GetInfo(m_nInuse, m_nFree);
cout << "CBuffer Release m_nInuse is " << m_nInuse << " m_nFree is "<< m_nFree << endl;
}
CMemPoolTest::CMemPoolTest()
{
}
CMemPoolTest::~CMemPoolTest()
{
list<CBuffer*>::const_iterator pos;
for(pos = m_BufferList.begin(); pos != m_BufferList.end(); ++pos){
delete (CBuffer *)(*pos);
}
m_BufferList.clear();
}
void *CMemPoolTest::operator new(size_t)
{
CMemPoolTest *p = NULL;
p = static_cast<CMemPoolTest*>(m_MemPool.Allocate());
/*
int m_nInuse, m_nFree;
m_MemPool.GetInfo(m_nInuse, m_nFree);
cout << "Allocate m_nInuse is " << m_nInuse << " m_nFree is "<< m_nFree << endl;
*/
return p;
}
void CMemPoolTest::operator delete(void *deadobject)
{
m_MemPool.Release((CMemPoolTest *)deadobject);
/*
int m_nInuse, m_nFree;
m_MemPool.GetInfo(m_nInuse, m_nFree);
cout << "Release m_nInuse is " << m_nInuse << " m_nFree is "<< m_nFree << endl;
*/
}
void CMemPoolTest::SetBuffer()
{
CBuffer *p = NULL;
p = new CBuffer();
m_BufferList.push_back(p);
}
//main()
#include "global.h"
#include <list>
#include "CMemPoolTest.h"
#include "UnfixedMem_Pool.h"
CMemPool<CMemPoolTest> CMemPoolTest::m_MemPool;//fixed memory manager
CMemPool<CBuffer> CBuffer::m_MemPool;//fixed memory manager
CUnfixedMemPool g_MemPool;//unfixed memory manager
int main()
{
//test CMemPool
int i = 0;
CMemPoolTest *test[1000];
for(i = 0; i < 1000; i++){
test[i] = new CMemPoolTest;
test[i]->SetBuffer();
test[i]->SetBuffer();
}
for(i = 0; i < 1000; i++){
if (test[i] != NULL) {
delete test[i];
test[i] = NULL;
}
}
//end test CMemPool
return SUCCESS_CODE;
}