Debian Codegraph @

libstdc++-v3/testsuite/18_support/new_aligned.cc

raw
1 // Copyright (C) 2018-2025 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.
8
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.
13
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/>.
17
18 // { dg-do run { target c++17 } }
19
20 #include <new>
21 #include <memory>
22 #include <testsuite_hooks.h>
23
24 struct Test
25 {
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 { }
30
31 ~Test() { ::operator delete(p, size, alignment); }
32
33 std::size_t size;
34 std::align_val_t alignment;
35 void* p;
36
37 bool valid() const { return p != nullptr; }
38
39 bool aligned() const
40 {
41 auto ptr = p;
42 auto space = size;
43 return std::align((std::size_t)alignment, size, ptr, space) == p;
44 }
45 };
46
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).
50 //
51 // Unlike posix_memalign there is no requirement that
52 // alignment >= sizeof(void*).
53 //
54 // Unlike aligned_alloc there is no requirement that
55 // size is an integer multiple of alignment.
56
57 void
58 test01()
59 {
60 // Test small values that would not be valid for
61 // posix_memalign or aligned_alloc.
62
63 Test t11{1, 1};
64 VERIFY( t11.valid() );
65 VERIFY( t11.aligned() );
66
67 Test t21{2, 1};
68 VERIFY( t21.valid() );
69 VERIFY( t21.aligned() );
70
71 Test t12{1, 2};
72 VERIFY( t12.valid() );
73 VERIFY( t12.aligned() );
74
75 Test t22{2, 2};
76 VERIFY( t22.valid() );
77 VERIFY( t22.aligned() );
78
79 Test t32{3, 2};
80 VERIFY( t32.valid() );
81 VERIFY( t32.aligned() );
82
83 Test t42{4, 2};
84 VERIFY( t42.valid() );
85 VERIFY( t42.aligned() );
86
87 Test t24{2, 4};
88 VERIFY( t24.valid() );
89 VERIFY( t24.aligned() );
90
91 Test t34{3, 4};
92 VERIFY( t34.valid() );
93 VERIFY( t34.aligned() );
94
95 Test t44{4, 4};
96 VERIFY( t44.valid() );
97 VERIFY( t44.aligned() );
98
99 // Test some larger values.
100
101 Test t128_16{128, 16};
102 VERIFY( t128_16.valid() );
103 VERIFY( t128_16.aligned() );
104
105 Test t128_64{128, 64};
106 VERIFY( t128_64.valid() );
107 VERIFY( t128_64.aligned() );
108
109 Test t64_128{64, 128};
110 VERIFY( t64_128.valid() );
111 VERIFY( t64_128.aligned() );
112 }
113
114 int
115 main()
116 {
117 test01();
118 }