• <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>
            面對現(xiàn)實,超越自己
            逆水行舟,不進則退
            posts - 269,comments - 32,trackbacks - 0
            在高效C++編程中看到一個不錯的內(nèi)存池實現(xiàn)方案,這里共享下,大家看看有什么不足。
            代碼很簡單,如下:
            template<typename T>
            class CMemoryPool
            {
                public:
                    enum { EXPANSION_SIZE = 32};

                    CMemoryPool(unsigned int nItemCount = EXPANSION_SIZE)
                    {
                        ExpandFreeList(nItemCount);
                    }
                    
                    ~CMemoryPool()
                    {
                        //free all memory in the list
                        CMemoryPool<T>* pNext = NULL;
                        for(pNext = m_pFreeList; pNext != NULL; pNext = m_pFreeList)
                        {
                            m_pFreeList = m_pFreeList->m_pFreeList;
                            delete [](char*)pNext;
                        }
                    }

                    void* Alloc(unsigned int /*size*/)
                    {
                        if(m_pFreeList == NULL)
                        {
                            ExpandFreeList();
                        }
                        
                        //get free memory from head
                        CMemoryPool<T>* pHead = m_pFreeList;
                        m_pFreeList = m_pFreeList->m_pFreeList;
                        return pHead;
                    }

                    void Free(void* p)
                    {
                        //push the free memory back to list
                        CMemoryPool<T>* pHead = static_cast<CMemoryPool<T>*>(p);
                        pHead->m_pFreeList = m_pFreeList;
                        m_pFreeList = pHead;
                    }

                protected:
                    //allocate memory and push to the list
                    void ExpandFreeList(unsigned nItemCount = EXPANSION_SIZE)
                    {
                        unsigned int nSize = sizeof(T) > sizeof(CMemoryPool<T>*) ? sizeof(T) : sizeof(CMemoryPool<T>*);
                        CMemoryPool<T>* pLastItem = static_cast<CMemoryPool<T>*>(static_cast<void*>(new char[nSize]));
                        m_pFreeList = pLastItem;
                        for(int i=0; i<nItemCount-1; ++i)
                        {
                            pLastItem->m_pFreeList = static_cast<CMemoryPool<T>*>(static_cast<void*>(new char[nSize]));
                            pLastItem = pLastItem->m_pFreeList;
                        }

                        pLastItem->m_pFreeList = NULL;
                    }

                private:
                    CMemoryPool<T>* m_pFreeList;
            };

            它的實現(xiàn)思想就是每次從List的頭上取內(nèi)存, 如果取不到則重新分配一定數(shù)量; 用完后把內(nèi)存放回List頭部,這樣的話效率很高,因為每次List上可以取到的話,肯定是空閑的內(nèi)存。

            當然上面的代碼只是針對單線程的,要支持多線程的話也很簡單,外面加一層就可以了,
            代碼如下:
            class CCriticalSection
            {
            public:
                CCriticalSection()
                {
                    InitializeCriticalSection(&m_cs);
                }

                ~CCriticalSection()
                {
                    DeleteCriticalSection(&m_cs);
                }

                void Lock()
                {
                    EnterCriticalSection(&m_cs); 
                }

                void Unlock()
                {
                    LeaveCriticalSection(&m_cs);
                }

            protected:
                CRITICAL_SECTION m_cs;
            };

            template<typename POOLTYPE, typename LOCKTYPE>
            class CMTMemoryPool
            {
                public:
                    void* Alloc(unsigned int size)
                    {
                        void* p = NULL;
                        m_lock.Lock();
                        p = m_pool.Alloc(size);
                        m_lock.Unlock();

                        return p;
                    }

                    void Free(void* p)
                    {
                        m_lock.Lock();
                        m_pool.Free(p);
                        m_lock.Unlock();    
                    }

                private:
                    POOLTYPE m_pool;
                    LOCKTYPE m_lock;
            };

            這是我的測試代碼:
            #include <iostream>
            #include <windows.h>

            using namespace std;

            #include "MemoryPool.h"
            #include "MTMemoryPool.h"

            class CTest
            {
            public:
                int m_n;
                int m_n1;

                voidoperator new(size_t size)
                {
                    void* p = s_pool->Alloc(size);
                    return p;
                }

                void operator delete(void* p, size_t size)
                {
                    s_pool->Free(p);
                }

                static void NewPool()
                {
                    //s_pool = new CMemoryPool<CTest>;
                    s_pool = new CMTMemoryPool<CMemoryPool<CTest>, CCriticalSection>;
                }

                static void DeletePool()
                {
                    delete s_pool;
                    s_pool = NULL;
                }
                
                //static CMemoryPool<CTest>* s_pool;
                static CMTMemoryPool<CMemoryPool<CTest>, CCriticalSection>* s_pool;
            };

            //CMemoryPool<CTest>* CTest::s_pool = NULL;
            CMTMemoryPool<CMemoryPool<CTest>, CCriticalSection>* CTest::s_pool = NULL;

            void testFun()
            {
                int i;
                const int nLoop = 10;
                const int nCount = 10000;
                
                for(int j = 0; j<nLoop; ++j)
                {
                    typedef CTest* LPTest;
                    LPTest arData[nCount];
                    for(i=0;i <nCount; ++i)
                    {
                        arData[i] = new CTest;
                    }

                    for(i=0;i <nCount; ++i)
                    {
                        delete arData[i];
                    }
                }
            }

            int main(int argc, char* argv[])
            {
                {
                    unsigned int dwStartTickCount = GetTickCount();

                    CTest::NewPool();

                    testFun();
                    
                    CTest::DeletePool();
                    
                    cout << "total cost" << GetTickCount() - dwStartTickCount << endl;
                }


                system("pause");

                return 0;
            }
            在我機器上測試結(jié)果比系統(tǒng)默認的CRT實現(xiàn)高效N倍。

            本文轉(zhuǎn)自:http://m.shnenglu.com/weiym/archive/2012/05/05/173785.aspx
            posted on 2012-09-11 11:30 王海光 閱讀(556) 評論(0)  編輯 收藏 引用 所屬分類: C++
            国产叼嘿久久精品久久| 久久久网中文字幕| 亚洲国产成人久久精品影视| 一级做a爱片久久毛片| 亚洲欧洲久久av| 久久青草国产手机看片福利盒子| 久久高清一级毛片| 久久水蜜桃亚洲av无码精品麻豆| 久久成人国产精品二三区| 欧美麻豆久久久久久中文| 久久91精品国产91久久户| 久久综合亚洲色HEZYO社区 | 久久久久精品国产亚洲AV无码| 99久久99久久精品免费看蜜桃 | 欧美与黑人午夜性猛交久久久 | 亚洲精品99久久久久中文字幕| 97久久精品午夜一区二区| 亚洲日本va中文字幕久久| 久久婷婷五月综合成人D啪| 国产成人精品久久免费动漫| 久久精品国产亚洲AV香蕉| 久久婷婷色综合一区二区| 中文字幕亚洲综合久久2| 99久久人妻无码精品系列| 亚洲AV乱码久久精品蜜桃| 欧美久久久久久| 亚洲Av无码国产情品久久| 久久男人中文字幕资源站| 精品无码久久久久久国产| 国产福利电影一区二区三区久久老子无码午夜伦不| 久久免费看黄a级毛片| 亚洲国产视频久久| 久久婷婷五月综合色奶水99啪| 久久久久久亚洲精品不卡| 国产 亚洲 欧美 另类 久久 | 久久天天躁狠狠躁夜夜网站| 亚洲国产精品无码久久SM| 麻豆AV一区二区三区久久| 国产精品国色综合久久| 久久ww精品w免费人成| 久久国产亚洲高清观看|