(root)/
gcc-13.2.0/
libstdc++-v3/
include/
bits/
allocator.h
       1  // Allocators -*- C++ -*-
       2  
       3  // Copyright (C) 2001-2023 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 incompatibility 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  	__attribute__((__always_inline__))
     109  	constexpr
     110  	allocator(const allocator<_Up>&) noexcept { }
     111  
     112        // No allocate member because it's ill-formed by LWG 3307.
     113        // No deallocate member because it would be undefined to call it
     114        // with any pointer which wasn't obtained from allocate.
     115  #endif // C++20
     116  #endif // C++11
     117      };
     118  
     119    /**
     120     * @brief  The @a standard allocator, as per C++03 [20.4.1].
     121     *
     122     *  See https://gcc.gnu.org/onlinedocs/libstdc++/manual/memory.html#std.util.memory.allocator
     123     *  for further details.
     124     *
     125     *  @tparam  _Tp  Type of allocated object.
     126     *
     127     *  @headerfile memory
     128     */
     129    template<typename _Tp>
     130      class allocator : public __allocator_base<_Tp>
     131      {
     132      public:
     133        typedef _Tp        value_type;
     134        typedef size_t     size_type;
     135        typedef ptrdiff_t  difference_type;
     136  
     137  #if __cplusplus <= 201703L
     138        // These were removed for C++20.
     139        typedef _Tp*       pointer;
     140        typedef const _Tp* const_pointer;
     141        typedef _Tp&       reference;
     142        typedef const _Tp& const_reference;
     143  
     144        template<typename _Tp1>
     145  	struct rebind
     146  	{ typedef allocator<_Tp1> other; };
     147  #endif
     148  
     149  #if __cplusplus >= 201103L
     150        // _GLIBCXX_RESOLVE_LIB_DEFECTS
     151        // 2103. std::allocator propagate_on_container_move_assignment
     152        using propagate_on_container_move_assignment = true_type;
     153  
     154        using is_always_equal
     155  	_GLIBCXX20_DEPRECATED_SUGGEST("std::allocator_traits::is_always_equal")
     156  	= true_type;
     157  #endif
     158  
     159        // _GLIBCXX_RESOLVE_LIB_DEFECTS
     160        // 3035. std::allocator's constructors should be constexpr
     161        __attribute__((__always_inline__))
     162        _GLIBCXX20_CONSTEXPR
     163        allocator() _GLIBCXX_NOTHROW { }
     164  
     165        __attribute__((__always_inline__))
     166        _GLIBCXX20_CONSTEXPR
     167        allocator(const allocator& __a) _GLIBCXX_NOTHROW
     168        : __allocator_base<_Tp>(__a) { }
     169  
     170  #if __cplusplus >= 201103L
     171        // Avoid implicit deprecation.
     172        allocator& operator=(const allocator&) = default;
     173  #endif
     174  
     175        template<typename _Tp1>
     176  	__attribute__((__always_inline__))
     177  	_GLIBCXX20_CONSTEXPR
     178  	allocator(const allocator<_Tp1>&) _GLIBCXX_NOTHROW { }
     179  
     180        __attribute__((__always_inline__))
     181  #if __cpp_constexpr_dynamic_alloc
     182        constexpr
     183  #endif
     184        ~allocator() _GLIBCXX_NOTHROW { }
     185  
     186  #if __cplusplus > 201703L
     187        [[nodiscard,__gnu__::__always_inline__]]
     188        constexpr _Tp*
     189        allocate(size_t __n)
     190        {
     191  	if (std::__is_constant_evaluated())
     192  	  {
     193  	    if (__builtin_mul_overflow(__n, sizeof(_Tp), &__n))
     194  	      std::__throw_bad_array_new_length();
     195  	    return static_cast<_Tp*>(::operator new(__n));
     196  	  }
     197  
     198  	return __allocator_base<_Tp>::allocate(__n, 0);
     199        }
     200  
     201        [[__gnu__::__always_inline__]]
     202        constexpr void
     203        deallocate(_Tp* __p, size_t __n)
     204        {
     205  	if (std::__is_constant_evaluated())
     206  	  {
     207  	    ::operator delete(__p);
     208  	    return;
     209  	  }
     210  	__allocator_base<_Tp>::deallocate(__p, __n);
     211        }
     212  #endif // C++20
     213  
     214        friend __attribute__((__always_inline__)) _GLIBCXX20_CONSTEXPR
     215        bool
     216        operator==(const allocator&, const allocator&) _GLIBCXX_NOTHROW
     217        { return true; }
     218  
     219  #if __cpp_impl_three_way_comparison < 201907L
     220        friend __attribute__((__always_inline__)) _GLIBCXX20_CONSTEXPR
     221        bool
     222        operator!=(const allocator&, const allocator&) _GLIBCXX_NOTHROW
     223        { return false; }
     224  #endif
     225  
     226        // Inherit everything else.
     227      };
     228  
     229    /** Equality comparison for std::allocator objects
     230     *
     231     * @return true, for all std::allocator objects.
     232     * @relates std::allocator
     233     */
     234    template<typename _T1, typename _T2>
     235      __attribute__((__always_inline__))
     236      inline _GLIBCXX20_CONSTEXPR bool
     237      operator==(const allocator<_T1>&, const allocator<_T2>&)
     238      _GLIBCXX_NOTHROW
     239      { return true; }
     240  
     241  #if __cpp_impl_three_way_comparison < 201907L
     242    template<typename _T1, typename _T2>
     243      __attribute__((__always_inline__))
     244      inline _GLIBCXX20_CONSTEXPR bool
     245      operator!=(const allocator<_T1>&, const allocator<_T2>&)
     246      _GLIBCXX_NOTHROW
     247      { return false; }
     248  #endif
     249  
     250    /// @cond undocumented
     251  
     252    // Invalid allocator<cv T> partial specializations.
     253    // allocator_traits::rebind_alloc can be used to form a valid allocator type.
     254    template<typename _Tp>
     255      class allocator<const _Tp>
     256      {
     257      public:
     258        typedef _Tp value_type;
     259        template<typename _Up> allocator(const allocator<_Up>&) { }
     260      };
     261  
     262    template<typename _Tp>
     263      class allocator<volatile _Tp>
     264      {
     265      public:
     266        typedef _Tp value_type;
     267        template<typename _Up> allocator(const allocator<_Up>&) { }
     268      };
     269  
     270    template<typename _Tp>
     271      class allocator<const volatile _Tp>
     272      {
     273      public:
     274        typedef _Tp value_type;
     275        template<typename _Up> allocator(const allocator<_Up>&) { }
     276      };
     277    /// @endcond
     278  
     279    /// @} group allocator
     280  
     281    // Inhibit implicit instantiations for required instantiations,
     282    // which are defined via explicit instantiations elsewhere.
     283  #if _GLIBCXX_EXTERN_TEMPLATE
     284    extern template class allocator<char>;
     285    extern template class allocator<wchar_t>;
     286  #endif
     287  
     288    // Undefine.
     289  #undef __allocator_base
     290  
     291  _GLIBCXX_END_NAMESPACE_VERSION
     292  } // namespace std
     293  
     294  #endif