MemoryPool内存池实现

#ifndef __MEMORY_POOL_H__
#define __MEMORY_POOL_H__
#include <list>
#include <malloc.h>
template<class T>
class MemoryPool
{
	typedef enum{USED, IDLE}STATE_TYPE;
	typedef struct _ST_MEMORY{
		STATE_TYPE state;
		T* buffer;
		size_t length;
		_ST_MEMORY():state(IDLE),buffer(NULL),length(0){}
	}ST_MEMORY;
public:
	MemoryPool(size_t totalsize = 20*1024*1024):m_totalsize(totalsize)
	{
		m_address = (T*)malloc(sizeof(T)*totalsize);
		memset(m_address, 0, sizeof(T)*totalsize);
		if (m_address){
			ST_MEMORY memory;
			memory.buffer = m_address;
			memory.length = m_totalsize;
			memory.state = IDLE;
			m_memorylist.push_back(memory);
		}
		else{
			printf("the memory pool malloc failed!!!!!!!!\n");
		}
	}
	~MemoryPool()
	{
		if (m_address){
			free(m_address);
			m_address = NULL;
		}
		m_memorylist.clear();
	}
	inline T* allocate(size_t blocksize)
	{
		T* buffer = NULL;
		if (blocksize == 0 || blocksize > m_totalsize || m_memorylist.empty())
			return buffer;
		for (std::list<ST_MEMORY>::iterator ite = m_memorylist.begin(); ite != m_memorylist.end(); ite++)
		{
			if (ite->state == IDLE && ite->length > blocksize){
				ST_MEMORY memory;
				buffer = ite->buffer;
				memory.buffer = ite->buffer;
				memory.state = USED;
				memory.length = blocksize;
				m_memorylist.push_back(memory);

				ite->buffer += blocksize;
				ite->length -= blocksize;
				break;
			}
			else if (ite->state == IDLE && ite->length == blocksize){
				buffer = ite->buffer;
				ite->state = USED;
				break;
			}
		}
		return buffer;
	}
	inline void deallocate(T* buffer)
	{
		if (!buffer && m_memorylist.empty())
			return;

		ST_MEMORY current;
		for (std::list<ST_MEMORY>::iterator ite = m_memorylist.begin(); ite != m_memorylist.end(); ite++)
		{
			if (ite->buffer == buffer){
				current.buffer = ite->buffer;
				current.length = ite->length;
				current.state = IDLE;
				m_memorylist.erase(ite);
				break;
			}
		}

		if (current.buffer == NULL && current.length == 0)
			return;

		std::list<ST_MEMORY>::iterator ite = m_memorylist.begin();
		while (ite != m_memorylist.end())
		{
			if (ite->state == IDLE && ite->buffer+ite->length == buffer){
				current.length += ite->length;
				current.buffer = ite->buffer;
				ite = m_memorylist.erase(ite);
				continue;
			}
			if (ite->state == IDLE && ite->buffer == current.buffer+current.length){
				current.length+=ite->length;
				ite = m_memorylist.erase(ite);
				continue;
			}
			ite++;
		}
		m_memorylist.push_back(current);
	}
private:
	size_t m_totalsize;
	std::list<ST_MEMORY> m_memorylist;
	T* m_address;
};
#endif /*__MEMORY_POOL_H__*/

详见:https://github.com/GeeeXXX/MemoryPool

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值