| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
fbwd
10年前发布

C++实现一个完整的内存管理工具(线程,内存池,萃取)

//让我们开始一个完整的内存管理工具的实现吧。  ///准备做一个完整的内存管理工具  //涉及线程,内存池,萃取,不仅仅是new跟delete的重载(或者说是函数重载),这是我的一个雏形,大家谁有什么好的指正谢谢提出,一起学习。  #include <iostream>  #include <string.h>  #include <stdlib.h>  #include <stdio.h>  #include <list>  #include <malloc.h>  using namespace std;  class String  {   public:      String():ptr(new char[1])      {          cout<<"String()"<<endl;          ptr[0]='\0';      }      ~String()      {          if(ptr){          delete ptr;          ptr = NULL;          }      }      String(const char *p):ptr(new char[strlen(p)+1])      {          strcpy(ptr,p);      }        void Printf()      {          cout<<"liuhuiyan"<<endl;          cout<<ptr<<endl;      }      String(const String& s):ptr(new char[strlen(s.ptr)+1])      {          strcpy(ptr,s.ptr);      }      String& operator = (const String &s)      {          if(this!=&s)          {              if(ptr)delete []ptr;              ptr = (char *)malloc(strlen(s.ptr)+1);              strcpy(ptr,s.ptr);          }      }      char& operator [](int size)      {          return ptr[size];      }    private:  char *ptr;  };  ///测试类    struct Node  {      void *_P;      size_t _Line;      char *_Filename;      size_t _Size;      Node(void *q = NULL,size_t len = size_t(),\      const char *name="",int d=int()):_P(q),\      _Line(len),_Filename(new char[strlen(name)+1]),_Size(d)      {             strcpy(_Filename,name);      }  };    #define ALLOC(size,type)  (_ALLOC(size,__FILE__,__LINE__,type()))  //type是传进来的一个类型,  list<Node> mlist;//调用stl的一个链表保存.    ostream& operator <<(ostream& os,const Node &node)//重载内存泄漏输出信息。  {      cout<<node._P<<endl;      cout<<node._Line<<endl;      cout<<node._Filename<<endl;      cout<<node._Size<<endl;   }  ///////////////////////////////////////////////  struct _false{};  struct _true{};//萃取.  template<typename type>//范化  class triast  {      public:          typedef  _false _ISPOD_ ;     };  template<>  class triast<int>//特化  {      public:      typedef _true _ISPOD_;  };  template<>//特化。  class triast<char>  {      public:      typedef _true _ISPOD_;  };  //////////////////////////////////////////  //相当与STL里面的萃取,别急,这只是我的一个雏形。    template<typename type>  class Traist  {      public:      typedef type _type_;  };    template<typename type>  static void traits(type &val)  {      typedef typename Traist<type> :: _type_ _TYPE_;      *val="123";      val->Printf();  }//类型萃取,我的目的是当我传入参数的时候,不需要传递它的类型。。  /////////////////////////////////////////////////  template<typename type>  static void* _ALLOC(int size,const char *_file_,int _line_,_true,type s1)  {           //不需要调用构造函数的赖皮空间的方式如int *,char *           void *p = malloc(size);          Node node(p,size,_file_,_line_);          mlist.push_back(node);          return p;         }  template<typename type>  static void* _ALLOC(int size,const char *_file_,int _line_,_false,type s1)  {          //需要调用构造函数的靠皮空间的方式如:String s();          void *p = malloc(size);          Node node(p,size,_file_,_line_);          mlist.push_back(node);          new(p)type();          return p;  }    template<typename type>  static void* _ALLOC(int size,const char *_file_,int _line_,type s1)  {      typedef typename triast<type> :: _ISPOD_ ispod;       _ALLOC(size,_file_,_line_,ispod(),s1);  }    static void Printf()  {          list<Node> :: iterator it;          it = mlist.begin();          while(it!=mlist.end())          {              cout<<*it<<"\t";              ++it;          }  }    static void DELETE(void *p)//这里就需要类型萃取,自动判别P的类型,看是否需要调用构造函数。  {      list<Node>::iterator it;      it = mlist.begin();      while(1)      {          if(it->_P==p){              mlist.erase(it);              break;              }          it++;      }  }    int main()  {      String *s = (String *)ALLOC(sizeof(String),String);  //int *a = (int *)ALLOC(sizeof(int),int);  //  DELETE(a);  //  Printf();      traits(s);//萃取测试.      return 0;  }