1 // Copyright (C) 2018-2024 Free Software Foundation, Inc.
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
18 // { dg-do run { target c++17 } }
22 #include <testsuite_hooks.h>
26 Test(std::size_t size
, std::size_t a
)
27 : size(size
), alignment(std::align_val_t
{a
}),
28 p(::operator new(size
, alignment
))
31 ~Test() { ::operator delete(p
, size
, alignment
); }
34 std::align_val_t alignment
;
37 bool valid() const { return p
!= nullptr; }
43 return std::align((std::size_t)alignment
, size
, ptr
, space
) == p
;
47 // operator new(size_t size, align_val_t alignment) has
48 // undefined behaviour if the alignment argument is not
49 // a valid alignment value (i.e. not a power of two).
51 // Unlike posix_memalign there is no requirement that
52 // alignment >= sizeof(void*).
54 // Unlike aligned_alloc there is no requirement that
55 // size is an integer multiple of alignment.
60 // Test small values that would not be valid for
61 // posix_memalign or aligned_alloc.
64 VERIFY( t11
.valid() );
65 VERIFY( t11
.aligned() );
68 VERIFY( t21
.valid() );
69 VERIFY( t21
.aligned() );
72 VERIFY( t12
.valid() );
73 VERIFY( t12
.aligned() );
76 VERIFY( t22
.valid() );
77 VERIFY( t22
.aligned() );
80 VERIFY( t32
.valid() );
81 VERIFY( t32
.aligned() );
84 VERIFY( t42
.valid() );
85 VERIFY( t42
.aligned() );
88 VERIFY( t24
.valid() );
89 VERIFY( t24
.aligned() );
92 VERIFY( t34
.valid() );
93 VERIFY( t34
.aligned() );
96 VERIFY( t44
.valid() );
97 VERIFY( t44
.aligned() );
99 // Test some larger values.
101 Test t128_16
{128, 16};
102 VERIFY( t128_16
.valid() );
103 VERIFY( t128_16
.aligned() );
105 Test t128_64
{128, 64};
106 VERIFY( t128_64
.valid() );
107 VERIFY( t128_64
.aligned() );
109 Test t64_128
{64, 128};
110 VERIFY( t64_128
.valid() );
111 VERIFY( t64_128
.aligned() );