MemoryManage.h #include<string> #include<list> #include<assert.h> #include"TypeTraits.hpp" #include<iostream> using namespace std; struct BlockInfo { void *_ptr; string _file; int _line; BlockInfo(void* ptr=0,char* file="",int line=0)// :_ptr(ptr) ,_file(file) ,_line(line) {} }; list<BlockInfo> BlockLists; void *Alloc(size_t size,char* file,int line) { void* ptr=malloc(size); if(ptr) { BlockInfo info(ptr,file,line); BlockLists.push_back(info); } return ptr; } void Dealloc(void* ptr) { free(ptr); list<BlockInfo>::iterator it=BlockLists.begin(); while(it!=BlockLists.end()) { if(it->_ptr==ptr) { BlockLists.erase(it); return; } ++it; } assert(false); } void Print() { cout<<"内存泄露的内存块"<<endl; list<BlockInfo>::iterator it=BlockLists.begin(); while(it!=BlockLists.end()) { printf("ptr:%p file:%s line:%d\n",it->_ptr,it->_file,it->_line); ++it; } } template<class T> T* _NEW(size_t size,char* file,int line) { //T* ptr=(T*)Alloc(size,file,line); void* ptr=(void*)Alloc(size,file,line); if(TypeTraits<T>::IsPODType().Get()) return ptr; else return new(ptr) T; }; template<class T> T* _DELETE(T *ptr) { if(!TypeTraits<T>::IsPODType().Get()) ptr->~T(); Dealloc(ptr); }; template<class T> T* _NEW_ARRAY(size_t size,int num,const char* file,int line) { void *ptr=Alloc(size,file,line); *((int*)ptr)=num; ptr=(void*)((int)ptr+4); T*cur=(T*)ptr; if(!TypeTraits<T>::IsPODType()Get()) { for(int i=0;i<num;i++) { new(cur)T; ++cur; } } return ptr; }; template<class T> T _DELETE_ARRAY(void* ptr) { int num=*((int*)ptr-1); T* cur=(T*)ptr; if(!TypeTraits<T>::IsPODType().Get()) { for(int i=0;i<num;i++) { cur->~T(); ++cur; } } Dealloc((void*)((int)ptr-4)); }; #define NEW(type) _NEW<type>(sizeof(type),__FILE__,__LINE__) #define DELETE(type,ptr) _DELETE<type>(ptr) #define NEW_ARRAY(type,num) _NEW_ARRAY<type>(sizeof(type)*num+4,__FILE__,__LINE__) #define DELETE_ARRAY(type,ptr) _DALETE_ARRAY<type>(ptr); MemoryManage.cpp #include"MemoryManage.h" #include<iostream> using namespace std; void Test1() { int* p1 = (int*)Alloc(sizeof(int)*10, __FILE__, __LINE__); int* p2 = (int*)Alloc(sizeof(int)*10, __FILE__, __LINE__); int* p3 = (int*)Alloc(sizeof(int)*10, __FILE__, __LINE__); int* p4 = (int*)Alloc(sizeof(int)*10, __FILE__, __LINE__); //Dealloc(p1); Dealloc(p2); Dealloc(p3); //Dealloc(p4); Print(); } int main() { Test1(); system("pause"); return 0; } TypeTraits.hpp #include <iostream> using namespace std; struct __TrueType { bool Get () { return true ; } }; struct __FalseType { bool Get () { return false ; } }; template <class _Tp> struct TypeTraits { typedef __FalseType __IsPODType; }; template <> struct TypeTraits< bool> { typedef __TrueType __IsPODType; }; template <> struct TypeTraits< char> { typedef __TrueType __IsPODType; }; template <> struct TypeTraits< unsigned char > { typedef __TrueType __IsPODType; }; template <> struct TypeTraits< short> { typedef __TrueType __IsPODType; }; template <> struct TypeTraits< unsigned short > { typedef __TrueType __IsPODType; }; template <> struct TypeTraits< int> { typedef __TrueType __IsPODType; }; template <> struct TypeTraits< unsigned int > { typedef __TrueType __IsPODType; }; template <> struct TypeTraits< long> { typedef __TrueType __IsPODType; }; template <> struct TypeTraits< unsigned long > { typedef __TrueType __IsPODType; }; template <> struct TypeTraits< long long > { typedef __TrueType __IsPODType; }; template <> struct TypeTraits< unsigned long long> { typedef __TrueType __IsPODType; }; template <> struct TypeTraits< float> { typedef __TrueType __IsPODType; }; template <> struct TypeTraits< double> { typedef __TrueType __IsPODType; }; template <> struct TypeTraits< long double > { typedef __TrueType __IsPODType; }; template <class _Tp> struct TypeTraits< _Tp*> { typedef __TrueType __IsPODType; }; // // 使用参数推到的萃取处理 // template <class T> void Copy (const T* src , T* dst, size_t size, __FalseType ) { cout<<"__FalseType:" <<typeid( T).name ()<<endl; for (size_t i = 0; i < size ; ++i) { dst[i ] = src[ i]; } } template <class T> void Copy (const T* src , T* dst, size_t size, __TrueType ) { cout<<"__TrueType:" <<typeid( T).name ()<<endl; memcpy(dst , src, size*sizeof (T)); } // // 使用萃取判断类型的Get函数判断是否是 POD类型来处理 // template <class T> void Copy (const T* src , T* dst, size_t size) { cout<<"__TrueType:" <<typeid( T).name ()<<endl; if (TypeTraits <T>:: __IsPODType().Get ()) { memcpy(dst , src, size*sizeof (T)); } else { for (size_t i = 0; i < size ; ++i) { dst[i ] = src[ i]; } } }
本文出自 “sunshine225” 博客,请务必保留此出处http://10707460.blog.51cto.com/10697460/1757303
原文:http://10707460.blog.51cto.com/10697460/1757303