STL源碼剖析-配置器(記憶體 Clerk)

來源:互聯網
上載者:User

標籤:stl記憶體 Clerk


全域的構造和解構函式:
template <class _T1, class _T2>inline void _Construct(_T1* __p, const _T2& __value) {  new ((void*) __p) _T1(__value);                placement new運算子:在已指派的記憶體上建構函式對象。opreator new ,new opreator,placement new區別請<a target=_blank href="http://www.cnblogs.com/luxiaoxun/archive/2012/08/10/2631812.html">Google</a>}template <class _T1>inline void _Construct(_T1* __p) {  new ((void*) __p) _T1();}template <class _Tp>inline void _Destroy(_Tp* __pointer) {  __pointer->~_Tp();}template <class _ForwardIterator>void__destroy_aux(_ForwardIterator __first, _ForwardIterator __last, __false_type){  for ( ; __first != __last; ++__first)    destroy(&*__first);}template <class _ForwardIterator> inline void __destroy_aux(_ForwardIterator, _ForwardIterator, __true_type) {}template <class _ForwardIterator, class _Tp>inline void __destroy(_ForwardIterator __first, _ForwardIterator __last, _Tp*){  typedef typename __type_traits<_Tp>::has_trivial_destructor          _Trivial_destructor;  __destroy_aux(__first, __last, _Trivial_destructor());}template <class _ForwardIterator>inline void _Destroy(_ForwardIterator __first, _ForwardIterator __last) {  __destroy(__first, __last, __VALUE_TYPE(__first));}
inline void _Destroy(char*, char*) {}inline void _Destroy(int*, int*) {}inline void _Destroy(long*, long*) {}inline void _Destroy(float*, float*) {}inline void _Destroy(double*, double*) {}#ifdef __STL_HAS_WCHAR_Tinline void _Destroy(wchar_t*, wchar_t*) {}



全域複製和填充大塊資料函數:
檔案:stl_uninitialized.h分析眾多函數中的一個:uninitialized_copy   函數。這個函數一共有三個版本:一個模板函數,一個char類型重載,一個wchar類型重載。具體請看。char類型重載:inline char* uninitialized_copy(const char* __first, const char* __last,char* __result) {  memmove(__result, __first, __last - __first);  return __result + (__last - __first);}wchar類型重載:inline wchar_t* uninitialized_copy(const wchar_t* __first, const wchar_t* __last,wchar_t* __result){  memmove(__result, __first, sizeof(wchar_t) * (__last - __first));  return __result + (__last - __first);}模板類型:用到了類型特性萃取template <class _InputIter, class _ForwardIter>inline _ForwardIter  uninitialized_copy(_InputIter __first, _InputIter __last, _ForwardIter __result){  return __uninitialized_copy(__first, __last, __result,__VALUE_TYPE(__result));}template <class _InputIter, class _ForwardIter, class _Tp>inline _ForwardIter__uninitialized_copy(_InputIter __first, _InputIter __last,_ForwardIter __result, _Tp*){  typedef typename __type_traits<_Tp>::is_POD_type _Is_POD;  return __uninitialized_copy_aux(__first, __last, __result, _Is_POD());}template <class _InputIter, class _ForwardIter>inline _ForwardIter __uninitialized_copy_aux(_InputIter __first, _InputIter __last,_ForwardIter __result,__true_type){  return copy(__first, __last, __result);}template <class _InputIter, class _ForwardIter>_ForwardIter __uninitialized_copy_aux(_InputIter __first, _InputIter __last,                         _ForwardIter __result,                         __false_type){  _ForwardIter __cur = __result;  __STL_TRY {    for ( ; __first != __last; ++__first, ++__cur)      _Construct(&*__cur, *__first);    return __cur;  }  __STL_UNWIND(_Destroy(__result, __cur));}

一二級記憶體 Clerk(使用malloc)

具體的見STL源碼檔案:stl_alloc.h

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

STL源碼剖析-配置器(記憶體 Clerk)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.