[patch] Only do shrink_to_fit() when exceptions enabled

Jonathan Wakely jwakely@redhat.com
Wed Nov 25 16:19:00 GMT 2015


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



More information about the Gcc-patches mailing list