From 1f3f99e85f0051f03c86dbbed98ca4fb4b6dc358 Mon Sep 17 00:00:00 2001 From: Vittorio Romeo Date: Fri, 24 Jun 2022 02:07:59 +0200 Subject: [PATCH 1/3] Remove `` dependency from `std::unique_ptr` --- libstdc++-v3/include/bits/unique_ptr.h | 143 ++++++++++++++++++++++--- 1 file changed, 127 insertions(+), 16 deletions(-) diff --git a/libstdc++-v3/include/bits/unique_ptr.h b/libstdc++-v3/include/bits/unique_ptr.h index e1ad7721a5935..d33d2829c0713 100644 --- a/libstdc++-v3/include/bits/unique_ptr.h +++ b/libstdc++-v3/include/bits/unique_ptr.h @@ -33,7 +33,6 @@ #include #include #include -#include #include #include #if __cplusplus > 201703L @@ -65,11 +64,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION #pragma GCC diagnostic pop #endif - /** Primary template of default_delete, used by unique_ptr for single objects - * - * @headerfile memory - * @since C++11 - */ + /// Primary template of default_delete, used by unique_ptr for single objects + /// @since C++11 template struct default_delete { @@ -102,11 +98,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION // _GLIBCXX_RESOLVE_LIB_DEFECTS // DR 740 - omit specialization for array objects with a compile time length - /** Specialization of default_delete for arrays, used by `unique_ptr` - * - * @headerfile memory - * @since C++11 - */ + /// Specialization of default_delete for arrays, used by `unique_ptr` template struct default_delete<_Tp[]> { @@ -140,6 +132,125 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } }; + namespace __detail + { +#if __has_cpp_attribute(__no_unique_address__) + template + struct _Holder + { + [[__no_unique_address__]] _Tp __data0; + [[__no_unique_address__]] _Dp __data1; + + _Holder() = default; + + template + _Holder(_TpFwd&& __tpFwd) + : __data0(std::forward<_TpFwd>(__tpFwd)) + { + } + + template + _Holder(_TpFwd&& __tpFwd, _DpFwd&& __dpFwd) + : __data0(std::forward<_TpFwd>(__tpFwd)), + __data1(std::forward<_DpFwd>(__dpFwd)) + { + } + + _Tp& _Fst() noexcept + { + return __data0; + } + + const _Tp& _Fst() const noexcept + { + return __data0; + } + + _Dp& _Dst() noexcept + { + return __data1; + } + + const _Dp& _Dst() const noexcept + { + return __data1; + } + }; +#else + template + struct _TaggedHolderBase + { + _Tp __data; + + _TaggedHolderBase() = default; + + template + _TaggedHolderBase(_TpFwd&& __tpFwd) + : __data(std::forward<_TpFwd>(__tpFwd)) + { + } + + _Tp& _Get() noexcept { return __data; } + const _Tp& _Get() const noexcept { return __data; } + }; + + template + struct _TaggedHolderBase<_Ip, _Tp, std::enable_if_t>> : _Tp + { + _TaggedHolderBase() = default; + + template + _TaggedHolderBase(_TpFwd&& __tpFwd) + : _Tp(std::forward<_TpFwd>(__tpFwd)) + { + } + + _Tp& _Get() noexcept { return *this; } + const _Tp& _Get() const noexcept { return *this; } + }; + + template + struct _Holder : _TaggedHolderBase<0, _Tp>, _TaggedHolderBase<1, _Dp> + { + using _TpBase = _TaggedHolderBase<0, _Tp>; + using _DpBase = _TaggedHolderBase<1, _Dp>; + + template + _Holder(_TpFwd&& __tpFwd) + : _TpBase(std::forward<_TpFwd>(__tpFwd)) + { + } + + template + _Holder(_TpFwd&& __tpFwd, _DpFwd&& __dpFwd) + : _TpBase(std::forward<_TpFwd>(__tpFwd)), + _DpBase(std::forward<_DpFwd>(__dpFwd)) + { + } + + _Tp& _Fst() noexcept + { + return static_cast<_TpBase&>(*this)._Get(); + } + + const _Tp& _Fst() const noexcept + { + return static_cast(*this)._Get(); + } + + _Dp& _Dst() noexcept + { + return static_cast<_DpBase&>(*this)._Get(); + } + + const _Dp& _Dst() const noexcept + { + return static_cast(*this)._Get(); + } + }; +#endif + } + /// @cond undocumented // Manages the pointer and deleter of a unique_ptr @@ -193,13 +304,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } _GLIBCXX23_CONSTEXPR - pointer& _M_ptr() noexcept { return std::get<0>(_M_t); } + pointer& _M_ptr() noexcept { return _M_t._Fst(); } _GLIBCXX23_CONSTEXPR - pointer _M_ptr() const noexcept { return std::get<0>(_M_t); } + pointer _M_ptr() const noexcept { return _M_t._Fst(); } _GLIBCXX23_CONSTEXPR - _Dp& _M_deleter() noexcept { return std::get<1>(_M_t); } + _Dp& _M_deleter() noexcept { return _M_t._Dst(); } _GLIBCXX23_CONSTEXPR - const _Dp& _M_deleter() const noexcept { return std::get<1>(_M_t); } + const _Dp& _M_deleter() const noexcept { return _M_t._Dst(); } _GLIBCXX23_CONSTEXPR void reset(pointer __p) noexcept @@ -228,7 +339,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } private: - tuple _M_t; + __detail::_Holder _M_t; }; // Defines move construction + assignment as either defaulted or deleted. From 622dfdc456ef6bf7b511a0ccd0ddbb3572c5c50d Mon Sep 17 00:00:00 2001 From: Vittorio Romeo Date: Fri, 24 Jun 2022 02:14:34 +0200 Subject: [PATCH 2/3] Update unique_ptr.h --- libstdc++-v3/include/bits/unique_ptr.h | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/libstdc++-v3/include/bits/unique_ptr.h b/libstdc++-v3/include/bits/unique_ptr.h index d33d2829c0713..9f44dcfee7cee 100644 --- a/libstdc++-v3/include/bits/unique_ptr.h +++ b/libstdc++-v3/include/bits/unique_ptr.h @@ -64,14 +64,16 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION #pragma GCC diagnostic pop #endif - /// Primary template of default_delete, used by unique_ptr for single objects - /// @since C++11 + /** Primary template of default_delete, used by unique_ptr for single objects + * + * @headerfile memory + * @since C++11 + */ template struct default_delete { /// Default constructor constexpr default_delete() noexcept = default; - /** @brief Converting constructor. * * Allows conversion from a deleter for objects of another type, `_Up`, @@ -81,7 +83,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION typename = _Require>> _GLIBCXX23_CONSTEXPR default_delete(const default_delete<_Up>&) noexcept { } - /// Calls `delete __ptr` _GLIBCXX23_CONSTEXPR void @@ -94,11 +95,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION delete __ptr; } }; - // _GLIBCXX_RESOLVE_LIB_DEFECTS // DR 740 - omit specialization for array objects with a compile time length - /// Specialization of default_delete for arrays, used by `unique_ptr` + /** Specialization of default_delete for arrays, used by `unique_ptr` + * + * @headerfile memory + * @since C++11 + */ template struct default_delete<_Tp[]> { From c40c98006787fc5cbaee72160af91779c2cb6c75 Mon Sep 17 00:00:00 2001 From: Vittorio Romeo Date: Fri, 24 Jun 2022 02:15:26 +0200 Subject: [PATCH 3/3] Update unique_ptr.h --- libstdc++-v3/include/bits/unique_ptr.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/libstdc++-v3/include/bits/unique_ptr.h b/libstdc++-v3/include/bits/unique_ptr.h index 9f44dcfee7cee..38917cb9fccb6 100644 --- a/libstdc++-v3/include/bits/unique_ptr.h +++ b/libstdc++-v3/include/bits/unique_ptr.h @@ -74,6 +74,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { /// Default constructor constexpr default_delete() noexcept = default; + /** @brief Converting constructor. * * Allows conversion from a deleter for objects of another type, `_Up`, @@ -83,6 +84,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION typename = _Require>> _GLIBCXX23_CONSTEXPR default_delete(const default_delete<_Up>&) noexcept { } + /// Calls `delete __ptr` _GLIBCXX23_CONSTEXPR void @@ -95,6 +97,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION delete __ptr; } }; + // _GLIBCXX_RESOLVE_LIB_DEFECTS // DR 740 - omit specialization for array objects with a compile time length