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 // template <class U, class ...Args>
21 // void polymorphic_allocator<T>::construct(U *, Args &&...)
22 
23 #include <memory_resource>
24 #include <type_traits>
25 #include <cassert>
26 #include <cstdlib>
27 
28 #include "test_macros.h"
29 #include "test_std_memory_resource.h"
30 #include "uses_alloc_types.h"
31 #include "controlled_allocators.h"
32 #include "test_allocator.h"
33 
34 template <class T>
35 struct PMATest {
36   TestResource R;
37   std::pmr::polymorphic_allocator<T> A;
38   T* ptr;
39   bool constructed;
40 
PMATestPMATest41   PMATest() : A(&R), ptr(A.allocate(1)), constructed(false) {}
42 
43   template <class... Args>
constructPMATest44   void construct(Args&&... args) {
45     A.construct(ptr, std::forward<Args>(args)...);
46     constructed = true;
47   }
48 
~PMATestPMATest49   ~PMATest() {
50     if (constructed)
51       A.destroy(ptr);
52     A.deallocate(ptr, 1);
53   }
54 };
55 
56 template <class T, class... Args>
doTest(UsesAllocatorType UAExpect,Args &&...args)57 bool doTest(UsesAllocatorType UAExpect, Args&&... args) {
58   PMATest<T> TH;
59   // UNDER TEST //
60   TH.construct(std::forward<Args>(args)...);
61   return checkConstruct<Args&&...>(*TH.ptr, UAExpect, &TH.R);
62   // ------- //
63 }
64 
65 template <class T, class... Args>
doTestUsesAllocV0(Args &&...args)66 bool doTestUsesAllocV0(Args&&... args) {
67   PMATest<T> TH;
68   // UNDER TEST //
69   TH.construct(std::forward<Args>(args)...);
70   return checkConstruct<Args&&...>(*TH.ptr, UA_None);
71   // -------- //
72 }
73 
74 template <class T, class EAlloc, class... Args>
doTestUsesAllocV1(EAlloc const & ealloc,Args &&...args)75 bool doTestUsesAllocV1(EAlloc const& ealloc, Args&&... args) {
76   PMATest<T> TH;
77   // UNDER TEST //
78   TH.construct(std::allocator_arg, ealloc, std::forward<Args>(args)...);
79   return checkConstruct<Args&&...>(*TH.ptr, UA_AllocArg, ealloc);
80   // -------- //
81 }
82 
83 template <class T, class EAlloc, class... Args>
doTestUsesAllocV2(EAlloc const & ealloc,Args &&...args)84 bool doTestUsesAllocV2(EAlloc const& ealloc, Args&&... args) {
85   PMATest<T> TH;
86   // UNDER TEST //
87   TH.construct(std::forward<Args>(args)..., ealloc);
88   return checkConstruct<Args&&...>(*TH.ptr, UA_AllocLast, ealloc);
89   // -------- //
90 }
91 
92 template <class Alloc, class... Args>
test_pmr_uses_alloc(Args &&...args)93 void test_pmr_uses_alloc(Args&&... args) {
94   TestResource R(12435);
95   std::pmr::memory_resource* M = &R;
96   {
97     // NotUsesAllocator provides valid signatures for each uses-allocator
98     // construction but does not supply the required allocator_type typedef.
99     // Test that we can call these constructors manually without
100     // polymorphic_allocator interfering.
101     using T = NotUsesAllocator<Alloc, sizeof...(Args)>;
102     assert(doTestUsesAllocV0<T>(std::forward<Args>(args)...));
103     assert((doTestUsesAllocV1<T>(M, std::forward<Args>(args)...)));
104     assert((doTestUsesAllocV2<T>(M, std::forward<Args>(args)...)));
105   }
106   {
107     // Test T(std::allocator_arg_t, Alloc const&, Args...) construction
108     using T = UsesAllocatorV1<Alloc, sizeof...(Args)>;
109     assert((doTest<T>(UA_AllocArg, std::forward<Args>(args)...)));
110   }
111   {
112     // Test T(Args..., Alloc const&) construction
113     using T = UsesAllocatorV2<Alloc, sizeof...(Args)>;
114     assert((doTest<T>(UA_AllocLast, std::forward<Args>(args)...)));
115   }
116   {
117     // Test that T(std::allocator_arg_t, Alloc const&, Args...) construction
118     // is preferred when T(Args..., Alloc const&) is also available.
119     using T = UsesAllocatorV3<Alloc, sizeof...(Args)>;
120     assert((doTest<T>(UA_AllocArg, std::forward<Args>(args)...)));
121   }
122 }
123 
124 // Test that polymorphic_allocator does not prevent us from manually
125 // doing non-pmr uses-allocator construction.
126 template <class Alloc, class AllocObj, class... Args>
test_non_pmr_uses_alloc(AllocObj const & A,Args &&...args)127 void test_non_pmr_uses_alloc(AllocObj const& A, Args&&... args) {
128   {
129     using T = NotUsesAllocator<Alloc, sizeof...(Args)>;
130     assert(doTestUsesAllocV0<T>(std::forward<Args>(args)...));
131     assert((doTestUsesAllocV1<T>(A, std::forward<Args>(args)...)));
132     assert((doTestUsesAllocV2<T>(A, std::forward<Args>(args)...)));
133   }
134   {
135     using T = UsesAllocatorV1<Alloc, sizeof...(Args)>;
136     assert(doTestUsesAllocV0<T>(std::forward<Args>(args)...));
137     assert((doTestUsesAllocV1<T>(A, std::forward<Args>(args)...)));
138   }
139   {
140     using T = UsesAllocatorV2<Alloc, sizeof...(Args)>;
141     assert(doTestUsesAllocV0<T>(std::forward<Args>(args)...));
142     assert((doTestUsesAllocV2<T>(A, std::forward<Args>(args)...)));
143   }
144   {
145     using T = UsesAllocatorV3<Alloc, sizeof...(Args)>;
146     assert(doTestUsesAllocV0<T>(std::forward<Args>(args)...));
147     assert((doTestUsesAllocV1<T>(A, std::forward<Args>(args)...)));
148     assert((doTestUsesAllocV2<T>(A, std::forward<Args>(args)...)));
149   }
150 }
151 
main(int,char **)152 int main(int, char**) {
153   using PMR   = std::pmr::memory_resource*;
154   using PMA   = std::pmr::polymorphic_allocator<char>;
155   using STDA  = std::allocator<char>;
156   using TESTA = test_allocator<char>;
157 
158   int value        = 42;
159   const int cvalue = 43;
160   {
161     test_pmr_uses_alloc<PMA>();
162     test_pmr_uses_alloc<PMA>(value);
163     test_pmr_uses_alloc<PMA>(cvalue);
164     test_pmr_uses_alloc<PMA>(cvalue, std::move(value));
165   }
166   {
167     STDA std_alloc;
168     TESTA test_alloc(42);
169     PMR mem_res = std::pmr::new_delete_resource();
170 
171     test_non_pmr_uses_alloc<PMR>(mem_res);
172     test_non_pmr_uses_alloc<STDA>(std_alloc);
173     test_non_pmr_uses_alloc<TESTA>(test_alloc);
174     test_non_pmr_uses_alloc<PMR>(mem_res, value);
175     test_non_pmr_uses_alloc<STDA>(std_alloc, value);
176     test_non_pmr_uses_alloc<TESTA>(test_alloc, value);
177     test_non_pmr_uses_alloc<PMR>(mem_res, cvalue);
178     test_non_pmr_uses_alloc<STDA>(std_alloc, cvalue);
179     test_non_pmr_uses_alloc<TESTA>(test_alloc, cvalue);
180     test_non_pmr_uses_alloc<PMR>(mem_res, cvalue, std::move(cvalue));
181     test_non_pmr_uses_alloc<STDA>(std_alloc, cvalue, std::move(value));
182     test_non_pmr_uses_alloc<TESTA>(test_alloc, cvalue, std::move(value));
183   }
184 
185   return 0;
186 }
187