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