1 // Copyright (C) 2018-2021 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-do run { target c++17 } }
20 #include <memory_resource>
21 #include <testsuite_allocator.h>
23 struct resource
: __gnu_test::memory_resource
25 int allocate_calls
= 0;
26 int deallocate_calls
= 0;
29 do_allocate(std::size_t bytes
, std::size_t align
) override
32 return __gnu_test::memory_resource::do_allocate(bytes
, align
);
36 do_deallocate(void* p
, std::size_t bytes
, std::size_t align
) override
39 __gnu_test::memory_resource::do_deallocate(p
, bytes
, align
);
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 );
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 );
78 std::pmr::monotonic_buffer_resource mbr
; // uses get_default_resource()
79 auto* const upstream
= mbr
.upstream_resource();
81 __gnu_test::default_resource_mgr
_(&r
); // calls set_default_resource(&r)
83 // release() doesn't change upstream resource:
84 VERIFY( mbr
.upstream_resource() == upstream
);
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 );
98 VERIFY( r
.allocate_calls
== r
.deallocate_calls
);
99 VERIFY( r
.number_of_active_allocations() == 0 );
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
);
114 unsigned char buffer
[1024];
115 std::pmr::monotonic_buffer_resource
mbr(buffer
, sizeof(buffer
), &r
);
116 void* p
= mbr
.allocate(800, 16);
117 VERIFY( p
>= buffer
&& p
< (buffer
+ 16) );
118 void* const p_in_buffer
= p
;
119 VERIFY( r
.allocate_calls
== 0 );
120 p
= mbr
.allocate(300, 1);
121 VERIFY( p
!= buffer
);
122 VERIFY( p
!= buffer
);
123 VERIFY( r
.allocate_calls
== 1 );
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:
129 p
= mbr
.allocate(1000, 16);
130 VERIFY( p
== p_in_buffer
);
131 VERIFY( r
.allocate_calls
== 1 );
139 std::pmr::monotonic_buffer_resource
mr(buffer
, sizeof(buffer
),
140 std::pmr::null_memory_resource());
142 (void) mr
.allocate(60);
146 std::pmr::monotonic_buffer_resource
mr(buffer
, sizeof(buffer
),
147 std::pmr::null_memory_resource());
148 (void) mr
.allocate(60);
150 (void) mr
.allocate(60);
155 std::pmr::monotonic_buffer_resource
mr(&r
);
156 for (int i
= 0; i
< 100; ++i
)
158 (void) mr
.allocate(1);
161 VERIFY( r
.number_of_active_allocations() == 0 );