]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/20_util/monotonic_buffer_resource/release.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 20_util / monotonic_buffer_resource / release.cc
1 // Copyright (C) 2018-2019 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
24 struct resource : __gnu_test::memory_resource
25 {
26 int allocate_calls = 0;
27 int deallocate_calls = 0;
28
29 void*
30 do_allocate(std::size_t bytes, std::size_t align) override
31 {
32 ++allocate_calls;
33 return __gnu_test::memory_resource::do_allocate(bytes, align);
34 }
35
36 void
37 do_deallocate(void* p, std::size_t bytes, std::size_t align) override
38 {
39 ++deallocate_calls;
40 __gnu_test::memory_resource::do_deallocate(p, bytes, align);
41 }
42 };
43
44 void
45 test01()
46 {
47 resource r;
48 std::pmr::monotonic_buffer_resource mbr(&r);
49 auto p = mbr.allocate(10, 16);
50 mbr.deallocate(p, 1, 2);
51 VERIFY( r.deallocate_calls == 0 );
52 p = mbr.allocate(10, 16);
53 p = mbr.allocate(10, 16);
54 p = mbr.allocate(10, 16);
55 p = mbr.allocate(1024, 64);
56 p = mbr.allocate(1024, 64);
57 p = mbr.allocate(128, 8);
58 p = mbr.allocate(128, 8);
59 p = mbr.allocate(128, 8);
60 p = mbr.allocate(128, 8);
61 p = mbr.allocate(128, 8);
62 p = mbr.allocate(128, 8);
63 p = mbr.allocate(128, 8);
64 mbr.deallocate(p, 1, 2);
65 p = mbr.allocate(1024, 16);
66 p = mbr.allocate(1024, 16);
67 mbr.deallocate(p, 1, 2);
68 VERIFY( r.deallocate_calls == 0 );
69 mbr.release();
70 VERIFY( r.deallocate_calls != 0 );
71 VERIFY( r.deallocate_calls == r.allocate_calls );
72 VERIFY( mbr.upstream_resource() == &r );
73 VERIFY( r.number_of_active_allocations() == 0 );
74 }
75
76 void
77 test02()
78 {
79 std::pmr::monotonic_buffer_resource mbr; // uses get_default_resource()
80 auto* const upstream = mbr.upstream_resource();
81 resource r;
82 __gnu_test::default_resource_mgr _(&r); // calls set_default_resource(&r)
83 mbr.release();
84 // release() doesn't change upstream resource:
85 VERIFY( mbr.upstream_resource() == upstream );
86 }
87
88 void
89 test03()
90 {
91 resource r;
92 __gnu_test::default_resource_mgr _(&r);
93 std::pmr::monotonic_buffer_resource mbr(16);
94 for (int i = 0; i < 100; ++i)
95 (void) mbr.allocate(4, 1);
96 const int allocations = r.allocate_calls;
97 VERIFY( allocations != 0 );
98 mbr.release();
99 VERIFY( r.allocate_calls == r.deallocate_calls );
100 VERIFY( r.number_of_active_allocations() == 0 );
101
102 // next_buffer_size should have been reset to the initial value,
103 // so the allocations from upstream should be the same as before.
104 r.allocate_calls = 0;
105 r.deallocate_calls = 0;
106 for (int i = 0; i < 100; ++i)
107 (void) mbr.allocate(4,1);
108 VERIFY( allocations == r.allocate_calls );
109 }
110
111 void
112 test04()
113 {
114 resource r;
115 unsigned char buffer[1024];
116 std::pmr::monotonic_buffer_resource mbr(buffer, sizeof(buffer), &r);
117 void* p = mbr.allocate(800, 16);
118 VERIFY( p >= buffer && p < (buffer + 16) );
119 void* const p_in_buffer = p;
120 VERIFY( r.allocate_calls == 0 );
121 p = mbr.allocate(300, 1);
122 VERIFY( p != buffer );
123 VERIFY( p != buffer );
124 VERIFY( r.allocate_calls == 1 );
125 mbr.release();
126 VERIFY( r.deallocate_calls == 1 );
127 VERIFY( mbr.upstream_resource() == &r );
128 VERIFY( r.number_of_active_allocations() == 0 );
129 // initial buffer should be used again now:
130 p = mbr.allocate(1000, 16);
131 VERIFY( p == p_in_buffer );
132 VERIFY( r.allocate_calls == 1 );
133 }
134
135 void
136 test05() // LWG 3120
137 {
138 char buffer[100];
139 {
140 std::pmr::monotonic_buffer_resource mr(buffer, sizeof(buffer),
141 std::pmr::null_memory_resource());
142 mr.release();
143 (void) mr.allocate(60);
144 }
145
146 {
147 std::pmr::monotonic_buffer_resource mr(buffer, sizeof(buffer),
148 std::pmr::null_memory_resource());
149 (void) mr.allocate(60);
150 mr.release();
151 (void) mr.allocate(60);
152 }
153
154 {
155 resource r;
156 std::pmr::monotonic_buffer_resource mr(&r);
157 for (int i = 0; i < 100; ++i)
158 {
159 (void) mr.allocate(1);
160 mr.release();
161 }
162 VERIFY( r.number_of_active_allocations() == 0 );
163 }
164 }
165
166 int
167 main()
168 {
169 test01();
170 test02();
171 test03();
172 test04();
173 test05();
174 }