STL源码剖析 空间配置器 查漏补缺
生活随笔
收集整理的這篇文章主要介紹了
STL源码剖析 空间配置器 查漏补缺
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
ptrdiff_t含義?
- 減去兩個指針的結(jié)果的帶符號整數(shù)類型
- ptrdiff_t (Type support) - C 中文開發(fā)手冊 - 開發(fā)者手冊 - 云+社區(qū) - 騰訊云
std::set_new_handler()函數(shù)的理解
- 關于set_new_handler的理解_wck0617-CSDN博客
- new分配內(nèi)存的時候? 如果分配的空間不足 采取什么樣的措施
自己實現(xiàn)空間配置器
#include <new> //for placement new #include <cstddef> //for ptrdiff_t size_t #include <cstdlib> //for exit #include <climits> //for UINT_MAX #include <iostream> //for cerr #include <vector>namespace Chy{template <class T>inline T* _allocate(ptrdiff_t size,T*){std::set_new_handler(0);T* tmp = (T*)(::operator new((std::size_t)(size * sizeof (T))));if (tmp == 0){std::cerr << "out of memory" << std::endl;exit(1);}return tmp;}template<class T>inline void _deallocate(T* buffer){::operator delete (buffer);}template<class T1,class T2>inline void _construct(T1 *p,const T2& value){new(p) T1 (value); //沒看懂}template <class T>inline void _destroy(T* ptr){ptr->~T();}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 std::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){return _allocate((difference_type)n,(pointer)0);}void deallocate(pointer p,size_type n){_deallocate(p);}void construct(pointer p,const T& value){_construct(p,value);}void destroy(pointer p){_destroy(p);}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));}}; } int main(){int ia[5] = {0,1,2,3,4};unsigned int i;std::vector<int,Chy::allocator<int>>iv(ia,ia+5);for (int j = 0; j < iv.size(); ++j) {std::cout << iv[j] << " ";}std::cout << std::endl; }- SGI STL 使用了專屬的 擁有層級配置的 特殊的空間配置器
- SGI STL 提供了 一個標準的空間配置器的接口 叫做 simple_alloc
SGI STL 封裝的 特殊的空間配置器 alloc
- 使用的時候 std::vector<int,std::alloc>iv;std::alloc 采用默認的形式
- 為了精密分工,STL a llo c a to r決定將這兩階段操作區(qū)分開來。內(nèi)存配置操作 由 alloc: al locate ()負責,內(nèi)存釋放操作由alloc : : deallocate ()負責;對象構(gòu)造操作由::construct:()負責,對象析構(gòu)操作由:;destroy負責
- new算式內(nèi)含兩階段操作: (1 )調(diào) 用 ::operator new配置內(nèi)存; ⑵ 調(diào) 用Foo::Foo()構(gòu)造對象內(nèi)容。
- delete算式也內(nèi)含兩階段操作:(1)調(diào)用Foo:~Foo ()將對象析構(gòu);(2 ) 調(diào) 用 ::operator delete釋放內(nèi)存。
?type_traits
- c++11——type_traits 類型萃取 - 農(nóng)民伯伯-Coding - 博客園
- C++ STL __type_traits解析 - 知乎
構(gòu)造和析構(gòu)的基本工具 construct()和destroy()
#include <new> //for placement new #include <cstddef> //for ptrdiff_t size_t #include <cstdlib> //for exit #include <climits> //for UINT_MAX #include <iostream> //for cerr #include <vector>using namespace std; template<class T> struct __type_traits {typedef __true_type this_dummy_member_must_be_first;typedef __false_type has_trivial_default_constructor;typedef __false_type has_trivial_copy_constructor;typedef __false_type has_trivial_assignment_constructor;typedef __false_type has_trivial_destructor;typedef __false_type is_POD_type; };template<class T1,class T2> inline void _construct(T1 *p,const T2& value){new(p) T1 (value); //placement new; 調(diào)用T1::T1(value) }/** destroy() 的兩個版本*/ //以下是 destroy() 第一版本 接收一個指針 template <class T> inline void _destroy(T* ptr){ptr->~T(); //調(diào)用 ~T() }/********************************************************************/ //如果元素的型別(value_type)有non_trivial destructor template <class ForwardIterator> inline void __destroy_aux(ForwardIterator first,ForwardIterator last,std::__false_type){for( ; first < last;++first){destroy(&*first); //調(diào)用destroy的第一個版本} } //如果元素的型別(value_type)有trivial destructor template <class ForwardIterator> inline void __destroy_aux(ForwardIterator,ForwardIterator,std::true_type){}//判斷元素的型別(value type) 是否有 trivial destructor template <class ForwardIterator,class T> inline void __destroy(ForwardIterator first,ForwardIterator last,T*){typedef typename __type_traits<T>::has_trivial_destructor trivial_destructor;__destroy_aux(first,last,trivial_destructor()); }//destroy()第二版本 接收兩個迭代器 這個函數(shù)設法找出元素的型別 //進而使用 __Type_traits<> 求取適當?shù)拇胧?template <class ForwardIterator> inline void destroy(ForwardIterator first,ForwardIterator last){ // __destroy() } /********************************************************************//** 以下是destroy() 第二版本針對迭代器為 char* 和 wchar_t 的特化版本*/ inline void destroy(char*,char*){} inline void destroy(wchar_t *,wchar_t *){}- ?第二版本的接受first和last兩個迭代器 準備將[first,last)范圍內(nèi)的元素都析構(gòu)掉,但是如果這段范圍很大,對于每一個元素的析構(gòu) 都 無關痛癢(即 trivial destructor類型的),對于效率是一種損失。
- 因此 需要進行偏特化的操作,在執(zhí)行析構(gòu)函數(shù)之前進行類型的判斷。如果是 true_type 什么都不做直接結(jié)束,如果是false_type的類型,使用循環(huán)的方式,針對每個元素使用 第一個版本的destroy()函數(shù)進行析構(gòu)釋放
-
問題:如何實現(xiàn)上述的 value_type() 和 __type_traits<> ??
空間的配置和釋放
- 對象構(gòu)造之前的空間配置和對象析構(gòu)之后的空間的釋放 由 <stl_alloc.h>負責
設計思路
- 向系統(tǒng)的heap 申請內(nèi)存空間
- 考慮 多線程狀態(tài)
- 考慮內(nèi)存不足的應對措施
- 考慮小型區(qū)塊可能造成的內(nèi)存碎片問題
- 代碼舉例 排除了 多線程的處理
設計思想
- 考慮到小型區(qū)塊造成的內(nèi)存破碎問題,SGI設計了雙層級配置器,第一層級使用malloc()和free(),第二層級視情況 采用不同的策略。
- 當配置的區(qū)塊超過128bytes時 視為足夠大 使用第一層級配置器
- 當配置的區(qū)塊小于128bytes時 為了降低額外的負擔,使用memory pool的方式
- alloc并不接受任何 template型別的參數(shù)
- 無論alloc使用的是第一級或者第二級配置器 都需要外包一層接口
- 內(nèi)部的四個函數(shù)本質(zhì)上是 單純的轉(zhuǎn)接調(diào)用的關系 調(diào)用傳遞給配置器的(可能是第一級別 也可能是 第二級別)
- SGI STL容器都使用 這個simple_alloc的接口
第一級配置器 __malloc_alloc_template剖析
#if 0 # include<new> # define __THROW_BAD_ALLOC throw bad_alloc #elif !defined(__THROW_BAD_ALLOC) # include<iostream> # define __THROW_BAD_ALLOC std::cerr << "out of memory" << std::endl; exit(1); #endif//malloc-based allocator 通常比稍后介紹的default alloc要慢 //但是它是線程安全的 thread-safe,并對于空間的運用比較高效 //以下是第一級別配置器 //無“template 型別參數(shù)” 至于非型別參數(shù) inst 完全沒有用到 template <int inst> class __malloc_alloc_template{ private://以下內(nèi)容都是函數(shù)指針,所代表的函數(shù)用于處理內(nèi)存不足的情況//oom : out of memorystatic void *oom_malloc(std::size_t);static void *oom_realloc(void*,std::size_t);static void (* __malloc_alloc_oom_handler)(); public:static void* allocate(std::size_t n){void * result = malloc(n);//第一級配置器 直接使用malloc進行內(nèi)存的分配//當無法滿足內(nèi)存分配的需求的時候,使用oom_malloc()if(result == 0){result = oom_malloc(n);}return result;}static void deallocate(void* p,size_t /* n */){free(p); //第一級配置器 直接使用 free()進行釋放}static void* reallocate(void* p,size_t /*old_size*/,size_t new_size){void *result = realloc(p,new_size); //第一級別配置器直接使用realloc()//以下無法滿足需求的時候 改用oom_realloc()if (0==result){result = oom_realloc(p,new_size);}return result;}//以下是仿真C++的set_new_handler(),換句話說 你可以通過他指定自己的out_of_handlerstatic void(*set_malloc_handler(void (*f)()))(){void (* old)() = __malloc_alloc_oom_handler;__malloc_alloc_oom_handler = f;return (old);}}; //malloc_alloc out_of_memory handling //初始化為0 template <int inst> void (* __malloc_alloc_template<inst>::__malloc_alloc_oom_handler)() = 0;template <int inst> void* __malloc_alloc_template<inst>::oom_malloc(std::size_t n) {void (*my_malloc_handler)();void * result;for(;;){ //不斷嘗試 釋放、配置、再次釋放、再次配置......my_malloc_handler = __malloc_alloc_oom_handler;if (0 == my_malloc_handler){__THROW_BAD_ALLOC;}(*my_malloc_handler)();//調(diào)用處理例程,企圖釋放內(nèi)存result = malloc(n); //再次配置內(nèi)存if (result){return result;}} }template <int inst> void *__malloc_alloc_template<inst>::oom_realloc(void *p, std::size_t n) {void (* my_malloc_handler)() = __malloc_alloc_oom_handler;void * result;if(0 == my_malloc_handler){__THROW_BAD_ALLOC;}(*my_malloc_handler)();//調(diào)用處理例程,企圖釋放內(nèi)存result = realloc(p,n);//再次嘗試配置內(nèi)存if (result){return result;} }//以下直接將參數(shù) inst 指定為0 typedef __malloc_alloc_template<0>malloc_alloc;- ?第一級配置器 使用malloc、free、realloc等C函數(shù)執(zhí)行內(nèi)存的配置、釋放、重新配置,并實現(xiàn)了類似C++ newhandler的機制 ,但是不能直接使用C++的new handler的機制 因為他并沒有使用 ::operator new的方式來配置內(nèi)存
- C++ 的new handler的機制是 要求系統(tǒng)在內(nèi)存配置需求無法被滿足的時候 調(diào)用一個用戶自定義的函數(shù) 即 ::operator new無法完成任務 拋出std::bad_alloc異常狀態(tài)之前 先調(diào)用客戶指定的處理例程 。這個處理的例程 被稱作new handler
- 第一級配置器? allocate()和 reallocate()都會在調(diào)用malloc 和 realloc不成功之后 改用 oom_malloc 和 oom_realloc 后者函數(shù)內(nèi)設定一個循環(huán),不斷調(diào)用 內(nèi)存不足處理的程序? ,期望某一次可以得到足夠的內(nèi)存。如果未指定例程 便會調(diào)用 __THROW_BAD_ALLOC 拋出bad_alloc異常信息,或者使用 exit(1) 終止程序
第二級配置器__default_alloc_template 剖析
- 第二級配置器 使用機制 避免造成內(nèi)存的碎片?
- 額外負擔 無法避免 涉及到操作系統(tǒng)的內(nèi)存管理;但是區(qū)塊越小,顯現(xiàn)的 額外的負擔的比例就會越來越大,就會顯得浪費
- ?大于128轉(zhuǎn)交第一級配置器,小于128使用內(nèi)存池管理。
- 次層配置:每次配置大內(nèi)存,維護對應的自由鏈表,下次若有相同大小的內(nèi)存需求,直接從自由鏈表中撥出
- 配置器 負責內(nèi)存的釋放和回收?
- 第二級配置器的小額區(qū)塊的內(nèi)存空間是以8的倍數(shù)進行劃分,劃分的時候 會自動將需求量上調(diào)至8的倍數(shù)
- 維護16個free_lists 各自管理 8 16 24 32 40 48 56 64 72 80 88 96 104 112 120 128
- 維護鏈表是需要 額外的指針 造成額外的負擔
- 考慮到 obj 使用的是union,可以視為一個指針 指向相同類型的另外一個obj ; obj也可以視為一個指針 指向?qū)嶋H的區(qū)塊? 一物二用
空間配置函數(shù) allocate()
- 這個函數(shù)首先判斷區(qū)塊的大小 ,大于128 使用第一級配置器,小于128 檢查對應的 free list。如果free list之內(nèi)有可用的區(qū)塊 就直接拿來使用
- 如果沒有可用的區(qū)塊,就將區(qū)塊大小上調(diào)至8的倍數(shù)邊界 使用refill()準備為free list填充空間?
注意事項
- 注意事項:類里面聲明的靜態(tài)函數(shù) 不能帶有{} ,否則后期實現(xiàn)函數(shù)邏輯的時候 編譯不通過
- 只有用戶自定義的類型作為函數(shù)的返回值的時候 需要使用typename關鍵字
?重新填充 free lists
- 當 free list中沒有可用的區(qū)塊的時候 需要調(diào)用 refill() 函數(shù) 為free list重新填充空間,新的空間將取自內(nèi)存池(通過chunk_alloc完成 )
- 缺省獲得 20個新節(jié)點,萬一內(nèi)存池空間不足 獲得的節(jié)點數(shù)可能 小于20
內(nèi)存池
- 從內(nèi)存池取空間 給free list使用? 是chunk alloc函數(shù)的作用
- 使用 end_free - start_free 判斷內(nèi)存池的容量,如果空間充足直接調(diào)出20個區(qū)塊返回給free_list
- 如果不足20個 但是大于1個以上的容量 先提供可以滿足的區(qū)塊 ,遞歸更新 (pass by reference的nobjs修改為實際可以提供的區(qū)塊數(shù))
- 如果一個都不可以滿足? 使用malloc從heap中配置內(nèi)存 為內(nèi)存池注入活水,新水量的大小是需求量的2倍,還需要加上隨著配置次數(shù)增加愈來愈大的附加量
- 例子:假設程序一開始客戶端 就調(diào)用?chunk_alloc(32,20),此刻內(nèi)存池和free_list里面均沒有可用的內(nèi)存空間,所以使用malloc() 配置40個32byte區(qū)塊,使用malloc分配的內(nèi)存空間是需求的兩倍。其中第一個 32byte交出,19個給free_list [3] 進行維護,其余的20個留給內(nèi)存池。
- 客戶端 使用?chunk_alloc(64,20),此刻free_list [7]沒有內(nèi)存 ,向內(nèi)存池提出申請 ;內(nèi)存池此刻具備的內(nèi)存 (32 * 20),所以只可以提供 (32 * 20)/64 = 10個64位區(qū)塊,返回10個區(qū)塊,第一個交給客戶端,剩余的9個由 free_list [7]。此刻內(nèi)存池全空
- 客戶端調(diào)用chunk_alloc(96,20)此刻free_list [11]和內(nèi)存池全為空,需要使用malloc分配內(nèi)存,分配40+n個96bytes區(qū)塊,將第一個交出,另外19個交給free_list [11]維護,其余的20+n 的容量交給內(nèi)存池
- 假設system heap空間不足了 (無法為內(nèi)存池注入 活水),malloc()行動失敗,chunk_malloc()就需要四處尋找合適的內(nèi)存。找到了就挖一塊,這個是從free_list上面去挖內(nèi)存,找不到就需要第一級配置器。
- 第一級配置器本質(zhì)上還是使用malloc() 進行內(nèi)存的配置,考慮到前提 system heap已經(jīng)沒有內(nèi)存了,為啥同樣使用malloc這里就可以了呢?因為 第一配置器具備out-free-memory處理機制(類似new-handler機制) 就有機會去釋放其余地方多余的內(nèi)存,進行內(nèi)存的分配和使用。如果可以就成功,如果不可以就返回bad_malloc的異常。
注意事項:
template<class T,class Alloc> class simple_alloc{}; //SGI 通常使用這種方式來使用配置器 template<class T,class Alloc = alloc> //缺省使用alloc為配置器 class vector{ public:typedef T value_type; protected://專屬空間配置器 每次配置一個元素的大小typedef simple_alloc<value_type,Alloc>data_allocator; };- 其中 第二個template參數(shù)所接受的缺省參數(shù)值alloc 可以是第一級配置器 也可以是第二級配置器
- 不過SGI STL已經(jīng)將其設定為第二級配置器?
完整代碼
#if 0 # include<new> # define __THROW_BAD_ALLOC throw bad_alloc #elif !defined(__THROW_BAD_ALLOC) # include<iostream> # define __THROW_BAD_ALLOC std::cerr << "out of memory" << std::endl; exit(1); #endif//malloc-based allocator 通常比稍后介紹的default alloc要慢 //但是它是線程安全的 thread-safe,并對于空間的運用比較高效 //以下是第一級別配置器 //無“template 型別參數(shù)” 至于非型別參數(shù) inst 完全沒有用到 template <int inst> class __malloc_alloc_template{ private://以下內(nèi)容都是函數(shù)指針,所代表的函數(shù)用于處理內(nèi)存不足的情況//oom : out of memorystatic void *oom_malloc(std::size_t);static void *oom_realloc(void*,std::size_t);static void (* __malloc_alloc_oom_handler)(); public:static void* allocate(std::size_t n){void * result = malloc(n);//第一級配置器 直接使用malloc進行內(nèi)存的分配//當無法滿足內(nèi)存分配的需求的時候,使用oom_malloc()if(result == 0){result = oom_malloc(n);}return result;}static void deallocate(void* p,size_t /* n */){free(p); //第一級配置器 直接使用 free()進行釋放}static void* reallocate(void* p,size_t /*old_size*/,size_t new_size){void *result = realloc(p,new_size); //第一級別配置器直接使用realloc()//以下無法滿足需求的時候 改用oom_realloc()if (0==result){result = oom_realloc(p,new_size);}return result;}//以下是仿真C++的set_new_handler(),換句話說 你可以通過他指定自己的out_of_handlerstatic void(*set_malloc_handler(void (*f)()))(){void (* old)() = __malloc_alloc_oom_handler;__malloc_alloc_oom_handler = f;return (old);}}; //malloc_alloc out_of_memory handling //初始化為0 template <int inst> void (* __malloc_alloc_template<inst>::__malloc_alloc_oom_handler)() = 0;template <int inst> void* __malloc_alloc_template<inst>::oom_malloc(std::size_t n) {void (*my_malloc_handler)();void * result;for(;;){ //不斷嘗試 釋放、配置、再次釋放、再次配置......my_malloc_handler = __malloc_alloc_oom_handler;if (0 == my_malloc_handler){__THROW_BAD_ALLOC;}(*my_malloc_handler)();//調(diào)用處理例程,企圖釋放內(nèi)存result = malloc(n); //再次配置內(nèi)存if (result){return result;}} }template <int inst> void *__malloc_alloc_template<inst>::oom_realloc(void *p, std::size_t n) {void (* my_malloc_handler)() = __malloc_alloc_oom_handler;void * result;if(0 == my_malloc_handler){__THROW_BAD_ALLOC;}(*my_malloc_handler)();//調(diào)用處理例程,企圖釋放內(nèi)存result = realloc(p,n);//再次嘗試配置內(nèi)存if (result){return result;} }//以下直接將參數(shù) inst 指定為0 typedef __malloc_alloc_template<0>malloc_alloc;enum {__ALIGN = 8}; //小型區(qū)塊的上調(diào)邊界 enum {__MAX_BYTES = 128};//小型區(qū)塊的上限 enum {__NFREELISTS = __MAX_BYTES/__ALIGN};//free-lists的個數(shù)using namespace std; //以下是第二級配置器 //注意:無 template型別參數(shù) 且第二參數(shù)哇怒氣按沒有被派上用場 //第一參數(shù)用于多線程的環(huán)境 本示例 暫未涉及 template <bool threads,int inst> class __default_alloc_template{ private://ROUND_UP() 將bytes上調(diào)至8的倍數(shù)static size_t ROUND_UP(size_t bytes){return (((bytes) + __ALIGN - 1) & ~(__ALIGN-1));}private:union obj{ //free-lists 節(jié)點構(gòu)造union obj * free_list_link;char client_data[1];// The client sees this}; private://16個free-listsstatic obj * volatile free_list[__NFREELISTS];//以下函數(shù)根據(jù)區(qū)塊的大小 決定使用第 n 號free-list,n從1開始算起static size_t FREELIST_INDEX(size_t bytes){return (((bytes) + __ALIGN-1)/__ALIGN-1);}//返回一個大小為n的對象 并可能加入大小為n 的其他區(qū)塊到free liststatic void* refill(size_t n);//配置一大塊區(qū)間 可以容納nobjs個“size”大小的區(qū)塊//如果配置nobjs個大小的區(qū)塊有所不方便 ,可以降低nobjsstatic char * chunk_alloc(size_t size,int &nobjs);//Chunk allocation statestatic char *start_free;//內(nèi)存池起始的位置 只在chunk_alloc()中變化static char *end_free;//內(nèi)存池結(jié)束的位置 只在chunk_alloc()中變化static size_t heap_size;public:static void* allocate(size_t n);static void deallocate(void* p,size_t n);static void *reallocate(void* p,size_t old_sz,size_t new_sz); };//以下是static data member 的定義與初值的設定 template <bool threads,int inst> char* __default_alloc_template<threads,inst>::start_free = 0;template <bool threads,int inst> char* __default_alloc_template<threads,inst>::end_free = 0;template <bool threads,int inst> size_t __default_alloc_template<threads,inst>::heap_size = 0;template <bool threads,int inst> typename __default_alloc_template<threads,inst>::obj * volatile __default_alloc_template<threads,inst>::free_list[__NFREELISTS] ={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};template <bool threads,int inst> void* __default_alloc_template<threads,inst>::allocate(size_t n) {obj* volatile *my_free_list;obj* result;//大于128就調(diào)用第一級配置器if(n>(size_t)__MAX_BYTES){return (malloc_alloc::allocate(n));}//尋找16個free lists中適當?shù)囊粋€my_free_list = free_list + FREELIST_INDEX(n);result = *my_free_list;if (result==0){//沒有找到free list,需要重新補充 free listvoid *r = refill(ROUND_UP(n));return r;}//調(diào)整free list*my_free_list = result->free_list_link;return result; }template <bool threads,int inst> void __default_alloc_template<threads,inst>::deallocate(void *p, size_t n) {obj *q = (obj*)p;obj *volatile * my_free_list;//大于128 就調(diào)用第一級配置器if(n > (size_t)__MAX_BYTES){malloc_alloc ::deallocate(p,n);return;}//尋找對應的free listmy_free_list = free_list + FREELIST_INDEX(n);//調(diào)整free list 回收區(qū)塊q->free_list_link = *my_free_list;*my_free_list = q; }//返回一個大小為n的對象,并且有的時候會適當?shù)奈籪ree list增加節(jié)點 //假設n已經(jīng)上調(diào)為8的倍數(shù) template <bool threads,int inst> void* __default_alloc_template<threads,inst>::refill(size_t n) {int nobjs = 20;//調(diào)用chunk_alloc() 嘗試獲取nobjs個區(qū)塊作為free list的新的節(jié)點//注意參數(shù)nobjs是pass by reference的方式char* chunk = chunk_alloc(n,nobjs);obj* volatile * my_free_list;obj* result;obj* current_obj,*next_obj;int i;//如果只獲得一個區(qū)塊 這個區(qū)塊就會被分配給調(diào)用者使用 free list無新的節(jié)點if (1 == nobjs){return chunk;}//否則準備調(diào)整free list 納入新的節(jié)點my_free_list = free_list + FREELIST_INDEX(n);//在chunk空間內(nèi) 建立free listresult = (obj*) chunk; //這一塊準備返回給客戶端//以下導引free list指向新的配置的空間(取自內(nèi)存池)*my_free_list = next_obj = (obj*)(chunk +n);//以下將free list的各個節(jié)點串接起來for(i=1;;i++){ //從1開始 因為第0個將會返回給客戶端current_obj = next_obj;next_obj = (obj*)((char *)next_obj + n);if (nobjs-1==i){current_obj->free_list_link = 0;break;} else{current_obj->free_list_link = next_obj;}}return (result); }//假設n已經(jīng)上調(diào)為8的倍數(shù) //注意參數(shù)nobjs是pass by reference /** size:每一個objs的大小+* nobjs:創(chuàng)建objs的數(shù)量*/ template <bool threads,int inst> char* __default_alloc_template<threads,inst>::chunk_alloc(size_t size, int &nobjs) {char* result;size_t total_bytes = size * nobjs;size_t byte_left = end_free - start_free;//內(nèi)存池剩余空間if (byte_left >= total_bytes){//內(nèi)存池的空間滿足需求量result = start_free;start_free += total_bytes;return result;} else if(byte_left >= size){}}參考鏈接
- STL 源碼剖析 空間配置器_CHYabc123456hh的博客-CSDN博客
總結(jié)
以上是生活随笔為你收集整理的STL源码剖析 空间配置器 查漏补缺的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 管理费用怎么记账
- 下一篇: 渔翁服务器密码机的环境配置