日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

STL空间配置器

發布時間:2025/3/20 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 STL空间配置器 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、什么是空間配置器?

  空間配置器負責空間配置與管理。配置器是一個實現了動態空間配置、空間管理、空間釋放的class template。以內存池方式實現小塊內存管理分配。關于內存池概念可以點擊:內存池。

2、STL空間配置器產生的緣由

  在軟件開發,程序設計中,我們不免因為程序需求,使用很多的小塊內存(基本類型以及小內存的自定義類型)。在程序中動態申請,釋放。這個過程過程并不是一定能夠控制好的,于是乎出現以下問題:

問題1:就出現了內存碎片問題。(ps外碎片問題)

問題2:一直在因為小塊內存而進行內存申請,調用malloc,系統調用產生性能問題。

注:內碎片:因為內存對齊/訪問效率(CPU取址次數)而產生 如 用戶需要3字節,實際得到4或者8字節的問題,其中的碎片是浪費掉的。

  外碎片:系統中內存總量足夠,但是不連續,所以無法分配給用戶使用而產生的浪費。如圖:

(1)一級空間配置器和二級空間配置器實現思路:

空間配置策略:

?用戶申請空間大于128?

  yes:調用一級空間配置器

  no:調用二級空間配置器

?客戶端可以通過宏__USE_MALLOC進行自定義選擇是否使用二級空間配置器。

  STL設計了雙層級配置器,第一級配置直接使用malloc()和free();第二級配置器則視情況采用不同的策略,當配置區大于128bytes時,直接調用第一級配置器;當配置區塊小于128bytes時,遍不借助第一級配置器,而使用一個memory pool來實現。究竟是使用第一級配置器還是第二級配置器,由一個__USE_MALLOC宏定義來控制。SGI STL中默認使用第二級配置器。

3、一級空間配置器

主要是Allocate()函數和Dellocate()函數,直接封裝了malloc,free進行處理,增加了C++中的set_handler機制,增加內存分配時客戶端可選處理機制。

1 #define __TRACE_DEBUG(...) \ 2 __trace_debug(__FUNCTION__ , __FILE__, __LINE__, __VA_ARGS__); 3 4 typedef void (*HANDLE_FUNC)(); 5 6 template<int inst> 7 class MallocAllocTemplate 8 { 9 public: 10 //static void(*__malloc_alloc_oom_handler)(); 11 static HANDLE_FUNC _malloc_alloc_oom_handler; 12 13 void* OOM_Malloc(size_t n) 14 { 15 while (1)//死循環一直申請空間,直到申請成功,或失敗拋異常 16 { 17 if (_malloc_alloc_oom_handler == 0) 18 { 19 throw bad_alloc(); 20 } 21 _malloc_alloc_oom_handler();//釋放內存 22 void* second = malloc(n); //再次申請空間 23 if (second) 24 return second; 25 } 26 } 27 // 1: 分配內存成功, 則直接返回 28 // 2: 若分配失敗, 則檢查是否設置處理的handler, 29 //有則調用以后再分配。 不斷重復這個過程, 直到分配成功為止。 30 //沒有設置處理的handler, 則直接結束程序。 31 static void* Allocate(size_t n) 32 { 33 __TRACE_DEBUG("一級空間配置器申請%ubytes\n", n); 34 35 void* first = malloc(n); 36 if (first == NULL) //第一次申請空間失敗 37 { 38 first = OOM_Malloc(0); 39 } 40 return first; 41 } 42 //realloc實現機制與allocate類似 43 void* OOM_Realloc(size_t n) 44 { 45 while (1)//死循環一直申請空間,直到申請成功,或失敗拋異常 46 { 47 if (_malloc_alloc_oom_handler == 0) 48 { 49 throw bad_alloc(); 50 } 51 _malloc_alloc_oom_handler(); 52 void* second = realloc(n); 53 if (second) 54 return second; 55 } 56 } 57 static void* Rllocate(size_t n) 58 { 59 __TRACE_DEBUG("一級空間配置器申請%ubytes\n", n); 60 61 void* first = realloc(n); 62 if (first == NULL) 63 { 64 first = OOM_Realloc(0); 65 } 66 return first; 67 } 68 69 static void Delloctate(void* p, size_t n) 70 { 71 __TRACE_DEBUG("一級空間配置器釋放%ubytes\n", n); 72 free(p); 73 } 74 75 static HANDLE_FUNC SetMallocHandler(HANDLE_FUNC f) 76 { 77 HANDLE_FUNC old = f; 78 __malloc_alloc_oom_handler = f; 79 return old; 80 } 81 // static void(*SetMallocHandler(void(*f)()))() 82 // { 83 // void(*old)() = __malloc_alloc_oom_handler; 84 // __malloc_alloc_oom_handler = f; 85 // return(old); 86 // } 87 private: 88 }; 89 90 //分配內存失敗處理函數的句柄函數指針 91 template<int inst> 92 HANDLE_FUNC __MallocAllocTemplate<inst>::__malloc_alloc_oom_handler = NULL;

SetMallocHandler(HANDLE_FUNC f)  

  malloc,free,realloc等庫函數是向系統申請內存并且操作的函數。平時我們并不太會遇到內存空間分配不出來的情況,但是如果這一套程序是運行在服務器上的,各種各樣的進程都需要內存。這樣頻繁的分配內存,終有一個時候,服務器再也分配不出內存,那么空間配置器該怎么辦呢?這個函數指針指向的句柄函數就是處理這種情況的設計。

? ? ?? SetMallocAllocHander()一般是自己設計的一種策略。這種策略想要幫助操作系統得到內存空間用以分配。所以,設計這個函數就是一個提升空間配置器效率的一個方法。如果并不想設計這個策略,也可以把句柄函數初始化為0。

__TRACE_DEBUG使用

  對于內存池的內部實現過程共還是比較復雜的,雖然代碼量,函數比較簡單。但是調用過程可能比較復雜。這時,如果我們選擇debug調試,過程會相當的繁瑣,需要仔細記錄調用堆棧過程以及數據流向,邏輯變更等。對于樓主這種水貨來說,估計完事就要苦了。

  所以,就__TRACE_DEBUG使用進行跟蹤,打印數據流向,邏輯走向,文件,函數,方法,行位置。那么我們就能根據這個記錄進行程序的排錯以及調優了。

1 //通過__TRACE_DEBUG做白盒測試 2 3 //#define __DEBUG__ 4 static string GetFileName(const string& path) 5 { 6 char ch = '/'; 7 8 #ifdef _WIN32 9 ch = '\\'; 10 #endif 11 12 size_t pos = path.rfind(ch); 13 if (pos == string::npos) 14 return path; 15 else 16 return path.substr(pos + 1); 17 } 18 19 // 用于調試追溯的trace log 20 inline static void __trace_debug(const char* function, 21 const char * filename, int line, char* format, ...) 22 { 23 // 讀取配置文件 24 #ifdef __DEBUG__ 25 // 輸出調用函數的信息 26 fprintf(stdout, "【 %s:%d】%s", GetFileName(filename).c_str(), line, function); 27 28 // 輸出用戶打的trace信息 29 va_list args; 30 va_start(args, format); 31 vfprintf(stdout, format, args); 32 va_end(args); 33 #endif 34 } 35 36 #define __TRACE_DEBUG(...) __trace_debug(__FUNCTION__ , __FILE__, __LINE__, __VA_ARGS__);

4、二級空間配置器

二級空間配置器的實現就比較復雜了,主要由內存池以及伙伴系統:自由鏈表組成。

1 template<bool threads, int inst> 2 class DefaultAllocTemplate 3 { 4 enum { __ALIGN = 8 }; //(排列基準值,即排列間隔) 5 enum { __MAX_BYTES = 128 }; //最大值 6 enum { __NFREELISTS = __MAX_BYTES / __ALIGN }; //排列鏈 7 public: 8 static size_t FreeList_index(size_t n) //計算應該去取內存塊的相應位置,對齊 9 { 10 return (n + __ALIGN - 1) / __ALIGN - 1; 11 } 12 static size_t Round_up(size_t bytes) //對齊 13 { 14 return ((bytes + __ALIGN - 1) & ~(__ALIGN - 1)); 15 } 16 17 // 到內存池申請nobjs個對象 18 static char* ChunkAlloc(size_t bytes, size_t& nobjs) 19 { 20 size_t needBytes = bytes * nobjs; 21 size_t leftBytes = _endfree - _startfree; 22 if (needBytes <= leftBytes) 23 { 24 __TRACE_DEBUG("狹義內存池有足夠%u個對象\n", nobjs); 25 26 char* ret = _startfree; 27 _startfree += needBytes; 28 return ret; //申請到20個 29 } 30 else if(leftBytes > bytes){ 31 nobjs = leftBytes / needBytes; 32 __TRACE_DEBUG("狹義內存池只有%u個對象\n", nobjs); 33 34 char* ret = _startfree; 35 _startfree += nobjs; 36 return ret; //申請到1~19個 37 } 38 else 39 { 40 //處理余下的小塊內存 41 if (leftBytes > 0)//掛到自由鏈表上的對應位置(頭插) 42 { 43 size_t index = FreeList_index(leftBytes); 44 ((obj*)_startfree)->_freelistlink = _freeList[index]; 45 _freeList[index] = (obj*)_startfree; 46 } 47 48 //一個也沒有 49 size_t sizeToGet = needBytes * 2 + Round_up(_heapsize >> 4); 50 __TRACE_DEBUG("一個對象都沒有,到系統申請%ubytes\n", sizeToGet); 51 52 _startfree = (char*)malloc(sizeToGet); 53 if (_startfree == NULL) 54 { 55 // 系統已經沒有足夠的內存-盡力為之 56 // 到更大的自由鏈表中去取內存塊,置入內存池 57 for (size_t i = FreeList_index(bytes); i < __NFREELISTS; i++) 58 { 59 if (_freeList[i]) //(頭刪) 60 { 61 _startfree = (char*)_freeList[i]; 62 _freeList[i] = ((obj*)_startfree)->_freelistlink; 63 _endfree = _startfree + (i + 1) * __ALIGN; 64 return ChunkAlloc(bytes, nobjs);//重新申請 65 } 66 } 67 // 山窮水盡 -- 求助一級空間配置器 68 //自由鏈表中也沒有分配到內存, 則再到一級配置器中分配內存, 69 //一級配置器中可能有設置的處理內存, 或許能分配到內存。 70 _endfree = NULL; 71 _startfree = (char*)MallocAllocTemplate<0>::Allocate(sizeToGet); 72 } 73 //更新內存池 74 _heapsize += sizeToGet; 75 _endfree = _startfree + sizeToGet; 76 return ChunkAlloc(bytes, nobjs); //重新申請 77 } 78 } 79 // 填充自由鏈表 80 static void* Refill(size_t bytes) 81 { 82 size_t nobjs = 20; 83 __TRACE_DEBUG("到狹義內存池取%u個%ubytes字節的對象\n", nobjs, bytes); 84 85 char* chunk = ChunkAlloc(bytes, nobjs); 86 __TRACE_DEBUG("取到了%u個對象,返回一個對象,將剩余的%u對象掛到自由鏈表的下面\n", nobjs, nobjs - 1); 87 88 if (nobjs == 1)//如果只分配到一塊,直接使用它 89 { 90 return chunk; 91 } 92 size_t index = FreeList_index(bytes); 93 obj *cur = NULL; 94 obj *next = NULL; 95 for (size_t i = 1; i < nobjs; i++) 96 { 97 next = (obj*)(chunk + i*bytes); 98 if (_freeList[index] == NULL) 99 { 100 _freeList[index] = next; 101 } 102 else { 103 cur->_freelistlink = next; 104 } 105 cur = next; 106 } 107 if (cur) 108 { 109 cur->_freelistlink = NULL; 110 } 111 return chunk; 112 } 113 static void* Allocate(size_t n) 114 { 115 __TRACE_DEBUG("二級空間配置器申請%ubytes\n", n); 116 117 if (n>__MAX_BYTES) 118 { 119 return MallocAllocTemplate<0>::Allocate(n); 120 } 121 122 // ps:多線程環境需要考慮加鎖 123 size_t index = FreeList_index(n); //計算n在自由鏈表中的位置 124 if (_freeList[index] == NULL) //自由鏈表為空 125 { 126 __TRACE_DEBUG("在freeList[%u]下面沒有內存塊對象\n", index); 127 128 return Refill(Round_up(0)); //獲取大塊內存插入到自由鏈表中 129 } 130 else{ //自由鏈表不為空,取第一塊,類似于頭刪 131 __TRACE_DEBUG("在freeList[%u]取一個內存塊對象\n", index); 132 133 obj* result = _freeList[index]; 134 _freeList[index] = result->_freelistlink; 135 return result; 136 } 137 } 138 static void Dellocate(void* ptr, size_t n) 139 { 140 141 if (n > __MAX_BYTES) 142 { 143 MallocAllocTemplate<0>::Dellocate(ptr, n); 144 return; 145 } 146 147 // ps:多線程環境需要考慮加鎖 148 size_t index = FreeList_index(n); 149 __TRACE_DEBUG("將釋放的內存塊對象掛到freeList[%u]\n", index); 150 151 if (ptr) 152 { 153 obj* back = (obj*)ptr; //將釋放的內存塊對象掛到_freeList[index]上,類似于頭插 154 back->_freelistlink = _freeList[index]; 155 _freeList[index] = back; 156 } 158 } 159 protected: 160 //定義自由鏈表 161 union obj 162 { 163 obj* _freelistlink; //指向下一個內存塊的指針 164 char clientdata[1]; /* The client sees this. */ 165 }; 166 static obj* _freeList[__NFREELISTS];//自由鏈表 167 168 //狹義內存池 169 static char* _startfree; //內存池水位線開始處 170 static char* _endfree; //內存池水位線結束處 171 static size_t _heapsize; //從系統堆分配的總大小 172 };

代碼中邏輯明了,而且已經注釋得很清楚了,這里就不在贅述了。

1 template <bool threads, int inst> 2 char* DefaultAllocTemplate<threads, inst>::_startfree = 0; 3 4 template <bool threads, int inst> 5 char* DefaultAllocTemplate<threads, inst>::_endfree = 0; 6 7 template <bool threads, int inst> 8 size_t DefaultAllocTemplate<threads, inst>::_heapsize = 0; 9 10 template <bool threads, int inst> 11 typename DefaultAllocTemplate<threads, inst>::obj* DefaultAllocTemplate<threads, inst>::_freeList[__NFREELISTS] = { 0 }; 12 13 #ifdef __USE_MALLOC 14 typedef MallocAllocTemplate<0> alloc; 15 #else 16 typedef DefaultAllocTemplate<false, 0> alloc; 17 #endif //__USE_MALLOC 18 19 template<class T, class Alloc> 20 class SimpleAlloc 21 { 22 public: 23 static T* Allocate(size_t n) 24 { 25 return 0 == n ? 0 : (T*)Alloc::Allocate(n * sizeof(T)); 26 } 27 28 static T* Allocate(void) 29 { 30 return (T*)Alloc::Allocate(sizeof(T)); 31 } 32 33 static void Dellocate(T *p, size_t n) 34 { 35 if (0 != n) 36 Alloc::Dellocate(p, n * sizeof(T)); 37 } 38 39 static void Dellocate(T *p) 40 { 41 Alloc::Dellocate(p, sizeof(T)); 42 } 43 }; 44 45 void TestAlloc1() 46 { 47 void *p1 = DefaultAllocTemplate<false, 0>::Allocate(200); 48 DefaultAllocTemplate<false, 0>::Dellocate(p1, 200); 49 50 void *p2 = DefaultAllocTemplate<false, 0>::Allocate(25); 51 void *p3 = DefaultAllocTemplate<false, 0>::Allocate(25); 52 53 DefaultAllocTemplate<false, 0>::Dellocate(p2, 25); 54 DefaultAllocTemplate<false, 0>::Dellocate(p3, 25); 55 } 56 57 void TestAlloc2() 58 { 59 cout << " 測試系統堆內存耗盡 " << endl; 60 61 DefaultAllocTemplate<false, 0>::Allocate(1024 * 1024 * 1024); 62 //DefaultAllocTemplate<false, 0>::Allocate(1024 * 1024 * 512); 63 //DefaultAllocTemplate<false, 0>::Allocate(1024 * 1024); 64 65 // 不好測試,說明系統管理小塊內存的能力還是很強的。 66 for (int i = 0; i < 1000; ++i) 67 { 68 DefaultAllocTemplate<false, 0>::Allocate(128); 69 } 70 }

?TestAlloc1()

TestAlloc2()

5、空間配置器存在的問題

(1)貌似二級空間配置器中的空間重頭到尾都沒看到他歸還給系統。那么問題就是,內存池空間何時釋放?

對于這個問題,在回頭瀏覽一下源碼及結構圖,你就會發現,大于128的內存,客戶程序Deallocate之后會調free釋放掉,歸還給了系統。

  但是,內存池中獲取的空間,最終,假定用戶都調用Dealloc釋放調了,那么他們又在哪里呢?

    沒有還給系統,沒有在內存池,在自由鏈表中。

Got it:程序中不曾釋放,只是在自由鏈表中,且配置器的所有方法,成員都是靜態的,那么他們就是存放在靜態區。釋放時機就是程序結束。

?(2)如果需要釋放,那么應該怎么處理呢?

  因為真正可以在程序運行中就歸還系統的只有自由鏈表中的未使用值,但是他們并不一定是連續的(用戶申請空間,釋放空間順序的不可控制性),所以想要在合適時間(eg一級配置器的handler中釋放,或者設置各閥值,分配空間量到達時處理),就必須保證釋放的空間要是連續的。保證連續的方案就是:跟蹤分配釋放過程,記錄節點信心。釋放時,僅釋放連續的大塊。

?(3)STL空間配置器的效率

  既然已經存在,而又被廣泛使用,那么,整體的效率,以及和STL內部容器之間的使用配合還是沒問題的。什么時候使用空間配置器最高效呢?就是你使用進行類似于容器中頻繁的插入刪除操作,而頻繁的操作小塊內存時,配置器就是比較高效的了,它的時間復雜度這個時候可以達到O(1)。而且避免了外碎片問題。

但是,我們考慮幾種情況:

  a. 用戶只需要無限的char類型空間,然而配置器中卻對齊到8,于是乎,整個程序中就會有7/8的空間浪費。

  b.對于假定用戶申請N次8空間,將系統資源耗到一定程度,然后全部釋放了,自由鏈表中的空間都是連續的。卻沒有釋放。

    但是:用戶需要申請大于8的空間時,卻依舊沒有空間可用。

總之:這個問題就是,空間可能全部積攢在小塊自由鏈表中,卻沒有用戶可用的。這就尷尬了。

?附:利用空間配置器給容器分配空間

1 //myList.h 2 #pragma once 3 4 #include "myAllocator.h" 5 6 template<class T> 7 struct ListNode 8 { 9 ListNode<T>* _prev; 10 ListNode<T>* _next; 11 12 T _data; 13 }; 14 15 template<class T, class Ref, class Ptr> 16 struct ListIterator 17 { 18 typedef ListNode<T> Node; 19 typedef ListIterator<T, Ref, Ptr> Self; 20 Node* _node; 21 22 ListIterator(Node* node) 23 :_node(node) 24 {} 25 Ref operator*() 26 { 27 return _node->_data; 28 } 29 Ptr operator->() 30 { 31 return &(operator*()); 32 } 33 Self& operator++() 34 { 35 _node = _node->_next; 36 return *this; 37 } 38 Self operator++(int) 39 { 40 Self tmp(*this); 41 _node = _node->_next; 42 return tmp; 43 } 44 Self& operator--() 45 { 46 _node = _node->_prev; 47 return *this; 48 } 49 Self operator--(int) 50 { 51 Self tmp(*this); 52 _node = _node->_prev; 53 return tmp; 54 } 55 bool operator==(const Self& s) const 56 { 57 return _node = s._node; 58 } 59 bool operator!=(const Self& s) const 60 { 61 return _node != s._node; 62 } 63 }; 64 65 template<class T, class Alloc = alloc> 66 class List 67 { 68 typedef ListNode<T> Node; 69 typedef SimpleAlloc<Node, Alloc> ListAllocator; 70 public: 71 typedef ListIterator<T, T&, T*> Iterator; 72 typedef ListIterator<T, const T&, const T*> ConstIterator; 73 74 Iterator Begin() 75 { 76 return _head->_next; 77 } 78 ConstIterator Begin() const 79 { 80 return _head->_next; 81 } 82 Iterator End() 83 { 84 return _head; 85 } 86 ConstIterator End() const 87 { 88 return _head; 89 } 90 91 List() 92 { 93 _head = Create(T()); 94 _head->_next = _head; 95 _head->_prev = _head; 96 } 97 Node* Create(const T&x) 98 { 99 Node* _node = ListAllocator::Allocate();//申請空間 100 new(&_node->_data)T(x); //構造對象 101 102 _node->_data = x; 103 _node->_next = NULL; 104 _node->_prev = NULL; 105 106 return _node; 107 } 108 ~List() 109 { 110 Clear(); 111 DestoryNode(_head); 112 _head = NULL; 113 } 114 void Clear() 115 { 116 Node* cur = _head->_next; 117 while (cur!=_head) 118 { 119 Node* next = cur->_next; 120 DestoryNode(cur); 121 cur = next; 122 } 123 _head->_next = _head; 124 _head->_prev = _head; 125 } 126 void DestoryNode(Node* node) 127 { 128 (&node->_data)->~T(); 129 ListAllocator::Dellocate(node); 130 } 131 132 void PushBack(const T& x) 133 { 134 Node* _tail = _head->_prev; 135 Node* tmp = Create(x); 136 137 _tail->_next = tmp; 138 tmp->_prev = _tail; 139 _head->_prev = tmp; 140 tmp->_next = _head; 141 142 Insert(--End(), x); 143 } 144 void PushFront(const T& x) 145 { 146 Insert(Begin(), x); 147 } 148 void Insert(Iterator it, const T& x) 149 { 150 Node* pos = it._node; 151 Node* tmp = Create(x); 152 Node* prev = pos->_prev; 153 assert(pos); 154 155 prev->_next = tmp; 156 tmp->_prev = prev; 157 tmp->_next = pos; 158 pos->_prev = tmp; 159 } 160 void PopBack() 161 { 162 Erase(--End()); 163 } 164 void PopFront() 165 { 166 Erase(Begin()); 167 } 168 Iterator Erase(Iterator& it) 169 { 170 Node* pos = it._node; 171 Node* prev = pos->_prev; 172 Node* next = pos->_next; 173 assert(pos&&pos != _head); 174 175 prev->_next = next; 176 next->_prev = prev; 177 178 DestoryNode(pos); 179 it._node = prev; 180 return next; 181 } 182 private: 183 Node* _head; 184 }; 185 186 void Print(const List<int>& l1) 187 { 188 List<int>::ConstIterator it = l1.Begin(); 189 while (it != l1.End()) 190 { 191 cout << *it << " "; 192 it++; 193 } 194 cout << endl; 195 } 196 197 void TestList1() 198 { 199 List<int> l1; 200 l1.PushBack(10); 201 l1.PushBack(12); 202 l1.PushBack(13); 203 l1.PushBack(19); 204 l1.PushBack(14); 205 l1.PushBack(17); 206 Print(l1); 207 List<int>::Iterator it2 = l1.Begin(); 208 while (it2!=l1.End()) 209 { 210 if (*it2%2==0) 211 { 212 l1.Erase(it2); 213 } 214 *it2++; 215 } 216 Print(l1); 217 } 218 219 void TestList2() 220 { 221 List<string> l2; 222 l2.PushBack("ping"); 223 l2.PushBack("z"); 224 l2.PushBack("987"); 225 l2.PushFront("1111"); 226 l2.PushFront("2344"); 227 l2.PushFront("22222222222222"); 228 l2.PushFront("34567"); 229 List<string>::Iterator it = l2.Begin(); 230 while (it != l2.End()) 231 { 232 cout << *it << " "; 233 ++it; 234 } 235 cout << endl; 236 } 1 //myVector.h 2 #pragma once 3 #include "myAllocator.h" 4 5 template<class T, class Alloc = alloc> 6 class Vector 7 { 8 typedef SimpleAlloc<T, Alloc> DataAllocator; 9 public: 10 typedef T* Iterator; 11 typedef const T* ConstIterator; 12 13 Vector() 14 :_start(NULL) 15 ,_finish(NULL) 16 ,_endofStorage(NULL) 17 {} 18 ~Vector() 19 { 20 for (size_t i=0;i<Size();i++) 21 { 22 (_start + i)->~T(); 23 } 24 DataAllocator::Dellocate(_start, Capacity()); 25 } 26 void PushBack(const T& x) 27 { 28 if (_finish == _endofStorage) 29 { 30 size_t size = Size(); 31 size_t newsize = size ? size * 2 : 3; 32 Expand(newsize); 33 } 34 35 new(_finish)T(x); 36 ++_finish; 37 } 38 void Expand(size_t n) 39 { 40 if (n > Capacity()) 41 { 42 size_t size = Size(); 43 size_t capacity = Capacity(); 44 T* tmp = DataAllocator::Allocate(n); 45 for (size_t i= 0;i<size;++i) 46 { 47 new(tmp + i)T(_start[i]); 48 (_start + i)->~T(); 49 } 50 51 DataAllocator::Dellocate(_start, capacity); 52 _start = tmp; 53 _finish = _start + size; 54 _endofStorage = _start + capacity; 55 } 56 } 57 void Reserver(size_t n) 58 { 59 Expand(n); 60 } 61 inline size_t Size() 62 { 63 return _finish - _start; 64 } 65 inline size_t Capacity() 66 { 67 return _endofStorage - _start; 68 } 69 Iterator Begin() 70 { 71 return _start; 72 } 73 Iterator End() 74 { 75 return _finish; 76 } 77 T& operator[](size_t pos) 78 { 79 assert(pos < Size()); 80 return _start[pos]; 81 } 82 const T& operator[](size_t pos) const 83 { 84 assert(pos < Size()); 85 return _start[pos]; 86 } 87 protected: 88 Iterator _start; //指向順序表頭 89 Iterator _finish; //指向指向順序表最后一個元素的下一個位置 90 Iterator _endofStorage; //指向順序表尾部 91 }; 92 93 void TestVector1() 94 { 95 Vector<int> v; 96 v.PushBack(0); 97 v.PushBack(1); 98 v.PushBack(2); 99 v.PushBack(3); 100 v.PushBack(4); 101 102 Vector<int>::Iterator it = v.Begin(); 103 while (it != v.End()) 104 { 105 cout << *it << " "; 106 ++it; 107 } 108 cout << endl; 109 } 110 111 void TestVector2() 112 { 113 Vector<string> v1; 114 v1.PushBack("1111"); 115 v1.PushBack("2222"); 116 v1.PushBack("3333"); 117 v1.PushBack("3333"); 118 119 for (size_t i = 0; i < v1.Size(); ++i) 120 { 121 cout << v1[i] << " "; 122 } 123 cout << endl; 124 }

?

The end.....

轉載于:https://www.cnblogs.com/33debug/p/7447540.html

總結

以上是生活随笔為你收集整理的STL空间配置器的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。