From patchwork Wed May 6 08:48:05 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 134533 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 1BD3D4BA23FA for ; Wed, 6 May 2026 08:54:08 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1BD3D4BA23FA Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=DeP3fKdO X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id AF0964BA23FA for ; Wed, 6 May 2026 08:53:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AF0964BA23FA Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org AF0964BA23FA Authentication-Results: sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1778057626; cv=none; b=A8Fqs4ulrotdkzi4WaFcxZp4Aa1fh8V1SvbFSlpFxH9mj7zcVotIbkwsZ+hW1W59ogqy/fUnRuwsoW1FtafUbJc+X20xz4N4LJMSkTzHnNOpOtAaBQzq3/Yr8jErqkll1rOYuMXsRoihlsWQypHZxKGtdFXN+5D8WmtYwzKD9nk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1778057626; c=relaxed/simple; bh=/Wr5MlrL/6zbYA8Llba0sDxUgecoX3gGGvrWSkhD24w=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=Go18GHUe71CD7UNuIamfy9UmPROsX1OiF6U7Cc199Q3PwHyir+VEl3B8BoZrnfefW2O3UPxFB8dZl+jtnulO9CrrbVRWSQsgnHdgXGVEiaBkftgznpm5D8ilStJb5JUYPao28fNmNJxxgyUKJdhdaY7FvtpKLPizi1juxaXc80g= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=DeP3fKdO DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AF0964BA23FA DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1778057625; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=1UMTh/tu3k+Tu+wO1jrQP/U9KOZy7t+gOu7ZbhBEFY0=; b=DeP3fKdOs3tzQnBkEYH7/xwrAuIksyeWcoy1Uvxg0qm7MYPjqb2E4umma8k5LQ4XDwumLn FJHqB/d+ZqcLMPU2mgqU1yUYldThTwj74squLciOR2nsZ6k1RNF8SeqvSPmMwSL3gZAss5 wH7OKsxiWZ9xVzHMe1x3grAzHRL+dno= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-194-a5zWVBzeO26gPVpVncfgUQ-1; Wed, 06 May 2026 04:53:44 -0400 X-MC-Unique: a5zWVBzeO26gPVpVncfgUQ-1 X-Mimecast-MFC-AGG-ID: a5zWVBzeO26gPVpVncfgUQ_1778057623 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id B8B7D1956096; Wed, 6 May 2026 08:53:43 +0000 (UTC) Received: from zen.kayari.org (unknown [10.44.32.38]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 81E551801A63; Wed, 6 May 2026 08:53:42 +0000 (UTC) From: Jonathan Wakely To: gcc-patches@gcc.gnu.org, libstdc++@gcc.gnu.org Subject: [PATCH v2 2/3] libstdc++: Simplify std::shared_ptr internals Date: Wed, 6 May 2026 09:48:05 +0100 Message-ID: <20260506085339.325517-2-jwakely@redhat.com> In-Reply-To: <20260506085339.325517-1-jwakely@redhat.com> References: <20260506085339.325517-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: genzqw8tMpdaCYrpk3G76fdfblimFT34uKPoEj0Bfb4_1778057623 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.5 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_BLOCKED, SPF_HELO_PASS, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org Now that we don't use the EBO we can flatten the class layout by removing some _Impl classes. libstdc++-v3/ChangeLog: * include/bits/out_ptr.h (out_ptr_t::_Impl::~_Impl): Adjust access to shared_ptr internals. * include/bits/shared_ptr_base.h (_Sp_counted_deleter): Remove _Impl class and replace _M_impl with the data members it contained. (_Sp_counted_ptr_inplace): Likewise. --- v2: Use the attribute on the _M_del and _M_alloc members. Tested x86_64-linux. libstdc++-v3/include/bits/out_ptr.h | 2 +- libstdc++-v3/include/bits/shared_ptr_base.h | 60 ++++++--------------- 2 files changed, 18 insertions(+), 44 deletions(-) diff --git a/libstdc++-v3/include/bits/out_ptr.h b/libstdc++-v3/include/bits/out_ptr.h index cc5a8a3adacb..f5fdc6cf910f 100644 --- a/libstdc++-v3/include/bits/out_ptr.h +++ b/libstdc++-v3/include/bits/out_ptr.h @@ -257,7 +257,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION auto& __pi = _M_smart._M_refcount._M_pi; if (_Sp __ptr = _M_smart.get()) - static_cast<_Scd*>(__pi)->_M_impl._M_ptr = __ptr; + static_cast<_Scd*>(__pi)->_M_ptr = __ptr; else // Destroy the control block manually without invoking deleter. std::__exchange(__pi, nullptr)->_M_destroy(); } diff --git a/libstdc++-v3/include/bits/shared_ptr_base.h b/libstdc++-v3/include/bits/shared_ptr_base.h index 3ab73f6e4a0d..01fd57180ecc 100644 --- a/libstdc++-v3/include/bits/shared_ptr_base.h +++ b/libstdc++-v3/include/bits/shared_ptr_base.h @@ -547,32 +547,16 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template class _Sp_counted_deleter final : public _Sp_counted_base<_Lp> { - class _Impl - { - [[__no_unique_address__]] _Sp_ebo_helper<_Deleter> _M_d; - [[__no_unique_address__]] _Sp_ebo_helper<_Alloc> _M_a; - - public: - _Impl(_Ptr __p, _Deleter __d, const _Alloc& __a) noexcept - : _M_d{std::move(__d)}, _M_a{__a}, _M_ptr(__p) - { } - - _Deleter& _M_del() noexcept { return _M_d._M_obj; } - _Alloc& _M_alloc() noexcept { return _M_a._M_obj; } - - _Ptr _M_ptr; - }; - public: using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_deleter>; // __d(__p) must not throw. _Sp_counted_deleter(_Ptr __p, _Deleter __d) noexcept - : _M_impl(__p, std::move(__d), _Alloc()) { } + : _M_del{std::move(__d)}, _M_alloc{}, _M_ptr(__p) { } // __d(__p) must not throw. _Sp_counted_deleter(_Ptr __p, _Deleter __d, const _Alloc& __a) noexcept - : _M_impl(__p, std::move(__d), __a) { } + : _M_del{std::move(__d)}, _M_alloc{__a}, _M_ptr(__p) { } #pragma GCC diagnostic push // PR tree-optimization/122197 #pragma GCC diagnostic ignored "-Wfree-nonheap-object" @@ -582,12 +566,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION virtual void _M_dispose() noexcept - { _M_impl._M_del()(_M_impl._M_ptr); } + { _M_del._M_obj(_M_ptr); } virtual void _M_destroy() noexcept { - __allocator_type __a(_M_impl._M_alloc()); + __allocator_type __a(_M_alloc._M_obj); __allocated_ptr<__allocator_type> __guard_ptr{ __a, this }; this->~_Sp_counted_deleter(); } @@ -598,19 +582,20 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION #if __cpp_rtti // _GLIBCXX_RESOLVE_LIB_DEFECTS // 2400. shared_ptr's get_deleter() should use addressof() - return __ti == typeid(_Deleter) - ? std::__addressof(_M_impl._M_del()) - : nullptr; -#else - return nullptr; + if (__ti == typeid(_Deleter)) + return std::__addressof(_M_del._M_obj); #endif + return nullptr; } private: #ifdef __glibcxx_out_ptr template friend class out_ptr_t; #endif - _Impl _M_impl; + + [[__no_unique_address__]] _Sp_ebo_helper<_Deleter> _M_del; + [[__no_unique_address__]] _Sp_ebo_helper<_Alloc> _M_alloc; + _Ptr _M_ptr; }; // helpers for make_shared / allocate_shared @@ -640,25 +625,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template class _Sp_counted_ptr_inplace final : public _Sp_counted_base<_Lp> { - class _Impl - { - [[__no_unique_address__]] _Sp_ebo_helper<_Alloc> _M_a; - - public: - explicit _Impl(_Alloc __a) noexcept : _M_a{std::move(__a)} { } - - _Alloc& _M_alloc() noexcept { return _M_a._M_obj; } - - __gnu_cxx::__aligned_buffer<__remove_cv_t<_Tp>> _M_storage; - }; - public: using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_ptr_inplace>; // Alloc parameter is not a reference so doesn't alias anything in __args template _Sp_counted_ptr_inplace(_Alloc __a, _Args&&... __args) - : _M_impl(__a) + : _M_alloc{__a} { // _GLIBCXX_RESOLVE_LIB_DEFECTS // 2070. allocate_shared should use allocator_traits::construct @@ -674,14 +647,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION virtual void _M_dispose() noexcept { - allocator_traits<_Alloc>::destroy(_M_impl._M_alloc(), _M_ptr()); + allocator_traits<_Alloc>::destroy(_M_alloc._M_obj, _M_ptr()); } // Override because the allocator needs to know the dynamic type virtual void _M_destroy() noexcept { - __allocator_type __a(_M_impl._M_alloc()); + __allocator_type __a(_M_alloc._M_obj); __allocated_ptr<__allocator_type> __guard_ptr{ __a, this }; this->~_Sp_counted_ptr_inplace(); } @@ -711,9 +684,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } __remove_cv_t<_Tp>* - _M_ptr() noexcept { return _M_impl._M_storage._M_ptr(); } + _M_ptr() noexcept { return _M_storage._M_ptr(); } - _Impl _M_impl; + [[__no_unique_address__]] _Sp_ebo_helper<_Alloc> _M_alloc; + __gnu_cxx::__aligned_buffer<__remove_cv_t<_Tp>> _M_storage; }; #ifdef __glibcxx_smart_ptr_for_overwrite // C++ >= 20 && HOSTED