1 // Copyright (C) 2018-2023 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 } }
19 // { dg-require-cstdint "" }
21 #include <memory_resource>
22 #include <testsuite_allocator.h>
24 struct resource
: __gnu_test::memory_resource
26 int allocate_calls
= 0;
27 int deallocate_calls
= 0;
30 do_allocate(std::size_t bytes
, std::size_t align
) override
33 return __gnu_test::memory_resource::do_allocate(bytes
, align
);
37 do_deallocate(void* p
, std::size_t bytes
, std::size_t align
) override
40 __gnu_test::memory_resource::do_deallocate(p
, bytes
, align
);
49 // test that it's possible to deallocate after each of the constructors
51 std::pmr::monotonic_buffer_resource
mr(&r
);
52 auto p
= mr
.allocate(1024);
53 VERIFY( p
!= nullptr );
54 const std::uintptr_t pi
= reinterpret_cast<std::uintptr_t>(p
);
55 mr
.deallocate(p
, 1024);
56 VERIFY( r
.deallocate_calls
== 0 );
57 auto q
= mr
.allocate(1024);
58 VERIFY( q
!= nullptr );
59 VERIFY( pi
!= reinterpret_cast<std::uintptr_t>(q
) );
60 mr
.deallocate(q
, 1024);
61 VERIFY( r
.deallocate_calls
== 0 );
63 VERIFY( r
.deallocate_calls
== r
.allocate_calls
);
64 VERIFY( r
.number_of_active_allocations() == 0 );
66 r
.deallocate_calls
= r
.allocate_calls
= 0;
67 std::pmr::monotonic_buffer_resource
mr(128, &r
);
68 auto p
= mr
.allocate(64);
69 VERIFY( p
!= nullptr );
70 const std::uintptr_t pi
= reinterpret_cast<std::uintptr_t>(p
);
72 auto q
= mr
.allocate(1024);
73 VERIFY( q
!= nullptr );
75 VERIFY( pi
!= reinterpret_cast<std::uintptr_t>(q
) );
76 mr
.deallocate(q
, 1024);
77 VERIFY( r
.deallocate_calls
== 0 );
79 VERIFY( r
.number_of_active_allocations() == 0 );
81 r
.deallocate_calls
= r
.allocate_calls
= 0;
82 unsigned char buf
[64];
83 std::pmr::monotonic_buffer_resource
mr((void*)buf
, sizeof(buf
), &r
);
84 auto p
= mr
.allocate(64);
85 VERIFY( p
!= nullptr );
86 const std::uintptr_t pi
= reinterpret_cast<std::uintptr_t>(p
);
88 auto q
= mr
.allocate(1024);
89 VERIFY( q
!= nullptr );
91 VERIFY( pi
!= reinterpret_cast<std::uintptr_t>(q
) );
92 mr
.deallocate(q
, 1024);
93 VERIFY( r
.deallocate_calls
== 0 );
95 VERIFY( r
.deallocate_calls
== r
.allocate_calls
);
96 VERIFY( r
.number_of_active_allocations() == 0 );