【C++】内存池

       C++中系统没有自动回收管理内存的方式,必须要用new和delete来申请和释放内存。但是如果频繁使用new和delete会有效率问题的存在和内存碎片的产生。

       内存池则是在真正使用内存之前,先申请分配一定数量的、大小相等的内存块留做备用。当有新的内存需求时,就从内存池中分出一部分内存块,若内存不够再继续申请新的内存。这样做的一个显著优点是,使得内存分配的效率得到提升。

       使用静态链表来实现内存池。

       下面是图形分析内存的申请与回收过程

 

首先写一个队列的栗子来描述内存池的问题

#include <iostream>

template <typename T>
class Queue;
template <typename T>
class QueueItem
{
public:
	QueueItem(T val = T()):data(val),pnext(NULL){}
	void* operator new(size_t size)
	{
		if(pool == NULL)
		{
			pool = (QueueItem<T>*)new char[size*10];
			QueueItem<T>* pCur = pool;
			for(pCur ; pCur < pool+10-1 ; pCur = pCur + 1)
			{
				pCur->pnext = pCur+1;
			}
			pCur->pnext = NULL;
		}
		QueueItem<T>* prt = pool;
		pool = pool->pnext;
		return prt;
	}
	void operator delete(void* ptr)
	{
		if(ptr == NULL)
		{
			return;
		}
		QueueItem<T>* mptr = (QueueItem<T>*) ptr;
		mptr->pnext = pool;
		pool = mptr;
	}
private:
	QueueItem<T> *pnext;
	T data;
	static QueueItem<T> *pool;
	friend class Queue<T>;
};

template <typename T>
QueueItem<T> *QueueItem<T>::pool = NULL;


template <typename T>
class Queue
{
public:
	Queue()
	{
		phead = ptail = new QueueItem<T>();
	}
	~Queue()
	{
		QueueItem<T>* pCur = phead;
		QueueItem<T>* pNext = pCur;
		while(pCur != NULL)
		{
			pNext = pCur->pnext;
			delete pCur;
			pCur = pNext;
		}
		phead = ptail = NULL;
	}
	void Push(T val)
	{
		QueueItem<T>* pnewnode = new QueueItem<T>(val);
		ptail->pnext = pnewnode;
		ptail = ptail->pnext;
	}
	void Pop()
	{
		if(empty())
		{
			throw std::exception("Queue Is NULL!");
		}
		QueueItem<T>* pCur = phead->pnext;
		phead->pnext = pCur->pnext;
		delete pCur;
	}
	T back()
	{
		if(empty())
		{
			throw std::exception("Queue Is NULL!");
		}
		return ptail->data;
	}
	T front()
	{
		if(empty())
		{
			throw std::exception("Queue Is NULL!");
		}
		return phead->pnext->data;
	}
	bool empty()
	{
		return (phead == ptail) && (phead != NULL);
	}
	void Show()
	{
		QueueItem<T>* pCur = phead->pnext;
		while(pCur != NULL)
		{
			std::cout<<pCur->data<<" ";
			pCur = pCur->pnext;
		}
		std::cout<<std::endl;
	}
private:
	QueueItem<T>* phead;
	QueueItem<T>* ptail;
};

int main()
{
	Queue<int> q1;
	q1.Push(10);
	for(int i = 0 ; i<10 ; i++)
	{
		q1.Push(i+1);
	}
	q1.Show();
	q1.Pop();
	q1.Show();
	return 0;
}
#include <iostream>

template<typename T>
class MEM_Mangement
{
public:
	static MEM_Mangement<T>& getInstance()
	{
		return mm;
	}
	void* alloc(size_t size)
	{
		if (pool == NULL)
		{
			pool = (Node*)new char[size * 10];
			Node* pCur = pool;
			for (pCur; pCur < pool + 10 - 1; pCur = pCur + 1)
			{
				pCur->pnext = pCur + 1;
			}
			pCur->pnext = NULL;
		}
		Node* prt = pool;
		pool = pool->pnext;
		return prt;
	}
	void dealloc(void* ptr)
	{
		if (ptr == NULL)
			return;
		Node* mptr = (Node*)ptr;
		mptr->pnext = pool;
		pool = mptr;
	}
private:
	MEM_Mangement(){}
	MEM_Mangement(const MEM_Mangement<T>&);
	class Node
	{
	public:
		Node(T val = T()) :mdata(val), pnext(NULL){}
	public:
		T mdata;//Student mdata;
		Node* pnext;
	};
	static Node* pool;

	static MEM_Mangement<T> mm;
};
template<typename T>
MEM_Mangement<T> MEM_Mangement<T>::mm;
template<typename T>
typename MEM_Mangement<T>::Node* MEM_Mangement<T>::pool = NULL;
class Student
{
public:
	Student(std::string name, std::string id, float score)
	{
		mname = name;
		mid = id;
		mscore = score;
	}
	Student()
	{}
	void* operator new(size_t size)
	{
		return mm.alloc(size);
	}
	void operator delete(void* ptr)
	{
		mm.dealloc(ptr);
	}
private:
	std::string mname;
	std::string mid;
	float mscore;
	static MEM_Mangement<Student>& mm;
};
MEM_Mangement<Student>& Student::mm = MEM_Mangement<Student>::getInstance();
int main()
{
	Student* pst1 = new Student("zhangsan", "001", 59);
	Student* pst2 = new Student("lisi", "002", 60);
	Student* pst3 = new Student("wangwu", "003", 61);

	delete pst1;
	delete pst2;
	delete pst3;
	return 0;
}

单列模式:

    构造函数和拷贝构造函数被屏蔽(即写在private下)

    在类中提供一个接口生成唯一对象(1、不能返回类类型    2、不能依赖对象调用)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值