#include <iostream.h>
#include <time.h>
#include "MemoryPool.h"
class CMyClass
{
public:
CMyClass()
{
mm = 0;
pp = 0;
}
~CMyClass()
{
}
static void* operator new(size_t size)
{
if(size != sizeof(CMyClass))
return ::operator new(size);
return CMyClass::m_pool.req_from_pool();
}
static void operator delete(void *obj)
{
CMyClass::m_pool.release_obj((CMyClass*) obj);
}
private:
static CMemoryPool<CMyClass> m_pool;
public:
int mm;
int pp;
};
const size_t LOOPS = 10000000;
CMemoryPool<CMyClass> CMyClass::m_pool(10000000);
int main()
{
clock_t t;
CMyClass *cc;
t = clock();
for (size_t i = 0; i < LOOPS; ++i)
{
cc = ::new CMyClass();
}
cout << (clock() - t) * 1.0 / CLOCKS_PER_SEC << endl;
t = clock();
for (size_t st = 0; st<LOOPS;st++)
{
cc = new CMyClass();
}
cout << (clock() - t) * 1.0 / CLOCKS_PER_SEC << endl;
/////////////////////////标准类型的用法
CMemoryPool<double> d_Pool(10);
double *p[10];
int j;
cout<<"start to new mem for elements"<<endl;
for(j=0; j<10; ++j)
{
p[j] = d_Pool.req_from_pool();
cout<<"element:"<<j<<" addr:"<< p[j]<<std::endl;
}
cout<<"start to delete elements"<<endl;
for(j=0; j<5; ++j)
{
d_Pool.release_obj(p[j]);
cout<<"element:"<<j<<" addr:"<< p[j]<<std::endl;
}
cout<<"start to new mem for elements"<<endl;
for(j=0; j<5; ++j)
{
p[j] = d_Pool.req_from_pool();
cout<<"element:"<<j<<" addr:"<< p[j]<<std::endl;
}
return 0;
}
没有合适的资源?快使用搜索试试~ 我知道了~
一个简单内存池的实现
共16个文件
plg:2个
opt:2个
dsw:2个
4星 · 超过85%的资源 需积分: 12 36 下载量 106 浏览量
2008-11-03
17:42:45
上传
评论 1
收藏 400KB RAR 举报
温馨提示
注意咯。应该在基类加上 ~_T()不然将无法析构对象。 这个类工作正常。
资源推荐
资源详情
资源评论
收起资源包目录
luMemoryPool.rar (16个子文件)
MemoryPooltemplate
main.cpp 2KB
main.plg 244B
main.dsp 3KB
MemoryPool.h 2KB
main.dsw 533B
stlport_vc646.dll 808KB
main.ncb 57KB
main.opt 53KB
luMemoryPool
main.cpp 1KB
main.plg 1KB
main.dsp 3KB
MemoryPool.h 2KB
main.dsw 533B
stlport_vc646.dll 808KB
main.ncb 49KB
main.opt 53KB
共 16 条
- 1
资源评论
- liuzongqide2012-11-12对于理解什么是内存池有帮助
- anhuipl20102012-07-31实时操作内存的时候非常有用,值得学习
glunoy
- 粉丝: 322
- 资源: 15
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功