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; }</pre>