From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 516143857C6F for ; Tue, 12 Apr 2022 21:41:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 516143857C6F 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-47-FYrFOGc1OPan3xm8M3EKBA-1; Tue, 12 Apr 2022 17:41:31 -0400 X-MC-Unique: FYrFOGc1OPan3xm8M3EKBA-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 7064B29AA389; Tue, 12 Apr 2022 21:41:30 +0000 (UTC) Received: from localhost (unknown [10.33.36.22]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2D1244047D1C; Tue, 12 Apr 2022 21:41:30 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed 2/5] libstdc++: Use nothrow new in std::stacktrace Date: Tue, 12 Apr 2022 22:41:25 +0100 Message-Id: <20220412214128.509227-2-jwakely@redhat.com> In-Reply-To: <20220412214128.509227-1-jwakely@redhat.com> References: <20220412214128.509227-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.11.54.2 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true 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, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libstdc++@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++ mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 12 Apr 2022 21:41:34 -0000 Tested powerpc64-linux, pushed to trunk. -- >8 -- We can avoid the overhead of handling a bad_alloc exception from std::allocator::allocate by just calling the nothrow operator new instead. libstdc++-v3/ChangeLog: * include/std/stacktrace (basic_stacktrace::_Impl::_M_allocate): Use nothrow new instead of try block for std::allocator. (basic_stacktrace::_Impl::_M_deallocate): Use delete for std::allocator. --- libstdc++-v3/include/std/stacktrace | 42 ++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/libstdc++-v3/include/std/stacktrace b/libstdc++-v3/include/std/stacktrace index 5f928f10dee..f36c5a9abef 100644 --- a/libstdc++-v3/include/std/stacktrace +++ b/libstdc++-v3/include/std/stacktrace @@ -30,6 +30,7 @@ #if __cplusplus > 202002L && _GLIBCXX_HAVE_STACKTRACE #include +#include #include #include #include @@ -589,23 +590,43 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return std::min(__size_max, __alloc_max); } +#if __has_builtin(__builtin_operator_new) >= 201802L +# define _GLIBCXX_OPERATOR_NEW __builtin_operator_new +# define _GLIBCXX_OPERATOR_DELETE __builtin_operator_delete +#else +# define _GLIBCXX_OPERATOR_NEW ::operator new +# define _GLIBCXX_OPERATOR_DELETE ::operator delete +#endif + // Precondition: _M_frames == nullptr && __n != 0 pointer _M_allocate(allocator_type& __alloc, size_type __n) noexcept { if (__n <= _S_max_size(__alloc)) [[likely]] { - __try + if constexpr (is_same_v>) { - _M_frames = __alloc.allocate(__n); - _M_capacity = __n; - return _M_frames; + __n *= sizeof(value_type); + void* const __p = _GLIBCXX_OPERATOR_NEW (__n, nothrow_t{}); + if (__p == nullptr) [[unlikely]] + return nullptr; + _M_frames = static_cast(__p); } - __catch (...) + else { + __try + { + _M_frames = __alloc.allocate(__n); + } + __catch (const std::bad_alloc&) + { + return nullptr; + } } + _M_capacity = __n; + return _M_frames; } - return nullptr;; + return nullptr; } void @@ -613,12 +634,19 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { if (_M_capacity) { - __alloc.deallocate(_M_frames, _M_capacity); + if constexpr (is_same_v>) + _GLIBCXX_OPERATOR_DELETE (static_cast(_M_frames), + _M_capacity * sizeof(value_type)); + else + __alloc.deallocate(_M_frames, _M_capacity); _M_frames = nullptr; _M_capacity = 0; } } +#undef _GLIBCXX_OPERATOR_DELETE +#undef _GLIBCXX_OPERATOR_NEW + void _M_destroy() noexcept { -- 2.34.1