1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // UNSUPPORTED: c++03, c++11, c++14
10 // TODO: Change to XFAIL once https://github.com/llvm/llvm-project/issues/40340 is fixed
11 // UNSUPPORTED: availability-pmr-missing
12 
13 // test_memory_resource requires RTTI for dynamic_cast
14 // UNSUPPORTED: no-rtti
15 
16 // <memory_resource>
17 
18 // template <class T> class polymorphic_allocator
19 
20 // T* polymorphic_allocator<T>::deallocate(T*, size_t size)
21 
22 #include <memory_resource>
23 #include <cassert>
24 #include <type_traits>
25 
26 #include "test_macros.h"
27 #include "test_std_memory_resource.h"
28 
29 template <std::size_t S, size_t Align>
testForSizeAndAlign()30 void testForSizeAndAlign() {
31   struct T {
32     alignas(Align) std::byte buf[S];
33   };
34 
35   TestResource R;
36   std::pmr::polymorphic_allocator<T> a(&R);
37 
38   for (int N = 1; N <= 5; ++N) {
39     auto ret = a.allocate(N);
40     assert(R.checkAlloc(ret, N * sizeof(T), alignof(T)));
41 
42     a.deallocate(ret, N);
43     assert(R.checkDealloc(ret, N * sizeof(T), alignof(T)));
44 
45     R.reset();
46   }
47 }
48 
main(int,char **)49 int main(int, char**) {
50   {
51     std::pmr::polymorphic_allocator<int> a;
52     ASSERT_SAME_TYPE(decltype(a.deallocate(nullptr, 0)), void);
53   }
54   {
55     constexpr std::size_t MA = alignof(std::max_align_t);
56     testForSizeAndAlign<1, 1>();
57     testForSizeAndAlign<1, 2>();
58     testForSizeAndAlign<1, MA>();
59     testForSizeAndAlign<2, 2>();
60     testForSizeAndAlign<73, alignof(void*)>();
61     testForSizeAndAlign<73, MA>();
62     testForSizeAndAlign<13, MA>();
63   }
64 
65   return 0;
66 }
67