Debian Codegraph @

libstdc++-v3/testsuite/20_util/unsynchronized_pool_resource/release.cc

raw
1 // Copyright (C) 2018-2025 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
8
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
17
18 // { dg-do run { target c++17 } }
19
20 #include <memory_resource>
21 #include <testsuite_allocator.h>
22 #include <testsuite_hooks.h>
23
24 void
25 test01()
26 {
27 __gnu_test::memory_resource test_mr;
28 std::pmr::unsynchronized_pool_resource r(&test_mr);
29 r.release();
30 VERIFY( test_mr.number_of_active_allocations() == 0 );
31 r.release();
32 VERIFY( test_mr.number_of_active_allocations() == 0 );
33 (void) r.allocate(1);
34 VERIFY( test_mr.number_of_active_allocations() != 0 );
35 r.release();
36 VERIFY( test_mr.number_of_active_allocations() == 0 );
37 r.release();
38 VERIFY( test_mr.number_of_active_allocations() == 0 );
39 }
40
41 void
42 test02()
43 {
44 struct nullable_memory_resource : public std::pmr::memory_resource
45 {
46 void*
47 do_allocate(std::size_t bytes, std::size_t alignment) override
48 { return upstream->allocate(bytes, alignment); }
49
50 void
51 do_deallocate(void* p, std::size_t bytes, std::size_t alignment) override
52 { upstream->deallocate(p, bytes, alignment); }
53
54 bool
55 do_is_equal(const memory_resource& r) const noexcept override
56 { return &r == this; }
57
58 std::pmr::memory_resource* upstream = std::pmr::get_default_resource();
59 };
60
61 nullable_memory_resource test_mr;
62 std::pmr::unsynchronized_pool_resource r(&test_mr);
63 r.release();
64 test_mr.upstream = nullptr;
65 r.release(); // should not need to call anything through upstream pointer
66 }
67
68 void
69 test03()
70 {
71 __gnu_test::memory_resource test_mr;
72 {
73 std::pmr::unsynchronized_pool_resource r(&test_mr);
74 // Destructor calls release()
75 }
76 VERIFY( test_mr.number_of_active_allocations() == 0 );
77 {
78 std::pmr::unsynchronized_pool_resource r(&test_mr);
79 (void) r.allocate(1);
80 VERIFY( test_mr.number_of_active_allocations() != 0 );
81 // Destructor calls release()
82 }
83 VERIFY( test_mr.number_of_active_allocations() == 0 );
84 {
85 std::pmr::unsynchronized_pool_resource r({10, 16}, &test_mr);
86 (void) r.allocate(2 * r.options().largest_required_pool_block);
87 VERIFY( test_mr.number_of_active_allocations() != 0 );
88 // Destructor calls release()
89 }
90 VERIFY( test_mr.number_of_active_allocations() == 0 );
91 {
92 std::pmr::unsynchronized_pool_resource r({16, 16}, &test_mr);
93 (void) r.allocate(2);
94 (void) r.allocate(8);
95 (void) r.allocate(16);
96 (void) r.allocate(2);
97 (void) r.allocate(8);
98 (void) r.allocate(16);
99 (void) r.allocate(2 * r.options().largest_required_pool_block);
100 VERIFY( test_mr.number_of_active_allocations() != 0 );
101 // Destructor calls release()
102 }
103 VERIFY( test_mr.number_of_active_allocations() == 0 );
104 }
105
106 int
107 main()
108 {
109 test01();
110 test02();
111 test03();
112 }