Add documentation for musttail attribute
[official-gcc.git] / libstdc++-v3 / testsuite / 18_support / new_aligned.cc
blobf1c738e66bd9c795d33fdebd7150c7216beafeb1
1 // Copyright (C) 2018-2024 Free Software Foundation, Inc.
2 //
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)
7 // any later version.
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 } }
20 #include <new>
21 #include <memory>
22 #include <testsuite_hooks.h>
24 struct Test
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))
29 { }
31 ~Test() { ::operator delete(p, size, alignment); }
33 std::size_t size;
34 std::align_val_t alignment;
35 void* p;
37 bool valid() const { return p != nullptr; }
39 bool aligned() const
41 auto ptr = p;
42 auto space = size;
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.
57 void
58 test01()
60 // Test small values that would not be valid for
61 // posix_memalign or aligned_alloc.
63 Test t11{1, 1};
64 VERIFY( t11.valid() );
65 VERIFY( t11.aligned() );
67 Test t21{2, 1};
68 VERIFY( t21.valid() );
69 VERIFY( t21.aligned() );
71 Test t12{1, 2};
72 VERIFY( t12.valid() );
73 VERIFY( t12.aligned() );
75 Test t22{2, 2};
76 VERIFY( t22.valid() );
77 VERIFY( t22.aligned() );
79 Test t32{3, 2};
80 VERIFY( t32.valid() );
81 VERIFY( t32.aligned() );
83 Test t42{4, 2};
84 VERIFY( t42.valid() );
85 VERIFY( t42.aligned() );
87 Test t24{2, 4};
88 VERIFY( t24.valid() );
89 VERIFY( t24.aligned() );
91 Test t34{3, 4};
92 VERIFY( t34.valid() );
93 VERIFY( t34.aligned() );
95 Test t44{4, 4};
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() );
115 main()
117 test01();