1 // Copyright (C) 2018-2019 Free Software Foundation, Inc.
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)
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.
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/>.
18 // { dg-options "-std=gnu++17" }
19 // { dg-do run { target c++17 } }
21 #include <memory_resource>
22 #include <testsuite_allocator.h>
23 #include <testsuite_hooks.h>
28 __gnu_test::memory_resource test_mr
;
29 std::pmr::unsynchronized_pool_resource
r(&test_mr
);
31 VERIFY( test_mr
.number_of_active_allocations() == 0 );
33 VERIFY( test_mr
.number_of_active_allocations() == 0 );
35 VERIFY( test_mr
.number_of_active_allocations() != 0 );
37 VERIFY( test_mr
.number_of_active_allocations() == 0 );
39 VERIFY( test_mr
.number_of_active_allocations() == 0 );
45 struct nullable_memory_resource
: public std::pmr::memory_resource
48 do_allocate(std::size_t bytes
, std::size_t alignment
) override
49 { return upstream
->allocate(bytes
, alignment
); }
52 do_deallocate(void* p
, std::size_t bytes
, std::size_t alignment
) override
53 { upstream
->deallocate(p
, bytes
, alignment
); }
56 do_is_equal(const memory_resource
& r
) const noexcept override
57 { return &r
== this; }
59 std::pmr::memory_resource
* upstream
= std::pmr::get_default_resource();
62 nullable_memory_resource test_mr
;
63 std::pmr::unsynchronized_pool_resource
r(&test_mr
);
65 test_mr
.upstream
= nullptr;
66 r
.release(); // should not need to call anything through upstream pointer
72 __gnu_test::memory_resource test_mr
;
74 std::pmr::unsynchronized_pool_resource
r(&test_mr
);
75 // Destructor calls release()
77 VERIFY( test_mr
.number_of_active_allocations() == 0 );
79 std::pmr::unsynchronized_pool_resource
r(&test_mr
);
81 VERIFY( test_mr
.number_of_active_allocations() != 0 );
82 // Destructor calls release()
84 VERIFY( test_mr
.number_of_active_allocations() == 0 );
86 std::pmr::unsynchronized_pool_resource
r({10, 16}, &test_mr
);
87 (void) r
.allocate(2 * r
.options().largest_required_pool_block
);
88 VERIFY( test_mr
.number_of_active_allocations() != 0 );
89 // Destructor calls release()
91 VERIFY( test_mr
.number_of_active_allocations() == 0 );
93 std::pmr::unsynchronized_pool_resource
r({16, 16}, &test_mr
);
96 (void) r
.allocate(16);
99 (void) r
.allocate(16);
100 (void) r
.allocate(2 * r
.options().largest_required_pool_block
);
101 VERIFY( test_mr
.number_of_active_allocations() != 0 );
102 // Destructor calls release()
104 VERIFY( test_mr
.number_of_active_allocations() == 0 );