引用于:https://www.cnblogs.com/geekpaul/p/4204124.html
做了一点小修改
1.内存的分配:
(1)如果分配大小超过1024,直接采用malloc分配,分配的时候多分配sizeof(size_t)字节,用于保存该块的大小;
(2)否则根据分配大小,查找到容纳该大小的最小size的MemoryChunk;
(3)查找MemoryChunk的链表指针pList,找到空闲的MemoryBlock返回;
(4)如果pList为NULL,临时创建MemoryBlock返回;
(5)MemoryBlock头部包含两个成员,pChunk指向的所属的MemoryChunk对象,size表明大小,其后才是给用户使用的空间;
2.内存的释放:
(1)根据释放的指针,查找器size头部,即减去sizeof(size_t)字节,判断该块的大小;
(2)如果大小超过1024,直接free;
(3)否则交给MemoryChunk处理,而块的头部保存了该指针,因此直接利用该指针就可以收回该内存。
注意的问题:
上述设计的内存池通过冗余的头部来实现内存块的分配与释放,减少了内存池的操作时间,速度上要优于原始的malloc和free操作,同时减少了内存碎片的增加。但是该设计中没有去验证释放的块冗余头部的正确性,因此故意释放不属于内存池中的块或者修改头部信息都会导致内存池操作失败,当然这些可以由程序员来控制。此外,内存池中分配出去的内存块如果不主动释放,内存池没有保留信息,不会自动释放,但是在退出的时候会验证验证是否完全释放,其实这个在系统测试时候就可以检测出来,我想这个缺陷也是可以弥补的,在此提出,希望使用者注意。
#include <bits/stdc++.h>
using namespace std;
#include <pthread.h>
typedef pthread_mutex_t MUTEXTYPE;
#define INITMUTEX(hMutex) pthread_mutex_init(&hMutex,nullptr)
#define DELMUTEX(hMutex) pthread_mutex_destroy(&hMutex)
#define LOCK(hMutex) pthread_mutex_lock(&hMutex)
#define UNLOCK(hMutex) pthread_mutex_unlock(&hMutex)
class MemoryChunk;
struct BlockHeader
{
MemoryChunk* pChunk;
size_t len; // data size or free mem
};
struct MemoryBlock;
struct BlockData
{
union{
MemoryBlock* pNext;
char *pBuffer;
};
};
struct MemoryBlock
{
BlockHeader header;
BlockData data;
};
class MemoryChunk
{
public:
MemoryChunk(size_t size, int count)
{
INITMUTEX(hMutex);
this->pFreeList=nullptr;
this->size=size;
this->count=0;
MemoryBlock* pBlock;
while(count--){
pBlock=CreateBlock();
if(!pBlock)break;
pBlock->data.pNext=pFreeList;
pFreeList=pBlock;
}
}
~MemoryChunk()
{
int tempcount=0;
MemoryBlock* pBlock;
pBlock = pFreeList;
while(pBlock){
pFreeList=pBlock->data.pNext;
DeleteBlock(pBlock);
++tempcount;
pBlock = pFreeList;
}
assert(tempcount==count);//!确保释放完全
DELMUTEX(hMutex);
}
void* malloc()
{
MemoryBlock* pBlock;
LOCK(hMutex);
if(pFreeList){
pBlock=pFreeList;
pFreeList=pBlock->data.pNext;
}else{
if(!(pBlock=CreateBlock())){
UNLOCK(hMutex);
return nullptr;
}
}
UNLOCK(hMutex);
return &pBlock->data.pBuffer;
}
static void free(void* pMem)
{
auto* pBlock=(MemoryBlock*)((char*)pMem-sizeof(BlockHeader));
pBlock->header.pChunk->free(pBlock);
}
void free(MemoryBlock* pBlock)
{
LOCK(hMutex);
pBlock->data.pNext=pFreeList;
pFreeList=pBlock;
UNLOCK(hMutex);
}
protected:
MemoryBlock* CreateBlock()
{
auto* pBlock=(MemoryBlock*)::malloc(sizeof(BlockHeader)+size);
if(pBlock){
pBlock->header.pChunk=this;
pBlock->header.len=size;
++count;
}
return pBlock;
}
void DeleteBlock(MemoryBlock* pBlock)
{
::free(pBlock);
}
private:
MemoryBlock* pFreeList;
size_t size;//!Block大小
int count;//!Block数目
MemoryChunk* pNext{};
MUTEXTYPE hMutex{};
};
struct HeapHeader
{
size_t size;
};
struct MemoryHeap
{
HeapHeader header;
char pBuffer;
};
class StaticMemory
{
public:
typedef enum{MAX_SIZE=1024,MIN_SIZE=sizeof(MemoryChunk*)};
StaticMemory()
{
chunkcount=0; // 类似 stl 二级管理
for(size_t size=MIN_SIZE; size<=MAX_SIZE; size*=2)++chunkcount;
pChunkList=new MemoryChunk*[chunkcount];
int index=0;
for(size_t size=MIN_SIZE; size<=MAX_SIZE; size*=2)
{
pChunkList[index++]=new MemoryChunk(size,1000);
}
}
~StaticMemory()
{
for(int index=0; index<chunkcount; ++index)
{
delete pChunkList[index];
}
//free(pChunkList);
delete[] pChunkList;
}
void* Malloc(size_t size)
{
if(size>MAX_SIZE){
return malloc(size);
}
int index=0; // 找到最接近的那一块
for(size_t tsize=MIN_SIZE; tsize<=MAX_SIZE; tsize*=2){
if(tsize>=size)break;
++index;
}
return pChunkList[index]->malloc();
}
void Free(void* pMem)
{
if(!free(pMem))MemoryChunk::free(pMem);
}
protected:
void* malloc(size_t size)
{
auto* pHeap=(MemoryHeap*)::malloc(sizeof(HeapHeader)+size);
if(pHeap){
pHeap->header.size=size;
return &pHeap->pBuffer;
}
return nullptr;
}
bool free(void* pMem)
{
auto* pHeap=(MemoryHeap*)((char*)pMem-sizeof(HeapHeader));
if(pHeap->header.size>MAX_SIZE){
::free(pHeap);
return true;
}
return false;
}
private:
MemoryChunk** pChunkList;
int chunkcount;
};
int main(){
StaticMemory st;
int *m1 = (int *)st.Malloc(12);
int *m2 = (int *)st.Malloc(4);
int *m3 = (int *)st.Malloc(4);
*m1=1;
*m2=2;
*m3=3;
cout<< *m1<<endl;
st.Free(m1);
st.Free(m2);
st.Free(m3);
}
网友评论