https://gcc.gnu.org/g:3b9885ac4df2e83cec2080d5a7013b71345a7a7b
commit r14-11362-g3b9885ac4df2e83cec2080d5a7013b71345a7a7b Author: Jonathan Wakely <jwak...@redhat.com> Date: Wed Jun 26 14:09:07 2024 +0100 libstdc++: Do not use C++11 alignof in C++98 mode [PR104395] 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 <memory> 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. (cherry picked from commit 03d3aeb0e0fa7dec9bd702cabf57ef73cdc32704) Diff: --- 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(-) diff --git a/libstdc++-v3/include/bits/new_allocator.h b/libstdc++-v3/include/bits/new_allocator.h index 21e7e22cae00..3a749dc91dbb 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 759c4937744b..0f267054613f 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 ef2ee13187b9..45b2283ca30d 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 cde06d83241a..2a58847b8a95 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 0b0bf8dc5f7c..77f7dcd27620 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 13895b2f71a2..679b4b3d276e 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 000000000000..252c40c96980 --- /dev/null +++ b/libstdc++-v3/testsuite/ext/104395.cc @@ -0,0 +1,8 @@ +// { dg-options "-std=gnu++98 -faligned-new" } +// { dg-do compile } +#include <ext/memory> +#include <ext/bitmap_allocator.h> +#include <ext/mt_allocator.h> +#include <ext/malloc_allocator.h> +#include <ext/new_allocator.h> +#include <ext/pool_allocator.h>