Debian Codegraph @

libcxx/test/std/utilities/utility/mem.res/mem.res.pool/mem.res.pool.mem/unsync_deallocate_matches_allocate.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: no-exceptions
10 // UNSUPPORTED: c++03, c++11, c++14
11 // TODO: Change to XFAIL once https://github.com/llvm/llvm-project/issues/40340 is fixed
12 // UNSUPPORTED: availability-pmr-missing
13
14 // <memory_resource>
15
16 // class unsynchronized_pool_resource
17
18 #include <memory_resource>
19 #include <algorithm>
20 #include <cassert>
21 #include <new>
22 #include <vector>
23
24 struct allocation_record {
25 std::size_t bytes;
26 std::size_t align;
27 explicit allocation_record(std::size_t b, size_t a) : bytes(b), align(a) {}
28 bool operator==(const allocation_record& rhs) const { return (bytes == rhs.bytes) && (align == rhs.align); }
29 bool operator<(const allocation_record& rhs) const {
30 if (bytes != rhs.bytes)
31 return (bytes < rhs.bytes);
32 return (align < rhs.align);
33 }
34 };
35
36 class test_resource : public std::pmr::memory_resource {
37 void* do_allocate(std::size_t bytes, size_t align) override {
38 void* result = std::pmr::new_delete_resource()->allocate(bytes, align);
39 successful_allocations.emplace_back(bytes, align);
40 return result;
41 }
42 void do_deallocate(void* p, std::size_t bytes, size_t align) override {
43 deallocations.emplace_back(bytes, align);
44 return std::pmr::new_delete_resource()->deallocate(p, bytes, align);
45 }
46 bool do_is_equal(const std::pmr::memory_resource&) const noexcept override { return false; }
47
48 public:
49 std::vector<allocation_record> successful_allocations;
50 std::vector<allocation_record> deallocations;
51 };
52
53 template <class F>
54 void test_allocation_pattern(F do_pattern) {
55 test_resource tr;
56 std::pmr::pool_options opts{0, 256};
57 std::pmr::unsynchronized_pool_resource uspr(opts, &tr);
58
59 try {
60 do_pattern(uspr);
61 } catch (const std::bad_alloc&) {
62 }
63 uspr.release();
64
65 assert(tr.successful_allocations.size() == tr.deallocations.size());
66 assert(std::is_permutation(
67 tr.successful_allocations.begin(),
68 tr.successful_allocations.end(),
69 tr.deallocations.begin(),
70 tr.deallocations.end()));
71 }
72
73 template <std::size_t Bytes, size_t Align>
74 auto foo() {
75 return [=](auto& mr) {
76 void* p = mr.allocate(Bytes, Align);
77 mr.deallocate(p, Bytes, Align);
78 };
79 }
80
81 int main(int, char**) {
82 test_allocation_pattern(foo<2, 1>());
83 test_allocation_pattern(foo<2, 8>());
84 test_allocation_pattern(foo<2, 64>());
85 test_allocation_pattern(foo<128, 1>());
86 test_allocation_pattern(foo<128, 8>());
87 test_allocation_pattern(foo<128, 64>());
88 test_allocation_pattern(foo<1024, 1>());
89 test_allocation_pattern(foo<1024, 8>());
90 test_allocation_pattern(foo<1024, 64>());
91
92 test_allocation_pattern([](auto& mr) {
93 void* p1 = mr.allocate(2, 1);
94 void* p2 = mr.allocate(2, 8);
95 void* p3 = mr.allocate(2, 64);
96 void* p4 = mr.allocate(128, 1);
97 void* p5 = mr.allocate(128, 8);
98 void* p6 = mr.allocate(128, 64);
99 void* p7 = mr.allocate(1024, 1);
100 void* p8 = mr.allocate(1024, 8);
101 void* p9 = mr.allocate(1024, 64);
102 mr.deallocate(p1, 2, 1);
103 mr.deallocate(p2, 2, 8);
104 mr.deallocate(p3, 2, 64);
105 mr.deallocate(p4, 128, 1);
106 mr.deallocate(p5, 128, 8);
107 mr.deallocate(p6, 128, 64);
108 mr.deallocate(p7, 1024, 1);
109 mr.deallocate(p8, 1024, 8);
110 mr.deallocate(p9, 1024, 64);
111 });
112
113 return 0;
114 }