]> git.ipfire.org Git - thirdparty/gcc.git/blobdiff - libstdc++-v3/include/experimental/memory_resource
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / include / experimental / memory_resource
index 3d2dce19868e6fcad6636225a02fe2d2fc81dd01..bc2fdbf082ea005966f8d18d05f63cc5a58fca4b 100644 (file)
@@ -1,6 +1,6 @@
 // <experimental/memory_resource> -*- C++ -*-
 
-// Copyright (C) 2015-2018 Free Software Foundation, Inc.
+// Copyright (C) 2015-2024 Free Software Foundation, Inc.
 //
 // This file is part of the GNU ISO C++ Library.  This library is free
 // software; you can redistribute it and/or modify it under the
 
 /** @file experimental/memory_resource
  *  This is a TS C++ Library header.
+ *  @ingroup libfund-ts
  */
 
 #ifndef _GLIBCXX_EXPERIMENTAL_MEMORY_RESOURCE
 #define _GLIBCXX_EXPERIMENTAL_MEMORY_RESOURCE 1
 
-#include <memory>
-#include <new>
-#include <atomic>
-#include <cstddef>
-#include <ext/new_allocator.h>
-#include <experimental/bits/lfts_config.h>
+#pragma GCC system_header
+
+#include <bits/requires_hosted.h> // experimental is currently omitted
+
+#if __cplusplus >= 201402L
+
+#include <memory>                      // align, uses_allocator, __uses_alloc
+#include <experimental/utility>                // pair, experimental::erased_type
+#include <tuple>                       // tuple, forward_as_tuple
+#include <atomic>                      // atomic
+#include <new>                         // placement new
+#include <cstddef>                     // max_align_t
+#include <bits/new_allocator.h>
+#include <debug/assertions.h>
+
+namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
+{
+_GLIBCXX_BEGIN_NAMESPACE_VERSION
+  template<typename _Tp> class malloc_allocator;
+_GLIBCXX_END_NAMESPACE_VERSION
+} // namespace __gnu_cxx
 
 namespace std {
 _GLIBCXX_BEGIN_NAMESPACE_VERSION
@@ -44,41 +60,43 @@ inline namespace fundamentals_v2 {
 namespace pmr {
 #define __cpp_lib_experimental_memory_resources 201402L
 
+  // Standard memory resources
+
+  // 8.5 Class memory_resource
   class memory_resource;
 
-  template <typename _Tp>
+  // 8.6 Class template polymorphic_allocator
+  template<typename _Tp>
     class polymorphic_allocator;
 
-  template <typename _Alloc>
+  template<typename _Alloc, typename _Resource = memory_resource>
     class __resource_adaptor_imp;
 
-  template <typename _Alloc>
+  // 8.7 Alias template resource_adaptor
+  template<typename _Alloc>
     using resource_adaptor = __resource_adaptor_imp<
       typename allocator_traits<_Alloc>::template rebind_alloc<char>>;
 
-  template <typename _Tp>
-    struct __uses_allocator_construction_helper;
-
-  // Global memory resources
+  // 8.8 Global memory resources
   memory_resource* new_delete_resource() noexcept;
   memory_resource* null_memory_resource() noexcept;
-
-  // The default memory resource
   memory_resource* get_default_resource() noexcept;
   memory_resource* set_default_resource(memory_resource* __r) noexcept;
 
-  // Standard memory resources
+  // TODO 8.9 Pool resource classes
 
-  // 8.5 Class memory_resource
   class memory_resource
   {
-  protected:
     static constexpr size_t _S_max_align = alignof(max_align_t);
 
   public:
-    virtual ~memory_resource() { }
+    memory_resource() = default;
+    memory_resource(const memory_resource&) = default;
+    virtual ~memory_resource() = default;
+
+    memory_resource& operator=(const memory_resource&) = default;
 
-    void*
+    _GLIBCXX_NODISCARD void*
     allocate(size_t __bytes, size_t __alignment = _S_max_align)
     { return do_allocate(__bytes, __alignment); }
 
@@ -102,40 +120,17 @@ namespace pmr {
   };
 
   inline bool
-  operator==(const memory_resource& __a,
-            const memory_resource& __b) noexcept
+  operator==(const memory_resource& __a, const memory_resource& __b) noexcept
   { return &__a == &__b || __a.is_equal(__b); }
 
   inline bool
-  operator!=(const memory_resource& __a,
-            const memory_resource& __b) noexcept
+  operator!=(const memory_resource& __a, const memory_resource& __b) noexcept
   { return !(__a == __b); }
 
 
-  // 8.6 Class template polymorphic_allocator
-  template <class _Tp>
+  template<typename _Tp>
     class polymorphic_allocator
     {
-      using __uses_alloc1_ = __uses_alloc1<memory_resource*>;
-      using __uses_alloc2_ = __uses_alloc2<memory_resource*>;
-
-      template<typename _Tp1, typename... _Args>
-       void
-       _M_construct(__uses_alloc0, _Tp1* __p, _Args&&... __args)
-       { ::new(__p) _Tp1(std::forward<_Args>(__args)...); }
-
-      template<typename _Tp1, typename... _Args>
-       void
-       _M_construct(__uses_alloc1_, _Tp1* __p, _Args&&...  __args)
-       { ::new(__p) _Tp1(allocator_arg, this->resource(),
-                         std::forward<_Args>(__args)...); }
-
-      template<typename _Tp1, typename... _Args>
-       void
-       _M_construct(__uses_alloc2_, _Tp1* __p, _Args&&...  __args)
-       { ::new(__p) _Tp1(std::forward<_Args>(__args)...,
-                         this->resource()); }
-
     public:
       using value_type = _Tp;
 
@@ -158,34 +153,34 @@ namespace pmr {
       polymorphic_allocator&
        operator=(const polymorphic_allocator& __rhs) = default;
 
-      _Tp* allocate(size_t __n)
+      _GLIBCXX_NODISCARD _Tp* allocate(size_t __n)
       { return static_cast<_Tp*>(_M_resource->allocate(__n * sizeof(_Tp),
                                                       alignof(_Tp))); }
 
-      void deallocate(_Tp* __p, size_t __n)
+      void
+      deallocate(_Tp* __p, size_t __n)
       { _M_resource->deallocate(__p, __n * sizeof(_Tp), alignof(_Tp)); }
 
       template <typename _Tp1, typename... _Args> //used here
-       void construct(_Tp1* __p, _Args&&... __args)
+       void
+       construct(_Tp1* __p, _Args&&... __args)
        {
-         memory_resource* const __resource = this->resource();
-         auto __use_tag
-           = __use_alloc<_Tp1, memory_resource*, _Args...>(__resource);
-         _M_construct(__use_tag, __p, std::forward<_Args>(__args)...);
+         std::__uses_allocator_construct(this->resource(), __p,
+                                         std::forward<_Args>(__args)...);
        }
 
       // Specializations for pair using piecewise construction
       template <typename _Tp1, typename _Tp2,
               typename... _Args1, typename... _Args2>
-       void construct(pair<_Tp1, _Tp2>* __p, piecewise_construct_t,
-                      tuple<_Args1...> __x,
-                      tuple<_Args2...> __y)
+       void
+       construct(pair<_Tp1, _Tp2>* __p, piecewise_construct_t,
+                 tuple<_Args1...> __x, tuple<_Args2...> __y)
        {
          memory_resource* const __resource = this->resource();
          auto __x_use_tag =
-           __use_alloc<_Tp1, memory_resource*, _Args1...>(__resource);
+           std::__use_alloc<_Tp1, memory_resource*, _Args1...>(__resource);
          auto __y_use_tag =
-           __use_alloc<_Tp2, memory_resource*, _Args2...>(__resource);
+           std::__use_alloc<_Tp2, memory_resource*, _Args2...>(__resource);
 
          ::new(__p) std::pair<_Tp1, _Tp2>(piecewise_construct,
                                           _M_construct_p(__x_use_tag, __x),
@@ -193,38 +188,53 @@ namespace pmr {
        }
 
       template <typename _Tp1, typename _Tp2>
-       void construct(pair<_Tp1,_Tp2>* __p)
+       void
+       construct(pair<_Tp1,_Tp2>* __p)
        { this->construct(__p, piecewise_construct, tuple<>(), tuple<>()); }
 
       template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
-       void construct(pair<_Tp1,_Tp2>* __p, _Up&& __x, _Vp&& __y)
-       { this->construct(__p, piecewise_construct,
-                         forward_as_tuple(std::forward<_Up>(__x)),
-                         forward_as_tuple(std::forward<_Vp>(__y))); }
+       void
+       construct(pair<_Tp1,_Tp2>* __p, _Up&& __x, _Vp&& __y)
+       {
+         this->construct(__p, piecewise_construct,
+             std::forward_as_tuple(std::forward<_Up>(__x)),
+             std::forward_as_tuple(std::forward<_Vp>(__y)));
+       }
 
       template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
-       void construct(pair<_Tp1,_Tp2>* __p, const std::pair<_Up, _Vp>& __pr)
-       { this->construct(__p, piecewise_construct, forward_as_tuple(__pr.first),
-                         forward_as_tuple(__pr.second)); }
+       void
+       construct(pair<_Tp1,_Tp2>* __p, const std::pair<_Up, _Vp>& __pr)
+       {
+         this->construct(__p, piecewise_construct,
+             std::forward_as_tuple(__pr.first),
+             std::forward_as_tuple(__pr.second));
+       }
 
       template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
-       void construct(pair<_Tp1,_Tp2>* __p, pair<_Up, _Vp>&& __pr)
-       { this->construct(__p, piecewise_construct,
-                         forward_as_tuple(std::forward<_Up>(__pr.first)),
-                         forward_as_tuple(std::forward<_Vp>(__pr.second))); }
+       void
+       construct(pair<_Tp1,_Tp2>* __p, pair<_Up, _Vp>&& __pr)
+       {
+         this->construct(__p, piecewise_construct,
+             std::forward_as_tuple(std::forward<_Up>(__pr.first)),
+             std::forward_as_tuple(std::forward<_Vp>(__pr.second)));
+       }
 
       template <typename _Up>
-       void destroy(_Up* __p)
+       void
+       destroy(_Up* __p)
        { __p->~_Up(); }
 
       // Return a default-constructed allocator (no allocator propagation)
-      polymorphic_allocator select_on_container_copy_construction() const
+      polymorphic_allocator
+      select_on_container_copy_construction() const
       { return polymorphic_allocator(); }
 
-      memory_resource* resource() const
-      { return _M_resource; }
+      memory_resource* resource() const { return _M_resource; }
 
     private:
+      using __uses_alloc1_ = __uses_alloc1<memory_resource*>;
+      using __uses_alloc2_ = __uses_alloc2<memory_resource*>;
+
       template<typename _Tuple>
        _Tuple&&
        _M_construct_p(__uses_alloc0, _Tuple& __t)
@@ -245,18 +255,22 @@ namespace pmr {
     };
 
   template <class _Tp1, class _Tp2>
-    bool operator==(const polymorphic_allocator<_Tp1>& __a,
-                   const polymorphic_allocator<_Tp2>& __b) noexcept
+    bool
+    operator==(const polymorphic_allocator<_Tp1>& __a,
+              const polymorphic_allocator<_Tp2>& __b) noexcept
     { return *__a.resource() == *__b.resource(); }
 
   template <class _Tp1, class _Tp2>
-    bool operator!=(const polymorphic_allocator<_Tp1>& __a,
-                   const polymorphic_allocator<_Tp2>& __b) noexcept
+    bool
+    operator!=(const polymorphic_allocator<_Tp1>& __a,
+              const polymorphic_allocator<_Tp2>& __b) noexcept
     { return !(__a == __b); }
 
+
+  /// @cond undocumented
   class __resource_adaptor_common
   {
-    template<typename> friend class __resource_adaptor_imp;
+    template<typename, typename> friend class __resource_adaptor_imp;
 
     struct _AlignMgr
     {
@@ -307,6 +321,10 @@ namespace pmr {
          __orig_ptr = __ptr - _S_read<unsigned int>(__end);
        else // (__token_size == sizeof(char*))
          __orig_ptr = _S_read<char*>(__end);
+       // The adjustment is always less than the requested alignment,
+       // so if that isn't true now then either the wrong size was passed
+       // to deallocate or the token was overwritten by a buffer overflow:
+       __glibcxx_assert(static_cast<size_t>(__ptr - __orig_ptr) < _M_align);
        return __orig_ptr;
       }
 
@@ -326,7 +344,7 @@ namespace pmr {
          return 1;
        if (_M_align <= (1ul << (sizeof(short) * __CHAR_BIT__)))
          return sizeof(short);
-       if (_M_align <= (1ul << (sizeof(int) * __CHAR_BIT__)))
+       if (_M_align <= (1ull << (sizeof(int) * __CHAR_BIT__)))
          return sizeof(int);
        return sizeof(char*);
       }
@@ -346,12 +364,15 @@ namespace pmr {
        }
     };
   };
+  /// @endcond
 
   // 8.7.1 __resource_adaptor_imp
-  template <typename _Alloc>
+  template<typename _Alloc, typename _Resource>
     class __resource_adaptor_imp
-    : public memory_resource, private __resource_adaptor_common
+    : public _Resource, private __resource_adaptor_common
     {
+      using memory_resource = _Resource;
+
       static_assert(is_same<char,
          typename allocator_traits<_Alloc>::value_type>::value,
          "Allocator's value_type is char");
@@ -389,12 +410,34 @@ namespace pmr {
       allocator_type get_allocator() const noexcept { return _M_alloc; }
 
     protected:
+#if (defined __sun__ || defined __VXWORKS__) && defined __i386__
+// Cannot use max_align_t on 32-bit Solaris x86, see PR libstdc++/77691
+# define _GLIBCXX_MAX_ALIGN_MATCHES_MALLOC 0
+#elif defined __hpux__ && defined __hppa__ && defined __LP64__
+// Ignore inconsistent long double and malloc alignment (libstdc++/77691)
+# define _GLIBCXX_MAX_ALIGN_MATCHES_MALLOC 0
+#else
+# define _GLIBCXX_MAX_ALIGN_MATCHES_MALLOC 1
+#endif
+
       virtual void*
       do_allocate(size_t __bytes, size_t __alignment) override
       {
-       if (__alignment == 1)
-         return _M_alloc.allocate(__bytes);
-
+#if _GLIBCXX_MAX_ALIGN_MATCHES_MALLOC
+       if (__alignment == alignof(max_align_t))
+         return _M_allocate<alignof(max_align_t)>(__bytes);
+#endif
+       switch (__alignment)
+         {
+         case 1:
+           return _M_alloc.allocate(__bytes);
+         case 2:
+           return _M_allocate<2>(__bytes);
+         case 4:
+           return _M_allocate<4>(__bytes);
+         case 8:
+           return _M_allocate<8>(__bytes);
+         }
        const _AlignMgr __mgr(__bytes, __alignment);
        // Assume _M_alloc returns 1-byte aligned memory, so allocate enough
        // space to fit a block of the right size and alignment, plus some
@@ -403,42 +446,78 @@ namespace pmr {
       }
 
       virtual void
-      do_deallocate(void* __p, size_t __bytes, size_t __alignment) noexcept
+      do_deallocate(void* __ptr, size_t __bytes, size_t __alignment) noexcept
       override
       {
-       auto __ptr = static_cast<char*>(__p);
-       if (__alignment == 1)
-         _M_alloc.deallocate(__ptr, __bytes);
-
+#if _GLIBCXX_MAX_ALIGN_MATCHES_MALLOC
+       if (__alignment == alignof(max_align_t))
+         return (void) _M_deallocate<alignof(max_align_t)>(__ptr, __bytes);
+#endif
+       switch (__alignment)
+         {
+         case 1:
+           return (void) _M_alloc.deallocate((char*)__ptr, __bytes);
+         case 2:
+           return (void) _M_deallocate<2>(__ptr, __bytes);
+         case 4:
+           return (void) _M_deallocate<4>(__ptr, __bytes);
+         case 8:
+           return (void) _M_deallocate<8>(__ptr, __bytes);
+         }
        const _AlignMgr __mgr(__bytes, __alignment);
-       // Use the stored token to retrieve the original pointer to deallocate.
-       _M_alloc.deallocate(__mgr._M_unadjust(__ptr), __mgr._M_alloc_size());
+       // Use the stored token to retrieve the original pointer.
+       _M_alloc.deallocate(__mgr._M_unadjust((char*)__ptr),
+           __mgr._M_alloc_size());
       }
 
       virtual bool
       do_is_equal(const memory_resource& __other) const noexcept override
       {
+#if __cpp_rtti
        if (auto __p = dynamic_cast<const __resource_adaptor_imp*>(&__other))
          return _M_alloc == __p->_M_alloc;
+#else
+       if (this == &__other) // Need RTTI to do better than this.
+         return true;
+#endif
        return false;
       }
 
     private:
+      template<size_t _Num>
+       struct _Aligned_type { alignas(_Num) char __c[_Num]; };
+
+      // Rebind the allocator to the specified type and use it to allocate.
+      template<size_t _Num, typename _Tp = _Aligned_type<_Num>>
+       void*
+       _M_allocate(size_t __bytes)
+       {
+         typename allocator_traits<_Alloc>::template
+           rebind_alloc<_Tp> __a2(_M_alloc);
+         const size_t __n = (__bytes + _Num - 1) / _Num;
+         return __a2.allocate(__n);
+       }
+
+      // Rebind the allocator to the specified type and use it to deallocate.
+      template<size_t _Num, typename _Tp = _Aligned_type<_Num>>
+       void
+       _M_deallocate(void* __ptr, size_t __bytes) noexcept
+       {
+         typename allocator_traits<_Alloc>::template
+           rebind_alloc<_Tp> __a2(_M_alloc);
+         const size_t __n = (__bytes + _Num - 1) / _Num;
+         __a2.deallocate((_Tp*)__ptr, __n);
+       }
+
       _Alloc _M_alloc{};
     };
 
   // Global memory resources
-  inline std::atomic<memory_resource*>&
-  __get_default_resource()
-  {
-    static atomic<memory_resource*> _S_default_resource(new_delete_resource());
-    return _S_default_resource;
-  }
 
   inline memory_resource*
   new_delete_resource() noexcept
   {
-    using type = resource_adaptor<__gnu_cxx::new_allocator<char>>;
+    using type = resource_adaptor<std::__new_allocator<char>>;
     alignas(type) static unsigned char __buf[sizeof(type)];
     static type* __r = new(__buf) type;
     return __r;
@@ -468,10 +547,34 @@ namespace pmr {
   }
 
   // The default memory resource
+
+  /// @cond undocumented
+  inline auto&
+  __get_default_resource()
+  {
+#ifndef _GLIBCXX_HAS_GTHREADS
+    struct type {
+      using value_type = memory_resource*;
+      explicit type(value_type __r) : _M_r(__r) { }
+      value_type _M_r;
+      value_type load() const { return _M_r; }
+      value_type exchange(value_type __r) { return std::__exchange(_M_r, __r); }
+    };
+#else
+    using type = atomic<memory_resource*>;
+#endif
+    alignas(type) static unsigned char __buf[sizeof(type)];
+    static type* __r = new(__buf) type(new_delete_resource());
+    return *__r;
+  }
+  /// @endcond
+
+  /// Get the current default resource.
   inline memory_resource*
   get_default_resource() noexcept
   { return __get_default_resource().load(); }
 
+  /// Change the default resource and return the previous one.
   inline memory_resource*
   set_default_resource(memory_resource* __r) noexcept
   {
@@ -479,11 +582,12 @@ namespace pmr {
       __r = new_delete_resource();
     return __get_default_resource().exchange(__r);
   }
+
 } // namespace pmr
 } // namespace fundamentals_v2
 } // namespace experimental
 
 _GLIBCXX_END_NAMESPACE_VERSION
 } // namespace std
-
-#endif
+#endif // C++14
+#endif // _GLIBCXX_EXPERIMENTAL_MEMORY_RESOURCE