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

王朝other·作者佚名  2006-01-09
宽屏版  字体: |||超大  

// 文件名 : 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()检测的功能,对同一个对象释放两次将会引发一个断言.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有