From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 8EC193858C53; Tue, 12 Apr 2022 21:39:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8EC193858C53 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-8122] libstdc++: Use nothrow new in std::stacktrace X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/master X-Git-Oldrev: 2ce0f5185ba95131b3c538507323d8ecb561a0c2 X-Git-Newrev: e48933f00d676658ad065d9691a1b61446745386 Message-Id: <20220412213927.8EC193858C53@sourceware.org> Date: Tue, 12 Apr 2022 21:39:27 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 12 Apr 2022 21:39:27 -0000 https://gcc.gnu.org/g:e48933f00d676658ad065d9691a1b61446745386 commit r12-8122-ge48933f00d676658ad065d9691a1b61446745386 Author: Jonathan Wakely Date: Tue Apr 12 10:35:43 2022 +0100 libstdc++: Use nothrow new in std::stacktrace 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. Diff: --- 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 {