From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by sourceware.org (Postfix) with ESMTP id 1AC8D3854812 for ; Tue, 3 Aug 2021 14:11:18 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 1AC8D3854812 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-563-3IM_FVNwNZKop2oTMYx_lA-1; Tue, 03 Aug 2021 10:11:15 -0400 X-MC-Unique: 3IM_FVNwNZKop2oTMYx_lA-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id B9D4D802923; Tue, 3 Aug 2021 14:11:14 +0000 (UTC) Received: from localhost (unknown [10.33.36.118]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4C6745DEFB; Tue, 3 Aug 2021 14:11:14 +0000 (UTC) Date: Tue, 3 Aug 2021 15:11:13 +0100 From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Avoid using std::unique_ptr in Message-ID: MIME-Version: 1.0 X-Clacks-Overhead: GNU Terry Pratchett X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: multipart/mixed; boundary="iu2KBmNW8T2wOSYb" Content-Disposition: inline X-Spam-Status: No, score=-14.2 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libstdc++@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++ mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 03 Aug 2021 14:11:25 -0000 --iu2KBmNW8T2wOSYb Content-Type: text/plain; charset=us-ascii Content-Disposition: inline std::wstring_convert and std::wbuffer_convert types are not copyable or movable, and store a plain pointer without a deleter. That means a much simpler type that just uses delete in its destructor can be used instead of std::unique_ptr. That avoids including and parsing all of in every header that includes . It also avoids instantiating unique_ptr and std::tuple> when the conversion utilities are used. Signed-off-by: Jonathan Wakely libstdc++-v3/ChangeLog: * include/bits/locale_conv.h (__detail::_Scoped_ptr): Define new RAII class template. (wstring_convert, wbuffer_convert): Use __detail::_Scoped_ptr instead of unique_ptr. Tested powerpc64le-linux. Committed to trunk. --iu2KBmNW8T2wOSYb Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="patch.txt" commit a1a2654cdc90e9aa561a0e853b4b1372892afb70 Author: Jonathan Wakely Date: Mon Aug 2 17:12:52 2021 libstdc++: Avoid using std::unique_ptr in std::wstring_convert and std::wbuffer_convert types are not copyable or movable, and store a plain pointer without a deleter. That means a much simpler type that just uses delete in its destructor can be used instead of std::unique_ptr. That avoids including and parsing all of in every header that includes . It also avoids instantiating unique_ptr and std::tuple> when the conversion utilities are used. Signed-off-by: Jonathan Wakely libstdc++-v3/ChangeLog: * include/bits/locale_conv.h (__detail::_Scoped_ptr): Define new RAII class template. (wstring_convert, wbuffer_convert): Use __detail::_Scoped_ptr instead of unique_ptr. diff --git a/libstdc++-v3/include/bits/locale_conv.h b/libstdc++-v3/include/bits/locale_conv.h index 0e409da9876..6af8a5bdc8f 100644 --- a/libstdc++-v3/include/bits/locale_conv.h +++ b/libstdc++-v3/include/bits/locale_conv.h @@ -38,7 +38,6 @@ #include #include #include -#include namespace std _GLIBCXX_VISIBILITY(default) { @@ -221,6 +220,39 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION #endif // _GLIBCXX_USE_CHAR8_T + namespace __detail + { + template + struct _Scoped_ptr + { + __attribute__((__nonnull__(2))) + explicit + _Scoped_ptr(_Tp* __ptr) noexcept + : _M_ptr(__ptr) + { } + + _Scoped_ptr(_Tp* __ptr, const char* __msg) + : _M_ptr(__ptr) + { + if (!__ptr) + __throw_logic_error(__msg); + } + + ~_Scoped_ptr() { delete _M_ptr; } + + _Scoped_ptr(const _Scoped_ptr&) = delete; + _Scoped_ptr& operator=(const _Scoped_ptr&) = delete; + + __attribute__((__returns_nonnull__)) + _Tp* operator->() const noexcept { return _M_ptr; } + + _Tp& operator*() const noexcept { return *_M_ptr; } + + private: + _Tp* _M_ptr; + }; + } + #ifdef _GLIBCXX_USE_WCHAR_T _GLIBCXX_BEGIN_NAMESPACE_CXX11 @@ -247,11 +279,8 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 * Takes ownership of @p __pcvt and will delete it in the destructor. */ explicit - wstring_convert(_Codecvt* __pcvt) : _M_cvt(__pcvt) - { - if (!_M_cvt) - __throw_logic_error("wstring_convert"); - } + wstring_convert(_Codecvt* __pcvt) : _M_cvt(__pcvt, "wstring_convert") + { } /** Construct with an initial converstion state. * @@ -262,11 +291,9 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 * The object's conversion state will persist between conversions. */ wstring_convert(_Codecvt* __pcvt, state_type __state) - : _M_cvt(__pcvt), _M_state(__state), _M_with_cvtstate(true) - { - if (!_M_cvt) - __throw_logic_error("wstring_convert"); - } + : _M_cvt(__pcvt, "wstring_convert"), + _M_state(__state), _M_with_cvtstate(true) + { } /** Construct with error strings. * @@ -279,10 +306,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 : _M_cvt(new _Codecvt), _M_byte_err_string(__byte_err), _M_wide_err_string(__wide_err), _M_with_strings(true) - { - if (!_M_cvt) - __throw_logic_error("wstring_convert"); - } + { } ~wstring_convert() = default; @@ -370,7 +394,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 state_type state() const { return _M_state; } private: - unique_ptr<_Codecvt> _M_cvt; + __detail::_Scoped_ptr<_Codecvt> _M_cvt; byte_string _M_byte_err_string; wide_string _M_wide_err_string; state_type _M_state = state_type(); @@ -405,13 +429,9 @@ _GLIBCXX_END_NAMESPACE_CXX11 explicit wbuffer_convert(streambuf* __bytebuf, _Codecvt* __pcvt = new _Codecvt, state_type __state = state_type()) - : _M_buf(__bytebuf), _M_cvt(__pcvt), _M_state(__state) + : _M_buf(__bytebuf), _M_cvt(__pcvt, "wbuffer_convert"), + _M_state(__state), _M_always_noconv(_M_cvt->always_noconv()) { - if (!_M_cvt) - __throw_logic_error("wbuffer_convert"); - - _M_always_noconv = _M_cvt->always_noconv(); - if (_M_buf) { this->setp(_M_put_area, _M_put_area + _S_buffer_length); @@ -593,9 +613,9 @@ _GLIBCXX_END_NAMESPACE_CXX11 return __next != __first; } - streambuf* _M_buf; - unique_ptr<_Codecvt> _M_cvt; - state_type _M_state; + streambuf* _M_buf; + __detail::_Scoped_ptr<_Codecvt> _M_cvt; + state_type _M_state; static const streamsize _S_buffer_length = 32; static const streamsize _S_putback_length = 3; --iu2KBmNW8T2wOSYb--