内存池主要文件:MemoryMgr.h
#ifndef _MemoeyMgr_hpp_
#define _MemoeyMgr_hpp_
#include<stdlib.h>
#include<assert.h>
#include<iostream>
#define MAX_MEMORY_SIZE 128
class MemoryAlloc;
class MemoryMgr;
//内存块 最小单元
class MemoryBlock{
public:
//内存块编号
int nID;
//引用次数
size_t nRef;
//所属最大内存块
MemoryAlloc* pAlloc;
// 下一块位置
MemoryBlock* pNext;
//是否在内存池中
bool bPool;
private:
//预留
char c1;
char c2;
char c3;
};
//内存池
class MemoryAlloc{
public:
MemoryAlloc(){
_pBuf = nullptr;
_pHeader = nullptr;
_nSize = 0;
_nBlockCount = 0;
}
~MemoryAlloc(){
if (_pBuf){
free(_pBuf);
}
}
//初始化内存池
void initMemory(){
//断言
assert(nullptr == _pBuf);
if (nullptr != _pBuf)
return;
//向系统申请内存池内存
size_t bufsize = (_nSize+sizeof(MemoryBlock))*_nBlockCount;
_pBuf = (char*)malloc(bufsize);
//初始化内存池
_pHeader = (MemoryBlock*)_pBuf;
_pHeader->bPool = true;
_pHeader->nID = 1;
_pHeader->nRef = 0;
_pHeader->pAlloc = this;
_pHeader->pNext = nullptr;
MemoryBlock* pTemp1 = _pHeader;
for (size_t n = 2; n < _nBlockCount; n++){
MemoryBlock* pTemp2 = (MemoryBlock*)(_pBuf + (n*(_nSize + sizeof(MemoryBlock))));
pTemp2->bPool = true;
pTemp2->nID = n;
pTemp2->nRef = 0;
pTemp2->pAlloc = this;
pTemp2->pNext = nullptr;
pTemp1->pNext =pTemp2;
pTemp1 = pTemp2;
}
}
//外部向内存池申请内存
void* allocMemmory(size_t nSize){
if (nullptr==_pBuf){
initMemory();
}
MemoryBlock* pReturn = nullptr;
if (nullptr == _pHeader){
pReturn = (MemoryBlock*)malloc(nSize + sizeof(MemoryBlock));
pReturn->bPool = false;
pReturn->nID = -1;
pReturn->nRef = 1;
pReturn->pAlloc = nullptr;
pReturn->pNext = nullptr;
}
else{
pReturn = _pHeader;
_pHeader = _pHeader->pNext;
assert(0 == pReturn->nRef);
std::cout << "allocMem" << pReturn << ",id=" << pReturn->nID << ",size=" << nSize << std::endl;
pReturn->nRef = 1;
}
//返回的内存空间也没有MemoryBlock的包头
return ((char*)pReturn + sizeof(MemoryBlock));
}
//内存池释放内存
void freeMemory(void* pMem){
//传进来的值没有MemoryBlock的包头
//所以传进来的指针需要向左移动sizeof(MemoryBlock)个字节
MemoryBlock* pBlock=(MemoryBlock*)((char*)pMem - sizeof(MemoryBlock));
assert(1 == pBlock->nRef);
if (--pBlock->nRef != 0){
return ;
}
if (pBlock->bPool){
pBlock->pNext = _pHeader;
_pHeader = pBlock;
}
else{
free(pBlock);
}
}
protected:
//内存池地址
char* _pBuf;
//头部单元内存
MemoryBlock* _pHeader;
// 内存单元的大小
size_t _nSize;
// 内存单元的数量
size_t _nBlockCount;
};
//便于声明类成员利用模板。
template<size_t nSize,size_t nBlocksize>
class MemoryAllocator:public MemoryAlloc{
public:
MemoryAllocator()
{
const int n = sizeof(void*);
_nSize = (nSize/n) * n+(nSize/n?0:n);
_nBlockCount = nBlocksize;
}
};
//内存管理工具
class MemoryMgr{
private:
MemoryMgr(){
init_Mem(0, 32, &_mem32);
init_Mem(33, 64, &_mem64);
init_Mem(65, 128, &_mem128);
}
~MemoryMgr(){
}
public:
//单例模式
static MemoryMgr& Instance(){
static MemoryMgr mgr;
return mgr;
}
//申请内存
void* allocMem(size_t nSize){
if (nSize <= MAX_MEMORY_SIZE){
return _szAlloc[nSize]->allocMemmory(nSize);
}
else{
MemoryBlock* pReturn = (MemoryBlock*)malloc(nSize + sizeof(MemoryBlock));
pReturn->bPool = false;
pReturn->nID = -1;
pReturn->nRef = 1;
pReturn->pAlloc = nullptr;
pReturn->pNext = nullptr;
//pReturn的地址
//xPrintf("allocMem:%llx,id=%d,size=%d\n", pReturn,pReturn->nID, nSize);
std::cout << "allocMem" << pReturn << ",id=" << pReturn->nID << ",size=" << nSize << std::endl;
return ((char*)pReturn+sizeof(MemoryBlock));
}
}
//释放内存
void freeMem(void* pMem){
MemoryBlock* pBlock = (MemoryBlock*)((char*) pMem - sizeof(MemoryBlock));
std::cout << "allocMem" << pBlock << ",id=" << pBlock->nID << std::endl;
if (pBlock->bPool){
pBlock->pAlloc->freeMemory(pMem);
}
else
{
if (--pBlock->nRef == 0){
free(pBlock);
}
}
}
void addRef(void* pMem){
MemoryBlock* pBlock = (MemoryBlock*)((char*)pMem - sizeof(MemoryBlock));
++pBlock->nRef;
}
private:
//初始化内存池映射数组
void init_Mem(int nBegin, int nEnd, MemoryAlloc* pMemA){
for (int n = nBegin; n <= nEnd; n++){
//
_szAlloc[n] = pMemA;
}
}
private:
MemoryAllocator<64, 10> _mem64;
MemoryAllocator<128, 10> _mem128;
MemoryAllocator<32, 10> _mem32;
//存放每个所需内存大小的的每个对应内存池地址
MemoryAlloc* _szAlloc[MAX_MEMORY_SIZE + 1];
};
#endif
分配内存的头文件:Alloctor.h
#ifndef _ALLOCTOR_H_
#define _ALLOCTOR_H_
void* operator new(size_t size);
void operator delete(void* p);
void* operator new[](size_t size);
void operator delete[](void* p);
void* mem_alloc(size_t size);
void mem_free(void* p);
#endif
分配内存的cpp文件:Alloctor.cpp
#include"Alloctor.h"
#include<stdlib.h>
#include"MemoryMgr.h"
void* operator new(size_t size){
return MemoryMgr::Instance().allocMem(size);
}
void operator delete(void* p){
MemoryMgr::Instance().freeMem(p);
}
void* operator new[](size_t size){
return MemoryMgr::Instance().allocMem(size);
}
void operator delete[](void* p){
MemoryMgr::Instance().freeMem(p);
}
void* mem_alloc(size_t size){
return malloc(size);
}
void mem_free(void* p){
free(p);
}