From patchwork Tue May 17 19:56:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1632533 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=RGUIPkS4; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4L2n6n41S7z9s0w for ; Wed, 18 May 2022 06:00:49 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A6FD73857815 for ; Tue, 17 May 2022 20:00:47 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A6FD73857815 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1652817647; bh=4MQrxJgVt/ScpghYerw3HHpu4/DfdpxK1Lt4YtYFHDE=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=RGUIPkS4ZVUtM2j3I7K1DcLbgfDj+05nuf6y0FpBCr5qJYfG53qj+Ki3qNvWbVSGS +E2fql+VfgPyFjzFKtg8dH3PB9K8j4Y5421/2Xet4rsRn79W31hbw29rhJyvXfeBOj bYVzowL1epizu47c8dWKLagLW9RNlq0H6Nal8skU= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 7AF823857343 for ; Tue, 17 May 2022 19:56:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 7AF823857343 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-164-dTi3sTiTOwWL39RwPKf_xQ-1; Tue, 17 May 2022 15:56:30 -0400 X-MC-Unique: dTi3sTiTOwWL39RwPKf_xQ-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id A2F313C01D91; Tue, 17 May 2022 19:56:29 +0000 (UTC) Received: from localhost (unknown [10.33.36.185]) by smtp.corp.redhat.com (Postfix) with ESMTP id 69E8BC27D8F; Tue, 17 May 2022 19:56:29 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Add attributes to functions in Date: Tue, 17 May 2022 20:56:28 +0100 Message-Id: <20220517195628.164318-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.85 on 10.11.54.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jonathan Wakely via Gcc-patches From: Jonathan Wakely Reply-To: Jonathan Wakely Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "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. 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. --- 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 #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); } @@ -369,6 +387,7 @@ namespace pmr }; template + [[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 + [[nodiscard]] inline bool operator!=(const polymorphic_allocator<_Tp1>& __a, const polymorphic_allocator<_Tp2>& __b) noexcept