From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 3F28B3882AF4; Tue, 28 Mar 2023 23:34:50 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3F28B3882AF4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1680046490; bh=IjUynkoikdPrad6hO+pGgLxm8Q5pJqSyC4i/ptZBDXI=; h=From:To:Subject:Date:From; b=dbNREHThVrSm9e+eJf+L6qXFp2q5MOrLBbLJb3Gav9Wc+NiZicrAPk6QsqIS7jnGg Kxgq0pkNWRRWMgF6I1zOb1/k7i+MotujqYfQEQJLCt1QDWnSM2b7FzZcT0YPggcCn/ nTJoZcs5FXCLMbVpNqwn1gDxghEbkdAKDdE1jzkM= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r12-9341] libstdc++: Add attributes to functions in X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/releases/gcc-12 X-Git-Oldrev: 99f7d9133595463426c6ea75c4f6f9ebf0058b6e X-Git-Newrev: 14d1ca9e5c4271e98afd025b2b36d229bcc2f6ea Message-Id: <20230328233450.3F28B3882AF4@sourceware.org> Date: Tue, 28 Mar 2023 23:34:50 +0000 (GMT) List-Id: https://gcc.gnu.org/g:14d1ca9e5c4271e98afd025b2b36d229bcc2f6ea commit r12-9341-g14d1ca9e5c4271e98afd025b2b36d229bcc2f6ea Author: Jonathan Wakely Date: Tue May 17 15:14:39 2022 +0100 libstdc++: Add attributes to functions in 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. libstdc++-v3/ChangeLog: * 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 attribute. (memory_resource::is_equal): Add nodiscard attribute. (operator==, operator!=): Likewise. (cherry picked from commit 5c2d703e6d6d47f41635ca4df06c555010462081) Diff: --- 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 5333765de05..745422ad5eb 100644 --- a/libstdc++-v3/include/std/memory_resource +++ b/libstdc++-v3/include/std/memory_resource @@ -74,11 +74,26 @@ namespace pmr #endif // 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 __attribute__((__nonnull__)) { return do_deallocate(__p, __bytes, __alignment); } + [[nodiscard]] bool 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); } @@ -385,6 +403,7 @@ namespace pmr }; template + [[nodiscard]] inline bool operator==(const polymorphic_allocator<_Tp1>& __a, const polymorphic_allocator<_Tp2>& __b) noexcept @@ -392,6 +411,7 @@ namespace pmr #if __cpp_impl_three_way_comparison < 201907L template + [[nodiscard]] inline bool operator!=(const polymorphic_allocator<_Tp1>& __a, const polymorphic_allocator<_Tp2>& __b) noexcept