1 // Copyright (C) 2018 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-options "-std=gnu++17" }
19 // { dg-do run { target c++17 } }
23 #include <testsuite_hooks.h>
27 Test(std::size_t size
, std::size_t a
)
28 : size(size
), alignment(std::align_val_t
{a
}),
29 p(::operator new(size
, alignment
))
32 ~Test() { ::operator delete(p
, size
, alignment
); }
35 std::align_val_t alignment
;
38 bool valid() const { return p
!= nullptr; }
44 return std::align((std::size_t)alignment
, size
, ptr
, space
) == p
;
48 // operator new(size_t size, align_val_t alignment) has
49 // undefined behaviour if the alignment argument is not
50 // a valid alignment value (i.e. not a power of two).
52 // Unlike posix_memalign there is no requirement that
53 // alignment >= sizeof(void*).
55 // Unlike aligned_alloc there is no requirement that
56 // size is an integer multiple of alignment.
61 // Test small values that would not be valid for
62 // posix_memalign or aligned_alloc.
65 VERIFY( t11
.valid() );
66 VERIFY( t11
.aligned() );
69 VERIFY( t21
.valid() );
70 VERIFY( t21
.aligned() );
73 VERIFY( t12
.valid() );
74 VERIFY( t12
.aligned() );
77 VERIFY( t22
.valid() );
78 VERIFY( t22
.aligned() );
81 VERIFY( t32
.valid() );
82 VERIFY( t32
.aligned() );
85 VERIFY( t42
.valid() );
86 VERIFY( t42
.aligned() );
89 VERIFY( t24
.valid() );
90 VERIFY( t24
.aligned() );
93 VERIFY( t34
.valid() );
94 VERIFY( t34
.aligned() );
97 VERIFY( t44
.valid() );
98 VERIFY( t44
.aligned() );
100 // Test some larger values.
102 Test t128_16
{128, 16};
103 VERIFY( t128_16
.valid() );
104 VERIFY( t128_16
.aligned() );
106 Test t128_64
{128, 64};
107 VERIFY( t128_64
.valid() );
108 VERIFY( t128_64
.aligned() );
110 Test t64_128
{64, 128};
111 VERIFY( t64_128
.valid() );
112 VERIFY( t64_128
.aligned() );