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

dKingyoArrayManager.h

解説を見る。
00001 
00002 
00003 #ifndef DKINGYO_ARRAY_MANAGER__
00004 #define DKINGYO_ARRAY_MANAGER__
00005 
00006 
00007 #include "AllLoad.h"
00008 
00009 
00010 
00011 namespace dkutil{//begin dkutil
00012 namespace memory{//begin memory
00013 
00014 
00015 
00017 template<class T>
00018 inline int ReallocateByNew(T **before,DWORD oldcount,T **after,DWORD extend,DWORD *aftercount)
00019 {
00020     DWORD total = oldcount + extend;//合計の新規確保分
00021     T *get = new T[total];//maxcountはここで更新
00022     if(get==NULL){
00023         return -1;//だめじゃん!!最悪〜〜
00024     }
00025     //メモリコピー
00026     for(DWORD i=0;i<oldcount;i++){
00027         get[i]=(*before)[i];
00028     }
00029     //memcpyはC++のオブジェクトも出来るかどうか分からないので^^;
00030 
00031     delete[] (*before);//元のは開放
00032     (*after) = get;//アドレスを入れる。
00033     (*aftercount) = total;//合計の確保した数を入れる
00034     return 0;
00035 }
00036 
00042 
00043 template <class T,DWORD EXNTEND_ARRAY=100>//EXTEND_ARRAYはメモリを再確保する時の数
00044 class dKingyoArrayOneByOneDynamic :public IdKingyoArrayOneByOne<T,DWORD *>{
00045 protected:
00046     T *m;// M emory
00047     DWORD maxcount;//配列の最大数
00048     DWORD nowmp;//now  m point
00049     bool mflag;//is OK?
00050     //T **mp[100];//Memory Point;(空いている領域のポインタを入れておくところ)
00051     std::deque<DWORD > mp;//キューにする!?
00052     //DWORD nowmpp;//now mp point;
00053     enum{//,DWORD EXTEND_ARRAY=100
00054         enuExtendArray=EXNTEND_ARRAY,
00055     };
00056 public:
00057 
00058     dKingyoArrayOneByOneDynamic(){
00059         maxcount=NULL;
00060         nowmp=NULL;
00061         m=NULL;
00062         mflag=false;
00063     }
00067 
00068     dKingyoArrayOneByOneDynamic(dKingyoArrayOneByOneDynamic<T> &a){
00069     mflag=false;
00070         maxcount = a.maxcount;
00071       nowmp = a.nowmp;
00072       getflag = a.getflag;
00073       mp = a.mp;
00074       if(a.maxcount > 0){
00075         m = new T[a.maxcount];
00076                 if(!m){//例外発生しました!!
00077                     char *estr="dKingyoArrayOneByOneDynamicコピーコンストラクタ内:アホ〜〜〜 メモリ確保くらいしてよね!";
00078                     DK_THROW(estr) -1;
00079                     return;
00080                 }
00081         for(int i = 0; i < a.maxcount; i++){//メモリコピー
00082                      *(m + i) = *(a.m + i);
00083         }
00084                 //memcpy(m, a.m, a.maxcount);これだとC++のオブジェクトも出来るかどうかは不明
00085       }
00086       else{
00087         m = NULL;
00088       }
00089             mflag=true;//初期化済みやねん!
00090     }//まぁ、こんなものでよいかのぉ?
00091 
00092     virtual ~dKingyoArrayOneByOneDynamic(){
00093         End();
00094     }
00096     inline T *Data(){return m;};//恐ろしい関数だなぁ。直接アクセスだよ^^;
00101 
00102     inline DWORD DataMax(){return maxcount;}//配列の最大数
00104     virtual bool isOK(){return mflag;}//コンストラクタが成功したか?とか?
00106     virtual int End(){
00107         if(m){
00108             delete[] m;
00109             m=NULL;
00110         }
00111         
00112         maxcount=NULL;
00113         nowmp=NULL;
00114         m=NULL;
00115         mp.clear();//キューもすべて削除
00116         mflag=false;//フラグを下ろす
00117         return 0;
00118     }
00120     virtual int Compaction(){return DKINGYO_NOT_MOUNTED;//使われませんよ〜〜^^;
00121     }
00127 
00128     virtual int Init(size_t memory_count,int flag=0,T *p=NULL)
00129     //virtual int Init(size_t memory_count)
00130     {
00131         if(mflag==true)return 1;//もう一回呼び出しはダメです。
00132         maxcount=memory_count;
00133         //Dyanamic memoryだったらnewしかないでしょ^^;
00134         m = new T[memory_count];
00135         if(!m) return -1;//だめじゃんNULLじゃ
00136 
00137         mflag=true;//フラグを立てる
00138         
00139         return 0;
00140     }
00142     virtual int Reallocate(){
00143         if(mflag==false){
00144             if(-1==Init(100))//百個くらい^^;
00145                 MessageBox(NULL,"ちゃんとこのクラスを使う時Init()してますか?","!最悪のパターンが発生!\n",MB_OK);
00146         }
00147         return ReallocateByNew<T>(&m,maxcount,&m,enuExtendArray,&maxcount);
00148         /*
00149         DWORD old=maxcount;
00150         T *get = new T[(maxcount=(DWORD)maxcount*2)];//maxcountはここで更新
00151         if(get==NULL){
00152             return -1;//だめじゃん!!最悪〜〜
00153         }
00154         if(old>=maxcount){
00155             MB("WHAT!!");
00156         }
00157         //初期化。
00158         memset((void *)get,0,sizeof(T)*maxcount);
00159         //メモリコピー
00160         for(DWORD i=0;i<old;i++){
00161             get[i]=m[i];
00162         }
00163         delete[] m;//元のは開放
00164         m = get;//アドレスを入れる。
00165         return 0;*/
00166     }
00171 
00172     T *GetArray(DWORD *ReleaseID){
00173         if(nowmp>=maxcount)
00174         {//ちょっとこのままじゃアクセスバイオレーションだ〜よ!
00175             if(mp.empty() )
00176             {//キューが空だったら? 
00177                 if(-1==Reallocate())//リアロックする。
00178                 {
00179                     return NULL;//最悪〜〜失敗。
00180                 }
00181                 else//上手くできたようなので
00182                 {
00183                     //PrintDebug("リアロックが成功!!再起呼び出し\n");
00184                     return GetArray(ReleaseID);//再起呼び出し
00185                 }
00186 
00187             }
00188             else//キューの中身が空じゃなかったら
00189             {
00190                 //DWORD l_num;//配列の添え字をゲットする
00191                 *ReleaseID=(mp.front());//ってことでGET!
00192                 mp.pop_front();//削除する
00193                 PrintDebug("キューからげっど! %d\n",*ReleaseID);
00194                 
00195                 return &m[(*ReleaseID)];
00196             }
00197 
00198         }else{
00199             //生の配列を返す
00200             *ReleaseID = nowmp;//しっかり入れとく
00201             PrintDebug("生配列ゲット %d \n",nowmp);
00202             return &m[nowmp++];
00203         }
00204         
00205         //実際、ありえないけど念のため書いておく^^;
00206         return NULL;
00207     }
00208     /*
00209     @param ReleaseID[in] GetArrayで得たハンドルを入れる
00210     @return 0は成功 0以外は失敗
00211     */
00212     virtual int ReleaseArray(DWORD *ReleaseID){
00213     #ifdef _DEBUG
00214         static int count=0;
00215         int itc=0;
00216         for(DWORD i=0;i<mp.size();i++){
00217             if(*ReleaseID==mp[i]){
00218                 MessageBox(NULL,"ReleaseArray 重複だ!!!","重大なERROR",MB_OK);
00219             }
00220             itc++;
00221         }
00222         count++;
00223         
00224     #endif
00225         mp.push_back(*ReleaseID);//キューに空いている配列番号を記録(配列の添え字)
00226         PrintDebug("ReleaseArray呼び出し。ID=%d\n",*ReleaseID);
00227         return 0;
00228     }
00230     void PrintDebug(char *str,...){
00231     #ifdef _DEBUG
00232         char s[1024];
00233         SET_VA_LIST(s,1024,str);
00234         //OutputDebugString(s);
00235         //dAddLog("dKingyoArrayOnebyOneDynamicClassDebug.log",s);
00236     #endif
00237     }
00239     void DebugPrint(){
00240         std::deque<DWORD >::iterator it=mp.begin();
00241 
00242         PrintDebug("//__________________________\ndKingyoArraySupporter debug");
00243         char my[1024];
00244         //****日時を取得
00245         SYSTEMTIME lpSystemTime={0};
00246         GetSystemTime(
00247           &lpSystemTime   // address of system time structure
00248         );
00249         _snprintf(my,1024,
00250           "//BEGIN_dKingyoArraySupporter Class Test/year=%d month=%d day=%d hour=%d minute=%d second=%d\n",
00251           lpSystemTime.wYear,
00252           lpSystemTime.wMonth,
00253           lpSystemTime.wDay,
00254           lpSystemTime.wHour,
00255           lpSystemTime.wMinute,
00256           lpSystemTime.wSecond
00257           );
00258 
00259         PrintDebug(my);
00260         PrintDebug("maxcount=%d,nowmp=%d",maxcount,nowmp);
00261         for(UINT i=0;i<maxcount;i++){
00262             PrintDebug("Vector Memory NO=%d,Value=%d",i,m[i]);
00263         }
00264         for(;it!=mp.end();it++){
00265             PrintDebug("Free Memory ArrayNum=%d\n",(*it));
00266         }
00267         PrintDebug("//____________END__________DEBUG \n");
00268     }
00269 };
00270 
00271 
00272 template <class T>
00273 class dKingyoArrayOneByOneStatic : public IdKingyoArrayOneByOne<T,DWORD *>{
00274 private:
00275     //コピーコンストラクタ//コピーは出来ない
00276     dKingyoArrayOneByOneStatic(dKingyoArrayOneByOneStatic<T> &a){}
00277 protected:
00278     T *m;// M emory
00279     DWORD maxcount;//配列の最大数
00280     DWORD nowmp;//now  m point
00281     bool mflag;//is OK?
00282     //T **mp[100];//Memory Point;(空いている領域のポインタを入れておくところ)
00283     std::deque<DWORD > mp;//キューにする!?
00284     //DWORD nowmpp;//now mp point;
00285 public:
00286 
00287     dKingyoArrayOneByOneStatic(){
00288         maxcount=NULL;
00289         nowmp=NULL;
00290         m=NULL;
00291         mflag=false;
00292     }
00293     
00294 
00295     virtual ~dKingyoArrayOneByOneStatic(){
00296         End();
00297     }
00298     inline T *Data(){return m;};//恐ろしい関数だなぁ。直接アクセスだよ^^;
00299     inline DWORD DataMax(){return maxcount;}//配列の最大数
00300     virtual bool isOK(){return mflag;}
00301     virtual int End(){
00302         maxcount=NULL;
00303         nowmp=NULL;
00304         m=NULL;
00305         mp.clear();//キューもすべて削除
00306         mflag=false;//フラグを下ろす
00307         return 0;
00308     }
00309     virtual int Compaction(){return -100;//使われませんよ〜〜^^;
00310     }
00311     virtual int Init(size_t memory_count,int flag=0,T *p=NULL)
00312     {
00313         if(mflag==true)return 1;//もう一回呼び出しはダメです。
00314         maxcount=memory_count;
00315         m = p;//STATIC メモリだからポインタよこして〜な!
00316         if(!m) return -1;//だめじゃんNULLじゃ
00317 
00318         mflag=true;//フラグを立てる
00319         
00320         return 0;
00321     }
00322     virtual int Reallocate(){
00323         return -100;//使われないものを呼ばないでね!!
00324     }
00325     T *GetArray(DWORD *ReleaseID){
00326         if(nowmp>=maxcount)
00327         {//ちょっとこのままじゃアクセスバイオレーションだ〜よ!
00328             if(mp.empty() )
00329             {//キューが空らしい
00330                     return NULL;//空だから最悪〜〜失敗
00331             }
00332             else//キューの中身が空じゃなかったら
00333             {
00334                 //DWORD l_num;//配列の添え字をゲットする
00335                 *ReleaseID=(mp.front());//ってことでGET!
00336                 mp.pop_front();//削除する
00337                 PrintDebug("キューからげっど! %d\n",*ReleaseID);
00338                 
00339                 return &m[(*ReleaseID)];
00340             }
00341 
00342         }else{
00343             //生の配列を返す
00344             *ReleaseID = nowmp;//しっかり入れとく
00345             PrintDebug("生配列ゲット %d \n",nowmp);
00346             return &m[nowmp++];
00347         }
00348         
00349         //実際、ありえないけど念のため書いておく^^;
00350         return NULL;
00351     }
00352     virtual int ReleaseArray(DWORD *ReleaseID){
00353     #ifdef _DEBUG
00354         static int count=0;
00355         int itc=0;
00356         for(DWORD i=0;i<mp.size();i++){
00357             if(*ReleaseID==mp[i]){
00358                 ;
00359                 ;
00360                 MessageBox(NULL,"ReleaseArray 重複だ!!!","重大なERROR",MB_OK);
00361             }
00362             itc++;
00363         }
00364         count++;
00365         
00366     #endif
00367         mp.push_back(*ReleaseID);//キューに空いている配列番号を記録(配列の添え字)
00368         PrintDebug("ReleaseArray呼び出し。ID=%d\n",*ReleaseID);
00369         return 0;
00370     }
00371     void PrintDebug(char *str,...){
00372     #ifdef _DEBUG
00373         char s[1024];
00374         SET_VA_LIST(s,1024,str);
00375         //OutputDebugString(s);
00376         //dAddLog("dKingyoArrayOnebyOneStaticClassDebug.log",s);
00377     #endif
00378     }
00379     void DebugPrint(){
00380         std::deque<DWORD >::iterator it=mp.begin();
00381 
00382         PrintDebug("//__________________________\ndKingyoArraySupporter debug");
00383         char my[1024];
00384         //****日時を取得
00385         SYSTEMTIME lpSystemTime={0};
00386         GetSystemTime(
00387           &lpSystemTime   // address of system time structure
00388         );
00389         _snprintf(my,1024,
00390           "//BEGIN_dKingyoArraySupporter Class Test/year=%d month=%d day=%d hour=%d minute=%d second=%d\n",
00391           lpSystemTime.wYear,
00392           lpSystemTime.wMonth,
00393           lpSystemTime.wDay,
00394           lpSystemTime.wHour,
00395           lpSystemTime.wMinute,
00396           lpSystemTime.wSecond
00397           );
00398 
00399         PrintDebug(my);
00400         PrintDebug("maxcount=%d,nowmp=%d",maxcount,nowmp);
00401         for(UINT i=0;i<maxcount;i++){
00402             PrintDebug("Vector Memory NO=%d,Value=%d",i,m[i]);
00403         }
00404         for(;it!=mp.end();it++){
00405             PrintDebug("Free Memory ArrayNum=%d\n",(*it));
00406         }
00407         PrintDebug("//____________END__________DEBUG \n");
00408     }
00409 };
00410 
00411 /*
00412 template<class T,DWORD EXNTEND_ARRAY>
00413 class dKingyoArray : public IdKingyoArray<T>{
00414 protected:
00415     enum{
00416         enuExtendArray=EXTEND_ARRAY,
00417     };
00418     T *m_a;//array of member
00419     DWORD maxcount;//配列の最大数
00420     DWORD nowmp;//now  m point
00421     bool mflag;//is OK?
00422     std::deque<tugFreeDomainArray > mp;//すでに開放済み領域をキューにする
00423     std::deque<tugFreeDomainArray > m_re;//すでに確保済みの領域をキューにする
00424 public:
00425     inline T *Data(){return m_a;};//恐ろしい関数だなぁ。直接アクセスだよ^^;
00426     inline DWORD DataMax(){return maxcount;}//配列の最大数
00427     dKingyoArray(){
00428         m_a=NULL;maxcount=0;nowmp=0;mflag=false;
00429         mp.clear();
00430     };
00431     
00432     virtual ~dKingyoArray(){
00433         AllFree();
00434     }
00435     virtual int ReallocateMemory(){
00436         return ReallocateByNew(m_a,maxcount,m_a,enuExtendArray,&maxcount);
00437     }
00438     virtual int Init(size_t count,int,void *){
00439         if(!count) return -1;//サイズが0ならエラーで返す
00440         if(mflag) return 1;//初期化済みなら返す
00441 
00442         m = new T[count];
00443         if(!m) return -1;//メモリ確保できない
00444 
00445         maxcount = count;//マックスカウントこれ以上アクセスするな!
00446 
00447         return 0;
00448     }
00449     virtual T *GetArray(size_t num,FREEDOMAINARRAY *ReleaseID){
00450         if(!mflag){//初期化できてないやん!
00451             if(0 != Init(enuExtendArray,NULL,NULL))
00452                 return NULL;
00453         }
00454 
00455         if(nowmp>=maxcount)
00456         {//ちょっとこのままじゃアクセスバイオレーションだ〜よ!
00457             if(mp.empty() )
00458             {//キューが空らしい
00459                 if(0 != ReallocateMemory()){//リアロックする
00460                     return NULL;//最悪〜〜
00461                 }else{//上手く出来たようなので再起呼び出し
00462                     return GetArray(num,ReleaseID);
00463                 }
00464             }
00465             else//キューの中身が空じゃなかったら
00466             {
00467                 //DWORD l_num;//配列の添え字をゲットする
00468                 FREEDOMAINARRAY atemp;
00469                 
00470                 atemp=(mp.front());//ってことでGET!
00471                 mp.pop_front();//削除する
00472                 PrintDebug("キューからげっど!No : %d\n",atemp.point);
00473                 *ReleaseID = atemp.point;
00474                 return &m[atemp.point];
00475             }
00476 
00477         }else{
00478             //生の配列を返す
00479             *ReleaseID = nowmp;//しっかり入れとく
00480             PrintDebug("生配列ゲット %d \n",nowmp);
00481             return &m[nowmp++];
00482         }
00483         
00484         //実際、ありえないけど念のため書いておく^^;
00485         return NULL;
00486     }
00487 
00488     virtual int ReleaseArray(DWORD *ReleaseID,T *Original){
00489         if(!mflag) return -1;//初期化くらいしろよ!
00490 #   ifdef _DEBUG
00491         for(DWORD i=0;i<mp.size();i++){
00492             if(*ReleaseID== mp[i].point){
00493                 MessageBox(NULL,"ReleaseArray 重複だ!!!","重大なERROR",MB_OK);
00494             }
00495         }
00496 #endif
00497         mp.push_back(*ReleaseID);//キューに空いている配列番号を記録(配列の添え字)
00498         PrintDebug("ReleaseArray呼び出し。ID=%d\n",*ReleaseID);
00499     virtual int Compaction() = 0;
00500     virtual int AllFree() = 0;
00501     virtual void PRINT_DEBUG(char *str,...) = 0;
00502     virtual void DEBUG_PRINT() = 0;
00503     virtual bool isOK(){return mflag;}
00504 };
00505 
00506 
00507 
00508 
00509 
00510 //**********************************************************
00513 //**********************************************************
00514 
00515 template<class T,DWORD = EXTEND_ARRAY = 100>
00516 class dKingyoObjectPool{
00517 public:
00518     //enum{
00519     //  enuExtendArray=EXTEND_ARRAY,
00520     //};
00521     inline void *operator new(size_t size){
00522     }
00523     inline void *operator new[](size_t size){
00524     }
00525     inline void operator delete[](void* obj){
00526     }
00527     inline void operator delete(void* obj){
00528     }
00529 */
00530 
00531 
00532 
00533 
00534 
00535 
00536 
00537 
00538 
00539 #ifdef USE_DKINGYO_OBJECT_TEST
00540 
00541 extern void MemoryTest3__new_vs_dKingyoArrayOneByOneDynamic();
00542 extern void MemoryTest2__();
00543 extern void MemoryTest__();
00544 extern void MemoryTest4__malloc_vs_OneByOne();
00545 
00546 #endif //end of USE_DKINGYO_OBJECT_TEST
00547 
00548 }//end of memory
00549 }//end of dkutil
00550 
00551 #endif  // DKINGYO_ARRAY_MANAGER__  end of once include
00552 
00553 
00554 
00555 
00556 

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