]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/experimental/memory_resource/resource_adaptor.cc
1 // { dg-do run { target c++14 } }
2 // { dg-require-cstdint "" }
4 // Copyright (C) 2016-2024 Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING3. If not see
19 // <http://www.gnu.org/licenses/>.
21 #include <experimental/memory_resource>
23 #include <ext/debug_allocator.h>
24 #include <ext/new_allocator.h>
25 #include <ext/malloc_allocator.h>
26 #include <testsuite_hooks.h>
27 #include <testsuite_allocator.h>
29 using std::experimental::pmr::memory_resource
;
30 using std::experimental::pmr::resource_adaptor
;
33 struct Allocator
: __gnu_test::SimpleAllocator
<T
>
35 Allocator(int) { } // not default constructible
38 Allocator(const Allocator
<U
>&) { }
41 template<std::size_t A
>
44 return (reinterpret_cast<std::uintptr_t>(p
) % A
) == 0;
49 { return aligned
<alignof(T
)>(p
); }
55 using std::max_align_t
;
59 Allocator
<int> a1(1), a2(2); // minimal interface allocators
60 resource_adaptor
<decltype(a1
)> r1(a1
), r2(a2
);
66 VERIFY( aligned
<max_align_t
>(p
) );
68 p
= r1
.allocate(1, alignof(short));
69 VERIFY( aligned
<short>(p
) );
70 r1
.deallocate(p
, 1, alignof(short));
71 p
= r1
.allocate(1, alignof(long));
72 VERIFY( aligned
<long>(p
) );
73 r1
.deallocate(p
, 1, alignof(long));
74 constexpr size_t big_al
= alignof(max_align_t
) * 8;
75 p
= r1
.allocate(1, big_al
);
76 VERIFY( aligned
<big_al
>(p
) );
77 r1
.deallocate(p
, 1, big_al
);
79 __gnu_test::uneq_allocator
<double> a3(3), a4(4); // non-equal allocators
80 resource_adaptor
<decltype(a3
)> r3(a3
), r4(a4
);
87 VERIFY( aligned
<max_align_t
>(p
) );
89 p
= r3
.allocate(1, alignof(short));
90 VERIFY( aligned
<short>(p
) );
91 r3
.deallocate(p
, 1, alignof(short));
92 p
= r3
.allocate(1, alignof(long));
93 VERIFY( aligned
<long>(p
) );
94 r3
.deallocate(p
, 1, alignof(long));
95 p
= r3
.allocate(1, big_al
);
96 VERIFY( aligned
<big_al
>(p
) );
97 r3
.deallocate(p
, 1, big_al
);
99 __gnu_cxx::debug_allocator
<std::allocator
<short>> a5
, a6
;
100 resource_adaptor
<decltype(a5
)> r5(a5
), r6(a6
);
108 VERIFY( aligned
<max_align_t
>(p
) );
110 p
= r5
.allocate(1, alignof(short));
111 VERIFY( aligned
<short>(p
) );
112 r5
.deallocate(p
, 1, alignof(short));
113 p
= r5
.allocate(1, alignof(long));
114 VERIFY( aligned
<long>(p
) );
115 r5
.deallocate(p
, 1, alignof(long));
116 p
= r5
.allocate(1, big_al
);
117 VERIFY( aligned
<big_al
>(p
) );
118 r5
.deallocate(p
, 1, big_al
);
120 // Test extended alignments
121 constexpr size_t al6
= (1ul << 6), al12
= (1ul << 12), al18
= (1ul << 18);
122 p
= r5
.allocate(1024, al6
);
123 VERIFY( aligned
<al6
>(p
) );
124 r5
.deallocate(p
, 1024, al6
);
125 p
= r5
.allocate(1024, al12
);
126 VERIFY( aligned
<al12
>(p
) );
127 r5
.deallocate(p
, 1024, al12
);
128 p
= r5
.allocate(1024, al18
);
129 VERIFY( aligned
<al18
>(p
) );
130 r5
.deallocate(p
, 1024, al18
);
132 __gnu_cxx::new_allocator
<short> a7
, a8
;
133 resource_adaptor
<decltype(a7
)> r7(a7
), r8(a8
);
142 VERIFY( aligned
<max_align_t
>(p
) );
144 p
= r7
.allocate(1, alignof(short));
145 VERIFY( aligned
<short>(p
) );
146 r7
.deallocate(p
, 1, alignof(short));
147 p
= r7
.allocate(1, alignof(long));
148 VERIFY( aligned
<long>(p
) );
149 r7
.deallocate(p
, 1, alignof(long));
150 p
= r7
.allocate(1, big_al
);
151 VERIFY( aligned
<big_al
>(p
) );
152 r7
.deallocate(p
, 1, big_al
);
153 // Test extended alignments
154 p
= r7
.allocate(1024, al6
);
155 VERIFY( aligned
<al6
>(p
) );
156 r7
.deallocate(p
, 1024, al6
);
157 p
= r7
.allocate(1024, al12
);
158 VERIFY( aligned
<al12
>(p
) );
159 r7
.deallocate(p
, 1024, al12
);
160 p
= r7
.allocate(1024, al18
);
161 VERIFY( aligned
<al18
>(p
) );
162 r7
.deallocate(p
, 1024, al18
);
164 __gnu_cxx::malloc_allocator
<short> a9
, a10
;
165 resource_adaptor
<decltype(a9
)> r9(a9
), r10(a10
);
175 VERIFY( aligned
<max_align_t
>(p
) );
177 p
= r9
.allocate(1, alignof(short));
178 VERIFY( aligned
<short>(p
) );
179 r9
.deallocate(p
, 1, alignof(short));
180 p
= r9
.allocate(1, alignof(long));
181 VERIFY( aligned
<long>(p
) );
182 r9
.deallocate(p
, 1, alignof(long));
183 p
= r9
.allocate(1, big_al
);
184 VERIFY( aligned
<big_al
>(p
) );
185 r9
.deallocate(p
, 1, big_al
);
186 // Test extended alignments
187 p
= r9
.allocate(1024, al6
);
188 VERIFY( aligned
<al6
>(p
) );
189 r9
.deallocate(p
, 1024, al6
);
190 p
= r9
.allocate(1024, al12
);
191 VERIFY( aligned
<al12
>(p
) );
192 r9
.deallocate(p
, 1024, al12
);
193 p
= r9
.allocate(1024, al18
);
194 VERIFY( aligned
<al18
>(p
) );
195 r9
.deallocate(p
, 1024, al18
);
197 std::allocator
<short> a11
, a12
;
198 resource_adaptor
<decltype(a11
)> r11(a11
), r12(a12
);
199 VERIFY( r11
== r11
);
201 VERIFY( r11
== r12
);
209 VERIFY( aligned
<max_align_t
>(p
) );
210 r11
.deallocate(p
, 1);
211 p
= r11
.allocate(1, alignof(short));
212 VERIFY( aligned
<short>(p
) );
213 r11
.deallocate(p
, 1, alignof(short));
214 p
= r11
.allocate(1, alignof(long));
215 VERIFY( aligned
<long>(p
) );
216 r11
.deallocate(p
, 1, alignof(long));
217 p
= r11
.allocate(1, big_al
);
218 VERIFY( aligned
<big_al
>(p
) );
219 r11
.deallocate(p
, 1, big_al
);
220 // Test extended alignments
221 p
= r11
.allocate(1024, al6
);
222 VERIFY( aligned
<al6
>(p
) );
223 r11
.deallocate(p
, 1024, al6
);
224 p
= r11
.allocate(1024, al12
);
225 VERIFY( aligned
<al12
>(p
) );
226 r11
.deallocate(p
, 1024, al12
);
227 p
= r11
.allocate(1024, al18
);
228 VERIFY( aligned
<al18
>(p
) );
229 r11
.deallocate(p
, 1024, al18
);