求救大神.关于内存池的问题

看书写了一段代码用来分配内存。以下是头文件。 

#ifndef MEMPOOL_H_INCLUDED
#define MEMPOOL_H_INCLUDED
#include
#include
#define max_list 10

struct p_data //基本的池单元
{
struct p_data *pre;
void *avi;
void *limi;
unsigned long p_size;
};
struct p_data *head=NULL;
struct p_data *start=NULL;
void new_pool(void) //开辟新的内存池
{
if(head!=NULL||start!=NULL)
{
printf("You has created a pool\n");
return;
}
head=(struct p_data *)malloc(sizeof(*head));
start=(struct p_free *)malloc(sizeof(*start));
if(start==NULL||head==NULL)
{
printf("The environment doesn't allow the allocation of memory\n");
return;
}
head->pre=NULL;
head->avi=NULL;
head->limi=NULL;
head->p_size=0;
start->limi=NULL;
start->avi=NULL;
start->pre=NULL;
start->p_size=0;
}

void *p_malloc(unsigned long n_size) //在池中开辟新的内存单元
{
void *re_p=NULL;
struct p_data *now=NULL;
struct p_data *go=NULL;
if(!(n_size>0))
{
printf("The size must be bigger than zero\n");
return NULL;
}
if(head->pre!=NULL)
{
now=head;
for(;;now=now->pre)
{
if((now->limi-now->avi)>=n_size)
{
re_p=now->avi;
now->avi=(char *)now->avi+n_size;
return re_p;
}
if(now->pre->pre==NULL)break;
else continue;
}
}
if(start->pre!=NULL)
{
now=start->pre;
go=start;
for(;;now=now->pre)
{
if(now->p_size>n_size)
{
if(now->pre==NULL)go->pre=NULL;
else go->pre=now->pre;
for(go=head;;go=go->pre)
if(go->pre==NULL)break;
go->pre=now;
now->pre=NULL;
re_p=(char *)now+sizeof(struct p_data);
go->avi=(char *)re_p+sizeof(struct p_data)+n_size;
go->limi=(char *)re_p+sizeof(struct p_data)+now->p_size;
memset((char *)now+sizeof(struct p_data),'\0',now->p_size);
start->p_size--;
head->p_size++;
return re_p;
}
if(now->pre==NULL)break;
else go=now;
}
}
if((now=(struct p_data *)malloc(sizeof(struct p_data)+n_size+1024*10))==NULL)
{
printf("The environment doesn't allow the allocation of memory.\n");
return NULL;
}
now->pre=NULL;
now->p_size=n_size+10*1024;
now->limi=NULL;
now->avi=NULL;
for(go=head;;go=go->pre)
{
if(go->pre==NULL)break;
}
go->pre=now;
re_p=(char *)now+sizeof(struct p_data);
go->avi=(char *)now+sizeof(struct p_data)+n_size;
go->limi=(char *)now+sizeof(struct p_data)+n_size+1024*10;
head->p_size++;
return re_p;
}

void clean_pool(void) //将所有池内存转移到空白列
{
if(head==NULL||head->pre==NULL)
{
printf("There's no data in the pool\n");
return;
}
struct p_data *now=head->pre;
struct p_data *mid=NULL;
for(;;)
{
now->avi=NULL;
now->limi=NULL;
mid=now->pre;
now->pre=start->pre;
start->pre=now;
start->p_size++;
if(mid==NULL||start->p_size==max_list)break;
else now=mid;
}
if(now->pre!=NULL&&max_list==start->p_size)
{
now=now->pre;
mid=now;
now=now->pre;
for(;;)
{
free(&mid);/*循环若干次后就是这里出了问题。。*/
if(now->pre==NULL)
{
mid=now;
free(&mid);
break;
}
mid=now;
now=now->pre;
}
}
head->pre=NULL;
head->limi=NULL;
head->p_size=0;
head->avi=NULL;
}

#endif // MEMPOOL_H_INCLUDED

 

以下是驱动小程序 

#include
#include "mempool.h"
int main(void)
{
int n;
int m=0;
char *test=NULL;
for(;;)
{
new_pool();
for(n=0;n<15;n++)p_malloc(10240+1);
printf("%d",m++);
clean_pool();
}
return 0;

 

不知道为什么在循环若干次后会出现c0000005问题... 
经排查是在free()出的问题。是内存泄露吗?

回复讨论(解决方案)

一般free出错很可能是你动态分配的内存访问时越界造成的

永不止步步 发表于01-13 11:04 浏览65535次
分享到:

已有0条评论

暂时还没有回复哟,快来抢沙发吧

添加一条新评论

只有登录用户才能评论,请先登录注册哦!

话题作者

永不止步步
金币:67417个|学分:363791个
立即注册
畅学电子网,带你进入电子开发学习世界
专业电子工程技术学习交流社区,加入畅学一起充电加油吧!

x

畅学电子网订阅号