• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            Cpper
            C/C++高級(jí)工程師 Android高級(jí)軟件工程師 IT集成工程師 音頻工程師 熟悉c,c++,java,c#,py,js,asp等多種語(yǔ)言 程序猿
            以前在codeproject上看到過(guò)一篇關(guān)于內(nèi)存池的文章(http://www.codeproject.com/KB/cpp/MemoryPool.aspx)
            下載下來(lái)試了試,感覺(jué)有點(diǎn)問(wèn)題
            想給引擎加入內(nèi)存池,考慮到當(dāng)前業(yè)余時(shí)間在看Loki
            就索性使用其SmallObject了
            對(duì)于內(nèi)存池當(dāng)然要求之一那就是速度
            其次對(duì)我來(lái)說(shuō)我比較關(guān)系以下的這類問(wèn)題
            一句話概括就是
            Base* ptr = new SubClass;
            索性我就根據(jù)Loki庫(kù)和Boost的Object_Pool
            設(shè)計(jì)了如下的引擎內(nèi)存池(當(dāng)然問(wèn)題還很多以后慢慢修改)
            #ifdef LOKI_EXT_LIB
            #include 
            <GEngine/Loki/Loki.hpp>
            #else
                
            #error 需要包含Loki庫(kù)
            #endif 

            namespace core
            {

            ////////////////////////////////////////////////////////////
            /// 定義蓋莫引擎2.1.2內(nèi)存池對(duì)象 
            ////////////////////////////////////////////////////////////    
            struct MemoryPool    
            {
            public:
                   
                
            ////////////////////////////////////////////////////////
                
            /// 獲取,釋放指定大小的內(nèi)存 
                
            ////////////////////////////////////////////////////////        
                template<class T>
                
            static T* Malloc(size_t size)
                {   
                    
            return (T*)MEMORY_POOL.Allocate(size,false);
                }   
                
                template
            <class T>
                
            static void  Free(T* ptr,size_t size)
                {
                    MEMORY_POOL.Deallocate(ptr,size);      
                }
                
                
            ////////////////////////////////////////////////////////
                
            /// 構(gòu)造無(wú)參數(shù)的對(duì)象類型并返回指針 
                
            ////////////////////////////////////////////////////////       
                template<class T>
                
            static T* Construct()
                {   
                    
            void* ptr = MEMORY_POOL.Allocate(OBJECT_SIZE(T),false);  
                    
            if(ptr == NULL)
                        
            return NULL; 
                    
            new(ptr)T();
                    
            return (T*)ptr;        
                }
                
                
            ////////////////////////////////////////////////////////
                
            /// 構(gòu)造帶有1個(gè)參數(shù)的對(duì)象類型并返回指針 
                
            ////////////////////////////////////////////////////////      
                template<class T,class P1>
                
            static T* Construct(const P1 &p1)
                {   
                    
            void* ptr = MEMORY_POOL.Allocate(OBJECT_SIZE(T),false);  
                    
            if(ptr == NULL)
                        
            return NULL; 
                    
            new(ptr)T(p1);
                    
            return (T*)ptr;        
                }
             
                
            ////////////////////////////////////////////////////////
                
            /// 構(gòu)造帶有2個(gè)參數(shù)的對(duì)象類型并返回指針 
                
            ////////////////////////////////////////////////////////      
                template<class T,class P1,class P2>
                
            static T* Construct(const P1 &p1,const P2 &p2)
                {   
                    
            void* ptr = MEMORY_POOL.Allocate(OBJECT_SIZE(T),false);  
                    
            if(ptr == NULL)
                        
            return NULL; 
                    
            new(ptr)T(p1,p2);
                    
            return (T*)ptr;        
                }
                
                
            ////////////////////////////////////////////////////////
                
            /// 構(gòu)造帶有3個(gè)參數(shù)的對(duì)象類型并返回指針 
                
            ////////////////////////////////////////////////////////    
                template<class T,class P1,class P2,class P3>
                
            static T* Construct(const P1 &p1,const P2 &p2,const P3 &p3)
                {   
                    
            void* ptr = MEMORY_POOL.Allocate(OBJECT_SIZE(T),false);  
                    
            if(ptr == NULL)
                        
            return NULL; 
                    
            new(ptr)T(p1,p2,p3);
                    
            return (T*)ptr;        
                }
                
                
            ////////////////////////////////////////////////////////
                
            /// 構(gòu)造帶有4個(gè)參數(shù)的對(duì)象類型并返回指針 
                
            ////////////////////////////////////////////////////////      
                template<class T,class P1,class P2,class P3,class P4>
                
            static T* Construct(const P1 &p1,const P2 &p2,const P3 &p3,const P4 &p4)
                {   
                    
            void* ptr = MEMORY_POOL.Allocate(OBJECT_SIZE(T),false);  
                    
            if(ptr == NULL)
                        
            return NULL; 
                    
            new(ptr)T(p1,p2,p3,p4);
                    
            return (T*)ptr;        
                }    
                
                
            ////////////////////////////////////////////////////////
                
            /// 構(gòu)造帶有5個(gè)參數(shù)的對(duì)象類型并返回指針 
                
            ////////////////////////////////////////////////////////      
                template<class T,class P1,class P2,class P3,class P4,class P5>
                
            static T* Construct(const P1 &p1,const P2 &p2,const P3 &p3,const P4 &p4,const P5 &p5)
                {   
                    
            void* ptr = MEMORY_POOL.Allocate(OBJECT_SIZE(T),false);  
                    
            if(ptr == NULL)
                        
            return NULL; 
                    
            new(ptr)T(p1,p2,p3,p4,p5);
                    
            return (T*)ptr;        
                }                
               
                
            ////////////////////////////////////////////////////////
                
            /// 給定對(duì)象的析構(gòu)(size為對(duì)象大小) 
                
            ////////////////////////////////////////////////////////      
                template<class T>
                
            static void Destruct(T* ptr, size_t size)
                {   
                    
            if(ptr == NULL || size <= 0)
                        
            return;
                    ptr
            ->~T();    
                    MEMORY_POOL.Deallocate(ptr,size);    
                }
                
                
            ////////////////////////////////////////////////////////
                
            /// 獲取可分配的最大對(duì)象大小 
                
            //////////////////////////////////////////////////////// 
                static int GetMaxObjSize()
                {    
                    
            return MEMORY_POOL.GetMaxObjectSize();
                }
                
                
            ////////////////////////////////////////////////////////
                
            /// 獲取字節(jié)對(duì)齊字節(jié)數(shù) 
                
            ////////////////////////////////////////////////////////  
                static int GetAlignment()
                {   
                    
            return MEMORY_POOL.GetAlignment();
                }    
            }; 
            靜態(tài)的Malloc和Free是分配和釋放原生態(tài)的內(nèi)存
            而Construct,Destruct則是構(gòu)造和析構(gòu)對(duì)象形式的內(nèi)存
            這里提供了6個(gè)版本的Construct函數(shù)
            分別對(duì)應(yīng)0-5個(gè)構(gòu)造函數(shù)參數(shù)

            記得以前遇到的一個(gè)問(wèn)題
            那就是假如有一個(gè)對(duì)象 她沒(méi)有默認(rèn)構(gòu)造函數(shù)(只有帶參數(shù)構(gòu)造函數(shù))
            如果現(xiàn)在需要分配N(xiāo)個(gè)她該如何操作?
            那就是placement new 了

            posted on 2010-04-20 15:56 ccsdu2009 閱讀(758) 評(píng)論(0)  編輯 收藏 引用 所屬分類: Game引擎
             
            日本精品久久久久影院日本| 久久综合亚洲色HEZYO国产| 99久久中文字幕| 精品久久久久久久无码| 久久亚洲精品无码AV红樱桃| 国色天香久久久久久久小说| 国内精品伊人久久久久网站| 久久AV无码精品人妻糸列| 精品精品国产自在久久高清| 亚洲精品久久久www| 91精品国产91久久久久久蜜臀| 中文国产成人精品久久亚洲精品AⅤ无码精品 | 99久久综合国产精品二区| 精品国产乱码久久久久软件| 精品视频久久久久| 久久精品中文无码资源站| 久久久久青草线蕉综合超碰| 一级做a爱片久久毛片| 亚洲狠狠婷婷综合久久久久| 人妻无码αv中文字幕久久琪琪布| 国产成人久久激情91| 久久天天躁狠狠躁夜夜不卡| 久久www免费人成看国产片| 99精品久久精品| 久久亚洲精品成人AV| 久久精品国产乱子伦| 国产欧美久久久精品影院| 亚洲美日韩Av中文字幕无码久久久妻妇| www.久久精品| av午夜福利一片免费看久久 | 久久婷婷五月综合97色| 久久亚洲熟女cc98cm| 亚洲精品乱码久久久久久不卡| 国产精品热久久无码av| 国产成人精品综合久久久| 激情五月综合综合久久69| 亚洲国产成人久久精品影视| 午夜精品久久影院蜜桃| 久久亚洲国产成人精品性色| 久久99精品久久久久久秒播| 精品国产乱码久久久久久浪潮|