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

dKingyoMemoryManager.h

解説を見る。
00001 
00002 #ifndef DKINGYO_MEMORY_MANAGER__
00003 #define DKINGYO_MEMORY_MANAGER__
00004 
00005 
00006 
00007 #include "AllLoad.h"
00008 //#include "dkutilTL.h"
00009 
00010 
00011 //BoehmGC使います。
00012 #   ifdef USE_DKINGYO_BOEHM_GC
00013 #       include <BOEHMGC_INCLUDE_PATH>
00014 #       pragma comment(lib,BOEHMGC_LIB_PATH)
00015 
00016 #       ifdef USE_DKINGYO_BEOHM_GC_LEAVE_FREE //GC_freeを使わない(BoehmGCに任せる)場合。
00017 #           define  DKINGYO_GC_FREE__(s)
00018 #       else
00019 #           define DKINGYO_GC_FREE__(s) GC_free(s)
00020 #       endif
00021 
00042 
00043 typedef struct tugOnlyforBoehmGCNew{
00044 }ONLYFOR_BOEHMGC_NEW;
00046 typedef struct tugOnlyforBoehmGCNewString{
00047 }ONLYFOR_BOEHMGC_NEW_STRING;
00049 typedef struct tugOnlyforBoehmGCNewUnCollect{
00050 }ONLYFOR_BOEHMGC_NEW_UNCOLLECT;
00051 
00053 inline void *operator new(size_t size,tugOnlyforBoehmGCNew //get
00054                                                     ){
00055     return GC_malloc(size);
00056 }
00058 inline void *operator new[](size_t size,tugOnlyforBoehmGCNew //get
00059                                                     ){
00060     return GC_malloc(size);
00061 }
00063 inline void *operator new(size_t size,tugOnlyforBoehmGCNewString //get
00064                                                     ){
00065     return GC_malloc_atomic(size);
00066 }
00068 inline void *operator new[](size_t size,tugOnlyforBoehmGCNewString //get
00069                                                     ){
00070     return GC_malloc_atomic(size);
00071 }
00073 inline void *operator new(size_t size,tugOnlyforBoehmGCNewUnCollect //get
00074                                                     ){
00075     return GC_malloc_uncollectable(size);
00076 }
00078 inline void *operator new[](size_t size,tugOnlyforBoehmGCNewUnCollect //get
00079                                                     ){
00080     return GC_malloc_uncollectable(size);
00081 }
00083 inline void operator delete[](void* obj,tugOnlyforBoehmGCNew //get
00084                                                             )
00085 {
00086     DKINGYO_GC_FREE__(obj);
00087 }
00089 inline void operator delete(void* obj,tugOnlyforBoehmGCNew //get
00090                                                             )
00091 {
00092     DKINGYO_GC_FREE__(obj);
00093 }
00094 
00095 namespace dkutil{//begin dkutil
00096 namespace memory{//begin memory
00097 
00098 class CBoehmGCWrapper{
00099 public:
00100     CBoehmGCWrapper(){}
00101     ~CBoehmGCWrapper(){}
00102     static ONLYFOR_BOEHMGC_NEW m_new;
00103     static ONLYFOR_BOEHMGC_NEW_STRING m_new_string;
00104     static ONLYFOR_BOEHMGC_NEW_UNCOLLECT m_new_uncollect;
00105     static void *operator new(size_t size);
00106     static void *operator new[](size_t size);
00107     static void operator delete[](void* obj);
00108     static void operator delete(void* obj);
00109 //かなりナンセンスゥな実装だなぁ^^;まぁ、プリプロセッサはぶりぶりプロセッサだから^^;
00110 //グローばルなのさ〜〜
00111 #   define DKINGYO_BOEHMGC_NEW new(CBoehmGCWrapper::m_new)
00112 #   define DKINGYO_BOEHMGC_NEW_STRING new(CBoehmGCWrapper::m_new_string)
00113 #   define DKINGYO_BOEHMGC_NEW_UNCOLLECT new(CBoehmGCWrapper::m_new_uncollect)
00114 #   define DKINGYO_BOEHMGC_DELETE(mem) ::operator delete(mem,CBoehmGCWrapper::m_new)
00115 
00116 #   define DKINGYO_BOEHMGC_NEW_A new(CBoehmGCWrapper::m_new)
00117 #   define DKINGYO_BOEHMGC_NEW_STRING_A new(CBoehmGCWrapper::m_new_string)
00118 #   define DKINGYO_BOEHMGC_NEW_UNCOLLECT_A new(CBoehmGCWrapper::m_new_uncollect)
00119 #   define DKINGYO_BOEHMGC_DELETE_A(mem) ::operator delete[](mem,CBoehmGCWrapper::m_new)
00120 
00121 };
00122 
00123 }//end of memory
00124 }//end of dkutil
00125 
00126 #   endif
00127 
00128 
00130 #define DKINGYO_NEW new
00131 
00132 #define DKINGYO_DELETE delete
00133 
00134 
00135 #   ifdef USE_DKINGYO_BOEHM_GC
00136 #       define DKINGYO_MALLOC(s) GC_malloc(s)
00137 #       define DKINGYO_FREE(s) GC_free(s)
00138 #       ifdef USE_DKINGYO_BEOHM_GC_LEAVE_FREE
00139 #           undef DKINGYO_FREE(s)
00140 #           define DKINGYO_FREE(s)//BoehmGCに開放処理はすべて任せます。
00141 #       endif
00142 #   else 
00143 #       ifdef USE_DKINGYO_MEMORYLEAK_CHECK
00144 #           define DKINGYO_MALLOC(s) malloc(s)
00145 #           define DKINGYO_FREE(s) free(s)
00146 #           undef DKINGYO_NEW
00147 #           define DKINGYO_NEW ::new(_NORMAL_BLOCK, __FILE__, __LINE__)
00148 #       else
00149 #           define DKINGYO_MALLOC(s) ::malloc(s)
00150 #           define DKINGYO_FREE(s) ::free(s)
00151 #       endif
00152 #   endif
00153 
00154 #ifdef USE_DKINGYO_NEW_OPERATOR
00155 
00156 inline void *operator new(size_t size)
00157 {
00158     return DKINGYO_MALLOC(size);
00159 }
00160 inline void *operator new[](size_t size)
00161 {   
00162     return DKINGYO_MALLOC(size);
00163 }
00164 inline void operator delete[](void* obj)
00165 {
00166     DKINGYO_FREE(obj);
00167 
00168 }
00169 inline void operator delete(void* obj)
00170 {
00171     DKINGYO_FREE(obj);
00172 }
00173 
00174 #endif // end of USE_DKINGYO_NEW_OPERATOR
00175 
00176 
00177 namespace dkutil{//begin dkutil
00178 namespace memory{//begin memory
00179 
00180 
00181 
00182 //**********************************************************
00183 //メモリ確保だけのラッパークラス等
00184 //**********************************************************
00185 #ifdef USE_DKINGYO_BOEHM_GC
00186 
00187 class dKingyoAllocateManagerBoehmGC :
00188     public IdKingyoAllocateManager ,//抽象クラス
00189     boost::noncopyable //コピー禁止クラス指定
00190 {
00191 public:
00192     void *Malloc(size_t s){
00193         return GC_malloc(s);
00194     }
00195     void Free(void *p){
00196         GC_free(p);
00197     }
00198     void *Realloc(void *p,size_t s){
00199         return GC_realloc(p,s);
00200     }
00201 };
00202 
00203 #endif 
00204 
00205 class dKingyoAllocateManagerDefault :
00206     public IdKingyoAllocateManager ,
00207     boost::noncopyable 
00208 {
00209 public:
00210     void *Malloc(size_t s){
00211         return ::malloc(s);
00212     }
00213     void Free(void *p){
00214         ::free(p);
00215     }
00216     void *Realloc(void *p,size_t s){
00217         return ::realloc(p,s);
00218     }
00219 };
00220 
00221 
00222 
00223 }//end of memory
00224 }//end of dkutil
00225 
00226 
00227 
00228 
00229 
00230 
00231 
00232 
00233 
00234 
00235 
00236 /*
00237 
00238 template<class T>
00239 class dKingyoVectorMemory : IdKingyoArray< DKINGYO_VECTORMEMORY<T> >{
00240 protected:
00241     T *m;//vector memory
00242     size_t allnum;//vecotr memory allnum;
00243     size_t now_num;//今の空いている配列ナンバー
00244     typedef std::list< tugdKingyoVectorMemory< T > > DKINGYO_VECTOR_LIST;
00245     std::list< tugdKingyoVectorMemory< T > > l;//list (use memory domain)
00246     std::list<tugdKingyoVectorMemory< T > >::iterator it;//list counter
00247     //int i;
00248     
00249 public:
00250     dKingyoVectorMemory(){
00251         m=NULL;allnum=now_num=0;
00252     }
00253     virtual ~dKingyoVectorMemory(){}
00254     virtual int ReallocateMemory(){
00255 //#error ここはエラー。newのやり方が間違っているぞ!!
00256         int old=allnum;
00257         T *get = new T[allnum=allnum*1.5f];
00258         if(get==NULL)
00259             return -1;//だめじゃん!!最悪〜〜
00260         //メモリコピー
00261         for(i=0;i<old;i++){
00262             get[i]=m[i];
00263         }
00264         delete[] m;//元のは開放
00265         return 0;
00266     }
00267     virtual int Init(size_t num){
00268         AllFree();
00269         allnum = num;
00270         m = new T[allnum];
00271         if(!m){
00272             return -1;//What are you DOING about!!!This PC is IDIOTICAL!!!
00273         }
00274         return 0;
00275     }
00276     
00277     virtual DKINGYO_VECTORMEMORY<T> *GetArray(size_t num=1){
00278         int i=0;
00279         if(num<=0)
00280             return NULL;//あほ〜〜0は駄目!!
00281         if(allnum <= now_num + num)//もし、配列が最後まで使っちまったら・・・
00282         {
00283             PRINT_DEBUG("dKingyoVectorMemory::GetArray(size_t num) :Compaction function begin\n");
00284             if(-1==Compaction())//コンパクション
00285             {
00286                 PRINT_DEBUG("Compaction failed");
00287                 //なんで駄目やねん!
00288                 if(-1==ReallocateMemory())//仕方ないから確保する
00289                 { 
00290                     PRINT_DEBUG("ReallocateVectorMemory failed.it's the worst!!");
00291                     return NULL; 
00292                 }//だめだぁ
00293             }
00294         }
00295         //使ってしまった。メモリ領域を記録
00296         l.push_back(DKINGYO_VECTORMEMORY<T>(&m[now_num],now_num,num,true));
00297         now_num += num;//今の未使用領域配列の添え字を変更
00298         
00299 
00300         return &l.back();
00301     }
00302     virtual int ReleaseArray(DKINGYO_VECTORMEMORY<T> *pb){
00303         it = l.begin();
00304         while (it!=l.end()){
00305             if ((*it).start_==pb->start_){
00306                 if (!(*it).bUsed_){
00307                     //英語分からないけど、使わないと覚えないから間違っていても使う・・・ごめんよぉ
00308                     PRINT_DEBUG("Don't call 'free' function ,at this time insert already free memory domain.\n");
00309                     return 1;//開放した奴は開放するな!!
00310                 }
00311                 (*it).bUsed_ = false;//削除フラグをつけてOK!?
00312                 return 0;//終わりかよ!これで(まぁ、あとはCompaction関数に任せる^^;
00313             }
00314             it++;
00315         }
00316         return -1;//そんなポインタは無いよ!!
00317     }
00318     inline int Free(DKINGYO_VECTORMEMORY<T> *pb){return ReleaseArray(pb);}
00319     inline DKINGYO_VECTORMEMORY<T> *Malloc(size_t num){return GetArray(num);}
00320     virtual int Compaction(){//再起構造にしたかったけど^^;なんか速度的に遅そうだから^^;
00321         it = l.begin();
00322         int r=-1;
00323         //DWORD prev_num;//前のポインタの配列数
00324         DKINGYO_VECTORMEMORY <T> prev(0,0,0,false);//前の奴
00325         DKINGYO_VECTOR_LIST::iterator getit;
00326         DEBUGINT(co,0);//カウンタ
00327         
00328         while (it!=l.end())
00329         {
00330             
00331             if(!(*it).bUsed_)//ファルスだったらリストから削除。Vector領域を動かします。
00332             {
00333             #ifdef _DEBUG
00334                 //配列の添え字の数が前のより多いはずが無い!
00335                 
00336                 DKINGYO_VECTORMEMORY<T> kore=(*it);
00337                 if(prev.array_num_ >= (*it).array_num_)
00338                 {
00339                 
00340                     DEBUGMB("Debuging!!! dKingyoMemoryVector");
00341                 }
00342             #endif
00343                 //コンパクションの例外処理
00344                 if((*it).start_ == m){
00345                     DEBUGMB("一番最初 だから特殊なコンパクションが必要");
00346                 }
00347                 if((*it).array_num_>=allnum){
00348                     DEBUGMB("コンパクションは必要ないかもね^^");
00349                 }
00350                 
00351                 //次のリストも空き領域が無いか検索。
00352                 {
00353                     DWORD check_count=1;
00354                     it++;//ひとつインクリメントしたからcheck_countも1と^^
00355                     //次のメモリもチェック
00356                     while(it!=l.end())
00357                     {
00358                         //ががーん!やっぱりFALSE!!めんどくせぇ!再起構造にしたい!
00359                         if(!(*it).bUsed_)
00360                         {
00361                             getit=it;//代入
00362                             getit--;//前の奴を指す
00363                             //前の方にあけわたします(無条件降伏じゃい!(謎))
00364                             (*(getit)).array_num_ += (*it).array_num_;
00365                             (*(getit)).num_ += (*it).num_;
00366                             l.erase(it);//さようなら〜
00367                         }
00368                         else//違うのだったらBreakBreak!!
00369                         {
00370                             break;
00371                         }
00372 
00373                         it++;
00374                         check_count++;
00375                     }
00376                     {for(int j=0;j<check_count;j++){
00377                         it --;//カウンタは戻します。
00378                     }}
00379 
00380                     
00381                 }
00382 
00383                 
00384                 //DWORD offset_num;//配列数
00385                 //DWORD offset;//コピーするサイズ数
00386                 //offset_num = (*(it + 1)).array_num_ - (*it).array_num_;
00387                 //offset = (*(it + 1)).num_;//確保している数はこれだけあるらしい。
00388                 getit=it;//いてれータ代入
00389                 getit++;
00390                 //後ろと同じは駄目でしょ^^;
00391                 if(getit!=l.end()){
00392                     #ifdef _DEBUG
00393                 
00394                         DKINGYO_VECTORMEMORY<T> kore_it=(*it);
00395                         DKINGYO_VECTORMEMORY<T> kore_getit=(*getit);
00396                 
00397                     #endif
00398                     
00399                     for(i=0;i<(*(getit)).num_;i++)//メモリコピー処理。
00400                     {
00401                         m[(*it).array_num_ + i] = m[(*(getit)).array_num_ + i];
00402                     }
00403                     
00404                     //保存データを更新。
00405                     // 次のデータ 前のデータ
00406                     (*getit).array_num_ =   (*it).array_num_;
00407                     (*getit).start_ = (*it).start_;
00408 
00409                     //用済みはいらないよん!!
00410                     l.erase(it);
00411                     it = getit;//itは無いのでこれを入れときます。
00412                     //while( これでOKなはず!?
00413                     r=0;//しっかり処理できてるよん!!
00414                 }
00415             }
00416             else//TRUEだったら
00417             {
00418                 
00419                 
00420             }
00421             
00422             DEBUGINC(co);
00423             prev = (*it);//内容をコピーします^^
00424             it++;//何故ここでやるかというとeraseで削除されているからです。
00425         }
00426         
00427         //実際どのくらいの領域が空いたのよ!?
00428         DKINGYO_VECTORMEMORY<T> mem=l.back();
00429     
00430         DWORD temp;//使える配列数
00431         temp = mem.array_num_ + mem.num_;//実際このくらいらしい
00432         if(temp >= allnum)
00433         {
00434             //DEBUGMB("もちろんだめだめです。エラーです。ありえません");
00435             PRINT_DEBUG("dKingyoVectorMemory:ReallocateMemory function is required.\n");
00436             return -1;
00437         }
00438         now_num = temp;//今つかえる領域はこのくらいです^^;
00439         
00440         
00441 
00442 
00443         return r;
00444     }
00445     virtual void AllFree(){
00446         l.clear();
00447         if(m){
00448             delete[] m;
00449         }
00450         m=NULL;
00451         allnum = now_num = 0;
00452     }
00453     virtual void PRINT_DEBUG(char *str,...){
00454         #ifdef _DEBUG
00455             char s[1024];
00456             SET_VA_LIST(s,1024,str);
00457             OutputDebugString(s);
00458         #endif
00459     }
00460     virtual void DEBUG_PRINT(){
00461         PRINT_DEBUG("//___________________________________\n");
00462         PRINT_DEBUG("dKingyoVectorMemory class DEBUG_PRINT\n");
00463         if(allnum <= now_num)
00464             PRINT_DEBUG("DEBUGPRINT :Vector Memory is Full ,Doesn't go into VectorMemory any longer.\n");
00465         PRINT_DEBUG("DEBUGPRINT :allnum=%d now_num=%d .\n",allnum,now_num);
00466         int c=0;
00467         for(it = l.begin();it!=l.end();it++){
00468             if(!(*it).bUsed_){
00469                 PRINT_DEBUG("NO Compaction Free domain = %d \n",(*it).array_num_);
00470             }
00471             c++;
00472         }
00473         PRINT_DEBUG("//____________END_____________________\n");
00474     }
00475         
00476 };
00477 */
00478 
00479 
00480 
00481 #endif

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