在分析完nginx的內存池以後,也想了解一下C++的內存管理,因而就很天然得想到STL。html
STL是一個重量級的做品,聽說當時的出現,徹底能夠說得上是一個劃時代意義的做品。ios
泛型、數據結構和算法的分離、底耦合、高複用… 啊,廢話很少說了,再說下去讓人感受像nginx
王婆賣瓜了。算法
啊,還忘了得加上兩位STL大師的名字來聊表個人敬意了。泛型大牛Alexander Stepanov數據結構
和 Meng Lee(李夢--讓人浮想的名字啊)。多線程
以一個簡單的例子開始。數據結構和算法
#include <vector> #include <algorithm> using namespace std; void print( int elem) { cout << elem << ' '; } int main() { vector<int> vec; for (int i = 0; i != 10; ++i) vec.push_back(i); for_each(vec.begin(), vec.end(), print); //請容許我賣弄一點點小特性 cout << endl; return 0; }
咱們想知道的時候, 當vec聲明的時候和push_back的時候,是怎麼分配的。函數
其實對於一個標準的STL 容器,當Vetor<int> vec 的真實語句應該是 vetor<int, allocator<int>>vec,post
allocator是一個標準的配置器,其做用就是爲各個容器管理內存。這裏須要注意的是在SGI STL中,有兩個this
配置器:allocator(標準的)和alloc(本身實現的,很是經典,這篇文章的主要目的就是爲了分析它)。
要寫一個配置器並非很難,最重要的問題是如何分配和回收內存。下面看下一個標準(也許只能稱爲典型)
的配置器的實現:
#include <new>// for new #include <cstddef> // size_t #include <climits> // for unit_max #include <iostream> // for cerr using namespace std; namespace SLD { template <class T> class allocator { public: typedef T value_type; typedef T* pointer; typedef const T* const_pointer; typedef T& reference; typedef const T& const_reference; typedef size_t size_type; typedef ptrdiff_t difference_type; template <class U> struct rebind { typedef allocator<U> other; }; //申請內存 pointer allocate(size_type n, const void* hint = 0) { T* tmp = (T*)(::operator new((size_t)(n * sizeof(T)))); //operator new 和new operator是不一樣的 if (!tmp) cerr << "out of memory"<<endl; return tmp; } //釋放內存 void deallocate(pointer p) { ::operator delete(p); } //構造 void construct(pointer p, const T& value) { new(p) T1(value); } //析構 void destroy(pointer p) { p->~T(); } //取地址 pointer address(reference x) { return (pointer)&x; } const_pointer const_address(const_reference x) { return (const_pointer)&x; } size_type max_size() const { return size_type(UINT_MAX/sizeof(T)); } }; }
注:代碼有比較大的改動,由於主要是爲了理解。
在使用的時候, 只需這樣vector<int, SLD::allocator<int>>vec; 便可。
vetor便會自動調用咱們的配置器分配內存了。
要本身寫個配置器徹底能夠以這個類爲模板。 而須要作的工做即是寫下本身的 allocate和deallocate便可。
其實SGI的allocator 就是這樣直接調用operator new 和::operator delete實現的,不過這樣作的話效率就很
差了。
SGI STL默認的適配器是alloc,因此咱們在聲明一個vector的時候其實是這樣的
vetor<int, alloc<int>>vec. 這個配置器寫得很是經典,下面就來慢慢分析它。
在咱們敲下以下代碼:
CSld* sld = new CSld;
的時候其實幹了兩件事情:(1) 調用::operator new 申請一塊內存(就是malloc了)
(2) 調用了CSld::CSld();
而在SGI中, 其內存分配把這兩步獨立出了兩個函數:allocate 申請內存, construct 調用構造函數。
他們分別在<stl_alloc.h>, <stl_construct.h> 中。
SGI的內存管理比上面所說的更復雜一些, 首先看一些SGI內存管理的幾個主要文件,以下圖所示:
<圖1. SGI 內存管理>
在stl_construct.h中定義了兩個全局函數construct()和destroy()來管理構造和析構。
在stl_allo.h中定義了5個配置器, 咱們如今關心的是malloc_alloc_template(一級)
和default_alloc_template(二級)。在SGI中,若是用了一級配置器,即是直接使用了
malloc()和free()函數,而若是使用了二級適配器,則若是所申請的內存區域大於128b,
直接使用一級適配器,不然,使用二級適配器。
而stl_uninitialized.h中,則定義了一下全局函數來進行大塊內存的申請和複製。
是否是和nginx中的內存池很類似啊,不過複雜多了。
上面說過, SGI STL中, 若是申請的內存區域大於128B的時候,就會調用一級適配器,
而一級適配器的調用也是很是簡單的, 直接用malloc申請內存,用free釋放內存。
可也看下以下的代碼:
class __malloc_alloc_template { private: // oom = out of memroy,當內存不足的時候,我要用下面這兩個函數 static void* _S_oom_malloc(size_t); static void* _S_oom_realloc(void*, size_t); public: //申請內存 static void* allocate(size_t __n) { void* __result = malloc(__n); //若是不足,我有不足的處理方法 if (0 == __result) __result = _S_oom_malloc(__n); return __result; } //直接釋放掉了 static void deallocate(void* __p, size_t /* __n */) { free(__p); } //從新分配內存 static void* reallocate(void* __p, size_t /* old_sz */, size_t __new_sz) { void* __result = realloc(__p, __new_sz); if (0 == __result) __result = _S_oom_realloc(__p, __new_sz); return __result; } //模擬C++的 set_new_handler,函數, //爲何要模擬,由於如今用的是C的內存管理函數。 static void (* __set_malloc_handler(void (*__f)()))() { void (* __old)() = __malloc_alloc_oom_handler; __malloc_alloc_oom_handler = __f; return(__old); } };
好了, 很簡單把,只是對malloc,free, realloc簡單的封裝。
按上文所說的,SGI的 __default_alloc_template 就是一個內存池了。
咱們首先來看一下它的代碼:
template <bool threads, int inst> class __default_alloc_template { private: // Really we should use static const int x = N // instead of enum { x = N }, but few compilers accept the former. enum {_ALIGN = 8};//小塊區域的上界 enum {_MAX_BYTES = 128};//小塊區域的降低 enum {_NFREELISTS = 16}; // _MAX_BYTES/_ALIGN,有多少個區域 /*SGI 爲了方便內存管理, 把128B 分紅16*8 的塊*/ //將Byte調到8的倍數 static size_t _S_round_up(size_t __bytes) { return (((__bytes) + (size_t) _ALIGN-1) & ~((size_t) _ALIGN - 1)); } //管理內存的鏈表,待會會詳細分析這個 union _Obj { union _Obj* _M_free_list_link; char _M_client_data[1]; /* The client sees this. */ }; private: //聲明瞭16個 free_list, 注意 _S_free_list是成員變量 static _Obj* __STL_VOLATILE _S_free_list[_NFREELISTS]; //同了第幾個free_list, 即_S_free_list[n],固然這裏是更具區域大小來計算的 static size_t _S_freelist_index(size_t __bytes) { return (((__bytes) + (size_t)_ALIGN-1)/(size_t)_ALIGN - 1); } // Returns an object of size __n, and optionally adds to size __n free list. static void* _S_refill(size_t __n); // Allocates a chunk for nobjs of size size. nobjs may be reduced // if it is inconvenient to allocate the requested number. static char* _S_chunk_alloc(size_t __size, int& __nobjs); // Chunk allocation state. static char* _S_start_free;//內存池的起始位置 static char* _S_end_free;//內存池的結束位置 static size_t _S_heap_size;//堆的大小 /*這裏刪除一堆多線程的代碼*/ public: //分配內存,容後分析 /* __n must be > 0 */ static void* allocate(size_t __n); //釋放內存,容後分析 /* __p may not be 0 */ static void deallocate(void* __p, size_t __n); //重新分配內存 static void* reallocate(void* __p, size_t __old_sz, size_t __new_sz); } //下面是一些 成員函數的初始值的設定 template <bool __threads, int __inst> char* __default_alloc_template<__threads, __inst>::_S_start_free = 0; template <bool __threads, int __inst> char* __default_alloc_template<__threads, __inst>::_S_end_free = 0; template <bool __threads, int __inst> size_t __default_alloc_template<__threads, __inst>::_S_heap_size = 0; template <bool __threads, int __inst> typename __default_alloc_template<__threads, __inst>::_Obj* __STL_VOLATILE __default_alloc_template<__threads, __inst> ::_S_free_list[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, };
咱們最關心的有三點:1. 內存池的建立。2.內存的分配。 3. 內存的釋放。
在分析內存池的建立以前咱們首先須要看下SGI內存池的結構。
在__default_alloc_template 內部,維護着這樣一個結構體:
union _Obj { union _Obj* _M_free_list_link; char _M_client_data[1]; /* The client sees this. */ };
static _Obj* _S_free_list[]; //我就是這樣用的
其實一個free_list 就是一個鏈表,以下圖所示:
<圖2. free_list的鏈表表示>
這裏須要注意的有兩點:
一:SGI 內部其實維護着16個free-list,對應管理的大小爲8,16,32……128.
二:_Obj是一個union而不是sturct, 咱們知道,union中的全部成員的引用在內存中的位置都是
相同的。這裏咱們用union就能夠把每個節點須要的額外的指針的負擔消除掉。
好比如今我要申請一塊30B的空間,我要怎麼申請呢?
首先會呼叫二級配置器, 調用 allocate,在allocate函數以內, 從對應的32B的鏈表中拿出空間。
若是對應的鏈表空間不足,就會先用填充至32B,而後用refill()沖洗填充該鏈表。
相應的代碼以下:
static void* allocate(size_t __n) { void* __ret = 0; if (__n > (size_t) _MAX_BYTES) { //若是大於128B, 直接調用一級配置器 __ret = malloc_alloc::allocate(__n); } else { //找出 16個free-list 中的一個 _Obj* __STL_VOLATILE* __my_free_list = _S_free_list + _S_freelist_index(__n); _Obj* __RESTRICT __result = *__my_free_list; if (__result == 0) //若是滿了,則我refill整一個鏈表 __ret = _S_refill(_S_round_up(__n)); else { *__my_free_list = __result -> _M_free_list_link; __ret = __result; } } return __ret; };
下面畫了一張圖來幫助理解:
<圖3. GetMemory>
有內存的分配,固然得要釋放了,下面就來看看是如何釋放的:
static void deallocate(void* __p, size_t __n) { if (__n > (size_t) _MAX_BYTES) //若是大於128,直接釋放 malloc_alloc::deallocate(__p, __n); else { //找到對應的鏈表 _Obj* __STL_VOLATILE* __my_free_list = _S_free_list + _S_freelist_index(__n); _Obj* __q = (_Obj*)__p; //回收,該鏈表 __q -> _M_free_list_link = *__my_free_list; *__my_free_list = __q; // lock is released here } }
前面說過,在分配內存時候若是空間不足會調用_S_refill函數,從新填充空間(ps:若是這是第一個的話,
就是建立了)。而_S_refill最終調用的又是chunk_alloc函數從內存池中提取內存空間。
首先咱們看一下它的源代碼:
/* We allocate memory in large chunks in order to avoid fragmenting */ /* the malloc heap too much. */ /* We assume that size is properly aligned. */ /* We hold the allocation lock. */ template <bool __threads, int __inst> char* __default_alloc_template<__threads, __inst>::_S_chunk_alloc(size_t __size, int& __nobjs) { char* __result; size_t __total_bytes = __size * __nobjs;//申請的總內存空間 size_t __bytes_left = _S_end_free - _S_start_free;//內存池剩餘的內存空間 if (__bytes_left >= __total_bytes) { //若是你能知足我 __result = _S_start_free; _S_start_free += __total_bytes; 00ff">return(__result); } else if (__bytes_left >= __size) { //若是能知足我一塊或一塊以上,參考__Obj這個聯合體(free_list) __nobjs = (int)(__bytes_left/__size); __total_bytes = __size * __nobjs; __result = _S_start_free; _S_start_free += __total_bytes; return(__result); } else { //若是連一塊都給不出 size_t __bytes_to_get = 2 * __total_bytes + _S_round_up(_S_heap_size >> 4); // Try to make use of the left-over piece. if (__bytes_left > 0) { _Obj* __STL_VOLATILE* __my_free_list = _S_free_list + _S_freelist_index(__bytes_left); ((_Obj*)_S_start_free) -> _M_free_list_link = *__my_free_list; *__my_free_list = (_Obj*)_S_start_free; } .//從堆空間從新分配內存 _S_start_free = (char*)malloc(__bytes_to_get); if (0 == _S_start_free) { //連堆都沒有內存了 size_t __i; _Obj* __STL_VOLATILE* __my_free_list; _Obj* __p; // Try to make do with what we have. That can't // hurt. We do not try smaller requests, since that tends // to result in disaster on multi-process machines. for (__i = __size; __i <= (size_t) _MAX_BYTES; __i += (size_t) _ALIGN) { __my_free_list = _S_free_list + _S_freelist_index(__i); __p = *__my_free_list; if (0 != __p) { *__my_free_list = __p -> _M_free_list_link; _S_start_free = (char*)__p; _S_end_free = _S_start_free + __i; return(_S_chunk_alloc(__size, __nobjs)); // Any leftover piece will eventually make it to the // right free list. } } _S_end_free = 0; // In case of exception. //調用一級配置器,主要是爲了調用_S_oom_malloc壓榨出內存來 _S_start_free = (char*)malloc_alloc::allocate(__bytes_to_get); // This should either throw an // exception or remedy the situation. Thus we assume it // succeeded. } //更改一下內存池 _S_heap_size += __bytes_to_get; _S_end_free = _S_start_free + __bytes_to_get; return(_S_chunk_alloc(__size, __nobjs)); } }
區間[_S_start_free, _S_end_free)即是內存池的總空間(參考類:__default_alloc_template的定義)。
當申請一塊內存時候,若是內存池總內存量充足,直接分配,否則就各有各的處理方法了。
下面舉一個例子來簡單得說明一下:
1. 當第一次調用chunk_alloc(32,10)的時候,表示我要申請10塊__Obje(free_list), 每塊大小32B,
此時,內存池大小爲0,從堆空間申請32*20的大小的內存,把其中32*10大小的分給free_list[3](參考圖3)。
2. 我再次申請64*5大小的空間,此時free_list[7]爲0, 它要從內存池提取內存,而此時內存池剩下320B,
恰好填充給free_list[7],內存池此時大小爲0。
3. 我第三次神奇一耳光72*10大小的空間,此時free_list[8]爲0,它要從內存池提取內存,此時內存池空間
不足,再次從堆空間申請72*20大小的空間,分72*10給free_list用。
整一個SGI內存分配的大致流程就是這樣了。
SIG的內存池比nginx中的複雜多了。簡單得分析一下+寫這篇文章花了我整整3個晚上的時間。
啊,個人青春啊。