This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Add configure flag for operator new (std::nothrow)


Currently, whenever operator new (std::nothrow) fails to allocate memory, it'll
check if there is a new-handler function available. If there is, it'll call
the handler and then try to allocate again. Otherwise, it'll return a null pointer.

This retrying behavior may not always be desirable. If the handler cannot fix
the memory allocation issue, we may end up being stuck in an infinite loop.
Whereas returning nullptr may be a valid alternative to keep calling the new_handler.
The workaround to end the loop, we would have to call std::set_new_handler(nullptr)
from within the handler itself, which gets complicated if the handler has to be
re-setted afterwards.

This patch adds the new_nothrow_no_retry configuration flag, which, if enabled,
will change the retrying behavior of operator new (std::nothrow) so that it only calls
the handler once when it fails to allocate memory and the return nullptr.
I have a company-wide copyright assignment, but I don't have commit access.

---
 ChangeLog                                 | 9 +++++++++
 libstdc++-v3/acinclude.m4                 | 9 +++++++++
 libstdc++-v3/configure.ac                 | 1 +
 libstdc++-v3/doc/xml/manual/configure.xml | 7 +++++++
 libstdc++-v3/libsupc++/new_opnt.cc        | 4 ++++
 5 files changed, 30 insertions(+)

diff --git a/ChangeLog b/ChangeLog
index 5b16ca2..a1cd0d3 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2015-10-30  Aurelio Remonda  <aurelio.remonda@tallertechnologies.com>
+
+	* libstdc++-v3/acinclude.m4: add enable_new_opnt_no_allocation_retry
+	flag definition.
+	* libstdc++-v3/configure.ac: add option flag
+	GLIBCXX_ENABLE_NEW_OPNT_NO_ALLOCATION_RETRY
+	* libstdc++-v3/libsupc++/new_opnt.cc use the defined macro
+	* libstdc++-v3/doc/xml/manual/configure.xml
+
 2015-10-09  Martin Liska  <mliska@suse.cz>
 
 	* MAINTAINERS (Write After Approval): Add myself.
diff --git a/libstdc++-v3/acinclude.m4 b/libstdc++-v3/acinclude.m4
index abf2e93..c8f7a75 100644
--- a/libstdc++-v3/acinclude.m4
+++ b/libstdc++-v3/acinclude.m4
@@ -2629,6 +2629,15 @@ AC_DEFUN([GLIBCXX_ENABLE_LONG_LONG], [
   AC_MSG_RESULT([$enable_long_long])
 ])
 
+AC_DEFUN([GLIBCXX_ENABLE_NEW_OPNT_NO_ALLOCATION_RETRY], [
+  GLIBCXX_ENABLE(new-opnt-no-allocation-retry,$1,,[enable new nothrow no allocation retry condition])
+  if test $enable_new_opnt_no_allocation_retry = yes; then
+    AC_DEFINE(_GLIBCXX_NEW_OPNT_NO_ALLOCATION_RETRY, 1,
+        [Define if operator new (std::nothrow) will retry allocation after callin the handler.])
+  fi
+  AC_MSG_CHECKING([whether no-throw operator new should retry allocation after calling the new handler])
+  AC_MSG_RESULT([$enable_new_opnt_no_allocation_retry])
+])
 
 dnl
 dnl Check for decimal floating point.
diff --git a/libstdc++-v3/configure.ac b/libstdc++-v3/configure.ac
index 3456348..2f3aaa9 100644
--- a/libstdc++-v3/configure.ac
+++ b/libstdc++-v3/configure.ac
@@ -165,6 +165,7 @@ GLIBCXX_ENABLE_CLOCALE
 GLIBCXX_ENABLE_ALLOCATOR
 GLIBCXX_ENABLE_CHEADERS($c_model)  dnl c_model from configure.host
 GLIBCXX_ENABLE_LONG_LONG([yes])
+GLIBCXX_ENABLE_NEW_OPNT_NO_ALLOCATION_RETRY([no])
 GLIBCXX_ENABLE_WCHAR_T([yes])
 GLIBCXX_ENABLE_C99([yes])
 GLIBCXX_ENABLE_CONCEPT_CHECKS([no])
diff --git a/libstdc++-v3/doc/xml/manual/configure.xml b/libstdc++-v3/doc/xml/manual/configure.xml
index 2f558d2..7787bc3 100644
--- a/libstdc++-v3/doc/xml/manual/configure.xml
+++ b/libstdc++-v3/doc/xml/manual/configure.xml
@@ -278,6 +278,13 @@
      </para>
  </listitem></varlistentry>
 
+<varlistentry><term><code>--enable-new-opnt-no-allocation-retry </code></term>
+ <listitem><para>This option will cause operator new (std::nothrow) not 
+ 	to retry allocation if a handler has been set.
+ 	The purpose of this is to call the handler just once and return.
+     </para>
+ </listitem></varlistentry>
+
  <varlistentry><term><code>--enable-fully-dynamic-string</code></term>
  <listitem><para>This option enables a special version of basic_string avoiding
 	the optimization that allocates empty objects in static memory.
diff --git a/libstdc++-v3/libsupc++/new_opnt.cc b/libstdc++-v3/libsupc++/new_opnt.cc
index a9eb465..fac86bc 100644
--- a/libstdc++-v3/libsupc++/new_opnt.cc
+++ b/libstdc++-v3/libsupc++/new_opnt.cc
@@ -40,7 +40,11 @@ operator new (std::size_t sz, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPT
   if (sz == 0)
     sz = 1;
 
+#ifdef GLIBCXX_ENABLE_NEW_OPNT_NO_ALLOCATION_RETRY
+  if (__builtin_expect ((p = malloc (sz)) == 0, false))
+#else
   while (__builtin_expect ((p = malloc (sz)) == 0, false))
+#endif
     {
       new_handler handler = std::get_new_handler ();
       if (! handler)
-- 
1.9.1


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]