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

            統(tǒng)計(jì)

            • 隨筆 - 50
            • 文章 - 42
            • 評(píng)論 - 147
            • 引用 - 0

            留言簿(6)

            隨筆分類

            文章分類

            Link

            搜索

            •  

            積分與排名

            • 積分 - 165652
            • 排名 - 159

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            MSVC++ 對(duì)象內(nèi)存模型深入解析與具體應(yīng)用 (三)

            MSVC++ 對(duì)象內(nèi)存模型深入解析與具體應(yīng)用

            前言:本文之所以強(qiáng)調(diào)MSVC, 旨在提醒讀者在不同平臺(tái)和解釋器下內(nèi)存布局和實(shí)現(xiàn)上存在差異,但編程思想通用,文中內(nèi)容大多來(lái)自筆者實(shí)際工作經(jīng)驗(yàn)和網(wǎng)上搜集,力求正確,但水平有限,如有不當(dāng)之處,敬請(qǐng)指出

            面向?qū)ο螅罕疚拿嫦蛴幸欢?/span>C/C++基礎(chǔ),并且可以讀懂部分匯編的讀者

            版權(quán):歡迎轉(zhuǎn)載,但請(qǐng)注明出處http://m.shnenglu.com/dawnbreak/ 保留對(duì)本文的一切權(quán)力

            目錄
            1. C++基本類型與結(jié)構(gòu)體內(nèi)存布局
                            Key words: class,  struct, memory alignment

            2.虛表多態(tài)與動(dòng)態(tài)綁定

                            Key words: Virtual Table, polymiorphism

            3.對(duì)象池

                            Key words: object pool , reload, new ,delete

            4.內(nèi)存泄漏檢測(cè)

                            Key words: memory leak detect

            5.智能指針

                            Key words: smart pointer

            6.   編譯期類型約束
               
                            Key words: compile-time ,type-constraint

            第三章 對(duì)象池

            Key words: object pool

            對(duì)象池

            對(duì)象池是一種避免在整個(gè)程序生存周期內(nèi)重復(fù)創(chuàng)建或刪除大量對(duì)象的方法。在代碼里無(wú)論何時(shí)當(dāng)你需要一個(gè)對(duì)象時(shí),你可以在對(duì)象池中申請(qǐng)一個(gè)。當(dāng)你使用完畢后,將其歸還到池中。對(duì)象池僅僅創(chuàng)建一次對(duì)象,所以他們的構(gòu)造函數(shù)僅僅被調(diào)用一次,并不是每次使用時(shí)都調(diào)用。所以在對(duì)象池創(chuàng)建時(shí)僅僅做一些通用的初始化,而在對(duì)象實(shí)例調(diào)用其他非構(gòu)造函數(shù)時(shí)進(jìn)行特別賦值及操作。

            一個(gè)對(duì)象池的實(shí)現(xiàn) 

             1#include <queue>
             2#include <vector>
             3#include <stdexcept>
             4#include <memory>
             5using std::queue;
             6using std::vector;
             7//
             8// template class ObjectPool
             9//
            10// Provides an object pool that can be used with any class that provides a
            11// default constructor
            12//
            13// The object pool constructor creates a pool of objects, which it hands out
            14// to clients when requested via the acquireObject() method. When a client is
            15// finished with the object it calls releaseObject() to put the object back
            16// into the object pool.
            17//
            18// The constructor and destructor on each object in the pool will be called only
            19// once each for the lifetime of the program, not once per acquisition and release.
            20//
            21// The primary use of an object pool is to avoid creating and deleting objects
            22// repeatedly. The object pool is most suited to applications that use large 
            23// numbers of objects for short periods of time.
            24//
            25// For efficiency, the object pool doesn’t perform sanity checks.
            26// It expects the user to release every acquired object exactly once.
            27// It expects the user to avoid using any objects that he or she has released.
            28//
            29// It expects the user not to delete the object pool until every object
            30// that was acquired has been released. Deleting the object pool invalidates
            31// any objects that the user has acquired, even if they have not yet been released.
            32//
            33template <typename T>
            34class ObjectPool
            35{
            36public:
            37//
            38// Creates an object pool with chunkSize objects.
            39// Whenever the object pool runs out of objects, chunkSize
            40// more objects will be added to the pool. The pool only grows:
            41// objects are never removed from the pool (freed), until
            42// the pool is destroyed.
            43//
            44// Throws invalid_argument if chunkSize is <= 0
            45//
            46ObjectPool(int chunkSize = kDefaultChunkSize)
            47throw(std::invalid_argument, std::bad_alloc);
            48//
            49// Frees all the allocated objects. Invalidates any objects that have
            50// been acquired for use
            51//
            52~ObjectPool();
            53//
            54// Reserve an object for use. The reference to the object is invalidated
            55// if the object pool itself is freed.
            56// 
            57// Clients must not free the object!
            58//
            59T& acquireObject();
            60//
            61// Return the object to the pool. Clients must not use the object after
            62// it has been returned to the pool.
            63//
            64void releaseObject(T& obj);
            65protected:
            66//
            67// mFreeList stores the objects that are not currently in use
            68// by clients.
            69//
            70queue<T*> mFreeList;
            71//
            72// mAllObjects stores pointers to all the objects, in use
            73// or not. This vector is needed in order to ensure that all
            74// objects are freed properly in the destructor.
            75//
            76vector<T*> mAllObjects;
            77int mChunkSize;
            78static const int kDefaultChunkSize = 10;
            79//
            80// Allocates mChunkSize new objects and adds them
            81// to the mFreeList
            82//
            83void allocateChunk();
            84static void arrayDeleteObject(T* obj);
            85private:
            86// Prevent assignment and pass-by-value.
            87ObjectPool(const ObjectPool<T>& src);
            88ObjectPool<T>& operator=(const ObjectPool<T>& rhs);
            89}
            ;

             

            template <typename T>
            ObjectPool
            <T>::ObjectPool(int chunkSize) throw(std::invalid_argument,
                                                           std::bad_alloc) : mChunkSize(chunkSize)
            {
                
            if (mChunkSize <= 0{
                    
            throw std::invalid_argument(“chunk size must be positive”);
                }

                
            // Create mChunkSize objects to start.
                allocateChunk();
            }

            //
            // Allocates an array of mChunkSize objects because that’s
            // more efficient than allocating each of them individually.
            // Stores a pointer to the first element of the array in the mAllObjects
            // vector. Adds a pointer to each new object to the mFreeList.
            //
            template <typename T>
            void ObjectPool<T>::allocateChunk()
            {
                T
            * newObjects = new T[mChunkSize];
                mAllObjects.push_back(newObjects);
                
            for (int i = 0; i < mChunkSize; i++{
                    mFreeList.push(
            &newObjects[i]);
                }

            }

            //
            // Freeing function for use in the for_each algorithm in the
            // destructor
            //
            template<typename T>
            void ObjectPool<T>::arrayDeleteObject(T* obj)
            {
                delete [] obj;
            }

            template 
            <typename T>
            ObjectPool
            <T>::~ObjectPool()
            {
                
            // Free each of the allocation chunks.
                for_each(mAllObjects.begin(), mAllObjects.end(), arrayDeleteObject);
            }

            template 
            <typename T>
            T
            & ObjectPool<T>::acquireObject()
            {
                
            if (mFreeList.empty()) {
                    allocateChunk();
                }

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

            template 
            <typename T>
            void ObjectPool<T>::releaseObject(T& obj)
            {
                mFreeList.push(
            &obj);
            }

            以上是對(duì)象池的一個(gè)簡(jiǎn)單實(shí)現(xiàn),使用隊(duì)列mFreeList記錄可以使用的對(duì)象,使用向量mAllObjects來(lái)記錄所有的對(duì)象,以便安全釋放內(nèi)存
            在實(shí)際使用中,可以使用棧來(lái)保存可用對(duì)象,這樣可以更加高效的使用內(nèi)存


            posted on 2010-06-05 14:13 pear_li 閱讀(2384) 評(píng)論(0)  編輯 收藏 引用 所屬分類: C++

            久久99国产精一区二区三区| 国内精品伊人久久久影院| 国产精品青草久久久久婷婷| 99久久精品免费观看国产| 久久久久久国产精品美女| 久久久久人妻一区二区三区| 精品久久久久久中文字幕| 久久男人中文字幕资源站| 囯产极品美女高潮无套久久久| 久久99精品国产| 久久精品国产99国产精品亚洲| 一本一道久久精品综合| 7777久久久国产精品消防器材 | 亚洲精品国产美女久久久 | 色播久久人人爽人人爽人人片aV| 国产亚洲精品久久久久秋霞| 国产精品永久久久久久久久久 | 99精品国产免费久久久久久下载| 成人资源影音先锋久久资源网| 久久91精品国产91| 婷婷久久精品国产| 久久99亚洲综合精品首页| 久久99国产精品久久| 久久午夜无码鲁丝片| 亚洲日本va中文字幕久久| 综合久久久久久中文字幕亚洲国产国产综合一区首 | 久久综合久久性久99毛片| 国产成人精品久久亚洲高清不卡 | 7777精品久久久大香线蕉| 国产精品久久久久久五月尺| 久久久精品国产亚洲成人满18免费网站 | 日韩人妻无码一区二区三区久久| 伊人久久精品影院| 久久久久亚洲AV片无码下载蜜桃| 一本综合久久国产二区| 久久久久久久97| 免费久久人人爽人人爽av| 亚洲AV无码久久精品色欲| 性欧美大战久久久久久久久| 久久亚洲日韩精品一区二区三区| 久久影院综合精品|