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