C語言多線程內(nèi)存管理模塊_第1頁
C語言多線程內(nèi)存管理模塊_第2頁
C語言多線程內(nèi)存管理模塊_第3頁
C語言多線程內(nèi)存管理模塊_第4頁
C語言多線程內(nèi)存管理模塊_第5頁
已閱讀5頁,還剩15頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、 C語言多線程內(nèi)存管理模塊摘要:一個(gè)多線程動(dòng)態(tài)內(nèi)存管理模塊,可以有效地檢測C語言中內(nèi)存泄漏和內(nèi)存越界等錯(cuò)誤。1原理分配通過重新改寫內(nèi)存分配函數(shù),把調(diào)用時(shí)的信息保存在一個(gè)節(jié)點(diǎn)中,節(jié)點(diǎn)中包括此內(nèi)存分配的首地址,大小以及分配所在的源文件、函數(shù)、行號(hào),并用一個(gè)HASH表來保存所有節(jié)點(diǎn)。越界檢測為了檢測寫越界的錯(cuò)誤,在用戶申請的內(nèi)存前后各增加了一定大小的內(nèi)存作為監(jiān)測區(qū)域,并初始化成預(yù)定值(Oxdeadbeef)。如果發(fā)生越界寫操作時(shí),預(yù)定值就會(huì)發(fā)生改變,即可檢測到越界操作錯(cuò)誤。釋放重新改寫內(nèi)存釋放函數(shù)free,釋放時(shí)節(jié)點(diǎn)從HASH表中刪除并進(jìn)行越界檢測。查看手動(dòng)調(diào)用show_memory()show_m

2、emory_summary()查看內(nèi)存使用情況并進(jìn)行越界檢測。以下涉及內(nèi)存分配和內(nèi)存釋放的函數(shù)被重新改寫:HASH表如下圖所示:節(jié)點(diǎn)結(jié)構(gòu)如下:staticstructmm_regionstructmm_region*next;charfile40;/*分配所在的文件*/charfunc40;/*分配所在的函數(shù)*/unsignedintlineno;/*分配所在的行*/size_tlen;/*內(nèi)存分配的大小*/unsignedintfence;/*內(nèi)存起始邊界,用于頭越界檢測*/unsignedchardata0;/*用戶內(nèi)存分配首地址,malloc等函數(shù)返回以此為首地址的len長度的一塊內(nèi)存*

3、/*regionsSOME_PRIME;內(nèi)存中一條節(jié)點(diǎn)的結(jié)構(gòu):mm_regionnextfilefunclinenolenfenceOxdeadbeefdatafence0 xdeadbeef內(nèi)存起始邊界檢測頭越界內(nèi)存結(jié)束邊界檢測尾越界2測試步驟:.引入頭文件:在需要檢測的C/C+文件中引入mm.h頭文件;.查看內(nèi)存使用情況:調(diào)用show_memory()函數(shù)查看本文件中內(nèi)存泄漏詳細(xì)情況,或調(diào)用show_memory_summary()函數(shù)查看本文件中內(nèi)存泄漏統(tǒng)計(jì)情況。內(nèi)存泄漏測試代碼#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);/*查看內(nèi)存泄漏*/show_memory();show_memory_summary();return0;測試結(jié)果LEAKDETHEL:11+main+c11+main+c120+nialn+c10bytesallocatedinnialnatLine16bytesallocatedin2allocationsLEAKSUMMARY:16bytesin2allocationsin-file匚:16bytesalLocatedin2allocatio

5、ns內(nèi)存越界測試代碼#include/*加入頭文件mm.h*/#includemm.hintmain(intargc,char*argv)char*mp=NULL;mp=(char*)malloc(6);/*越界操作*/memset(mp,0,10);/*釋放或查看內(nèi)存時(shí)檢測free(mp);*/return0;測試結(jié)果HARN工NG:Hi蕓hFenceuicilmtLiciriatOmE:7c:7口6日,inmmindFmmin.c:,in曰9釋放錯(cuò)誤此類錯(cuò)誤包括:釋放空指針釋放野指針重復(fù)釋放內(nèi)存釋放的起始地址與內(nèi)存分配的起始地址不一致2.3.1測試代碼#include/*加入頭文件mm.h

6、*/#includemm.hintmain(intargc,char*argv)char*mp=NULL;mp=(char*)malloc(6);free(mp);/*重復(fù)釋放*/free(mp);return0;2.3.2測試結(jié)果HARN工HG:F已ei足:u門口三:日二1小曰mmt13KE;1b,wOEELi口mmin匚,F(xiàn)川mi門.口-1i門曰1孑3源碼兩個(gè)文件: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)/*使用系統(tǒng)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);/*內(nèi)存起始標(biāo)志*/reg-fence=FENCEMAGIC;/*內(nèi)存結(jié)束標(biāo)志*/fence=(unsignedint*)(ptr+reg-len);*fence=FENCEMAGIC;pthread_mutex_lock(&mmlock);reg-next=regionshash;/*一個(gè)hash可能對應(yīng)多個(gè)值*/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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論