1 // Copyright (C) 2018-2019 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-options "-std=gnu++17" }
19 // { dg-do run { target c++17 } }
20 // { dg-require-cstdint "" }
22 #include <memory_resource>
23 #include <testsuite_allocator.h>
25 struct resource
: __gnu_test::memory_resource
27 int allocate_calls
= 0;
28 int deallocate_calls
= 0;
31 do_allocate(std::size_t bytes
, std::size_t align
) override
34 return __gnu_test::memory_resource::do_allocate(bytes
, align
);
38 do_deallocate(void* p
, std::size_t bytes
, std::size_t align
) override
41 __gnu_test::memory_resource::do_deallocate(p
, bytes
, align
);
50 // test that it's possible to deallocate after each of the constructors
52 std::pmr::monotonic_buffer_resource
mr(&r
);
53 auto p
= mr
.allocate(1024);
54 VERIFY( p
!= nullptr );
55 const std::uintptr_t pi
= reinterpret_cast<std::uintptr_t>(p
);
56 mr
.deallocate(p
, 1024);
57 VERIFY( r
.deallocate_calls
== 0 );
58 auto q
= mr
.allocate(1024);
59 VERIFY( q
!= nullptr );
60 VERIFY( pi
!= reinterpret_cast<std::uintptr_t>(q
) );
61 mr
.deallocate(q
, 1024);
62 VERIFY( r
.deallocate_calls
== 0 );
64 VERIFY( r
.deallocate_calls
== r
.allocate_calls
);
65 VERIFY( r
.number_of_active_allocations() == 0 );
67 r
.deallocate_calls
= r
.allocate_calls
= 0;
68 std::pmr::monotonic_buffer_resource
mr(128, &r
);
69 auto p
= mr
.allocate(64);
70 VERIFY( p
!= nullptr );
71 const std::uintptr_t pi
= reinterpret_cast<std::uintptr_t>(p
);
73 auto q
= mr
.allocate(1024);
74 VERIFY( q
!= nullptr );
76 VERIFY( pi
!= reinterpret_cast<std::uintptr_t>(q
) );
77 mr
.deallocate(q
, 1024);
78 VERIFY( r
.deallocate_calls
== 0 );
80 VERIFY( r
.number_of_active_allocations() == 0 );
82 r
.deallocate_calls
= r
.allocate_calls
= 0;
83 unsigned char buf
[64];
84 std::pmr::monotonic_buffer_resource
mr((void*)buf
, sizeof(buf
), &r
);
85 auto p
= mr
.allocate(64);
86 VERIFY( p
!= nullptr );
87 const std::uintptr_t pi
= reinterpret_cast<std::uintptr_t>(p
);
89 auto q
= mr
.allocate(1024);
90 VERIFY( q
!= nullptr );
92 VERIFY( pi
!= reinterpret_cast<std::uintptr_t>(q
) );
93 mr
.deallocate(q
, 1024);
94 VERIFY( r
.deallocate_calls
== 0 );
96 VERIFY( r
.deallocate_calls
== r
.allocate_calls
);
97 VERIFY( r
.number_of_active_allocations() == 0 );