Home | History | Annotate | Line # | Download | only in bits
      1 // Allocators -*- C++ -*-
      2 
      3 // Copyright (C) 2001-2022 Free Software Foundation, Inc.
      4 //
      5 // This file is part of the GNU ISO C++ Library.  This library is free
      6 // software; you can redistribute it and/or modify it under the
      7 // terms of the GNU General Public License as published by the
      8 // Free Software Foundation; either version 3, or (at your option)
      9 // any later version.
     10 
     11 // This library is distributed in the hope that it will be useful,
     12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14 // GNU General Public License for more details.
     15 
     16 // Under Section 7 of GPL version 3, you are granted additional
     17 // permissions described in the GCC Runtime Library Exception, version
     18 // 3.1, as published by the Free Software Foundation.
     19 
     20 // You should have received a copy of the GNU General Public License and
     21 // a copy of the GCC Runtime Library Exception along with this program;
     22 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
     23 // <http://www.gnu.org/licenses/>.
     24 
     25 /*
     26  * Copyright (c) 1996-1997
     27  * Silicon Graphics Computer Systems, Inc.
     28  *
     29  * Permission to use, copy, modify, distribute and sell this software
     30  * and its documentation for any purpose is hereby granted without fee,
     31  * provided that the above copyright notice appear in all copies and
     32  * that both that copyright notice and this permission notice appear
     33  * in supporting documentation.  Silicon Graphics makes no
     34  * representations about the suitability of this software for any
     35  * purpose.  It is provided "as is" without express or implied warranty.
     36  */
     37 
     38 /** @file bits/allocator.h
     39  *  This is an internal header file, included by other library headers.
     40  *  Do not attempt to use it directly. @headername{memory}
     41  */
     42 
     43 #ifndef _ALLOCATOR_H
     44 #define _ALLOCATOR_H 1
     45 
     46 #include <bits/c++allocator.h> // Define the base class to std::allocator.
     47 #include <bits/memoryfwd.h>
     48 #if __cplusplus >= 201103L
     49 #include <type_traits>
     50 #endif
     51 
     52 #define __cpp_lib_incomplete_container_elements 201505L
     53 
     54 namespace std _GLIBCXX_VISIBILITY(default)
     55 {
     56 _GLIBCXX_BEGIN_NAMESPACE_VERSION
     57 
     58   /**
     59    *  @addtogroup allocators
     60    *  @{
     61    */
     62 
     63   // Since C++20 the primary template should be used for allocator<void>,
     64   // but then it would have a non-trivial default ctor and dtor for C++20,
     65   // but trivial for C++98-17, which would be an ABI incompatibiliy between
     66   // different standard dialects. So C++20 still uses the allocator<void>
     67   // explicit specialization, with the historical ABI properties, but with
     68   // the same members that are present in the primary template.
     69 
     70   /** std::allocator<void> specialization.
     71    *
     72    * @headerfile memory
     73    */
     74   template<>
     75     class allocator<void>
     76     {
     77     public:
     78       typedef void        value_type;
     79       typedef size_t      size_type;
     80       typedef ptrdiff_t   difference_type;
     81 
     82 #if __cplusplus <= 201703L
     83       // These were removed for C++20, allocator_traits does the right thing.
     84       typedef void*       pointer;
     85       typedef const void* const_pointer;
     86 
     87       template<typename _Tp1>
     88 	struct rebind
     89 	{ typedef allocator<_Tp1> other; };
     90 #endif
     91 
     92 #if __cplusplus >= 201103L
     93       // _GLIBCXX_RESOLVE_LIB_DEFECTS
     94       // 2103. std::allocator propagate_on_container_move_assignment
     95       using propagate_on_container_move_assignment = true_type;
     96 
     97       using is_always_equal
     98 	_GLIBCXX20_DEPRECATED_SUGGEST("std::allocator_traits::is_always_equal")
     99 	= true_type;
    100 
    101 #if __cplusplus >= 202002L
    102       // As noted above, these members are present for C++20 to provide the
    103       // same API as the primary template, but still trivial as in pre-C++20.
    104       allocator() = default;
    105       ~allocator() = default;
    106 
    107       template<typename _Up>
    108 	constexpr
    109 	allocator(const allocator<_Up>&) noexcept { }
    110 
    111       // No allocate member because it's ill-formed by LWG 3307.
    112       // No deallocate member because it would be undefined to call it
    113       // with any pointer which wasn't obtained from allocate.
    114 #endif // C++20
    115 #endif // C++11
    116     };
    117 
    118   /**
    119    * @brief  The @a standard allocator, as per C++03 [20.4.1].
    120    *
    121    *  See https://gcc.gnu.org/onlinedocs/libstdc++/manual/memory.html#std.util.memory.allocator
    122    *  for further details.
    123    *
    124    *  @tparam  _Tp  Type of allocated object.
    125    *
    126    *  @headerfile memory
    127    */
    128   template<typename _Tp>
    129     class allocator : public __allocator_base<_Tp>
    130     {
    131     public:
    132       typedef _Tp        value_type;
    133       typedef size_t     size_type;
    134       typedef ptrdiff_t  difference_type;
    135 
    136 #if __cplusplus <= 201703L
    137       // These were removed for C++20.
    138       typedef _Tp*       pointer;
    139       typedef const _Tp* const_pointer;
    140       typedef _Tp&       reference;
    141       typedef const _Tp& const_reference;
    142 
    143       template<typename _Tp1>
    144 	struct rebind
    145 	{ typedef allocator<_Tp1> other; };
    146 #endif
    147 
    148 #if __cplusplus >= 201103L
    149       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    150       // 2103. std::allocator propagate_on_container_move_assignment
    151       using propagate_on_container_move_assignment = true_type;
    152 
    153       using is_always_equal
    154 	_GLIBCXX20_DEPRECATED_SUGGEST("std::allocator_traits::is_always_equal")
    155 	= true_type;
    156 #endif
    157 
    158       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    159       // 3035. std::allocator's constructors should be constexpr
    160       _GLIBCXX20_CONSTEXPR
    161       allocator() _GLIBCXX_NOTHROW { }
    162 
    163       _GLIBCXX20_CONSTEXPR
    164       allocator(const allocator& __a) _GLIBCXX_NOTHROW
    165       : __allocator_base<_Tp>(__a) { }
    166 
    167 #if __cplusplus >= 201103L
    168       // Avoid implicit deprecation.
    169       allocator& operator=(const allocator&) = default;
    170 #endif
    171 
    172       template<typename _Tp1>
    173 	_GLIBCXX20_CONSTEXPR
    174 	allocator(const allocator<_Tp1>&) _GLIBCXX_NOTHROW { }
    175 
    176 #if __cpp_constexpr_dynamic_alloc
    177       constexpr
    178 #endif
    179       ~allocator() _GLIBCXX_NOTHROW { }
    180 
    181 #if __cplusplus > 201703L
    182       [[nodiscard,__gnu__::__always_inline__]]
    183       constexpr _Tp*
    184       allocate(size_t __n)
    185       {
    186 	if (std::__is_constant_evaluated())
    187 	  {
    188 	    if (__builtin_mul_overflow(__n, sizeof(_Tp), &__n))
    189 	      std::__throw_bad_array_new_length();
    190 	    return static_cast<_Tp*>(::operator new(__n));
    191 	  }
    192 
    193 	return __allocator_base<_Tp>::allocate(__n, 0);
    194       }
    195 
    196       [[__gnu__::__always_inline__]]
    197       constexpr void
    198       deallocate(_Tp* __p, size_t __n)
    199       {
    200 	if (std::__is_constant_evaluated())
    201 	  {
    202 	    ::operator delete(__p);
    203 	    return;
    204 	  }
    205 	__allocator_base<_Tp>::deallocate(__p, __n);
    206       }
    207 #endif // C++20
    208 
    209       friend _GLIBCXX20_CONSTEXPR bool
    210       operator==(const allocator&, const allocator&) _GLIBCXX_NOTHROW
    211       { return true; }
    212 
    213 #if __cpp_impl_three_way_comparison < 201907L
    214       friend _GLIBCXX20_CONSTEXPR bool
    215       operator!=(const allocator&, const allocator&) _GLIBCXX_NOTHROW
    216       { return false; }
    217 #endif
    218 
    219       // Inherit everything else.
    220     };
    221 
    222   /** Equality comparison for std::allocator objects
    223    *
    224    * @return true, for all std::allocator objects.
    225    * @relates std::allocator
    226    */
    227   template<typename _T1, typename _T2>
    228     inline _GLIBCXX20_CONSTEXPR bool
    229     operator==(const allocator<_T1>&, const allocator<_T2>&)
    230     _GLIBCXX_NOTHROW
    231     { return true; }
    232 
    233 #if __cpp_impl_three_way_comparison < 201907L
    234   template<typename _T1, typename _T2>
    235     inline _GLIBCXX20_CONSTEXPR bool
    236     operator!=(const allocator<_T1>&, const allocator<_T2>&)
    237     _GLIBCXX_NOTHROW
    238     { return false; }
    239 #endif
    240 
    241   /// @cond undocumented
    242 
    243   // Invalid allocator<cv T> partial specializations.
    244   // allocator_traits::rebind_alloc can be used to form a valid allocator type.
    245   template<typename _Tp>
    246     class allocator<const _Tp>
    247     {
    248     public:
    249       typedef _Tp value_type;
    250       allocator() { }
    251       template<typename _Up> allocator(const allocator<_Up>&) { }
    252     };
    253 
    254   template<typename _Tp>
    255     class allocator<volatile _Tp>
    256     {
    257     public:
    258       typedef _Tp value_type;
    259       allocator() { }
    260       template<typename _Up> allocator(const allocator<_Up>&) { }
    261     };
    262 
    263   template<typename _Tp>
    264     class allocator<const volatile _Tp>
    265     {
    266     public:
    267       typedef _Tp value_type;
    268       allocator() { }
    269       template<typename _Up> allocator(const allocator<_Up>&) { }
    270     };
    271 
    272   /// @} group allocator
    273 
    274   // Inhibit implicit instantiations for required instantiations,
    275   // which are defined via explicit instantiations elsewhere.
    276 #if _GLIBCXX_EXTERN_TEMPLATE
    277   extern template class allocator<char>;
    278   extern template class allocator<wchar_t>;
    279 #endif
    280 
    281   // Undefine.
    282 #undef __allocator_base
    283 
    284   // To implement Option 3 of DR 431.
    285   template<typename _Alloc, bool = __is_empty(_Alloc)>
    286     struct __alloc_swap
    287     { static void _S_do_it(_Alloc&, _Alloc&) _GLIBCXX_NOEXCEPT { } };
    288 
    289   template<typename _Alloc>
    290     struct __alloc_swap<_Alloc, false>
    291     {
    292       static void
    293       _S_do_it(_Alloc& __one, _Alloc& __two) _GLIBCXX_NOEXCEPT
    294       {
    295 	// Precondition: swappable allocators.
    296 	if (__one != __two)
    297 	  swap(__one, __two);
    298       }
    299     };
    300 
    301   // Optimize for stateless allocators.
    302   template<typename _Alloc, bool = __is_empty(_Alloc)>
    303     struct __alloc_neq
    304     {
    305       static bool
    306       _S_do_it(const _Alloc&, const _Alloc&)
    307       { return false; }
    308     };
    309 
    310   template<typename _Alloc>
    311     struct __alloc_neq<_Alloc, false>
    312     {
    313       static bool
    314       _S_do_it(const _Alloc& __one, const _Alloc& __two)
    315       { return __one != __two; }
    316     };
    317 
    318 #if __cplusplus >= 201103L
    319   template<typename _Tp, bool
    320     = __or_<is_copy_constructible<typename _Tp::value_type>,
    321             is_nothrow_move_constructible<typename _Tp::value_type>>::value>
    322     struct __shrink_to_fit_aux
    323     { static bool _S_do_it(_Tp&) noexcept { return false; } };
    324 
    325   template<typename _Tp>
    326     struct __shrink_to_fit_aux<_Tp, true>
    327     {
    328       _GLIBCXX20_CONSTEXPR
    329       static bool
    330       _S_do_it(_Tp& __c) noexcept
    331       {
    332 #if __cpp_exceptions
    333 	try
    334 	  {
    335 	    _Tp(__make_move_if_noexcept_iterator(__c.begin()),
    336 		__make_move_if_noexcept_iterator(__c.end()),
    337 		__c.get_allocator()).swap(__c);
    338 	    return true;
    339 	  }
    340 	catch(...)
    341 	  { return false; }
    342 #else
    343 	return false;
    344 #endif
    345       }
    346     };
    347 #endif
    348   /// @endcond
    349 
    350 _GLIBCXX_END_NAMESPACE_VERSION
    351 } // namespace std
    352 
    353 #endif
    354