[代码]内存池操作的模板类

来源:互联网 发布:java spring mvc 实例 编辑:程序博客网 时间:2024/05/17 04:05
// 文件名 : Mempool.h
// 作者 : Moonwell
// Msn : archonzhao@hotmail.com
// 将#include "mempool.h"放在#include <windows.h>之前
// 否则会出现"InitializeCriticalSectionAndSpinCount函数没定义"的错误

#ifndef _MEM_POOL_H
#define _MEM_POOL_H

#define _WIN32_WINNT 0x0403   //For InitializeCriticalSectionAndSpinCount

#ifdef _DEBUG
#define _DEBUG_POOL
#endif

#include <windows.h>
#include <crtdbg.h>
#include <vector>
#ifdef _DEBUG_POOL
#include <map>
#endif 

using namespace std;

template<typename memtype>
class MemPool
{
public:
//init_count = 初始分配的对象个数
MemPool(int init_count);

~MemPool(void);

memtype *alloc();// 读取一个元素
void free(memtype *obj);// 删除一个元素

private:
vector<memtype *> m_free_node; //空闲的对象列表
vector<memtype *> m_new_node;  //额外创建的对象列表
#ifdef _DEBUG_POOL
map<memtype *,memtype *> m_map;//用于检测对象是否被释放了多次
#endif
memtype *m_head;//初始创建时候的内存块的指针

CRITICAL_SECTION m_sec;
};

template<typename memtype>
MemPool<memtype>::MemPool(int init_count)
{
m_head = new memtype[init_count];

//先把vector对象的内存增加到原来的两倍大小,可以提高效率
m_free_node.resize(init_count*2);
m_free_node.resize(0);

//将对象添加到空闲列表
for(int i=0;i<init_count;i++)
m_free_node.push_back(&m_head[i]); 

InitializeCriticalSectionAndSpinCount(&m_sec,64);
}

template<typename memtype>
MemPool<memtype>::~MemPool(void)
{
vector<memtype *>::iterator it;
DeleteCriticalSection(&m_sec);

delete [] m_head;

// 删除所有额外创建的对象
for(it = m_new_node.begin();it != m_new_node.end();++it)
delete [] *it;

}

template<typename memtype>
memtype *MemPool<memtype>::alloc()
{
memtype *ptmp;

EnterCriticalSection(&m_sec);
if(!m_free_node.empty()) {
ptmp = *(--m_free_node.end()); //取最后一个对象
m_free_node.pop_back();
#ifdef _DEBUG_POOL
m_map.erase(ptmp);
#endif

} else {
ptmp = new memtype; //创建额外的对象
m_new_node.push_back(ptmp); //添加到额外列表
}

LeaveCriticalSection(&m_sec);

return ptmp;
}

template<typename memtype>
void MemPool<memtype>::free(memtype *obj)
{
EnterCriticalSection(&m_sec);
m_free_node.push_back(obj);

#ifdef _DEBUG_POOL //obj已经被释放了吗?
_ASSERTE(m_map.find(obj) == m_map.end());
m_map[obj] = obj;
#endif
LeaveCriticalSection(&m_sec);
}

#endif


在原来基础上增加了多次free()检测的功能,对同一个对象释放两次将会引发一个断言.