SEO [PR] 爆速!無料ブログ 無料ホームページ開設 無料ライブ放送
メインページ   名前空間一覧   クラス階層   構成   ファイル一覧   名前空間メンバ   構成メンバ   ファイルメンバ  

dKingyoAnalogGC.h

解説を見る。
00001 
00002 
00003 #include "AllLoad.h"
00004 
00005 namespace dkutil{//begin dkutil
00006 namespace memory{//begin memory
00007 
00008 //**********************************************************
00009 //d金魚流??ガベコレもどき
00010 //**********************************************************
00011 
00013 
00015 #   ifdef USE_DKINGYO_DEBUG_VIEW
00016 
00018 class INL_dKingyoAllocatorGC{
00019     
00020     //typedef std::deque<DKINGYOALLOCATOR_GC_DEBUG>  GC_QUEUE;
00021     typedef std::deque<void *>  GC_QUEUE;
00022     //typedef DKUTIL_HASHSET<DKINGYOALLOCATOR_GC_DEBUG> GC_SET;
00023     typedef std::set<DKINGYOALLOCATOR_GC_DEBUG> GC_SET;
00024     typedef std::pair<GC_SET::iterator,bool> GC_SET_RESULT;
00025     GC_QUEUE sm_fq;//Freeに使うキュー
00026     GC_SET sm_taset;//使い捨てメモリ用セット
00027     GC_SET sm_set;//セット
00028     enum{
00029         enuSTLsetNotFound = 1,//set内で見つからない。
00030         enuSTLdequeNotFound,//deque内で見つからない。
00031         enuSTLempty,//STLコンテナ内に何も格納されてない。
00032     };
00033     inline int SetQueue(void *getpower,size_t size,bool ThrowingAwayFlag,char *str){
00034         if(ThrowingAwayFlag){
00035             sm_taset.insert(DKINGYOALLOCATOR_GC_DEBUG(size,getpower,str));
00036         }else{
00037             sm_set.insert(DKINGYOALLOCATOR_GC_DEBUG(size,getpower,str));
00038         }
00039         return 0;
00040     }
00042     inline int PopFreeQueue(void *get){
00043         GC_SET::iterator it = sm_taset.begin();
00044         bool findflag=false;
00045         while( it != sm_taset.end() ){
00046             if( (*it).address == get ){//見つかった
00047                 findflag=true;
00048                 break;
00049             }
00050             it++;
00051         }
00052         if(findflag){
00053             sm_taset.erase(it);//見つかった
00054         }else{
00055             //見つからなかったらエラー値を返す
00056             return enuSTLsetNotFound;
00057         }
00058         return 0;
00059     }
00061     int PopQueue(void *get){
00062         if(!sm_set.empty())
00063         {
00064             GC_SET::iterator it = sm_set.begin();
00065             bool findflag=false;
00066             while( it != sm_set.end() ){
00067                 if( (*it).address == get ){//見つかった
00068                     findflag=true;
00069                     break;
00070                 }
00071                 it++;
00072             }
00073             if(findflag){//見つかった!
00074                 sm_set.erase(it);
00075             }else{//見つからない!!
00076                 return enuSTLsetNotFound;
00077             }
00078         }else{
00079             return enuSTLempty;
00080         }
00081         return 0;
00082     }
00083 public:
00084     INL_dKingyoAllocatorGC(){}
00085     ~INL_dKingyoAllocatorGC(){
00086         AllClear();
00087     }
00088     
00089     inline void *Malloc(size_t size,bool ThrowingAwayFlag,char *str){
00090         void *getpower = ::malloc(size);
00091         if(!getpower) return NULL;
00092         SetQueue(getpower,size,ThrowingAwayFlag,str);
00093         return getpower;
00094     }
00095     inline void *Realloc(void *get,size_t size,bool ThrowingAwayFlag,char *str){
00096         void *getpower = ::realloc(get,size);
00097         if(!getpower) return NULL;
00098         SetQueue(getpower,size,ThrowingAwayFlag,str);
00099         return getpower;
00100     }
00101     inline int Free(void *get,bool ImmediatelyFreeFlag=false){
00102         if(ImmediatelyFreeFlag){//すぐに開放するならば
00103             if(0 != PopQueue(get)){
00104                 return -1;//不正なアドレスを代入したらしい
00105             }
00106             ::free(get);
00107             return 0;
00108         }else{//キューに入れる。
00109 
00110             sm_fq.push_back(get);
00111         }
00112         return -1;
00113     }
00114 
00115     inline int FreeQueueClear(){
00116         GC_QUEUE::iterator it = sm_fq.begin();
00117         int invalidNum=0;//無効だった数
00118         if(!sm_fq.empty()){//空じゃない場合
00119             while( it != sm_fq.end() ){
00120                 if( 0 != PopFreeQueue((*it))){
00121                     invalidNum++;
00122                 }else{//不正なアドレスではなかったら
00123                     ::free((*it));
00124                 }
00125                 it++;
00126             }
00127         }else{
00128             return enuSTLempty;
00129         }
00130         sm_fq.clear();//キュー内をクリアする。
00131 #       ifdef _DEBUG
00132         if(invalidNum){
00133             dOutputDebugString(" FreeQueueClear() 不正開放数 = %d\n",invalidNum);
00134         }
00135 #       endif
00136         return 0;
00137     
00138     }
00139     inline int AllClear(){
00140         int invalid=0;
00141 
00142         FreeQueueClear();
00143         
00144         if(!sm_set.empty())
00145         {
00146             GC_SET::iterator it = sm_set.begin();
00147             while( it != sm_set.end() ){
00148                 ::free((*it).address);
00149                 it++;
00150             }
00151             sm_set.clear();
00152         }
00153         if(!sm_taset.empty())
00154         {
00155             GC_SET::iterator it = sm_taset.begin();
00156             while( it != sm_taset.end() ){
00157                 ::free((*it).address);
00158                 it++;
00159             }
00160             sm_taset.clear();
00161         }
00162         return 0;
00163     }
00164 };
00165 
00166 #else 
00167 
00168 class INL_dKingyoAllocatorGC{
00169 protected:
00170 
00171     typedef std::deque<void * > GC_QUEUE;
00172     //typedef DKUTIL_HASHSET<void * > GC_SET;
00173     typedef std::set<void * > GC_SET;
00174     typedef std::pair<GC_SET::iterator,bool> GC_SET_RESULT;
00175 
00176     GC_QUEUE sm_fq;//Freeに使うキュー
00177     GC_SET sm_taset;//使い捨てメモリ用セット
00178     GC_SET sm_set;//セット
00179     enum{
00180         enuSTLsetNotFound = 1,//set内で見つからない。
00181         enuSTLdequeNotFound,//deque内で見つからない。
00182         enuSTLempty,//STLコンテナ内に何も格納されてない。
00183     };
00185     inline int SetQueue(void *getpower,size_t //size
00186                                                                         ,bool ThrowingAwayFlag
00187                                                                         ,char * //str
00188                                                                         ){
00189         if(ThrowingAwayFlag){//使い捨て指定だったら使い捨て用キューに入れる。
00190             sm_taset.insert(getpower);
00191         }else{
00194             sm_set.insert(getpower);
00195         }
00196         return 0;
00197     }
00199     inline int PopFreeQueue(void *get){
00200         //フリー用キュー内を検索して見つかったら削除する
00201         GC_SET::iterator it=sm_taset.begin();
00202         it = sm_taset.find(get);
00203         if(it != sm_taset.begin() ){
00204             sm_taset.erase(it);
00205         }else{
00206             return enuSTLsetNotFound;
00207         }
00208         return 0;
00209     }
00211     int PopQueue(void *get){
00212         //すべてのメモリアドレスを管理しているsetを検索して見つかったら削除
00213         //見つからなかったらエラー値を返す。
00214         if(!sm_set.empty())
00215         {
00216             GC_SET::iterator it=sm_set.begin();
00217             it = sm_set.find(get);
00218             if(it != sm_set.begin() ){
00219                 sm_set.erase(it);
00220             }else{
00221                 //見つからなかったら使い捨て専用領域から探す。
00222                 //PopFreeQueue(get);
00223                 //いや、見つからない。
00224                 return enuSTLsetNotFound;
00225             }
00226         }else{
00227             return enuSTLempty;
00228         }
00229 
00230         return 0;
00231     }
00232 public:
00233     INL_dKingyoAllocatorGC(){}
00234     ~INL_dKingyoAllocatorGC(){
00235         AllClear();
00236     }
00237     
00238     inline void *Malloc(size_t size,bool ThrowingAwayFlag,char *str){
00239         void *getpower = ::malloc(size);
00240         if(!getpower) return NULL;
00241         SetQueue(getpower,size,ThrowingAwayFlag,str);
00242         return getpower;
00243     }
00244     inline void *Realloc(void *get,size_t size,bool ThrowingAwayFlag,char *str){
00245         void *getpower = ::realloc(get,size);
00246         if(!getpower) return NULL;
00247         SetQueue(getpower,size,ThrowingAwayFlag,str);
00248         return getpower;
00249     }
00250 
00251     inline int Free(void *get,bool ImmediatelyFreeFlag=false){
00252         if(ImmediatelyFreeFlag){//すぐに開放するならば
00253             if(0 != PopQueue(get)){
00254                 return -1;//不正なアドレスを代入したらしい
00255             }
00256             ::free(get);
00257             return 0;
00258         }else{//キューに入れる。
00259             sm_fq.push_back(get);
00260         }
00261         return -1;
00262     }
00263     inline int FreeQueueClear(){
00264         GC_QUEUE::iterator it = sm_fq.begin();
00265         int invalidNum=0;//無効だった数
00266         if(!sm_fq.empty()){//空じゃない場合
00267             while( it != sm_fq.end() ){
00268                 if( 0 != PopFreeQueue((*it))){
00269                     invalidNum++;
00270                 }else{//不正なアドレスではなかったら
00271                     ::free((*it));
00272                 }
00273                 it++;
00274             }
00275         }else{
00276             return enuSTLempty;
00277         }
00278         sm_fq.clear();//キュー内をクリアする。
00279 #       ifdef _DEBUG
00280         if(invalidNum){
00281             dOutputDebugString(" FreeQueueClear() 不正開放数 = %d\n",invalidNum);
00282         }
00283 #       endif
00284         return 0;
00285     
00286     }
00287     inline int AllClear(){
00288         int invalid=0;
00289 
00290         FreeQueueClear();
00291         
00292         if(!sm_set.empty())
00293         {
00294             GC_SET::iterator it = sm_set.begin();
00295             while( it != sm_set.end() ){
00296                 ::free((*it));
00297                 it++;
00298             }
00299             sm_set.clear();
00300         }
00301         if(!sm_taset.empty())
00302         {
00303             GC_SET::iterator it = sm_taset.begin();
00304             while( it != sm_taset.end() ){
00305                 void *itit = (*it);
00306                 ::free((*it));
00307                 it++;
00308             }
00309             sm_taset.clear();
00310         }
00311         return 0;
00312     }
00313 };
00314 #endif // end of USE_DKINGYO_DEBUG_VIEW
00315 
00317 inline INL_dKingyoAllocatorGC &GetdKingyoGCOriginalRefelence(){
00318     static INL_dKingyoAllocatorGC dgc;
00319     return dgc;
00320 }
00321 
00322 #ifdef _DEBUG
00323 inline void CheckNullAddress(void *p){
00324     if(p==NULL) MB("NULL Plase break;");
00325 }
00326 #else
00327 inline void CheckNullAddress(void *p){}
00328 #endif
00329 
00330 
00331 // /*
00333 template<class T>
00334 class dKingyoAllocatorGC : public IdKingyoAllocatorGC<T>
00335 {
00336   typedef size_t    size_type;
00337   typedef ptrdiff_t difference_type;
00338   typedef T*        pointer;
00339   typedef const T*  const_pointer;
00340   typedef T&        reference;
00341   typedef const T&  const_reference;
00342   typedef T         value_type;
00343 protected:
00344     bool m_CollectFlag;
00345     bool m_ImmediatelyFreeFlag;
00346 
00347 #   ifdef _DEBUG
00348     DWORD m_alloc_count;
00349     char m_tempstr[256];
00350     enum{
00351         enuTempStrNum=256,
00352     };
00353     inline pointer alllocate_proto(size_type n){
00354         _snprintf(m_tempstr,enuTempStrNum," dKingyoAllocatorGC::allocate No=%d,size=%d",
00355             m_alloc_count++,n);// ++してます
00356         return DKINGYO_GC_NEW_A(m_CollectFlag,m_tempstr) value_type[n];
00357     }
00358     inline void deallocate_proto(pointer p){
00359         CheckNullAddress(p);
00360     DKINGYO_GC_DELETE_A(p,m_ImmediatelyFreeFlag);
00361     }
00362 #   else
00363     inline pointer alllocate_proto(size_type n){
00364         return DKINGYO_GC_NEW_A(m_CollectFlag,"release") value_type[n];
00365     }
00366     inline void deallocate_proto(pointer p){
00367     DKINGYO_GC_DELETE_A(p,m_ImmediatelyFreeFlag);
00368     }
00369 # endif
00370 
00371 public:
00372     inline void SetCollectFlag(bool f){ m_CollectFlag=f;}
00373     inline void SetImmediatelyFreeFlag(bool f){ m_ImmediatelyFreeFlag = f;}
00374     dKingyoAllocatorGC(){
00375         SetCollectFlag(false);
00376         SetImmediatelyFreeFlag(false);
00377     }
00378     virtual ~dKingyoAllocatorGC(){}
00379 
00380     virtual void *MallocGC(size_t,bool,char *) const;
00381     virtual int FreeGC(void *,bool) const;
00382     virtual void *ReallocGC(void *,size_t,bool,char *) const;
00383     virtual int Collection() const;//ガベコレもどきをする。
00384   
00385     pointer       address(reference x) const
00386     { return &x; }
00387   const_pointer address(const_reference x) const
00388     { return &x; }
00389   pointer       allocate(size_type n, const void* hint = 0) {
00390     return alllocate_proto(n);
00391     }
00392   void      deallocate(pointer p, size_type n = 0) {
00393         deallocate_proto(p);
00394   }
00395   size_type max_size() const{
00396         return (size_t)-1 / sizeof(T);
00397     }
00398   void construct(pointer p, const T& val) {
00399     new ((void*)p) T(val);
00400   }
00401   void destroy(pointer p) {
00402         ((T*)p)->~T();
00403   }
00404   char* _Charalloc(size_type n) {
00405     return allocate(n);
00406   }
00407 };
00408 
00409 // */
00410 
00411 
00412 }//end of memory
00413 }//end of dkutil
00414 
00415 
00416 //**********************************************************
00417 //偽装ガーベジコレクタ(ガベコレもどき)をnew演算子に対応させる
00418 //**********************************************************
00420 struct tugOnlyfordKingyoGCNew{
00421 };
00423 //マクロを用意しているのでこれは見なかったことにしてください。
00424 extern tugOnlyfordKingyoGCNew dKingyoGCNewArg;
00425 
00426 inline void *operator new(size_t size,bool CollectionFlag,
00427                                                     char *String,char *File,int line,tugOnlyfordKingyoGCNew //get
00428                                                     ){
00429     //d金魚用ガベコレもどき^^;
00430     return dkutil::memory::GetdKingyoGCOriginalRefelence().Malloc(size,CollectionFlag,String);
00431 }
00432 inline void *operator new[](size_t size,bool CollectionFlag,
00433                                                         char *String,char *File,int line,tugOnlyfordKingyoGCNew //get
00434                                                         ){
00435     return dkutil::memory::GetdKingyoGCOriginalRefelence().Malloc(size,CollectionFlag,String);
00436 }
00437 
00438 inline void operator delete[](void* obj,bool ImmediatelyFreeFlag,
00439                                                             char *File,int line,tugOnlyfordKingyoGCNew //get
00440                                                             )
00441 {
00442     dkutil::memory::GetdKingyoGCOriginalRefelence().Free(obj,ImmediatelyFreeFlag);
00443 
00444 }
00445 inline void operator delete(void* obj,bool ImmediatelyFreeFlag,
00446                                                         char *File,int line,tugOnlyfordKingyoGCNew //get
00447                                                         )
00448 {
00449     dkutil::memory::GetdKingyoGCOriginalRefelence().Free(obj,ImmediatelyFreeFlag);
00450 }
00451 
00453 #define DKINGYO_GC_NEW(flag,str) new(flag,str,__FILE__,__LINE__,dKingyoGCNewArg)
00454 
00455 #define DKINGYO_GC_DELETE(p,flag) ::operator delete(p,flag,__FILE__,__LINE__,dKingyoGCNewArg) 
00456 
00458 #define DKINGYO_GC_NEW_A(flag,str) new(flag,str,__FILE__,__LINE__,dKingyoGCNewArg)
00459 
00460 #define DKINGYO_GC_DELETE_A(p,flag) ::operator delete[](p,flag,__FILE__,__LINE__,dKingyoGCNewArg) 
00461 

dKingyoUtilClass (dkutil)に対してMon Jun 9 01:32:40 2003に生成されました。 doxygen1.3