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