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 // <memory_resource>
14 
15 // class unsynchronized_pool_resource
16 
17 #include <cassert>
18 #include <cstddef>
19 #include <memory> // std::align
20 #include <memory_resource>
21 
22 #include "count_new.h"
23 #include "test_macros.h"
24 
25 bool is_aligned_to(void* p, std::size_t alignment) {
26   void* p2     = p;
27   std::size_t space = 1;
28   void* result = std::align(alignment, 1, p2, space);
29   return (result == p);
30 }
31 
32 int main(int, char**) {
33   globalMemCounter.reset();
34   std::pmr::pool_options opts{1, 1024};
35   auto unsync1                  = std::pmr::unsynchronized_pool_resource(opts, std::pmr::new_delete_resource());
36   std::pmr::memory_resource& r1 = unsync1;
37 
38   constexpr std::size_t big_alignment = 8 * alignof(std::max_align_t);
39   static_assert(big_alignment > 4);
40 
41   assert(globalMemCounter.checkNewCalledEq(0));
42 
43   void* ret = r1.allocate(2048, big_alignment);
44   assert(ret != nullptr);
45   assert(is_aligned_to(ret, big_alignment));
46   ASSERT_WITH_LIBRARY_INTERNAL_ALLOCATIONS(globalMemCounter.checkNewCalledGreaterThan(0));
47 
48   ret = r1.allocate(16, 4);
49   assert(ret != nullptr);
50   assert(is_aligned_to(ret, 4));
51   ASSERT_WITH_LIBRARY_INTERNAL_ALLOCATIONS(globalMemCounter.checkNewCalledGreaterThan(1));
52 
53   return 0;
54 }
55