From patchwork Wed Oct 2 10:49:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1991968 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=M3gQ8Cqt; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4XJWnQ2X7Dz1xtY for ; Wed, 2 Oct 2024 20:51:06 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 87D4B384DEE0 for ; Wed, 2 Oct 2024 10:51:04 +0000 (GMT) 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 7BE00385843B for ; Wed, 2 Oct 2024 10:50:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7BE00385843B Authentication-Results: sourceware.org; dmarc=pass (p=none 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 7BE00385843B Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1727866210; cv=none; b=HQ9XH/Lna8r4tf6svn0rRNWmSpCxInQi0oYPAq4YPzYHh1tjETo5FPvtK076N4FNNTThg4czAFU8DXEiAVA6vcpdaZtTQRBjnbVq/1r84zSFL1Nybhwp7oYqVYx/nLF/RU3vLblDLlB4buEUIJssnR1SB4rfjTW1gxlvTNRP7rc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1727866210; c=relaxed/simple; bh=0mnUmdHrxtAbm32EEWh+VMGyMMg/5/myAs/GQUcMbGg=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=EX1Ib4SINy2gmkRrkcrqaCobrv/kDx3y3MXhqZaMyHcihSiVmCtDa/I84OoAwda2fjeOe5EEFsnE0h18J14dfmxaQO8zSgr+EQ5ZG0SogxcNcFhOAh9DI0Pqkjk/CI4pIHPKXDnb4yQLYo8u5Ka5m62jz6TYRhC533gichIoFAY= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1727866207; 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; bh=eSLkfXKggUQSnTeMUtO16KpvhqR8DK7AQbbEmkcfFHs=; b=M3gQ8CqtlT/YL7zl36KwkXVAVOZuhss2uqQFqAzgtIiAvvDSvRSOGje+DClqamBxv5vyb3 8Ui+/Qwl5SahaO0Mou7Dlq+FeNV2a4Ib4Hs8awFS5CdOAkDAbh4m8MoXySymjr1X8swX4G G5dF8zzvKCnmG9tVXSARNLSdcKuDiqg= Received: from mx-prod-mc-03.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-214-AoLRcmhYMu-SQV5R_lHEiQ-1; Wed, 02 Oct 2024 06:50:04 -0400 X-MC-Unique: AoLRcmhYMu-SQV5R_lHEiQ-1 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (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-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id A8E2D1977026; Wed, 2 Oct 2024 10:50:03 +0000 (UTC) Received: from localhost (unknown [10.42.28.136]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id C621719560A2; Wed, 2 Oct 2024 10:50:02 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH] libstdc++: Simplify std::aligned_storage and fix for versioned namespace [PR61458] Date: Wed, 2 Oct 2024 11:49:36 +0100 Message-ID: <20241002105001.3171475-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.0 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_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.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~incoming=patchwork.ozlabs.org@gcc.gnu.org Tested x86_64-linux. -- >8 -- This simplifies the implementation of std::aligned_storage. For the unstable ABI it also fixes the bug where its size is too large when the default alignment is used. We can't fix that for the stable ABI though, so just add a comment about the bug. libstdc++-v3/ChangeLog: PR libstdc++/61458 * doc/doxygen/user.cfg.in (GENERATE_BUGLIST): Set to NO. * include/std/type_traits (__aligned_storage_msa): Remove. (__aligned_storage_max_align_t): New struct. (__aligned_storage_default_alignment): New function. (aligned_storage): Use __aligned_storage_default_alignment for default alignment. Replace union with a struct containing an aligned buffer. Improve Doxygen comment. (aligned_storage_t): Use __aligned_storage_default_alignment for default alignment. --- libstdc++-v3/doc/doxygen/user.cfg.in | 2 +- libstdc++-v3/include/std/type_traits | 83 ++++++++++++++++++++-------- 2 files changed, 60 insertions(+), 25 deletions(-) diff --git a/libstdc++-v3/doc/doxygen/user.cfg.in b/libstdc++-v3/doc/doxygen/user.cfg.in index 8fe337adf75..ae50f6dd0c7 100644 --- a/libstdc++-v3/doc/doxygen/user.cfg.in +++ b/libstdc++-v3/doc/doxygen/user.cfg.in @@ -681,7 +681,7 @@ GENERATE_TESTLIST = NO # list. This list is created by putting \bug commands in the documentation. # The default value is: YES. -GENERATE_BUGLIST = YES +GENERATE_BUGLIST = NO # The GENERATE_DEPRECATEDLIST tag can be used to enable (YES) or disable (NO) # the deprecated list. This list is created by putting \deprecated commands in diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits index 6e6778078dc..28e403460a4 100644 --- a/libstdc++-v3/include/std/type_traits +++ b/libstdc++-v3/include/std/type_traits @@ -2234,39 +2234,74 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION using add_pointer_t = typename add_pointer<_Tp>::type; #endif - template - struct __aligned_storage_msa - { - union __type - { - unsigned char __data[_Len]; - struct __attribute__((__aligned__)) { } __align; - }; - }; + /// @cond undocumented + + // Aligned to maximum fundamental alignment + struct __attribute__((__aligned__)) __aligned_storage_max_align_t + { }; + + constexpr size_t + __aligned_storage_default_alignment([[__maybe_unused__]] size_t __len) + { +#if _GLIBCXX_INLINE_VERSION + using _Max_align + = integral_constant; + + return __len > (_Max_align::value / 2) + ? _Max_align::value +# if _GLIBCXX_USE_BUILTIN_TRAIT(__builtin_clzg) + : 1 << (__SIZE_WIDTH__ - __builtin_clzg(__len - 1u)); +# else + : 1 << (__LLONG_WIDTH__ - __builtin_clzll(__len - 1ull)); +# endif +#else + // Returning a fixed value is incorrect, but kept for ABI compatibility. + // XXX GLIBCXX_ABI Deprecated + return alignof(__aligned_storage_max_align_t); +#endif + } + /// @endcond /** - * @brief Alignment type. + * @brief Aligned storage * - * The value of _Align is a default-alignment which shall be the - * most stringent alignment requirement for any C++ object type - * whose size is no greater than _Len (3.9). The member typedef - * type shall be a POD type suitable for use as uninitialized - * storage for any object whose size is at most _Len and whose - * alignment is a divisor of _Align. + * The member typedef `type` is be a POD type suitable for use as + * uninitialized storage for any object whose size is at most `_Len` + * and whose alignment is a divisor of `_Align`. + * + * It is important to use the nested `type` as uninitialized storage, + * not the `std::aligned_storage` type itself which is an empty class + * with 1-byte alignment. So this is correct: + * + * `typename std::aligned_storage::type m_xobj;` + * + * This is wrong: + * + * `std::aligned_storage m_xobj;` + * + * In C++14 and later `std::aligned_storage_t` + * can be used to refer to the `type` member typedef. + * + * The default value of _Align is supposed to be the most stringent + * fundamental alignment requirement for any C++ object type whose size + * is no greater than `_Len` (see [basic.align] in the C++ standard). + * + * @bug In this implementation the default value for _Align is always the + * maximum fundamental alignment, i.e. `alignof(max_align_t)`, which is + * incorrect. It should be an alignment value no greater than `_Len`. * * @deprecated Deprecated in C++23. Uses can be replaced by an - * array std::byte[_Len] declared with alignas(_Align). + * array `std::byte[_Len]` declared with `alignas(_Align)`. */ - template::__type)> + template struct _GLIBCXX23_DEPRECATED aligned_storage { - union type + struct type { - unsigned char __data[_Len]; - struct __attribute__((__aligned__((_Align)))) { } __align; + alignas(_Align) unsigned char __data[_Len]; }; }; @@ -2766,8 +2801,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wdeprecated-declarations" /// Alias template for aligned_storage - template::__type)> + template using aligned_storage_t _GLIBCXX23_DEPRECATED = typename aligned_storage<_Len, _Align>::type; template