public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely@redhat.com>
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	[thread overview]
Message-ID: <20220412214128.509227-2-jwakely@redhat.com> (raw)
In-Reply-To: <20220412214128.509227-1-jwakely@redhat.com>

Tested powerpc64-linux, pushed to trunk.

-- >8 --

We can avoid the overhead of handling a bad_alloc exception from
std::allocator<std::stacktrace_entry>::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 <compare>
+#include <new>
 #include <string>
 #include <sstream>
 #include <bits/stl_algobase.h>
@@ -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<allocator_type, allocator<value_type>>)
 		{
-		  _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<pointer>(__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<allocator_type, allocator<value_type>>)
+		_GLIBCXX_OPERATOR_DELETE (static_cast<void*>(_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


  reply	other threads:[~2022-04-12 21:41 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-12 21:41 [committed 1/5] libstdc++: Reduce memory usage in std::stacktrace::current Jonathan Wakely
2022-04-12 21:41 ` Jonathan Wakely [this message]
2022-04-12 21:41 ` [committed 3/5] libstdc++: Use allocator to construct std::stacktrace_entry objects Jonathan Wakely
2022-04-12 21:41 ` [committed 4/5] libstdc++: shrink-to-fit in std::basic_stacktrace::current(skip, max) Jonathan Wakely
2022-04-12 21:41 ` [committed 5/5] libstdc++: Prefer to use mmap instead of malloc in libbacktrace Jonathan Wakely

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220412214128.509227-2-jwakely@redhat.com \
    --to=jwakely@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=libstdc++@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).