(root)/
gcc-13.2.0/
libstdc++-v3/
include/
ext/
extptr_allocator.h
       1  // <extptr_allocator.h> -*- C++ -*-
       2  
       3  // Copyright (C) 2008-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   *  @file ext/extptr_allocator.h
      27   *  This file is a GNU extension to the Standard C++ Library.
      28   *
      29   *  @author Bob Walters
      30   *
      31   * An example allocator which uses an alternative pointer type from
      32   * bits/pointer.h.  Supports test cases which confirm container support
      33   * for alternative pointers.
      34   */
      35  
      36  #ifndef _EXTPTR_ALLOCATOR_H
      37  #define _EXTPTR_ALLOCATOR_H 1
      38  
      39  #include <bits/requires_hosted.h> // GNU extensions are currently omitted
      40  
      41  #include <memory>
      42  #include <ext/numeric_traits.h>
      43  #include <ext/pointer.h>
      44  
      45  namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
      46  {
      47  _GLIBCXX_BEGIN_NAMESPACE_VERSION
      48  
      49    /**
      50     * @brief An example allocator which uses a non-standard pointer type.
      51     * @ingroup allocators
      52     *
      53     * This allocator specifies that containers use a 'relative pointer' as it's
      54     * pointer type.  (See ext/pointer.h)  Memory allocation in this example
      55     * is still performed using std::allocator.
      56     */
      57    template<typename _Tp>
      58      class _ExtPtr_allocator
      59      {
      60      public:
      61        typedef std::size_t     size_type;
      62        typedef std::ptrdiff_t  difference_type;
      63  
      64        // Note the non-standard pointer types.
      65        typedef _Pointer_adapter<_Relative_pointer_impl<_Tp> >       pointer;
      66        typedef _Pointer_adapter<_Relative_pointer_impl<const _Tp> > 
      67                                                               const_pointer;
      68  
      69        typedef _Tp&       reference;
      70        typedef const _Tp& const_reference;
      71        typedef _Tp        value_type;
      72  
      73        template<typename _Up>
      74          struct rebind
      75          { typedef _ExtPtr_allocator<_Up> other; };
      76  
      77        _ExtPtr_allocator() _GLIBCXX_USE_NOEXCEPT 
      78        : _M_real_alloc() { }
      79  
      80        _ExtPtr_allocator(const _ExtPtr_allocator& __rarg) _GLIBCXX_USE_NOEXCEPT
      81        : _M_real_alloc(__rarg._M_real_alloc) { }
      82  
      83        template<typename _Up>
      84          _ExtPtr_allocator(const _ExtPtr_allocator<_Up>& __rarg)
      85  	_GLIBCXX_USE_NOEXCEPT
      86          : _M_real_alloc(__rarg._M_getUnderlyingImp()) { }
      87  
      88        ~_ExtPtr_allocator() _GLIBCXX_USE_NOEXCEPT
      89        { }
      90  
      91        pointer address(reference __x) const _GLIBCXX_NOEXCEPT
      92        { return std::__addressof(__x); }
      93  
      94        const_pointer address(const_reference __x) const _GLIBCXX_NOEXCEPT
      95        { return std::__addressof(__x); }
      96  
      97        _GLIBCXX_NODISCARD pointer allocate(size_type __n, const void* = 0)
      98        { return _M_real_alloc.allocate(__n); }
      99  
     100        void deallocate(pointer __p, size_type __n)
     101        { _M_real_alloc.deallocate(__p.get(), __n); }
     102  
     103        size_type max_size() const _GLIBCXX_USE_NOEXCEPT
     104        { return __numeric_traits<size_type>::__max / sizeof(_Tp); }
     105  
     106  #if __cplusplus >= 201103L
     107        template<typename _Up, typename... _Args>
     108          void
     109          construct(_Up* __p, _Args&&... __args)
     110  	{ ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
     111  
     112        template<typename... _Args>
     113          void
     114          construct(pointer __p, _Args&&... __args)
     115  	{ construct(__p.get(), std::forward<_Args>(__args)...); }
     116  
     117        template<typename _Up>
     118          void 
     119          destroy(_Up* __p)
     120          { __p->~_Up(); }
     121  
     122        void destroy(pointer __p)
     123        { destroy(__p.get()); }
     124  
     125  #else
     126  
     127        void construct(pointer __p, const _Tp& __val)
     128        { ::new(__p.get()) _Tp(__val); }
     129  
     130        void destroy(pointer __p)
     131        { __p->~_Tp(); }
     132  #endif
     133  
     134        template<typename _Up>
     135          inline bool
     136          operator==(const _ExtPtr_allocator<_Up>& __rarg) const
     137          { return _M_real_alloc == __rarg._M_getUnderlyingImp(); }
     138  
     139        inline bool
     140        operator==(const _ExtPtr_allocator& __rarg) const
     141        { return _M_real_alloc == __rarg._M_real_alloc; }
     142  
     143  #if __cpp_impl_three_way_comparison < 201907L
     144        template<typename _Up>
     145          inline bool
     146          operator!=(const _ExtPtr_allocator<_Up>& __rarg) const
     147          { return _M_real_alloc != __rarg._M_getUnderlyingImp(); }
     148  
     149        inline bool
     150        operator!=(const _ExtPtr_allocator& __rarg) const
     151        { return _M_real_alloc != __rarg._M_real_alloc; }
     152  #endif
     153  
     154        template<typename _Up>
     155          inline friend void
     156          swap(_ExtPtr_allocator<_Up>&, _ExtPtr_allocator<_Up>&);
     157  
     158        // A method specific to this implementation.
     159        const std::allocator<_Tp>&
     160        _M_getUnderlyingImp() const
     161        { return _M_real_alloc; }
     162  
     163      private:
     164        std::allocator<_Tp>  _M_real_alloc;
     165      };
     166  
     167    // _ExtPtr_allocator<void> specialization.
     168    template<>
     169      class _ExtPtr_allocator<void>
     170      {
     171      public:
     172        typedef std::size_t      size_type;
     173        typedef std::ptrdiff_t   difference_type;
     174        typedef void             value_type;
     175  
     176        // Note the non-standard pointer types
     177        typedef _Pointer_adapter<_Relative_pointer_impl<void> >       pointer;
     178        typedef _Pointer_adapter<_Relative_pointer_impl<const void> >
     179                                                                const_pointer;
     180  
     181        _ExtPtr_allocator() { }
     182  
     183        template<typename _Up>
     184  	_ExtPtr_allocator(const _ExtPtr_allocator<_Up>&) { }
     185  
     186        template<typename _Up>
     187          struct rebind
     188          { typedef _ExtPtr_allocator<_Up> other; };
     189  
     190      private:
     191        std::allocator<void>  _M_real_alloc;
     192      };
     193  
     194    template<typename _Tp>
     195      inline void
     196      swap(_ExtPtr_allocator<_Tp>& __larg, _ExtPtr_allocator<_Tp>& __rarg)
     197      {
     198        std::allocator<_Tp> __tmp( __rarg._M_real_alloc );
     199        __rarg._M_real_alloc = __larg._M_real_alloc;
     200        __larg._M_real_alloc = __tmp;
     201      }
     202  
     203  _GLIBCXX_END_NAMESPACE_VERSION
     204  } // namespace
     205  
     206  #endif /* _EXTPTR_ALLOCATOR_H */