]>
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-2020 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>
22 #include <ext/debug_allocator.h>
23 #include <ext/new_allocator.h>
24 #include <ext/malloc_allocator.h>
25 #include <testsuite_hooks.h>
26 #include <testsuite_allocator.h>
28 using std::experimental::pmr::memory_resource
;
29 using std::experimental::pmr::resource_adaptor
;
32 struct Allocator
: __gnu_test::SimpleAllocator
<T
>
34 Allocator(int) { } // not default constructible
37 Allocator(const Allocator
<U
>&) { }
40 template<std::size_t A
>
43 return (reinterpret_cast<std::uintptr_t>(p
) % A
) == 0;
48 { return aligned
<alignof(T
)>(p
); }
54 using std::max_align_t
;
58 Allocator
<int> a1(1), a2(2); // minimal interface allocators
59 resource_adaptor
<decltype(a1
)> r1(a1
), r2(a2
);
63 VERIFY( aligned
<max_align_t
>(p
) );
65 p
= r1
.allocate(1, alignof(short));
66 VERIFY( aligned
<short>(p
) );
67 r1
.deallocate(p
, 1, alignof(short));
68 p
= r1
.allocate(1, alignof(long));
69 VERIFY( aligned
<long>(p
) );
70 r1
.deallocate(p
, 1, alignof(long));
71 constexpr size_t big_al
= alignof(max_align_t
) * 8;
72 p
= r1
.allocate(1, big_al
);
73 VERIFY( aligned
<big_al
>(p
) );
74 r1
.deallocate(p
, 1, big_al
);
76 __gnu_test::uneq_allocator
<double> a3(3), a4(4); // non-equal allocators
77 resource_adaptor
<decltype(a3
)> r3(a3
), r4(a4
);
84 VERIFY( aligned
<max_align_t
>(p
) );
86 p
= r3
.allocate(1, alignof(short));
87 VERIFY( aligned
<short>(p
) );
88 r3
.deallocate(p
, 1, alignof(short));
89 p
= r3
.allocate(1, alignof(long));
90 VERIFY( aligned
<long>(p
) );
91 r3
.deallocate(p
, 1, alignof(long));
92 p
= r3
.allocate(1, big_al
);
93 VERIFY( aligned
<big_al
>(p
) );
94 r3
.deallocate(p
, 1, big_al
);
96 __gnu_cxx::debug_allocator
<std::allocator
<short>> a5
, a6
;
97 resource_adaptor
<decltype(a5
)> r5(a5
), r6(a6
);
103 VERIFY( aligned
<max_align_t
>(p
) );
105 p
= r5
.allocate(1, alignof(short));
106 VERIFY( aligned
<short>(p
) );
107 r5
.deallocate(p
, 1, alignof(short));
108 p
= r5
.allocate(1, alignof(long));
109 VERIFY( aligned
<long>(p
) );
110 r5
.deallocate(p
, 1, alignof(long));
111 p
= r5
.allocate(1, big_al
);
112 VERIFY( aligned
<big_al
>(p
) );
113 r5
.deallocate(p
, 1, big_al
);
115 // Test extended alignments
116 constexpr size_t al6
= (1ul << 6), al12
= (1ul << 12), al18
= (1ul << 18);
117 p
= r5
.allocate(1024, al6
);
118 VERIFY( aligned
<al6
>(p
) );
119 r5
.deallocate(p
, 1024, al6
);
120 p
= r5
.allocate(1024, al12
);
121 VERIFY( aligned
<al12
>(p
) );
122 r5
.deallocate(p
, 1024, al12
);
123 p
= r5
.allocate(1024, al18
);
124 VERIFY( aligned
<al18
>(p
) );
125 r5
.deallocate(p
, 1024, al18
);
127 __gnu_cxx::new_allocator
<short> a7
, a8
;
128 resource_adaptor
<decltype(a7
)> r7(a7
), r8(a8
);
135 VERIFY( aligned
<max_align_t
>(p
) );
137 p
= r7
.allocate(1, alignof(short));
138 VERIFY( aligned
<short>(p
) );
139 r7
.deallocate(p
, 1, alignof(short));
140 p
= r7
.allocate(1, alignof(long));
141 VERIFY( aligned
<long>(p
) );
142 r7
.deallocate(p
, 1, alignof(long));
143 p
= r7
.allocate(1, big_al
);
144 VERIFY( aligned
<big_al
>(p
) );
145 r7
.deallocate(p
, 1, big_al
);
146 // Test extended alignments
147 p
= r7
.allocate(1024, al6
);
148 VERIFY( aligned
<al6
>(p
) );
149 r7
.deallocate(p
, 1024, al6
);
150 p
= r7
.allocate(1024, al12
);
151 VERIFY( aligned
<al12
>(p
) );
152 r7
.deallocate(p
, 1024, al12
);
153 p
= r7
.allocate(1024, al18
);
154 VERIFY( aligned
<al18
>(p
) );
155 r7
.deallocate(p
, 1024, al18
);
157 __gnu_cxx::malloc_allocator
<short> a9
, a10
;
158 resource_adaptor
<decltype(a9
)> r9(a9
), r10(a10
);
166 VERIFY( aligned
<max_align_t
>(p
) );
168 p
= r9
.allocate(1, alignof(short));
169 VERIFY( aligned
<short>(p
) );
170 r9
.deallocate(p
, 1, alignof(short));
171 p
= r9
.allocate(1, alignof(long));
172 VERIFY( aligned
<long>(p
) );
173 r9
.deallocate(p
, 1, alignof(long));
174 p
= r9
.allocate(1, big_al
);
175 VERIFY( aligned
<big_al
>(p
) );
176 r9
.deallocate(p
, 1, big_al
);
177 // Test extended alignments
178 p
= r9
.allocate(1024, al6
);
179 VERIFY( aligned
<al6
>(p
) );
180 r9
.deallocate(p
, 1024, al6
);
181 p
= r9
.allocate(1024, al12
);
182 VERIFY( aligned
<al12
>(p
) );
183 r9
.deallocate(p
, 1024, al12
);
184 p
= r9
.allocate(1024, al18
);
185 VERIFY( aligned
<al18
>(p
) );
186 r9
.deallocate(p
, 1024, al18
);
188 std::allocator
<short> a11
, a12
;
189 resource_adaptor
<decltype(a11
)> r11(a11
), r12(a12
);
190 VERIFY( r11
== r11
);
191 VERIFY( r11
== r12
);
198 VERIFY( aligned
<max_align_t
>(p
) );
199 r11
.deallocate(p
, 1);
200 p
= r11
.allocate(1, alignof(short));
201 VERIFY( aligned
<short>(p
) );
202 r11
.deallocate(p
, 1, alignof(short));
203 p
= r11
.allocate(1, alignof(long));
204 VERIFY( aligned
<long>(p
) );
205 r11
.deallocate(p
, 1, alignof(long));
206 p
= r11
.allocate(1, big_al
);
207 VERIFY( aligned
<big_al
>(p
) );
208 r11
.deallocate(p
, 1, big_al
);
209 // Test extended alignments
210 p
= r11
.allocate(1024, al6
);
211 VERIFY( aligned
<al6
>(p
) );
212 r11
.deallocate(p
, 1024, al6
);
213 p
= r11
.allocate(1024, al12
);
214 VERIFY( aligned
<al12
>(p
) );
215 r11
.deallocate(p
, 1024, al12
);
216 p
= r11
.allocate(1024, al18
);
217 VERIFY( aligned
<al18
>(p
) );
218 r11
.deallocate(p
, 1024, al18
);