libstdc++
allocator.h
Go to the documentation of this file.
00001 // Allocators -*- C++ -*-
00002 
00003 // Copyright (C) 2001-2019 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 3, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 /*
00026  * Copyright (c) 1996-1997
00027  * Silicon Graphics Computer Systems, Inc.
00028  *
00029  * Permission to use, copy, modify, distribute and sell this software
00030  * and its documentation for any purpose is hereby granted without fee,
00031  * provided that the above copyright notice appear in all copies and
00032  * that both that copyright notice and this permission notice appear
00033  * in supporting documentation.  Silicon Graphics makes no
00034  * representations about the suitability of this software for any
00035  * purpose.  It is provided "as is" without express or implied warranty.
00036  */
00037 
00038 /** @file bits/allocator.h
00039  *  This is an internal header file, included by other library headers.
00040  *  Do not attempt to use it directly. @headername{memory}
00041  */
00042 
00043 #ifndef _ALLOCATOR_H
00044 #define _ALLOCATOR_H 1
00045 
00046 #include <bits/c++allocator.h> // Define the base class to std::allocator.
00047 #include <bits/memoryfwd.h>
00048 #if __cplusplus >= 201103L
00049 #include <type_traits>
00050 #endif
00051 
00052 #define __cpp_lib_incomplete_container_elements 201505
00053 #if __cplusplus >= 201103L
00054 # define __cpp_lib_allocator_is_always_equal 201411
00055 #endif
00056 
00057 namespace std _GLIBCXX_VISIBILITY(default)
00058 {
00059 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00060 
00061   /**
00062    *  @addtogroup allocators
00063    *  @{
00064    */
00065 
00066   /// allocator<void> specialization.
00067   template<>
00068     class allocator<void>
00069     {
00070     public:
00071       typedef size_t      size_type;
00072       typedef ptrdiff_t   difference_type;
00073       typedef void*       pointer;
00074       typedef const void* const_pointer;
00075       typedef void        value_type;
00076 
00077       template<typename _Tp1>
00078         struct rebind
00079         { typedef allocator<_Tp1> other; };
00080 
00081 #if __cplusplus >= 201103L
00082       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00083       // 2103. std::allocator propagate_on_container_move_assignment
00084       typedef true_type propagate_on_container_move_assignment;
00085 
00086       typedef true_type is_always_equal;
00087 
00088       template<typename _Up, typename... _Args>
00089         void
00090         construct(_Up* __p, _Args&&... __args)
00091         noexcept(noexcept(::new((void *)__p)
00092                             _Up(std::forward<_Args>(__args)...)))
00093         { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
00094 
00095       template<typename _Up>
00096         void
00097         destroy(_Up* __p)
00098         noexcept(noexcept(__p->~_Up()))
00099         { __p->~_Up(); }
00100 #endif
00101     };
00102 
00103   /**
00104    * @brief  The @a standard allocator, as per [20.4].
00105    *
00106    *  See https://gcc.gnu.org/onlinedocs/libstdc++/manual/memory.html#std.util.memory.allocator
00107    *  for further details.
00108    *
00109    *  @tparam  _Tp  Type of allocated object.
00110    */
00111   template<typename _Tp>
00112     class allocator : public __allocator_base<_Tp>
00113     {
00114    public:
00115       typedef size_t     size_type;
00116       typedef ptrdiff_t  difference_type;
00117       typedef _Tp*       pointer;
00118       typedef const _Tp* const_pointer;
00119       typedef _Tp&       reference;
00120       typedef const _Tp& const_reference;
00121       typedef _Tp        value_type;
00122 
00123       template<typename _Tp1>
00124         struct rebind
00125         { typedef allocator<_Tp1> other; };
00126 
00127 #if __cplusplus >= 201103L
00128       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00129       // 2103. std::allocator propagate_on_container_move_assignment
00130       typedef true_type propagate_on_container_move_assignment;
00131 
00132       typedef true_type is_always_equal;
00133 #endif
00134 
00135       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00136       // 3035. std::allocator's constructors should be constexpr
00137       _GLIBCXX20_CONSTEXPR
00138       allocator() _GLIBCXX_NOTHROW { }
00139 
00140       _GLIBCXX20_CONSTEXPR
00141       allocator(const allocator& __a) _GLIBCXX_NOTHROW
00142       : __allocator_base<_Tp>(__a) { }
00143 
00144 #if __cplusplus >= 201103L
00145       // Avoid implicit deprecation.
00146       allocator& operator=(const allocator&) = default;
00147 #endif
00148 
00149       template<typename _Tp1>
00150         _GLIBCXX20_CONSTEXPR
00151         allocator(const allocator<_Tp1>&) _GLIBCXX_NOTHROW { }
00152 
00153       ~allocator() _GLIBCXX_NOTHROW { }
00154 
00155       friend bool
00156       operator==(const allocator&, const allocator&) _GLIBCXX_NOTHROW
00157       { return true; }
00158 
00159       friend bool
00160       operator!=(const allocator&, const allocator&) _GLIBCXX_NOTHROW
00161       { return false; }
00162 
00163       // Inherit everything else.
00164     };
00165 
00166   template<typename _T1, typename _T2>
00167     inline bool
00168     operator==(const allocator<_T1>&, const allocator<_T2>&)
00169     _GLIBCXX_NOTHROW
00170     { return true; }
00171 
00172   template<typename _T1, typename _T2>
00173     inline bool
00174     operator!=(const allocator<_T1>&, const allocator<_T2>&)
00175     _GLIBCXX_NOTHROW
00176     { return false; }
00177 
00178   // Invalid allocator<cv T> partial specializations.
00179   // allocator_traits::rebind_alloc can be used to form a valid allocator type.
00180   template<typename _Tp>
00181     class allocator<const _Tp>
00182     {
00183     public:
00184       typedef _Tp value_type;
00185       template<typename _Up> allocator(const allocator<_Up>&) { }
00186     };
00187 
00188   template<typename _Tp>
00189     class allocator<volatile _Tp>
00190     {
00191     public:
00192       typedef _Tp value_type;
00193       template<typename _Up> allocator(const allocator<_Up>&) { }
00194     };
00195 
00196   template<typename _Tp>
00197     class allocator<const volatile _Tp>
00198     {
00199     public:
00200       typedef _Tp value_type;
00201       template<typename _Up> allocator(const allocator<_Up>&) { }
00202     };
00203 
00204   /// @} group allocator
00205 
00206   // Inhibit implicit instantiations for required instantiations,
00207   // which are defined via explicit instantiations elsewhere.
00208 #if _GLIBCXX_EXTERN_TEMPLATE
00209   extern template class allocator<char>;
00210   extern template class allocator<wchar_t>;
00211 #endif
00212 
00213   // Undefine.
00214 #undef __allocator_base
00215 
00216   // To implement Option 3 of DR 431.
00217   template<typename _Alloc, bool = __is_empty(_Alloc)>
00218     struct __alloc_swap
00219     { static void _S_do_it(_Alloc&, _Alloc&) _GLIBCXX_NOEXCEPT { } };
00220 
00221   template<typename _Alloc>
00222     struct __alloc_swap<_Alloc, false>
00223     {
00224       static void
00225       _S_do_it(_Alloc& __one, _Alloc& __two) _GLIBCXX_NOEXCEPT
00226       {
00227         // Precondition: swappable allocators.
00228         if (__one != __two)
00229           swap(__one, __two);
00230       }
00231     };
00232 
00233   // Optimize for stateless allocators.
00234   template<typename _Alloc, bool = __is_empty(_Alloc)>
00235     struct __alloc_neq
00236     {
00237       static bool
00238       _S_do_it(const _Alloc&, const _Alloc&)
00239       { return false; }
00240     };
00241 
00242   template<typename _Alloc>
00243     struct __alloc_neq<_Alloc, false>
00244     {
00245       static bool
00246       _S_do_it(const _Alloc& __one, const _Alloc& __two)
00247       { return __one != __two; }
00248     };
00249 
00250 #if __cplusplus >= 201103L
00251   template<typename _Tp, bool
00252     = __or_<is_copy_constructible<typename _Tp::value_type>,
00253             is_nothrow_move_constructible<typename _Tp::value_type>>::value>
00254     struct __shrink_to_fit_aux
00255     { static bool _S_do_it(_Tp&) noexcept { return false; } };
00256 
00257   template<typename _Tp>
00258     struct __shrink_to_fit_aux<_Tp, true>
00259     {
00260       static bool
00261       _S_do_it(_Tp& __c) noexcept
00262       {
00263 #if __cpp_exceptions
00264         try
00265           {
00266             _Tp(__make_move_if_noexcept_iterator(__c.begin()),
00267                 __make_move_if_noexcept_iterator(__c.end()),
00268                 __c.get_allocator()).swap(__c);
00269             return true;
00270           }
00271         catch(...)
00272           { return false; }
00273 #else
00274         return false;
00275 #endif
00276       }
00277     };
00278 #endif
00279 
00280 _GLIBCXX_END_NAMESPACE_VERSION
00281 } // namespace std
00282 
00283 #endif