From patchwork Thu Jun 27 08:55:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1953083 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=emfPijjq; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; 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 [IPv6:2620:52:3:1:0:246e:9693:128c]) (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 4W8syj3NP5z20X6 for ; Thu, 27 Jun 2024 19:02:21 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A01FD38346B3 for ; Thu, 27 Jun 2024 09:02:19 +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.129.124]) by sourceware.org (Postfix) with ESMTPS id 6B5243835C0D for ; Thu, 27 Jun 2024 09:00:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6B5243835C0D 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 6B5243835C0D Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1719478853; cv=none; b=SBTtjRPFGDBkhI6YIF+SWbmtBScSBTpgVnL+AE8t+PqrsHxD31LHxAzu6kglhsbEzQjg80NIpS7mFkqso62vcQ+gTZf7qz+zmyRNrZNjWcGfG/LWnd6If1swkrVRgwnuTqUfhIUVYGgP8lfm7MNjWbYEmDaLECFw0n+H84ZIhQM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1719478853; c=relaxed/simple; bh=Re80TIJ7nUlIb/Y8ZymF8qUgTGRIYkdRhm9oBzfmnGY=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=HM/Qx4Il+oeK7kXowvuc7a8DX7ln7cE9FWn+nqlhADv8Lym6u5RK20jkNEdTDjhg4THD0FGOMayV/kkOFHRinbHOQbSUop87Abf3apqXpFLD3JpYgkoLcUA4eJXjc+iyrLVUfs/mSNUdahD+8kn6/vimYT84dirOTc+Hy96t9Vk= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1719478850; 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=pZEaJjqauIN1kZzpb9encDnkerqLVEkTfhGsl16H13A=; b=emfPijjqLU8x8p6we3OEbp52e/HfzGdshpVvFkPrLxjzYn18o57hx+C/VZ5KMyN3J91GRD YT1T7gxnuW4l1f+G2HEa8Yv9yOKtGsM78bOqlsA88pqFdixEeTBqiM5KO9vpnIBylEVYBj ze6W2xywNlzN335/6JbpfaWmDq4jQ6Q= Received: from mx-prod-mc-04.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-653-xWhdj_HqOqKQnE7H600i9A-1; Thu, 27 Jun 2024 05:00:46 -0400 X-MC-Unique: xWhdj_HqOqKQnE7H600i9A-1 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (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-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 8584A19560B7; Thu, 27 Jun 2024 09:00:45 +0000 (UTC) Received: from localhost (unknown [10.42.28.171]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 0E91E19560A3; Thu, 27 Jun 2024 09:00:43 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH 2/2] libstdc++: Do not use C++11 alignof in C++98 mode [PR104395] Date: Thu, 27 Jun 2024 09:55:56 +0100 Message-ID: <20240627090040.95665-2-jwakely@redhat.com> In-Reply-To: <20240627090040.95665-1-jwakely@redhat.com> References: <20240627090040.95665-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.4 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_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, URI_HEX autolearn=unavailable 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 As I commented in the PR, I think it would be nice if the compiler accepted C++11 alignof in C++98 mode when -faligned-new is used. But even if G++ added that, we'd need Clang to use it, and then wait a few releases for that new Clang support to be in widespread use. So let's just disable the extended alignment support in allocators. Using -std=c++98 -faligned-new seems like a silly combination anyway. Tested x86_64-linux. -- >8 -- When -faligned-new (or Clang's -faligned-allocation) is used our allocators try to support extended alignments, gated on the __cpp_aligned_new macro. However, because they use alignof(_Tp) which is not a keyword in C++98 mode, using -std=c++98 -faligned-new results in errors from and other headers. We could change them to use __alignof__ instead of alignof, but that would potentially alter the result of the conditions, because e.g. alignof(long long) != __alignof__(long long) on some targets. That's probably not an issue for any types with extended alignment, so maybe it would be a safe change. For now, it seems acceptable to just disable the extended alignment support in C++98 mode, so that -faligned-new enables std::align_val_t and the corresponding operator new overloads, but doesn't affect std::allocator, __gnu_cxx::__bitmap_allocator etc. libstdc++-v3/ChangeLog: PR libstdc++/104395 * include/bits/new_allocator.h: Disable extended alignment support in C++98 mode. * include/bits/stl_tempbuf.h: Likewise. * include/ext/bitmap_allocator.h: Likewise. * include/ext/malloc_allocator.h: Likewise. * include/ext/mt_allocator.h: Likewise. * include/ext/pool_allocator.h: Likewise. * testsuite/ext/104395.cc: New test. --- libstdc++-v3/include/bits/new_allocator.h | 4 ++-- libstdc++-v3/include/bits/stl_tempbuf.h | 6 +++--- libstdc++-v3/include/ext/bitmap_allocator.h | 4 ++-- libstdc++-v3/include/ext/malloc_allocator.h | 2 +- libstdc++-v3/include/ext/mt_allocator.h | 4 ++-- libstdc++-v3/include/ext/pool_allocator.h | 4 ++-- libstdc++-v3/testsuite/ext/104395.cc | 8 ++++++++ 7 files changed, 20 insertions(+), 12 deletions(-) create mode 100644 libstdc++-v3/testsuite/ext/104395.cc diff --git a/libstdc++-v3/include/bits/new_allocator.h b/libstdc++-v3/include/bits/new_allocator.h index 0e90c8819ac..5dcdee11c4d 100644 --- a/libstdc++-v3/include/bits/new_allocator.h +++ b/libstdc++-v3/include/bits/new_allocator.h @@ -140,7 +140,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION std::__throw_bad_alloc(); } -#if __cpp_aligned_new +#if __cpp_aligned_new && __cplusplus >= 201103L if (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__) { std::align_val_t __al = std::align_val_t(alignof(_Tp)); @@ -161,7 +161,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION # define _GLIBCXX_SIZED_DEALLOC(p, n) (p) #endif -#if __cpp_aligned_new +#if __cpp_aligned_new && __cplusplus >= 201103L if (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__) { _GLIBCXX_OPERATOR_DELETE(_GLIBCXX_SIZED_DEALLOC(__p, __n), diff --git a/libstdc++-v3/include/bits/stl_tempbuf.h b/libstdc++-v3/include/bits/stl_tempbuf.h index 759c4937744..0f267054613 100644 --- a/libstdc++-v3/include/bits/stl_tempbuf.h +++ b/libstdc++-v3/include/bits/stl_tempbuf.h @@ -85,7 +85,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION if (__builtin_expect(size_t(__len) > (size_t(-1) / sizeof(_Tp)), 0)) return 0; -#if __cpp_aligned_new +#if __cpp_aligned_new && __cplusplus >= 201103L if (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__) return (_Tp*) _GLIBCXX_OPERATOR_NEW(__len * sizeof(_Tp), align_val_t(alignof(_Tp)), @@ -107,7 +107,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION # define _GLIBCXX_SIZED_DEALLOC(T, p, n) (p) #endif -#if __cpp_aligned_new +#if __cpp_aligned_new && __cplusplus >= 201103L if (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__) { _GLIBCXX_OPERATOR_DELETE(_GLIBCXX_SIZED_DEALLOC(_Tp, __p, __len), @@ -168,7 +168,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION inline void return_temporary_buffer(_Tp* __p) { -#if __cpp_aligned_new +#if __cpp_aligned_new && __cplusplus >= 201103L if (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__) _GLIBCXX_OPERATOR_DELETE(__p, align_val_t(alignof(_Tp))); else diff --git a/libstdc++-v3/include/ext/bitmap_allocator.h b/libstdc++-v3/include/ext/bitmap_allocator.h index ef2ee13187b..45b2283ca30 100644 --- a/libstdc++-v3/include/ext/bitmap_allocator.h +++ b/libstdc++-v3/include/ext/bitmap_allocator.h @@ -1017,7 +1017,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION if (__n > this->max_size()) std::__throw_bad_alloc(); -#if __cpp_aligned_new +#if __cpp_aligned_new && __cplusplus >= 201103L if (alignof(value_type) > __STDCPP_DEFAULT_NEW_ALIGNMENT__) { const size_type __b = __n * sizeof(value_type); @@ -1044,7 +1044,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { if (__builtin_expect(__p != 0, true)) { -#if __cpp_aligned_new +#if __cpp_aligned_new && __cplusplus >= 201103L // Types with extended alignment are handled by operator delete. if (alignof(value_type) > __STDCPP_DEFAULT_NEW_ALIGNMENT__) { diff --git a/libstdc++-v3/include/ext/malloc_allocator.h b/libstdc++-v3/include/ext/malloc_allocator.h index b0a87b62d5a..36513780925 100644 --- a/libstdc++-v3/include/ext/malloc_allocator.h +++ b/libstdc++-v3/include/ext/malloc_allocator.h @@ -120,7 +120,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } _Tp* __ret = 0; -#if __cpp_aligned_new +#if __cpp_aligned_new && __cplusplus >= 201103L #if __cplusplus > 201402L && _GLIBCXX_HAVE_ALIGNED_ALLOC if (alignof(_Tp) > alignof(std::max_align_t)) { diff --git a/libstdc++-v3/include/ext/mt_allocator.h b/libstdc++-v3/include/ext/mt_allocator.h index 0b0bf8dc5f7..77f7dcd2762 100644 --- a/libstdc++-v3/include/ext/mt_allocator.h +++ b/libstdc++-v3/include/ext/mt_allocator.h @@ -693,7 +693,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION if (__n > this->max_size()) std::__throw_bad_alloc(); -#if __cpp_aligned_new +#if __cpp_aligned_new && __cplusplus >= 201103L // Types with extended alignment are handled by operator new/delete. if (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__) { @@ -748,7 +748,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { if (__builtin_expect(__p != 0, true)) { -#if __cpp_aligned_new +#if __cpp_aligned_new && __cplusplus >= 201103L // Types with extended alignment are handled by operator new/delete. if (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__) { diff --git a/libstdc++-v3/include/ext/pool_allocator.h b/libstdc++-v3/include/ext/pool_allocator.h index 13895b2f71a..679b4b3d276 100644 --- a/libstdc++-v3/include/ext/pool_allocator.h +++ b/libstdc++-v3/include/ext/pool_allocator.h @@ -224,7 +224,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION const size_t __bytes = __n * sizeof(_Tp); -#if __cpp_aligned_new +#if __cpp_aligned_new && __cplusplus >= 201103L if (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__) { std::align_val_t __al = std::align_val_t(alignof(_Tp)); @@ -272,7 +272,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION using std::size_t; if (__builtin_expect(__n != 0 && __p != 0, true)) { -#if __cpp_aligned_new +#if __cpp_aligned_new && __cplusplus >= 201103L if (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__) { ::operator delete(__p, std::align_val_t(alignof(_Tp))); diff --git a/libstdc++-v3/testsuite/ext/104395.cc b/libstdc++-v3/testsuite/ext/104395.cc new file mode 100644 index 00000000000..252c40c9698 --- /dev/null +++ b/libstdc++-v3/testsuite/ext/104395.cc @@ -0,0 +1,8 @@ +// { dg-options "-std=gnu++98 -faligned-new" } +// { dg-do compile } +#include +#include +#include +#include +#include +#include