• <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>

            5D空間

            學(xué)習(xí)總結(jié)與經(jīng)驗(yàn)交流

               :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
              對(duì)象池的指針版本。池中本質(zhì)上是一群對(duì)象,只構(gòu)造一次,返回指針。主要支持頻繁重建的內(nèi)存空間不連續(xù)的自建結(jié)構(gòu),如樹(shù),鏈表。
            /******************************************************************
             *名稱(chēng):PointerPool(指針池)(模板)
             *版本號(hào):0.1
             *作者:趙耀(中山大學(xué)2010級(jí))
             *時(shí)間:2011.4.11
             *簡(jiǎn)介:
             *        對(duì)應(yīng)于對(duì)象池,這是一個(gè)指針池,用于緩存頻繁調(diào)用的指針(對(duì)象).主要支
             *    持頻繁重建的樹(shù)或鏈表等結(jié)構(gòu).具有默認(rèn)構(gòu)造函數(shù)接收一個(gè)size_t對(duì)象表明指針
             *    分塊的大小,默認(rèn)為10.接收的參數(shù)如果<=0則會(huì)拋出異常invalid_argument.
             *    public方法:
             *        T* getPointer(); 從池中返回一個(gè)該類(lèi)型對(duì)象的指針;
             *        void recyclePointer( T* ); 接受需要回收的指針;
             *        void clear(); 清空池以釋放占用的內(nèi)存;
             *        bool empty(); 返回池是否為空;
             *        size_t size(); 返回池中總指針數(shù)量;
             *        size_t remain(); 返回指針池剩余可用指針的數(shù)量;
             *    注意:
             *        使用方法可簡(jiǎn)單地想成通過(guò)兩個(gè)成員函數(shù)來(lái)代替new和delete.必須確保分
             *    配的指針得到回收!必須確保回收的指針不是外來(lái)指針!因?yàn)槲催M(jìn)行二次初始化,
             *    重新分配得到的指針?biāo)笇?duì)象可能是上一次使用后的殘余對(duì)象,請(qǐng)進(jìn)行必要的初
             *    話(huà)工作.
             *
             *未完成特性:
             *        因?yàn)橹饕С謽?shù),鏈表等內(nèi)存空間不連續(xù)的結(jié)構(gòu),所以未實(shí)現(xiàn)成數(shù)組規(guī)模
             *    地分配指針的功能.
             *
             *已知bug:暫無(wú)
             *
             *版權(quán)信息:
             *        該代碼為開(kāi)源代碼,原作者保留其所有權(quán).你可以拷貝,修改,使用該代碼,但
             *    是請(qǐng)保留必要的版權(quán)信息.
            *****************************************************************
            */

            #ifndef POINTERPOOL_H
            #define POINTERPOOL_H

            #include 
            <vector>
            #include 
            <queue>
            #include 
            <stdexcept>
            #include 
            <memory>
            using namespace std;

            template
            < typename T >
            class PointerPool
            {
            public:
                
            //Create a PointerPool to contain size pointers.
                PointerPool( size_t size = kDefaultChunkSize )
                    
            throw ( invalid_argument, bad_alloc );
                
            ~PointerPool();

                
            //Return a specific pointer to client.
                T *getPointer();
                
            //Recycle the pointer that the client doesn't need any more.
                void recyclePointer( T* );
                
            //Clear the pool and release the memory.
                void clear();
                
            bool empty();
                
            //Return the total number of pointers the pool contains.
                size_t size();
                
            //Return the number of pointers remain available.
                size_t remain();

            protected:
                queue
            < T* > mFreeList;
                vector
            < T* > mAllpointers;//A record of all pointers which help
                
            //to destroy them.

                size_t mChunkSize;
                
            static const size_t kDefaultChunkSize = 10;

                
            //Allocate mChunkSize new pointers and add them to the
                
            //mFreeList.
                void allocateChunk();
                
            //help the destructor the delete the pointers int the pool.
                static void deleteHelper( T* );

            private:
                
            //Hide the copy constructor and assignment symbol.
                PointerPool( const PointerPool< T > & );
                PointerPool
            < T > &operator=const PointerPool< T > & );
            }
            ;

            template
            < typename T >
            size_t PointerPool
            <T>::remain()
            {
                
            return mFreeList.size();
            }


            template
            < typename T >
            bool PointerPool<T>::empty()
            {
                
            return remain() == 0;
            }


            template
            < typename T >
            size_t PointerPool
            <T>::size()
            {
                
            return mChunkSize * mAllpointers.size();
            }


            template
            < typename T >
            PointerPool
            <T>::PointerPool( size_t size /*= kDefaultChunkSize */ ) throw ( invalid_argument, bad_alloc )
            {
                
            if ( size <= 0 )
                    
            throw invalid_argument( "chunk size must be positive" );

                mChunkSize 
            = size;
                allocateChunk();
            }


            template
            < typename T >
            *PointerPool<T>::getPointer()
            {
                
            if ( mFreeList.empty() )
                    allocateChunk();

                T 
            *ptr = mFreeList.front();
                mFreeList.pop();
                
            return ptr;
            }


            template
            < typename T >
            void PointerPool<T>::clear()
            {
                for_each( mAllpointers.begin(), mAllpointers.end(), deleteHelper );

                mAllpointers.clear();
                
            while ( !mFreeList.empty() )
                    mFreeList.pop();
            }


            template
            < typename T >
            void PointerPool<T>::recyclePointer( T *ptr )
            {
                mFreeList.push( ptr );
            }


            template
            < typename T >
            void PointerPool<T>::allocateChunk()
            {
                T
            * newPointerChunk = new T[ mChunkSize ];

                mAllpointers.push_back( newPointerChunk );
                
            for ( int i = 0; i < mChunkSize; i++ )
                
            {
                    mFreeList.push( 
            &newPointerChunk[i] );
                }

            }


            template
            < typename T >
            void PointerPool<T>::deleteHelper( T *pointerChunk )
            {
                delete [] pointerChunk;
            }


            template
            < typename T >
            PointerPool
            <T>::~PointerPool()
            {
                for_each( mAllpointers.begin(), mAllpointers.end(), deleteHelper );
            }


            #endif

            以下為測(cè)試代碼:
            #include "PointerPool.h"
            #include 
            <iostream>
            using namespace std;

            class ListNode
            {
            public:
                
            int value;
                ListNode 
            *nextPtr;

                ListNode() : value(
            0), nextPtr(0)
                
            {}
            }
            ;

            int growing()
            {
                
            static int numGrouwing = 0;
                
            return numGrouwing++;
            }


            int main()
            {
                
            const int cases = 2;
                PointerPool
            < ListNode > lnPool;

                
            //Make 2 tests.
                for ( int i = 0; i < cases; i++ )
                
            {
                    
            if ( lnPool.empty() )
                        cout 
            << "The pool is empty!" << endl;
                    
            else
                        cout 
            << "The pool has " << lnPool.remain()
                        
            << " pointers available" << endl;

                    
            //Create the head of a list.
                    ListNode *start = lnPool.getPointer();
                    start
            ->value = growing();
                    ListNode 
            *last = start;

                    
            //Complete the list with length of 100.
                    for ( int j = 1; j < 100; j++ )
                    
            {
                        ListNode 
            *tmp = lnPool.getPointer();
                        tmp
            ->value = growing();
                        last
            ->nextPtr = tmp;
                        last 
            = tmp;
                    }


                    
            //Travel and cout the value of each node.
                    ListNode *current = start;
                    
            while ( current != 0 )
                    
            {
                        cout 
            << current->value << '\t';
                        current 
            = current->nextPtr;
                    }


                    
            //Recycle all pointers back to the pool.
                    while ( start != 0 )
                    
            {
                        current 
            = start;
                        start 
            = start->nextPtr;
                        lnPool.recyclePointer( current );
                    }


                    cout 
            << "The pool has " << lnPool.size() << " pointers"
                        
            << "\n\n";
                    
            //Try clear the pool
                    lnPool.clear();
                }

            }
            posted on 2011-04-11 16:13 今晚打老虎 閱讀(1857) 評(píng)論(5)  編輯 收藏 引用 所屬分類(lèi): 我的開(kāi)源庫(kù)

            評(píng)論

            # re: PointerPool(指針池) 2011-04-11 18:26 ooseven
            本質(zhì)上就是一個(gè)ptrvector吧?  回復(fù)  更多評(píng)論
              

            # re: PointerPool(指針池) 2011-04-11 18:49 bennycen
            。。。。。。。。。  回復(fù)  更多評(píng)論
              

            # re: PointerPool(指針池) 2011-04-11 20:42 千暮(zblc)
            測(cè)試了下,如果把鏈表長(zhǎng)度改為1w,kDefaultChunkSize長(zhǎng)度改為100,注釋掉所有輸出,cases改為2w 比不用你的指針池而直接new快4-5倍.

              回復(fù)  更多評(píng)論
              

            # re: PointerPool(指針池) 2011-04-12 00:19 今晚打老虎
            @千暮(zblc)
            感謝測(cè)試。其實(shí)我還在c++的摸爬滾打中,對(duì)性能測(cè)試的概念還比較模糊,也沒(méi)有學(xué)習(xí)和體驗(yàn)過(guò)系統(tǒng)的測(cè)試。希望能得到有關(guān)方面的指教,比如,測(cè)試工具需要什么么?參考指標(biāo)有哪些?  回復(fù)  更多評(píng)論
              

            # re: PointerPool(指針池) 2011-04-12 00:46 千暮(zblc)
            @今晚打老虎
            其實(shí)不用考慮得太復(fù)雜 就是計(jì)算時(shí)間(在代碼中插入時(shí)鐘計(jì)算間隔)、空間消耗(重載分配堆內(nèi)存的內(nèi)置函數(shù))和運(yùn)行效率的穩(wěn)定性(代碼運(yùn)行的邊界情況進(jìn)行分析)。至于系統(tǒng)測(cè)試,也只是把情況劃分得更加細(xì)致而已。  回復(fù)  更多評(píng)論
              

            伊人久久大香线蕉综合网站| 91精品国产综合久久精品| 亚洲天堂久久久| 国内精品久久久久影院老司| 伊人久久综合精品无码AV专区| 久久久无码精品亚洲日韩蜜臀浪潮| 国内精品久久久久伊人av| 99国内精品久久久久久久| 精品久久无码中文字幕| 久久不射电影网| 日韩久久无码免费毛片软件| 亚洲综合伊人久久综合| 亚洲七七久久精品中文国产 | 久久AV高清无码| 精品久久香蕉国产线看观看亚洲| 91精品观看91久久久久久| 亚洲人成电影网站久久| 久久永久免费人妻精品下载| 国产亚洲成人久久| 久久WWW免费人成一看片| 色欲久久久天天天综合网| 91久久精品视频| 丁香色欲久久久久久综合网| 99久久国产热无码精品免费久久久久| 日本高清无卡码一区二区久久| 国产高潮国产高潮久久久| 亚洲国产成人久久一区WWW| 久久精品国产99国产精品澳门| 怡红院日本一道日本久久| 亚洲av伊人久久综合密臀性色| 久久激情五月丁香伊人| 久久亚洲私人国产精品| 污污内射久久一区二区欧美日韩 | 91精品国产综合久久精品| 伊人久久五月天| 久久露脸国产精品| 国产99久久久久久免费看| 91精品国产91久久综合| 亚洲AV无码久久精品色欲| 久久久久亚洲国产| 亚洲国产成人精品女人久久久 |