C语言多线程内存管理模块_第1页
C语言多线程内存管理模块_第2页
C语言多线程内存管理模块_第3页
C语言多线程内存管理模块_第4页
C语言多线程内存管理模块_第5页
已阅读5页,还剩15页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、 C语言多线程内存管理模块摘要:一个多线程动态内存管理模块,可以有效地检测C语言中内存泄漏和内存越界等错误。1原理分配通过重新改写内存分配函数,把调用时的信息保存在一个节点中,节点中包括此内存分配的首地址,大小以及分配所在的源文件、函数、行号,并用一个HASH表来保存所有节点。越界检测为了检测写越界的错误,在用户申请的内存前后各增加了一定大小的内存作为监测区域,并初始化成预定值(Oxdeadbeef)。如果发生越界写操作时,预定值就会发生改变,即可检测到越界操作错误。释放重新改写内存释放函数free,释放时节点从HASH表中删除并进行越界检测。查看手动调用show_memory()show_m

2、emory_summary()查看内存使用情况并进行越界检测。以下涉及内存分配和内存释放的函数被重新改写:HASH表如下图所示:节点结构如下:staticstructmm_regionstructmm_region*next;charfile40;/*分配所在的文件*/charfunc40;/*分配所在的函数*/unsignedintlineno;/*分配所在的行*/size_tlen;/*内存分配的大小*/unsignedintfence;/*内存起始边界,用于头越界检测*/unsignedchardata0;/*用户内存分配首地址,malloc等函数返回以此为首地址的len长度的一块内存*

3、/*regionsSOME_PRIME;内存中一条节点的结构:mm_regionnextfilefunclinenolenfenceOxdeadbeefdatafence0 xdeadbeef内存起始边界检测头越界内存结束边界检测尾越界2测试步骤:.引入头文件:在需要检测的C/C+文件中引入mm.h头文件;.查看内存使用情况:调用show_memory()函数查看本文件中内存泄漏详细情况,或调用show_memory_summary()函数查看本文件中内存泄漏统计情况。内存泄漏测试代码#include/*加入头文件mm.h*/#includemm.hintmain(intargc,char*a

4、rgv)char*mp=NULL;char*cp=NULL;mp=(char*)malloc(6);cp=(char*)calloc(1,10);/*查看内存泄漏*/show_memory();show_memory_summary();return0;测试结果LEAKDETHEL:11+main+c11+main+c120+nialn+c10bytesallocatedinnialnatLine16bytesallocatedin2allocationsLEAKSUMMARY:16bytesin2allocationsin-file匚:16bytesalLocatedin2allocatio

5、ns内存越界测试代码#include/*加入头文件mm.h*/#includemm.hintmain(intargc,char*argv)char*mp=NULL;mp=(char*)malloc(6);/*越界操作*/memset(mp,0,10);/*释放或查看内存时检测free(mp);*/return0;测试结果HARN工NG:Hi芸hFenceuicilmtLiciriatOmE:7c:7口6日,inmmindFmmin.c:,in曰9释放错误此类错误包括:释放空指针释放野指针重复释放内存释放的起始地址与内存分配的起始地址不一致2.3.1测试代码#include/*加入头文件mm.h

6、*/#includemm.hintmain(intargc,char*argv)char*mp=NULL;mp=(char*)malloc(6);free(mp);/*重复释放*/free(mp);return0;2.3.2测试结果HARN工HG:F已ei足:u门口三:日二1小曰mmt13KE;1b,wOEELi口mmin匚,F川mi门.口-1i门曰1孑3源码两个文件:mm.h和mm.cmm.h/*mm.h*memoryusagedebugging(fromAsterisk)*/#ifndef_MM_H_#define_MM_H_#ifdef_cplusplusexternC#endif/*U

7、ndefineanymacros*/#undefmalloc#undefcalloc#undeffree#undefrealloc#undefstrdup#undefstrndup#undefasprintf#undefvasprintfvoid*_mm_calloc(size_tnmemb,size_tsize,constchar*file,intlineno,constchar*func);void*_mm_malloc(size_tsize,constchar*file,intlineno,constchar*func);void_mm_free(void*ptr,constchar*f

8、ile,intlineno,constchar*func);void*_mm_realloc(void*ptr,size_tsize,constchar*file,intlineno,constchar*func);char*_mm_strdup(constchar*s,constchar*file,intlineno,constchar*func);char*_mm_strndup(constchar*s,size_tn,constchar*file,intlineno,constchar*func);int_mm_asprintf(constchar*file,intlineno,cons

9、tchar*func,char*strp,constchar*format,.);int_mm_vasprintf(char*strp,constchar*format,va_listap,constchar*file,intlineno,constchar*func);/*Provideourowndefinitions*/#definecalloc(a,b)_mm_calloc(a,b,_FILE_,_LINE_,_PRETTY_FUNCTION_)#definemalloc(a)_mm_malloc(a,_FILE_,_LINE_,_PRETTY_FUNCTION_)#definefre

10、e(a)_mm_free(a,_FILE_,_LINE_,_PRETTY_FUNCTION_)#definerealloc(a,b)_mm_realloc(a,b,_FILE_,_LINE_,_PRETTY_FUNCTION_)#definestrdup(a)_mm_strdup(a,_FILE_,_LINE_,_PRETTY_FUNCTION_)#definestrndup(a,b)_mm_strndup(a,b,_FILE_,_LINE_,_PRETTY_FUNCTION_)#defineasprintf(a,b,c.)_mm_asprintf(_FILE_,_LINE_,_PRETTY_

11、FUNCTION_,a,b,c)#definevasprintf(a,b,c)_mm_vasprintf(a,b,c,_FILE_,_LINE_,_PRETTY_FUNCTION_)intshow_memory(void);intshow_memory_summary(void);#ifdef_cplusplus#endif#endif/*_MM_H_*/*mm.c*MemoryManagement(fromAsterisk)*/staticpthread_mutex_tmmlock=PTHREAD_MUTEX_INITIALIZER;staticinlinevoid*mmallocregio

12、n(sizetsize,constchar*file,intlineno,constchar*func)structmmregion*reg;void*ptr=NULL;unsignedint*fence;inthash;if(!(reg=(structmm_region*)malloc(size+sizeof(*reg)+sizeof(*fence)/*使用系统malloc*/mm_log(MemoryAllocationFailure-%dbytesinfunction%satline%dof%sn”,(int)size,func,lineno,file);strncpy(reg-file

13、,file,sizeof(reg-file);strncpy(reg-func,func,sizeof(reg-func);reg-lineno=lineno;reg-len=size;ptr=reg-data;hash=HASH(ptr);/*内存起始标志*/reg-fence=FENCEMAGIC;/*内存结束标志*/fence=(unsignedint*)(ptr+reg-len);*fence=FENCEMAGIC;pthread_mutex_lock(&mmlock);reg-next=regionshash;/*一个hash可能对应多个值*/regionshash=reg;pthr

14、eadmutexunlock(&mmlock);returnptr;staticinlinesizetmmsizeofregion(void*ptr)inthash=HASH(ptr);structmmregion*reg;sizetlen=0;pthread_mutex_lock(&mmlock);for(reg=regionshash;reg;reg=reg-next)if(reg-data=ptr)len=reg-len;break;pthreadmutexunlock(&mmlock);returnlen;staticvoidmmfreeregion(void*ptr,constcha

15、r*file,intlineno,constchar*func)inthash=HASH(ptr);structmmregion*reg,*prev=NULL;unsignedint*fence;pthread_mutex_lock(&mmlock);for(reg=regionshash;reg;reg=reg-next)if(reg-data=ptr)if(prev)prev-next=reg-next;elseregionshash=reg-next;break;prev=reg;pthreadmutexunlock(&mmlock);if(reg)if(!s)returnNULL;si

16、ze_tlen;void*ptr;if(!s)returnNULL;len=strlen(s)+1;if(lenn)len=n;if(ptr=_mm_alloc_region(len,file,lineno,func)strcpy(char*)ptr,s);return(char*)ptr;intmmasprintf(constchar*file,intlineno,constchar*func,char*strp,constchar*fmt,.)intsize;va_listap,ap2;chars;*strp=NULL;va_start(ap,fmt);va_copy(ap2,ap);si

17、ze=vsnprintf(&s,1,fmt,ap2);va_end(ap2);if(!(*strp=(char*)_mm_alloc_region(size+1,file,lineno,func)va_end(ap);return-1;vsnprintf(*strp,size+1,fmt,ap);vaend(ap);returnsize;intmmvasprintf(char*strp,constchar*fmt,valistap,constchar*file,intlineno,constchar*func)intsize;valistap2;chars;*strp=NULL;va_copy

18、(ap2,ap);size=vsnprintf(&s,1,fmt,ap2);va_end(ap2);if(!(*strp=(char*)_mm_alloc_region(size+1,file,lineno,func)vaend(ap);return-1;vsnprintf(*strp,size+1,fmt,ap);returnsize;intshowmemory(void)char*fn=NULL;structmmregion*reg;unsignedintx;unsignedintlen=0;unsignedintcount=0;unsignedint*fence;mm_log(nLEAK

19、DETAIL:n);pthread_mutex_lock(&mmlock);for(x=0;xnext)if(!fn|!strcasecmp(fn,reg-file)|!strcasecmp(fn,anomolies)/*头越界检测*/if(reg-fence!=FENCE_MAGIC)mm_log(WARNING:Headfenceviolationat%p,in%sof%s,line%dn,reg-data,reg-func,reg-file,reg-lineno);/*尾越界检测*/fence=(unsignedint*)(reg-data+reg-len);if(*fence!=FEN

20、CEMAGIC)mmlog(WARNING:Tailfenceviolationat%p,in%sof%sline%dn”,reg-data,reg-func,reg-file,reg-lineno);if(!fn|!strcasecmp(fn,reg-file)mm_log(%10dbytesallocatedin%20satline%5dof%sn,(int)reg-len,reg-func,reg-lineno,reg-file);len+=reg-len;count+;pthread_mutex_unlock(&mmlock);mm_log(%dbytesallocatedin%dallocationsn”,len,count);return0;intshowmemorysummary(void)char*fn=NULL;intx;structmm_region*reg;unsignedintlen=0;intcount=0;structfile_summarycharfn80;intlen;intcount;structfilesummary*next;*list=NULL,*cur;mm_l

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论