//Let's start with a complete memory management tool implementation. /// ready to make a complete memory management tool//threads involved, memory pools, extraction, not just new and delete overloads (or function overloads), this is a prototype of my, everyone who has what good to say thank you for the proposed, study together. #include <iostream>#include <string.h>#include <stdlib.h>#include <stdio.h>#include <list>#include <malloc.h>using namespace STD;classstring{ Public: String ():p TR (New Char[1]) {cout<<"String ()"<<endl; ptr[0]=' + '; } ~string () {if(PTR) {Deleteptr ptr = NULL; }} String (Const Char*P):p TR (New Char[strlen(p) +1]) {strcpy(PTR,P); }voidPrintf () {cout<<"Liuhuiyan"<<endl;cout<<ptr<<endl; } String (Conststring& s):p tr (New Char[strlen(S.PTR) +1]) {strcpy(PTR,S.PTR); } string&operator= (ConstString &s) {if( This!=&s) {if(PTR)Delete[]ptr; PTR = (Char*)malloc(strlen(S.PTR) +1);strcpy(PTR,S.PTR); } }Char&operator[](intSize) {returnPtr[size]; }Private:Char*ptr;};/// test classstructnode{void*_p; size_t _line;Char*_filename; size_t _size; Node (void*q = null,size_t len = size_t (),Const Char*name="",intD=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 is a type of passing in, list<Node>Mlist;//Call the STL to save a linked list.ostream&operator<< (ostream& OS,ConstNode &node)//Overload memory leak output information. {cout<<node._P<<endl;cout<<node._Line<<endl;cout<<node._Filename<<endl;cout<<node._Size<<endl; }///////////////////////////////////////////////struct_false{};struct_true{};//extraction.Template<TypeNameType>//Standardizingclasstriast{ Public:typedef_false _ispod_; };Template<>classtriast<int>//Special{ Public:typedef_true _ispod_;};Template<>//Special. classtriast<Char>{ Public:typedef_true _ispod_;};////////////////////////////////////////////Fairly with the STL inside the extraction, don't worry, this is just a prototype of me. Template<TypeNameType>classtraist{ Public:typedefType _type_;};Template<TypeNameType>Static voidTraits (type &val) {typedef TypeNameTraist<type>:: _type_ _type_; *val="123"; Val->printf ();}//Type extraction, my goal is that when I pass in the parameter, it does not need to pass its type. /////////////////////////////////////////////////Template<TypeNameType>Static void* _ALLOC (intSizeConst Char*_file_,int_line_,_true,type S1) {//Do not need to call the constructor's cheat space in such a way as int *,char * void*p =malloc(size); Node node (p,size,_file_,_line_); Mlist.push_back (node);returnP }Template<TypeNameType>Static void* _ALLOC (intSizeConst Char*_file_,int_line_,_false,type S1) {//The way in which the skin space of the constructor needs to be called, such as: String S (); void*p =malloc(size); Node node (p,size,_file_,_line_); Mlist.push_back (node);New(p) type ();returnP;}Template<TypeNameType>Static void* _ALLOC (intSizeConst Char*_file_,int_line_,type S1) {typedef TypeNameTriast<type>:: _ispod_ ispod; _alloc (Size,_file_,_line_,ispod (), S1);}Static voidPrintf () { list<Node>:: Iterator it; it = Mlist.begin (); while(It!=mlist.end ()) {cout<<*it<<"\ T"; ++it; }}Static voidDELETE (void*P)//The type extraction is required here, and the type of P is automatically judged to see if the constructor needs to be called. { list<Node>:: Iterator it; it = Mlist.begin (); while(1) {if(it->_p==p) {mlist.erase (IT); Break; } it++; }}intMain () {string *s = (String *) ALLOC (sizeof(string), string);//int *a = (int *) ALLOC (sizeof (int), int);//DELETE (a);//Printf ();Traits (s);//Extraction test. return 0;}
Let's implement a complete memory management tool (thread, memory pool, extraction)