1 // Copyright (C) 2018-2020 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-options "-std=gnu++17" }
19 // { dg-do run { target c++17 } }
20 
21 #include <memory_resource>
22 #include <testsuite_allocator.h>
23 #include <testsuite_hooks.h>
24 
25 void
test01()26 test01()
27 {
28   __gnu_test::memory_resource test_mr;
29   std::pmr::unsynchronized_pool_resource r(&test_mr);
30   r.release();
31   VERIFY( test_mr.number_of_active_allocations() == 0 );
32   r.release();
33   VERIFY( test_mr.number_of_active_allocations() == 0 );
34   (void) r.allocate(1);
35   VERIFY( test_mr.number_of_active_allocations() != 0 );
36   r.release();
37   VERIFY( test_mr.number_of_active_allocations() == 0 );
38   r.release();
39   VERIFY( test_mr.number_of_active_allocations() == 0 );
40 }
41 
42 void
test02()43 test02()
44 {
45   struct nullable_memory_resource : public std::pmr::memory_resource
46   {
47     void*
48     do_allocate(std::size_t bytes, std::size_t alignment) override
49     { return upstream->allocate(bytes, alignment); }
50 
51     void
52     do_deallocate(void* p, std::size_t bytes, std::size_t alignment) override
53     { upstream->deallocate(p, bytes, alignment); }
54 
55     bool
56     do_is_equal(const memory_resource& r) const noexcept override
57     { return &r == this; }
58 
59     std::pmr::memory_resource* upstream = std::pmr::get_default_resource();
60   };
61 
62   nullable_memory_resource test_mr;
63   std::pmr::unsynchronized_pool_resource r(&test_mr);
64   r.release();
65   test_mr.upstream = nullptr;
66   r.release(); // should not need to call anything through upstream pointer
67 }
68 
69 void
test03()70 test03()
71 {
72   __gnu_test::memory_resource test_mr;
73   {
74     std::pmr::unsynchronized_pool_resource r(&test_mr);
75     // Destructor calls release()
76   }
77   VERIFY( test_mr.number_of_active_allocations() == 0 );
78   {
79     std::pmr::unsynchronized_pool_resource r(&test_mr);
80     (void) r.allocate(1);
81     VERIFY( test_mr.number_of_active_allocations() != 0 );
82     // Destructor calls release()
83   }
84   VERIFY( test_mr.number_of_active_allocations() == 0 );
85   {
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()
90   }
91   VERIFY( test_mr.number_of_active_allocations() == 0 );
92   {
93     std::pmr::unsynchronized_pool_resource r({16, 16}, &test_mr);
94     (void) r.allocate(2);
95     (void) r.allocate(8);
96     (void) r.allocate(16);
97     (void) r.allocate(2);
98     (void) r.allocate(8);
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()
103   }
104   VERIFY( test_mr.number_of_active_allocations() == 0 );
105 }
106 
107 int
main()108 main()
109 {
110   test01();
111   test02();
112   test03();
113 }
114