1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
// Copyright (C) 2018-2024 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
// { dg-do run { target c++17 } }
#include <memory_resource>
#include <testsuite_allocator.h>
struct resource : __gnu_test::memory_resource
{
int allocate_calls = 0;
int deallocate_calls = 0;
void*
do_allocate(std::size_t bytes, std::size_t align) override
{
++allocate_calls;
return __gnu_test::memory_resource::do_allocate(bytes, align);
}
void
do_deallocate(void* p, std::size_t bytes, std::size_t align) override
{
++deallocate_calls;
__gnu_test::memory_resource::do_deallocate(p, bytes, align);
}
};
void
test01()
{
resource r;
std::pmr::monotonic_buffer_resource mbr(&r);
auto p = mbr.allocate(10, 16);
mbr.deallocate(p, 1, 2);
VERIFY( r.deallocate_calls == 0 );
p = mbr.allocate(10, 16);
p = mbr.allocate(10, 16);
p = mbr.allocate(10, 16);
p = mbr.allocate(1024, 64);
p = mbr.allocate(1024, 64);
p = mbr.allocate(128, 8);
p = mbr.allocate(128, 8);
p = mbr.allocate(128, 8);
p = mbr.allocate(128, 8);
p = mbr.allocate(128, 8);
p = mbr.allocate(128, 8);
p = mbr.allocate(128, 8);
mbr.deallocate(p, 1, 2);
p = mbr.allocate(1024, 16);
p = mbr.allocate(1024, 16);
mbr.deallocate(p, 1, 2);
VERIFY( r.deallocate_calls == 0 );
mbr.release();
VERIFY( r.deallocate_calls != 0 );
VERIFY( r.deallocate_calls == r.allocate_calls );
VERIFY( mbr.upstream_resource() == &r );
VERIFY( r.number_of_active_allocations() == 0 );
}
void
test02()
{
std::pmr::monotonic_buffer_resource mbr; // uses get_default_resource()
auto* const upstream = mbr.upstream_resource();
resource r;
__gnu_test::default_resource_mgr _(&r); // calls set_default_resource(&r)
mbr.release();
// release() doesn't change upstream resource:
VERIFY( mbr.upstream_resource() == upstream );
}
void
test03()
{
resource r;
__gnu_test::default_resource_mgr _(&r);
std::pmr::monotonic_buffer_resource mbr(16);
for (int i = 0; i < 100; ++i)
(void) mbr.allocate(4, 1);
const int allocations = r.allocate_calls;
VERIFY( allocations != 0 );
mbr.release();
VERIFY( r.allocate_calls == r.deallocate_calls );
VERIFY( r.number_of_active_allocations() == 0 );
// next_buffer_size should have been reset to the initial value,
// so the allocations from upstream should be the same as before.
r.allocate_calls = 0;
r.deallocate_calls = 0;
for (int i = 0; i < 100; ++i)
(void) mbr.allocate(4,1);
VERIFY( allocations == r.allocate_calls );
}
void
test04()
{
resource r;
unsigned char buffer[1024];
std::pmr::monotonic_buffer_resource mbr(buffer, sizeof(buffer), &r);
void* p = mbr.allocate(800, 16);
VERIFY( p >= buffer && p < (buffer + 16) );
void* const p_in_buffer = p;
VERIFY( r.allocate_calls == 0 );
p = mbr.allocate(300, 1);
VERIFY( p != buffer );
VERIFY( p != buffer );
VERIFY( r.allocate_calls == 1 );
mbr.release();
VERIFY( r.deallocate_calls == 1 );
VERIFY( mbr.upstream_resource() == &r );
VERIFY( r.number_of_active_allocations() == 0 );
// initial buffer should be used again now:
p = mbr.allocate(1000, 16);
VERIFY( p == p_in_buffer );
VERIFY( r.allocate_calls == 1 );
}
void
test05() // LWG 3120
{
char buffer[100];
{
std::pmr::monotonic_buffer_resource mr(buffer, sizeof(buffer),
std::pmr::null_memory_resource());
mr.release();
(void) mr.allocate(60);
}
{
std::pmr::monotonic_buffer_resource mr(buffer, sizeof(buffer),
std::pmr::null_memory_resource());
(void) mr.allocate(60);
mr.release();
(void) mr.allocate(60);
}
{
resource r;
std::pmr::monotonic_buffer_resource mr(&r);
for (int i = 0; i < 100; ++i)
{
(void) mr.allocate(1);
mr.release();
}
VERIFY( r.number_of_active_allocations() == 0 );
}
}
int
main()
{
test01();
test02();
test03();
test04();
test05();
}
|