diff mbox

Only do shrink_to_fit() when exceptions enabled

Message ID 20150917145644.GD2969@redhat.com
State New
Headers show

Commit Message

Jonathan Wakely Sept. 17, 2015, 2:56 p.m. UTC
When exceptions are disabled a failed allocation while trying to
shrink_to_fit() will abort the program. Since shrink_to_fit() is a
non-binding request we should just ignore it rather than risk taking
down the whole process.

Tested powerpc64le-linux, committed to trunk.

Comments

Jonathan Wakely Nov. 25, 2015, 4:12 p.m. UTC | #1
On 17/09/15 15:56 +0100, Jonathan Wakely wrote:
>When exceptions are disabled a failed allocation while trying to
>shrink_to_fit() will abort the program. Since shrink_to_fit() is a
>non-binding request we should just ignore it rather than risk taking
>down the whole process.
>
>Tested powerpc64le-linux, committed to trunk.

Also committed to gcc-5-branch.


>commit 13cf19282acf42a52d5eacd2c293a944bd3e5ebe
>Author: Jonathan Wakely <jwakely@redhat.com>
>Date:   Thu Sep 17 00:07:33 2015 +0100
>
>    Only do shrink_to_fit() when exceptions enabled
>    
>    	* include/bits/allocator.h (__shrink_to_fit_aux<T, true>::_S_do_it):
>    	Do nothing if exceptions are disabled.
>    	* include/bits/basic_string.h (basic_string::shrink_to_fit): Likewise.
>
>diff --git a/libstdc++-v3/include/bits/allocator.h b/libstdc++-v3/include/bits/allocator.h
>index 6fd3214..0131521 100644
>--- a/libstdc++-v3/include/bits/allocator.h
>+++ b/libstdc++-v3/include/bits/allocator.h
>@@ -209,15 +209,19 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>       static bool
>       _S_do_it(_Tp& __c) noexcept
>       {
>-	__try
>+#if __cpp_exceptions
>+	try
> 	  {
> 	    _Tp(__make_move_if_noexcept_iterator(__c.begin()),
> 		__make_move_if_noexcept_iterator(__c.end()),
> 		__c.get_allocator()).swap(__c);
> 	    return true;
> 	  }
>-	__catch(...)
>+	catch(...)
> 	  { return false; }
>+#else
>+	return false;
>+#endif
>       }
>     };
> #endif
>diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h
>index e6e7bb5..b5e7e36 100644
>--- a/libstdc++-v3/include/bits/basic_string.h
>+++ b/libstdc++-v3/include/bits/basic_string.h
>@@ -833,13 +833,15 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
>       void
>       shrink_to_fit() noexcept
>       {
>+#if __cpp_exceptions
> 	if (capacity() > size())
> 	  {
>-	    __try
>+	    try
> 	      { reserve(0); }
>-	    __catch(...)
>+	    catch(...)
> 	      { }
> 	  }
>+#endif
>       }
> #endif
> 
>@@ -3282,12 +3284,14 @@ _GLIBCXX_END_NAMESPACE_CXX11
>       void
>       shrink_to_fit() _GLIBCXX_NOEXCEPT
>       {
>+#if __cpp_exceptions
> 	if (capacity() > size())
> 	  {
>-	    __try
>+	    try
> 	      { reserve(0); }
>-	    __catch(...)
>+	    catch(...)
> 	      { }
>+#endif
> 	  }
>       }
> #endif
diff mbox

Patch

commit 13cf19282acf42a52d5eacd2c293a944bd3e5ebe
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Thu Sep 17 00:07:33 2015 +0100

    Only do shrink_to_fit() when exceptions enabled
    
    	* include/bits/allocator.h (__shrink_to_fit_aux<T, true>::_S_do_it):
    	Do nothing if exceptions are disabled.
    	* include/bits/basic_string.h (basic_string::shrink_to_fit): Likewise.

diff --git a/libstdc++-v3/include/bits/allocator.h b/libstdc++-v3/include/bits/allocator.h
index 6fd3214..0131521 100644
--- a/libstdc++-v3/include/bits/allocator.h
+++ b/libstdc++-v3/include/bits/allocator.h
@@ -209,15 +209,19 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
       static bool
       _S_do_it(_Tp& __c) noexcept
       {
-	__try
+#if __cpp_exceptions
+	try
 	  {
 	    _Tp(__make_move_if_noexcept_iterator(__c.begin()),
 		__make_move_if_noexcept_iterator(__c.end()),
 		__c.get_allocator()).swap(__c);
 	    return true;
 	  }
-	__catch(...)
+	catch(...)
 	  { return false; }
+#else
+	return false;
+#endif
       }
     };
 #endif
diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h
index e6e7bb5..b5e7e36 100644
--- a/libstdc++-v3/include/bits/basic_string.h
+++ b/libstdc++-v3/include/bits/basic_string.h
@@ -833,13 +833,15 @@  _GLIBCXX_BEGIN_NAMESPACE_CXX11
       void
       shrink_to_fit() noexcept
       {
+#if __cpp_exceptions
 	if (capacity() > size())
 	  {
-	    __try
+	    try
 	      { reserve(0); }
-	    __catch(...)
+	    catch(...)
 	      { }
 	  }
+#endif
       }
 #endif
 
@@ -3282,12 +3284,14 @@  _GLIBCXX_END_NAMESPACE_CXX11
       void
       shrink_to_fit() _GLIBCXX_NOEXCEPT
       {
+#if __cpp_exceptions
 	if (capacity() > size())
 	  {
-	    __try
+	    try
 	      { reserve(0); }
-	    __catch(...)
+	    catch(...)
 	      { }
+#endif
 	  }
       }
 #endif