• <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>

            Zero Lee的專欄

            關(guān)于STL allocator

            關(guān)于STL 中allocator的接口與實現(xiàn),C++標準有比較清楚的定義:
            http://en.wikipedia.org/wiki/Allocator_(C%2B%2B) 

            1. 在GNU C++中,STL allocator嚴格遵守C++的標準:
            看一下的代碼:(定義在bits/allocator.h文件中)
             1 namespace std
             2 {
             3   template<typename _Tp>
             4     class allocator;
             5 
             6   template<>
             7     class allocator<void>
             8     {
             9     public:
            10       typedef size_t      size_type;
            11       typedef ptrdiff_t   difference_type;
            12       typedef void*       pointer;
            13       typedef const void* const_pointer;
            14       typedef void        value_type;
            15 
            16       template<typename _Tp1>
            17         struct rebind
            18         { typedef allocator<_Tp1> other; };
            19     };
            20 
            21   /**
            22    *  @brief  The "standard" allocator, as per [20.4].
            23    *
            24    *  (See @link Allocators allocators info @endlink for more.)
            25    */
            26   template<typename _Tp>
            27     class allocator: public ___glibcxx_base_allocator<_Tp>
            28     {
            29    public:
            30       typedef size_t     size_type;
            31       typedef ptrdiff_t  difference_type;
            32       typedef _Tp*       pointer;
            33       typedef const _Tp* const_pointer;
            34       typedef _Tp&       reference;
            35       typedef const _Tp& const_reference;
            36 
            37       typedef _Tp        value_type;
            38 
            39       template<typename _Tp1>
            40         struct rebind
            41         { typedef allocator<_Tp1> other; };
            42 
            43       allocator() throw() { }
            44 
            45       allocator(const allocator& a) throw()
            46       : ___glibcxx_base_allocator<_Tp>(a) { }
            47 
            48       template<typename _Tp1>
            49         allocator(const allocator<_Tp1>&) throw() { }
            50 
            51       ~allocator() throw() { }
            52 
            53       // Inherit everything else.
            54     };
            55 
            56   template<typename _T1, typename _T2>
            57     inline bool
            58     operator==(const allocator<_T1>&, const allocator<_T2>&)
            59     { return true; }
            60 
            61   template<typename _T1, typename _T2>
            62     inline bool
            63     operator!=(const allocator<_T1>&, const allocator<_T2>&)
            64     { return false; }
            65 
            66   // Inhibit implicit instantiations for required instantiations,
            67   // which are defined via explicit instantiations elsewhere.
            68   // NB: This syntax is a GNU extension.
            69 #if _GLIBCXX_EXTERN_TEMPLATE
            70   extern template class allocator<char>;
            71 
            72   extern template class allocator<wchar_t>;
            73 #endif
            74 
            75   // Undefine.
            76 #undef ___glibcxx_base_allocator
            77 } // namespace std
            78 

            template ___glibcxx_base_allocator 定義在具體的平臺相關(guān)的頭文件中,例如i386-redhat-linux/bits/c++allocator.h:
            可以看出GNU c++的allocator其實采用的是new/delete-based allocation.

             1 namespace __gnu_cxx
             2 {
             3   /**
             4    *  @brief  An allocator that uses global new, as per [20.4].
             5    *
             6    *  This is precisely the allocator defined in the C++ Standard.
             7    *    - all allocation calls operator new
             8    *    - all deallocation calls operator delete
             9    *
            10    *  (See @link Allocators allocators info @endlink for more.)
            11    */
            12   template<typename _Tp>
            13     class new_allocator
            14     {
            15     public:
            16       typedef size_t     size_type;
            17       typedef ptrdiff_t  difference_type;
            18       typedef _Tp*       pointer;
            19       typedef const _Tp* const_pointer;
            20       typedef _Tp&       reference;
            21       typedef const _Tp& const_reference;
            22       typedef _Tp        value_type;
            23 
            24       template<typename _Tp1>
            25         struct rebind
            26         { typedef new_allocator<_Tp1> other; };
            27 
            28       new_allocator() throw() { }
            29 
            30       new_allocator(const new_allocator&) throw() { }
            31 
            32       template<typename _Tp1>
            33         new_allocator(const new_allocator<_Tp1>&) throw() { }
            34 
            35       ~new_allocator() throw() { }
            36 
            37 
            38       pointer
            39       address(reference __x) const { return &__x; }
            40 
            41       const_pointer
            42       address(const_reference __x) const { return &__x; }
            43 
            44       // NB: __n is permitted to be 0.  The C++ standard says nothing
            45       // about what the return value is when __n == 0.
            46       pointer
            47       allocate(size_type __n, const void* = 0)
            48       { return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp))); }
            49 
            50       // __p is not permitted to be a null pointer.
            51       void
            52       deallocate(pointer __p, size_type)
            53       { ::operator delete(__p); }
            54 
            55       size_type
            56       max_size() const throw()
            57       { return size_t(-1) / sizeof(_Tp); }
            58 
            59       // _GLIBCXX_RESOLVE_LIB_DEFECTS
            60       // 402. wrong new expression in [some_] allocator::construct
            61       void
            62       construct(pointer __p, const _Tp& __val)
            63       { ::new(__p) _Tp(__val); }
            64 
            65       void
            66       destroy(pointer __p) { __p->~_Tp(); }
            67     };
            68 
            69   template<typename _Tp>
            70     inline bool
            71     operator==(const new_allocator<_Tp>&, const new_allocator<_Tp>&)
            72     { return true; }
            73 
            74 
            75   template<typename _Tp>
            76     inline bool
            77     operator!=(const new_allocator<_Tp>&, const new_allocator<_Tp>&)
            78     { return false; }
            79 } // namespace __gnu_cxx
            80 

            posted on 2012-06-17 10:37 Zero Lee 閱讀(531) 評論(0)  編輯 收藏 引用

            国产精品久久国产精品99盘| 综合久久久久久中文字幕亚洲国产国产综合一区首 | 色综合久久久久无码专区| 久久精品国产清自在天天线 | 色综合久久中文色婷婷| 久久精品二区| 亚洲色大成网站WWW久久九九| 99re久久精品国产首页2020| 狠狠色综合网站久久久久久久| 久久99精品国产麻豆宅宅| 久久国产精品-久久精品| 久久亚洲精品国产亚洲老地址 | 囯产极品美女高潮无套久久久| 国产情侣久久久久aⅴ免费| 午夜精品久久久久久久无码| 无码人妻少妇久久中文字幕蜜桃 | 久久妇女高潮几次MBA| 久久香蕉国产线看观看99| 久久无码高潮喷水| 久久91亚洲人成电影网站| 中文无码久久精品| 欧美粉嫩小泬久久久久久久| 国产成人久久精品区一区二区| 久久只这里是精品66| 久久精品无码一区二区三区免费 | 97久久综合精品久久久综合| 亚洲午夜精品久久久久久浪潮 | 久久免费精品视频| 久久亚洲国产成人精品性色| 久久精品中文无码资源站| 日产久久强奸免费的看| 精品国产婷婷久久久| 狠狠色丁香婷综合久久| 狠狠88综合久久久久综合网| 亚洲女久久久噜噜噜熟女| 中文字幕久久精品| 亚洲精品WWW久久久久久| 性欧美大战久久久久久久| 久久精品国产亚洲Aⅴ蜜臀色欲| 一本久久久久久久| 成人精品一区二区久久|