]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/20_util/monotonic_buffer_resource/deallocate.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 20_util / monotonic_buffer_resource / deallocate.cc
1 // Copyright (C) 2018-2023 Free Software Foundation, Inc.
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
18 // { dg-do run { target c++17 } }
19 // { dg-require-cstdint "" }
20
21 #include <memory_resource>
22 #include <testsuite_allocator.h>
23
24 struct resource : __gnu_test::memory_resource
25 {
26 int allocate_calls = 0;
27 int deallocate_calls = 0;
28
29 void*
30 do_allocate(std::size_t bytes, std::size_t align) override
31 {
32 ++allocate_calls;
33 return __gnu_test::memory_resource::do_allocate(bytes, align);
34 }
35
36 void
37 do_deallocate(void* p, std::size_t bytes, std::size_t align) override
38 {
39 ++deallocate_calls;
40 __gnu_test::memory_resource::do_deallocate(p, bytes, align);
41 }
42 };
43
44 void
45 test01()
46 {
47 resource r;
48
49 // test that it's possible to deallocate after each of the constructors
50 {
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 );
62 }
63 VERIFY( r.deallocate_calls == r.allocate_calls );
64 VERIFY( r.number_of_active_allocations() == 0 );
65 {
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);
71 mr.deallocate(p, 64);
72 auto q = mr.allocate(1024);
73 VERIFY( q != nullptr );
74 VERIFY( p != q );
75 VERIFY( pi != reinterpret_cast<std::uintptr_t>(q) );
76 mr.deallocate(q, 1024);
77 VERIFY( r.deallocate_calls == 0 );
78 }
79 VERIFY( r.number_of_active_allocations() == 0 );
80 {
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);
87 mr.deallocate(p, 64);
88 auto q = mr.allocate(1024);
89 VERIFY( q != nullptr );
90 VERIFY( p != q );
91 VERIFY( pi != reinterpret_cast<std::uintptr_t>(q) );
92 mr.deallocate(q, 1024);
93 VERIFY( r.deallocate_calls == 0 );
94 }
95 VERIFY( r.deallocate_calls == r.allocate_calls );
96 VERIFY( r.number_of_active_allocations() == 0 );
97 }
98
99 int
100 main()
101 {
102 test01();
103 }