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
10
11 // <experimental/memory_resource>
12
13 // template <class T> class polymorphic_allocator
14
15 // T* polymorphic_allocator<T>::deallocate(T*, size_t size)
16
17 #include <experimental/memory_resource>
18 #include <type_traits>
19 #include <cassert>
20
21 #include "test_memory_resource.h"
22
23 #include "test_macros.h"
24
25 namespace ex = std::experimental::pmr;
26
27 template <size_t S, size_t Align>
testForSizeAndAlign()28 void testForSizeAndAlign() {
29 using T = typename std::aligned_storage<S, Align>::type;
30
31 TestResource R;
32 ex::polymorphic_allocator<T> a(&R);
33
34 for (int N = 1; N <= 5; ++N) {
35 auto ret = a.allocate(N);
36 assert(R.checkAlloc(ret, N * sizeof(T), alignof(T)));
37
38 a.deallocate(ret, N);
39 assert(R.checkDealloc(ret, N * sizeof(T), alignof(T)));
40
41 R.reset();
42 }
43 }
44
main(int,char **)45 int main(int, char**)
46 {
47 {
48 ex::polymorphic_allocator<int> a;
49 static_assert(
50 std::is_same<decltype(a.deallocate(nullptr, 0)), void>::value, "");
51 }
52 {
53 constexpr std::size_t MA = alignof(std::max_align_t);
54 testForSizeAndAlign<1, 1>();
55 testForSizeAndAlign<1, 2>();
56 testForSizeAndAlign<1, MA>();
57 testForSizeAndAlign<2, 2>();
58 testForSizeAndAlign<73, alignof(void*)>();
59 testForSizeAndAlign<73, MA>();
60 testForSizeAndAlign<13, MA>();
61 }
62
63 return 0;
64 }
65