一个简单实用的内存池类

头文件(mymem.h):

#ifndef _my_mem_h6314854
#define _my_mem_h6314854

typedef struct humem
{
 int flag;        //标记是否在使用   // iFlag  nFlag
 int size;        //内存块的大小
 humem *pNext;    //下一个块的指针
}tMem,*ptMem; 

class mymem
{
 int num;         //内存块数   m_
 ptMem pHead;     //块区头指针  

 ptMem createMem(int size);   //申请新内存
public:
 mymem();
 ~mymem();
 void * getMem(int size);      //得到合适的内存
 ptMem getHead() { return pHead; }
 void freeMem(void * );//释放内存,即设置flag标记为0
};

#endif

实现文件(mymem.cpp):

#include "mymem.h"
#define NULL 0
mymem::mymem()
{
 num = 0;       //内存块数
 pHead = NULL;    //块区头指针
}
mymem::~mymem()
{
 ptMem temp;
 while (pHead!=NULL)
 {
  temp=pHead;
  pHead=pHead->pNext;
  delete(temp);
 }
}

ptMem mymem::createMem(int size)
{
 ptMem temptmem;
 temptmem = (tMem *)new char[size + sizeof(tMem)];
 if (temptmem == NULL)
 {
  return NULL;
 }

 temptmem->flag=0;  //未使用内存
 temptmem->size=size;

 if (pHead==NULL)//第一次申请内存
 {
  pHead=temptmem;
  temptmem->pNext=NULL;
 }
 else
 {
  temptmem->pNext=pHead;
  pHead=temptmem;
 }
 return temptmem;
}
void * mymem::getMem(int sizex)
{
 ptMem tempm;
 tempm=pHead;
 while (tempm!=NULL && tempm->flag==0)
 {
  if (tempm->size>sizex)   // 已经找到
  {
   tempm->flag=1;
   return (void *)(tempm + 1);
  }
  tempm=tempm->pNext;
 }

 tempm = createMem(sizex);
 tempm->flag = 1;  // 设置为使用状态
 return (void *)(tempm + 1);;

}

void mymem::freeMem(void * pMem)//释放内存,即设置flag标记为0然后将其移动到链表表头
{
 ptMem  tempm;
 tempm = (ptMem)pMem - 1;
 ptMem tempmh=pHead;
 tempm->flag=0;  // 设置为可用状态
 if (tempmh!=tempm)
 {
  while (tempmh->pNext!=tempm)
  {
   tempmh=tempmh->pNext;
  }
  tempmh->pNext = tempm->pNext;
  tempm->pNext=pHead;
  pHead=tempm; 
 }
}

 

主程序(main.cpp):

#include <iostream>
using namespace std;
#include "mymem.h"
void main()
{
 int i;
 mymem humem;
 ptMem tempmem;

 void * huproA[10];
 for (i=0;i<10;i++)
 {
  huproA[i]=humem.getMem(i*400+4);
 }
 
 tempmem=humem.getHead();
 while (tempmem!=NULL)
 {
  cout<<"内存大小:"<<tempmem->size<<"是否在使用"<<tempmem->flag<<endl;
  tempmem=tempmem->pNext;
 }

 humem.freeMem(huproA[0]);
 cout<<"_____________________/n";
 tempmem=humem.getHead();
 while (tempmem!=NULL)
 {
  cout<<"内存大小:"<<tempmem->size<<"是否在使用"<<tempmem->flag<<endl;
  tempmem=tempmem->pNext;
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我会尽力回答你的问题。对于你的问题,我可以提供如下的解决方案: 首先,内存池一个预分配一定数量的内存,用于重复分配和释放小块内存的优化技术。内存池的实现可以使用C++11中的std::allocator和std::vector,也可以使用自定义的内存管理方法。 针对你的要求,我们可以实现一个MemoryPool,包含以下成员函数: 1. allocate(size_t size):分配内存函数,接受需要分配的内存大小作为参数,返回分配的内存地址。在函数内部,首先判断内存池中是否有足够的空闲内存,如果有则从空闲内存中分配,否则从系统中分配。如果从系统中分配,则需要扩展内存池的大小,使用std::vector或者自定义内存管理方法来实现内存池的扩展。 2. deallocate(void* ptr):释放内存函数,接受需要释放的内存地址作为参数。在函数内部,将该内存块标记为空闲状态,并且可以合并相邻的空闲内存块。 3. clear():清空内存池函数,将内存池中的所有内存块标记为空闲状态。 在MemoryPool中,我们需要定义一个内存块的结构体,包含一个指向下一个内存块的指针和一个标记当前内存块是否为空闲的布尔变量。 在allocate函数中,我们需要使用std::mutex和std::lock_guard来实现线程安全,避免出现多个线程同时分配内存的情况。 在deallocate函数中,我们需要使用std::mutex和std::lock_guard来实现线程安全,避免出现多个线程同时释放内存的情况。同时,为了避免内存碎片的产生,我们需要在释放内存块时,将相邻的空闲内存块进行合并。 这里提供一个简单的代码框架,具体实现需要根据具体的需求进行调整和完善: ```c++ #include <iostream> #include <vector> #include <mutex> using namespace std; class MemoryPool { public: MemoryPool(size_t blockSize, size_t blockNum) { // TODO: 初始化内存池 } void* allocate(size_t size) { // TODO: 分配内存 return nullptr; } void deallocate(void* ptr) { // TODO: 释放内存 } void clear() { // TODO: 清空内存池 } private: struct MemoryBlock { MemoryBlock* next; bool isFree; // TODO: 定义其他需要的成员变量 }; size_t m_blockSize; size_t m_blockNum; vector<MemoryBlock*> m_blocks; MemoryBlock* m_freeList; // TODO: 定义其他需要的成员变量 mutex m_mutex; }; int main() { MemoryPool pool(1024, 100); void* mem1 = pool.allocate(100); void* mem2 = pool.allocate(200); pool.deallocate(mem1); pool.deallocate(mem2); pool.clear(); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值