青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

統計

  • 隨筆 - 50
  • 文章 - 42
  • 評論 - 147
  • 引用 - 0

留言簿(6)

隨筆分類

文章分類

Link

搜索

  •  

積分與排名

  • 積分 - 166526
  • 排名 - 159

最新評論

閱讀排行榜

評論排行榜

MSVC++ 對象內存模型深入解析與具體應用 (三)

MSVC++ 對象內存模型深入解析與具體應用

前言:本文之所以強調MSVC, 旨在提醒讀者在不同平臺和解釋器下內存布局和實現上存在差異,但編程思想通用,文中內容大多來自筆者實際工作經驗和網上搜集,力求正確,但水平有限,如有不當之處,敬請指出

面向對象:本文面向有一定C/C++基礎,并且可以讀懂部分匯編的讀者

版權:歡迎轉載,但請注明出處http://m.shnenglu.com/dawnbreak/ 保留對本文的一切權力

目錄
1. C++基本類型與結構體內存布局
                Key words: class,  struct, memory alignment

2.虛表多態與動態綁定

                Key words: Virtual Table, polymiorphism

3.對象池

                Key words: object pool , reload, new ,delete

4.內存泄漏檢測

                Key words: memory leak detect

5.智能指針

                Key words: smart pointer

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

第三章 對象池

Key words: object pool

對象池

對象池是一種避免在整個程序生存周期內重復創建或刪除大量對象的方法。在代碼里無論何時當你需要一個對象時,你可以在對象池中申請一個。當你使用完畢后,將其歸還到池中。對象池僅僅創建一次對象,所以他們的構造函數僅僅被調用一次,并不是每次使用時都調用。所以在對象池創建時僅僅做一些通用的初始化,而在對象實例調用其他非構造函數時進行特別賦值及操作。

一個對象池的實現 

 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);
}

以上是對象池的一個簡單實現,使用隊列mFreeList記錄可以使用的對象,使用向量mAllObjects來記錄所有的對象,以便安全釋放內存
在實際使用中,可以使用棧來保存可用對象,這樣可以更加高效的使用內存


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

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            99精品视频免费在线观看| 国产精品日日摸夜夜摸av| 亚洲高清在线视频| 欧美一区二区三区播放老司机| 久久野战av| 欧美一区视频| 久久精品亚洲一区二区三区浴池| 亚洲欧美中文日韩在线| 欧美一区国产二区| 蜜月aⅴ免费一区二区三区| 欧美大片免费观看| 国产精品福利网站| 国产一区二区三区视频在线观看| 精品96久久久久久中文字幕无| 在线观看亚洲一区| 宅男噜噜噜66国产日韩在线观看| 午夜精品久久久久久久99水蜜桃| 欧美伊人精品成人久久综合97| 欧美一区二区黄色| 亚洲电影免费观看高清完整版在线观看 | 欧美精品一区二区高清在线观看| 欧美日韩一卡二卡| 国产日韩欧美精品在线| 亚洲人www| 久久久精品tv| 日韩视频在线观看| 久久综合成人精品亚洲另类欧美| 欧美视频日韩| 最近看过的日韩成人| 香蕉久久国产| 亚洲人成在线播放网站岛国| 欧美在线国产精品| 国产精品久久| 99成人精品| 欧美大尺度在线观看| 亚洲欧美国产高清va在线播| 欧美搞黄网站| 亚洲黑丝在线| 免费在线看一区| 欧美一级艳片视频免费观看| 欧美日韩国产区一| 亚洲成色999久久网站| 久久精品人人做人人综合| 亚洲久色影视| 你懂的视频一区二区| 好吊色欧美一区二区三区四区| 亚洲视频图片小说| 亚洲精品中文字幕在线观看| 久久综合久久久| 一区二区三区在线观看国产| 亚洲欧美日韩国产中文在线| 亚洲第一福利在线观看| 久久久久国产一区二区三区四区 | 亚洲国产一二三| 久久久噜噜噜久久人人看| 一区二区三区在线免费视频| 欧美理论电影在线播放| 亚洲电影在线看| 久久亚洲欧美| 亚洲深夜福利在线| 最新日韩在线| 欧美成人精品1314www| 亚洲欧美国产精品桃花| 国产精品亚洲综合久久| 午夜精品久久99蜜桃的功能介绍| 亚洲美女中文字幕| 欧美国产国产综合| 在线性视频日韩欧美| 日韩视频中文字幕| 国产精品成人观看视频免费| 亚洲网友自拍| 亚洲欧美视频在线观看视频| 国产女优一区| 麻豆精品视频在线观看| 久久综合伊人77777麻豆| 亚洲丶国产丶欧美一区二区三区| 免费不卡中文字幕视频| 欧美成人精精品一区二区频| 亚洲精品三级| 亚洲一区二区三区高清不卡| 国产精品久久久久秋霞鲁丝| 欧美在线观看视频| 久久一区二区精品| 日韩视频在线观看免费| 中日韩高清电影网| 国内成+人亚洲| 亚洲国产精品热久久| 欧美日韩一区二区免费在线观看| 亚洲综合视频1区| 久久精品99国产精品| 亚洲国产日韩在线一区模特| 亚洲精品婷婷| 国产日韩欧美不卡| 欧美黄色一区二区| 欧美亚男人的天堂| 老鸭窝毛片一区二区三区| 欧美电影免费观看| 性欧美videos另类喷潮| 久久一二三四| 欧美一区二区国产| 欧美精品福利视频| 久久精品视频免费播放| 欧美精品videossex性护士| 亚洲欧美另类在线观看| 久久久久久国产精品mv| 亚洲综合99| 久久蜜桃香蕉精品一区二区三区| 一区二区福利| 免费观看日韩| 久久精品国产欧美激情| 欧美网站在线观看| 亚洲国产成人porn| 国产视频一区欧美| 一区二区黄色| 日韩性生活视频| 久久久久久久综合日本| 午夜欧美精品久久久久久久| 女同一区二区| 欧美二区在线播放| 午夜在线观看欧美| 亚洲香蕉伊综合在人在线视看| 久久久一二三| 久久久久久国产精品mv| 国产精品免费观看视频| 亚洲精选在线| 99在线精品视频在线观看| 久久在线91| 欧美福利一区| 亚洲国产天堂久久综合网| 久久久国产精彩视频美女艺术照福利| 午夜在线一区二区| 国产精品婷婷| 亚洲网址在线| 欧美一二区视频| 国产精品午夜久久| 中文在线资源观看网站视频免费不卡 | 欧美激情一区二区三级高清视频| 国产一区二区三区不卡在线观看| 亚洲网友自拍| 午夜久久tv| 国产女人aaa级久久久级| 亚洲网站在线播放| 久久爱www久久做| 国产三级精品在线不卡| 午夜日韩福利| 久久夜色精品一区| 在线欧美日韩国产| 欧美大色视频| 99国产精品视频免费观看一公开| 99综合在线| 国产精品劲爆视频| 欧美亚洲专区| 亚洲第一福利视频| 亚洲午夜精品国产| 国产精品综合久久久| 欧美在线在线| 欧美国内亚洲| 中文日韩在线| 国产一区亚洲一区| 久久综合精品国产一区二区三区| 亚洲国产黄色片| 亚洲精品日韩欧美| 国产精品九色蝌蚪自拍| 久久国产精品久久久久久久久久| 久久久久五月天| 亚洲精品在线观看免费| 欧美日韩一区二区三| 亚洲欧美大片| 欧美电影在线观看| 亚洲视频中文| 国产亚洲欧美另类一区二区三区| 久久综合久久综合这里只有精品| 亚洲国内精品| 欧美一区二区精品| 亚洲国产精品一区二区久 | 欧美精品一区在线观看| 亚洲欧美日韩国产一区二区三区| 亚洲国产欧美精品| 国产精品视频一二三| 久久天天躁狠狠躁夜夜爽蜜月| 最新高清无码专区| 欧美在线一区二区| 欧美va日韩va| 国产欧美丝祙| 久久久久**毛片大全| 亚洲国产一二三| 国产精品视频久久久| 蜜臀av一级做a爰片久久| 亚洲视频自拍偷拍| 欧美福利一区二区| 久久久久欧美精品| 亚洲欧美日韩一区在线观看| 亚洲国产高清视频| 国产精品一区视频| 欧美日韩在线视频一区二区| 久久久999国产| 亚洲免费在线视频| 在线视频精品一| 亚洲欧洲一区二区在线观看| 久久夜色精品国产欧美乱极品|