Debian Codegraph @

libcxx/test/std/utilities/utility/mem.res/mem.res.pool/mem.res.pool.mem/unsync_allocate_overaligned_request.pass.cpp

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