public inbox for
 help / color / mirror / Atom feed
* [committed] libstdc++: Add attributes to functions in <memory_resource>
@ 2022-05-17 19:56 Jonathan Wakely
  0 siblings, 0 replies; only message in thread
From: Jonathan Wakely @ 2022-05-17 19:56 UTC (permalink / raw)
  To: libstdc++, gcc-patches

Tested powerpc64le-linux, pushed to trunk.

-- >8 --

Add attributes to the accessors for the global memory resource objects,
to allow the compiler to eliminate redundant calls to them. For example,
multiple calls to std::pmr::new_delete_resource() will always return the
same object, and so the compiler can replace them with a single call.

Ideally we would like adjacent calls to std::pmr::get_default_resource()
to be combined into a single call by the CSE pass. The 'pure' attribute
would permit that. However, the standard requires that calls to
std::pmr::set_default_resource() synchronize with subsequent calls to
std::pmr::get_default_resource().  With 'pure' the DCE pass might
eliminate seemingly redundant calls to std::pmr::get_default_resource().
That might be unsafe, because the caller might be relying on the
associated synchronization. We could use a hypothetical attribute that
allows CSE but not DCE, but we don't have one. So it can't be 'pure'.

Also add [[nodiscard]] to equality operators.


	* include/std/memory_resource (new_delete_resource): Add
	nodiscard, returns_nonnull and const attributes.
	(null_memory_resource): Likewise.
	(set_default_resource, get_default_resource): Add returns_nonnull
	(memory_resource::is_equal): Add nodiscard attribute.
	(operator==, operator!=): Likewise.
 libstdc++-v3/include/std/memory_resource | 30 ++++++++++++++++++++----
 1 file changed, 25 insertions(+), 5 deletions(-)

diff --git a/libstdc++-v3/include/std/memory_resource b/libstdc++-v3/include/std/memory_resource
index 4a18d3e8598..88e8abd60fa 100644
--- a/libstdc++-v3/include/std/memory_resource
+++ b/libstdc++-v3/include/std/memory_resource
@@ -74,11 +74,26 @@ namespace pmr
   // Global memory resources
-  memory_resource* new_delete_resource() noexcept;
-  memory_resource* null_memory_resource() noexcept;
-  memory_resource* set_default_resource(memory_resource* __r) noexcept;
-  memory_resource* get_default_resource() noexcept
-    __attribute__((__returns_nonnull__));
+  /// A pmr::memory_resource that uses `new` to allocate memory
+  [[nodiscard, __gnu__::__returns_nonnull__, __gnu__::__const__]]
+  memory_resource*
+  new_delete_resource() noexcept;
+  /// A pmr::memory_resource that always throws `bad_alloc`
+  [[nodiscard, __gnu__::__returns_nonnull__, __gnu__::__const__]]
+  memory_resource*
+  null_memory_resource() noexcept;
+  /// Replace the default memory resource pointer
+  [[__gnu__::__returns_nonnull__]]
+  memory_resource*
+  set_default_resource(memory_resource* __r) noexcept;
+  /// Get the current default memory resource pointer
+  [[__gnu__::__returns_nonnull__]]
+  memory_resource*
+  get_default_resource() noexcept;
   // Pool resource classes
   struct pool_options;
@@ -111,6 +126,7 @@ namespace pmr
     { return do_deallocate(__p, __bytes, __alignment); }
+    [[nodiscard]]
     is_equal(const memory_resource& __other) const noexcept
     { return do_is_equal(__other); }
@@ -126,11 +142,13 @@ namespace pmr
     do_is_equal(const memory_resource& __other) const noexcept = 0;
+  [[nodiscard]]
   inline bool
   operator==(const memory_resource& __a, const memory_resource& __b) noexcept
   { return &__a == &__b || __a.is_equal(__b); }
 #if __cpp_impl_three_way_comparison < 201907L
+  [[nodiscard]]
   inline bool
   operator!=(const memory_resource& __a, const memory_resource& __b) noexcept
   { return !(__a == __b); }
@@ -369,6 +387,7 @@ namespace pmr
   template<typename _Tp1, typename _Tp2>
+    [[nodiscard]]
     inline bool
     operator==(const polymorphic_allocator<_Tp1>& __a,
 	       const polymorphic_allocator<_Tp2>& __b) noexcept
@@ -376,6 +395,7 @@ namespace pmr
 #if __cpp_impl_three_way_comparison < 201907L
   template<typename _Tp1, typename _Tp2>
+    [[nodiscard]]
     inline bool
     operator!=(const polymorphic_allocator<_Tp1>& __a,
 	       const polymorphic_allocator<_Tp2>& __b) noexcept

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-05-17 19:56 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-17 19:56 [committed] libstdc++: Add attributes to functions in <memory_resource> Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).