[gcc r17-2124] libstdc++: fix allocate_at_least test for small alignments [PR126072]
Nathan Myers
ncm@gcc.gnu.org
Fri Jul 3 17:22:06 GMT 2026
https://gcc.gnu.org/g:0b9e5f95108c44a57b3713329af35fdd04a5e744
commit r17-2124-g0b9e5f95108c44a57b3713329af35fdd04a5e744
Author: Nathan Myers <ncm@cantrip.org>
Date: Wed Jul 1 21:00:04 2026 -0400
libstdc++: fix allocate_at_least test for small alignments [PR126072]
A test for P0401 allocate_at_least fails on target cris-elf,
which has a default allocator with alignment 4. This patch
adjusts tests to accommodate alignments down to 1, and removes
assumptions about short int.
Tested on x86 -m64 and -m32. Need assistance for cris-elf.
libstdc++-v3/Changelog:
PR libstdc++/126072
* testsuite/20_util/allocator/allocate_at_least.cc: Fix.
Diff:
---
.../20_util/allocator/allocate_at_least.cc | 181 ++++++++++++---------
1 file changed, 100 insertions(+), 81 deletions(-)
diff --git a/libstdc++-v3/testsuite/20_util/allocator/allocate_at_least.cc b/libstdc++-v3/testsuite/20_util/allocator/allocate_at_least.cc
index 987e2472490a..0dcfcefc2c9e 100644
--- a/libstdc++-v3/testsuite/20_util/allocator/allocate_at_least.cc
+++ b/libstdc++-v3/testsuite/20_util/allocator/allocate_at_least.cc
@@ -73,17 +73,19 @@ void extra()
VERIFY(n == __STDCPP_DEFAULT_NEW_ALIGNMENT__);
SatC::deallocate(satc, p, n);
}
- {
- auto [p, n] = SatC::allocate_at_least(satc, 2);
- VERIFY(n == __STDCPP_DEFAULT_NEW_ALIGNMENT__);
- SatC::deallocate(satc, p, n);
- }
- {
- auto [p, n] =
- SatC::allocate_at_least(satc, __STDCPP_DEFAULT_NEW_ALIGNMENT__ - 1);
- VERIFY(n == __STDCPP_DEFAULT_NEW_ALIGNMENT__);
- SatC::deallocate(satc, p, n);
- }
+ if constexpr (__STDCPP_DEFAULT_NEW_ALIGNMENT__ >= 2)
+ {
+ auto [p, n] = SatC::allocate_at_least(satc, 2);
+ VERIFY(n == __STDCPP_DEFAULT_NEW_ALIGNMENT__);
+ SatC::deallocate(satc, p, n);
+ }
+ if constexpr (__STDCPP_DEFAULT_NEW_ALIGNMENT__ > 1)
+ {
+ auto [p, n] =
+ SatC::allocate_at_least(satc, __STDCPP_DEFAULT_NEW_ALIGNMENT__ - 1);
+ VERIFY(n == __STDCPP_DEFAULT_NEW_ALIGNMENT__);
+ SatC::deallocate(satc, p, n);
+ }
{
auto [p, n] = SatC::allocate_at_least(
satc, __STDCPP_DEFAULT_NEW_ALIGNMENT__);
@@ -91,77 +93,94 @@ void extra()
SatC::deallocate(satc, p, n);
}
- using SatS = std::allocator_traits<std::allocator<short>>;
- std::allocator<short> sats;
- {
- auto [p, n] = SatS::allocate_at_least(sats, 1);
- VERIFY(n == __STDCPP_DEFAULT_NEW_ALIGNMENT__ / sizeof(short));
- SatS::deallocate(sats, p, n);
- }
- {
- auto [p, n] = SatS::allocate_at_least(sats, 2);
- VERIFY(n == __STDCPP_DEFAULT_NEW_ALIGNMENT__ / sizeof(short));
- SatS::deallocate(sats, p, n);
- }
- {
- auto [p, n] = SatS::allocate_at_least(sats,
- (__STDCPP_DEFAULT_NEW_ALIGNMENT__ - 1) / sizeof(short));
- VERIFY(n == __STDCPP_DEFAULT_NEW_ALIGNMENT__ / sizeof(short));
- SatS::deallocate(sats, p, n);
- }
- {
- auto [p, n] = SatS::allocate_at_least(sats,
- __STDCPP_DEFAULT_NEW_ALIGNMENT__ / sizeof(short));
- VERIFY(n == __STDCPP_DEFAULT_NEW_ALIGNMENT__ / sizeof(short));
- SatS::deallocate(sats, p, n);
- }
-
- struct A3 { char s[3]; };
- using SatA3 = std::allocator_traits<std::allocator<A3>>;
- std::allocator<A3> sata3;
- {
- auto [p, n] = SatA3::allocate_at_least(sata3, 1);
- VERIFY(n == __STDCPP_DEFAULT_NEW_ALIGNMENT__ / sizeof(A3));
- SatA3::deallocate(sata3, p, n);
- }
- {
- auto [p, n] = SatA3::allocate_at_least(sata3, 2);
- VERIFY(n == __STDCPP_DEFAULT_NEW_ALIGNMENT__ / sizeof(A3));
- SatA3::deallocate(sata3, p, n);
- }
- {
- auto [p, n] = SatA3::allocate_at_least(sata3,
- (__STDCPP_DEFAULT_NEW_ALIGNMENT__ - 1) / sizeof(A3));
- VERIFY(n == __STDCPP_DEFAULT_NEW_ALIGNMENT__ / sizeof(A3));
- SatA3::deallocate(sata3, p, n);
- }
- {
- auto [p, n] = SatA3::allocate_at_least(sata3,
- __STDCPP_DEFAULT_NEW_ALIGNMENT__ / sizeof(A3));
- VERIFY(n == __STDCPP_DEFAULT_NEW_ALIGNMENT__ / sizeof(A3));
- SatA3::deallocate(sata3, p, n);
- }
+ if constexpr (__STDCPP_DEFAULT_NEW_ALIGNMENT__ > 2)
+ {
+ struct A2 { char s[2]; };
+ using SatA2 = std::allocator_traits<std::allocator<A2>>;
+ std::allocator<A2> sats;
+ if (sizeof(A2) <= __STDCPP_DEFAULT_NEW_ALIGNMENT__)
+ {
+ auto [p, n] = SatA2::allocate_at_least(sats, 1);
+ VERIFY(n == __STDCPP_DEFAULT_NEW_ALIGNMENT__ / sizeof(A2));
+ SatA2::deallocate(sats, p, n);
+ }
+ if (2*sizeof(A2) < __STDCPP_DEFAULT_NEW_ALIGNMENT__)
+ {
+ auto [p, n] = SatA2::allocate_at_least(sats, 2);
+ VERIFY(n == __STDCPP_DEFAULT_NEW_ALIGNMENT__ / sizeof(A2));
+ SatA2::deallocate(sats, p, n);
+ }
+ if ((__STDCPP_DEFAULT_NEW_ALIGNMENT__ - 1) / sizeof(A2) > 0)
+ {
+ auto [p, n] = SatA2::allocate_at_least(sats,
+ (__STDCPP_DEFAULT_NEW_ALIGNMENT__ - 1) / sizeof(A2));
+ VERIFY(n == __STDCPP_DEFAULT_NEW_ALIGNMENT__ / sizeof(A2));
+ SatA2::deallocate(sats, p, n);
+ }
+ if (__STDCPP_DEFAULT_NEW_ALIGNMENT__ / sizeof(A2) > 0)
+ {
+ auto [p, n] = SatA2::allocate_at_least(sats,
+ __STDCPP_DEFAULT_NEW_ALIGNMENT__ / sizeof(A2));
+ VERIFY(n == __STDCPP_DEFAULT_NEW_ALIGNMENT__ / sizeof(A2));
+ SatA2::deallocate(sats, p, n);
+ }
+ }
- struct Anm1 { char s[__STDCPP_DEFAULT_NEW_ALIGNMENT__ - 1]; };
- using SatAnm1 = std::allocator_traits<std::allocator<Anm1>>;
- std::allocator<Anm1> satanm1;
- {
- auto [p, n] = SatAnm1::allocate_at_least(satanm1, 1);
- VERIFY(n == 1);
- SatAnm1::deallocate(satanm1, p, n);
- }
- {
- auto [p, n] = SatAnm1::allocate_at_least(satanm1,
- __STDCPP_DEFAULT_NEW_ALIGNMENT__);
- VERIFY(n == __STDCPP_DEFAULT_NEW_ALIGNMENT__);
- SatAnm1::deallocate(satanm1, p, n);
- }
- {
- auto [p, n] = SatAnm1::allocate_at_least(satanm1,
- __STDCPP_DEFAULT_NEW_ALIGNMENT__ - 1);
- VERIFY(n == __STDCPP_DEFAULT_NEW_ALIGNMENT__);
- SatAnm1::deallocate(satanm1, p, n);
- }
+ if constexpr (__STDCPP_DEFAULT_NEW_ALIGNMENT__ > 2)
+ {
+ struct A3 { char s[3]; };
+ using SatA3 = std::allocator_traits<std::allocator<A3>>;
+ std::allocator<A3> sata3;
+ {
+ auto [p, n] = SatA3::allocate_at_least(sata3, 1);
+ VERIFY(n == __STDCPP_DEFAULT_NEW_ALIGNMENT__ / sizeof(A3));
+ SatA3::deallocate(sata3, p, n);
+ }
+ if constexpr (2*sizeof(A3) <= __STDCPP_DEFAULT_NEW_ALIGNMENT__)
+ {
+ auto [p, n] = SatA3::allocate_at_least(sata3, 2);
+ VERIFY(n == __STDCPP_DEFAULT_NEW_ALIGNMENT__ / sizeof(A3));
+ SatA3::deallocate(sata3, p, n);
+ }
+ if constexpr ((__STDCPP_DEFAULT_NEW_ALIGNMENT__ - 1) / sizeof(A3) > 0)
+ {
+ auto [p, n] = SatA3::allocate_at_least(sata3,
+ (__STDCPP_DEFAULT_NEW_ALIGNMENT__ - 1) / sizeof(A3));
+ VERIFY(n == __STDCPP_DEFAULT_NEW_ALIGNMENT__ / sizeof(A3));
+ SatA3::deallocate(sata3, p, n);
+ }
+ if constexpr (__STDCPP_DEFAULT_NEW_ALIGNMENT__ / sizeof(A3) > 0)
+ {
+ auto [p, n] = SatA3::allocate_at_least(sata3,
+ __STDCPP_DEFAULT_NEW_ALIGNMENT__ / sizeof(A3));
+ VERIFY(n == __STDCPP_DEFAULT_NEW_ALIGNMENT__ / sizeof(A3));
+ SatA3::deallocate(sata3, p, n);
+ }
+ }
+
+ if constexpr (__STDCPP_DEFAULT_NEW_ALIGNMENT__ > 1)
+ {
+ struct Anm1 { char s[__STDCPP_DEFAULT_NEW_ALIGNMENT__ - 1]; };
+ using SatAnm1 = std::allocator_traits<std::allocator<Anm1>>;
+ std::allocator<Anm1> satanm1;
+ {
+ auto [p, n] = SatAnm1::allocate_at_least(satanm1, 1);
+ VERIFY(n == 1);
+ SatAnm1::deallocate(satanm1, p, n);
+ }
+ {
+ auto [p, n] = SatAnm1::allocate_at_least(satanm1,
+ __STDCPP_DEFAULT_NEW_ALIGNMENT__);
+ VERIFY(n == __STDCPP_DEFAULT_NEW_ALIGNMENT__);
+ SatAnm1::deallocate(satanm1, p, n);
+ }
+ {
+ auto [p, n] = SatAnm1::allocate_at_least(satanm1,
+ __STDCPP_DEFAULT_NEW_ALIGNMENT__ - 1);
+ VERIFY(n == __STDCPP_DEFAULT_NEW_ALIGNMENT__);
+ SatAnm1::deallocate(satanm1, p, n);
+ }
+ }
}
int main()
More information about the Libstdc++-cvs
mailing list