简易的实现对象内存池
MemoryNode结构体是保存每一个申请的内存节点,然后构成一个单链表。
MemoryNodeList 结构体是保存的是每一块内存,当上一个内存块用完时,再次创建一个内存块。
numofMemoryNode:一个内存块的内存节点数量
numofmemoryList:内存块数量
numofAlloc:已经分配出去的内存数量
删除2 改为回收值为2的节点内存
#pragma once
template<class T>
class MemoryPool
{
public:struct MemoryNode {T element;MemoryNode* next;};struct MemoryNodeList {MemoryNode* memoryNode;MemoryNodeList* next;};MemoryPool() {currenetNode = nullptr; memoryList= nullptr;}~MemoryPool();bool create(const int num);//创建一个对象池void destroy();//释放内存T* Alloc();//分配内存void Free(const T* obj);//回收一个对象内存int getPoolSize()const { return numofmemoryList * numofMemoryNode; }bool IsCreate()const { return memoryList != nullptr; }int getAlloc()const { return numofAlloc; }
private:MemoryNodeList* memoryList;MemoryNode* currenetNode;int numofMemoryNode;int numofAlloc;int numofmemoryList;
};
#include "MemoryPool.h"
template<class T>
MemoryPool<T>::~MemoryPool() {
//防止没有人工释放内存,利用析构函数if (getPoolSize()>0||IsCreate()) {destroy();}
}template<class T>
bool MemoryPool<T>::create(const int num) {numofMemoryNode = num;memoryList = new MemoryNodeList;memoryList->next = nullptr;numofmemoryList = 1;memoryList->memoryNode = new MemoryNode[numofMemoryNode];//连成链表for (int i = 1; i < numofMemoryNode; i++){memoryList->memoryNode[i - 1].next = &memoryList->memoryNode[i];}memoryList->memoryNode[numofMemoryNode - 1].next = nullptr;currenetNode = memoryList->memoryNode;numofAlloc = 0;return true;
}template<class T>
void MemoryPool<T>::destroy() {while (memoryList) {if (memoryList->memoryNode) {delete[] memoryList->memoryNode;memoryList->memoryNode = nullptr;}MemoryNodeList* pList = memoryList;memoryList = memoryList->next;delete pList;}numofMemoryNode = 0;numofAlloc = 0;numofmemoryList = 0;
}template<class T>
T* MemoryPool<T>::Alloc() {if (currenetNode == nullptr) {MemoryNodeList* pList = new MemoryNodeList;numofmemoryList++;pList->memoryNode = new MemoryNode[numofMemoryNode];for (int i = 1; i < numofMemoryNode; i++){pList->memoryNode[i - 1].next = &pList->memoryNode[i];}pList->memoryNode[numofMemoryNode - 1].next = nullptr;pList->next = memoryList;memoryList = pList;currenetNode = memoryList->memoryNode;}MemoryNode* res = currenetNode;currenetNode = currenetNode->next;numofAlloc++;return &(res->element);
}template<class T>
void MemoryPool<T>::Free(const T* obj) {MemoryNode* pNode = (MemoryNode*)obj;pNode->next = currenetNode;currenetNode = pNode;numofAlloc--;
}
#include<iostream>
#include"MemoryPool.cpp"
using namespace std;void main() {MemoryPool<int> memory;memory.create(3);int* a1 = memory.Alloc(); *a1 = 1;int* a2 = memory.Alloc(); *a2 = 2;int* a3 = memory.Alloc(); *a3 = 3;int* a4 = memory.Alloc(); *a4 = 4;memory.Free(a2);memory.Free(a1);int* a5 = memory.Alloc(); *a5 = 5;int* a6 = memory.Alloc(); *a6 = 6;int* a7 = memory.Alloc(); *a7 = 7;int* a8 = memory.Alloc(); *a8 = 8;cout << "a1=" << *a1 << endl;cout << "a2=" << *a2 << endl;cout << "a3=" << *a3 << endl;cout << "a4=" << *a4 << endl;cout << "a5=" << *a5 << endl;cout << "a6=" << *a6 << endl;cout << "a7=" << *a7 << endl;cout << "a8=" << *a8 << endl; memory.destroy();cout << "a6=" << *a6 << endl;system("pause");
}
学习于这位大佬