]>
Commit | Line | Data |
---|---|---|
99dee823 | 1 | // Copyright (C) 2019-2021 Free Software Foundation, Inc. |
0e318273 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++2a" } | |
19 | // { dg-do run { target c++2a } } | |
20 | ||
21 | #include <memory_resource> | |
22 | #include <cstring> | |
23 | #include <testsuite_hooks.h> | |
24 | #include <testsuite_allocator.h> | |
25 | ||
26 | void | |
27 | test01() | |
28 | { | |
29 | __gnu_test::memory_resource res; | |
30 | std::pmr::polymorphic_allocator<> alloc(&res); | |
31 | static_assert( std::is_same_v<decltype(alloc)::value_type, std::byte> ); | |
32 | ||
33 | void* p = alloc.allocate_bytes(100); | |
34 | VERIFY( res.number_of_active_allocations() == 1 ); | |
35 | alloc.deallocate_bytes(p, 100); | |
36 | VERIFY( res.number_of_active_allocations() == 0 ); | |
37 | p = alloc.allocate_bytes(100, 64); | |
38 | VERIFY( res.number_of_active_allocations() == 1 ); | |
39 | alloc.deallocate_bytes(p, 100, 64); | |
40 | VERIFY( res.number_of_active_allocations() == 0 ); | |
41 | ||
42 | int* p1 = alloc.allocate_object<int>(); | |
43 | int* p2 = alloc.allocate_object<int>(2); | |
44 | struct X { double d[10]; }; | |
45 | X* px = alloc.allocate_object<X>(20); | |
46 | VERIFY( res.number_of_active_allocations() == 3 ); | |
47 | ||
48 | alloc.deallocate_object(p1); | |
49 | alloc.deallocate_object(p2, 2); | |
50 | alloc.deallocate_object(px, 20); | |
51 | VERIFY( res.number_of_active_allocations() == 0 ); | |
52 | ||
53 | struct Y { | |
54 | Y(int i, const char* s, bool* alive) | |
55 | : i(i), s(s), alive(alive) | |
56 | { *alive = true; } | |
57 | ||
58 | ~Y() { *alive = false; } | |
59 | ||
60 | int i; | |
61 | const char* s; | |
62 | bool* alive; | |
63 | }; | |
64 | ||
65 | bool alive = false; | |
66 | Y* py = alloc.new_object<Y>(1, "two", &alive); | |
67 | VERIFY( alive ); | |
68 | VERIFY( py->i == 1 ); | |
69 | VERIFY( std::strcmp(py->s, "two") == 0 ); | |
70 | VERIFY( res.number_of_active_allocations() == 1 ); | |
71 | ||
72 | alloc.delete_object(py); | |
73 | VERIFY( alive == false ); | |
74 | } | |
75 | ||
76 | int | |
77 | main() | |
78 | { | |
79 | test01(); | |
80 | } |