]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/experimental/memory_resource/1.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / experimental / memory_resource / 1.cc
1 // { dg-do run { target c++14 } }
2 // { dg-require-atomic-builtins "" }
3
4 // Copyright (C) 2015-2020 Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
10 // any later version.
11
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING3. If not see
19 // <http://www.gnu.org/licenses/>.
20
21 #include <experimental/memory_resource>
22 #include <vector>
23 #include <cstdlib>
24 #include <testsuite_hooks.h>
25 #include <testsuite_allocator.h>
26
27 using std::experimental::pmr::polymorphic_allocator;
28 using std::experimental::pmr::memory_resource;
29 using std::experimental::pmr::new_delete_resource;
30 using std::experimental::pmr::get_default_resource;
31 using std::experimental::pmr::set_default_resource;
32
33 struct A
34 {
35 A() { ++ctor_count; }
36 ~A() { ++dtor_count; }
37 static int ctor_count;
38 static int dtor_count;
39 };
40
41 int A::ctor_count = 0;
42 int A::dtor_count = 0;
43
44 struct CountedResource : public memory_resource
45 {
46 public:
47 CountedResource() = default;
48 ~CountedResource() = default;
49
50 static size_t get_alloc_count() { return alloc_count; }
51 static size_t get_dalloc_count() { return dalloc_count; }
52
53 static size_t alloc_count;
54 static size_t dalloc_count;
55 protected:
56 void* do_allocate(size_t bytes, size_t alignment)
57 {
58 alloc_count += bytes;
59 if (auto ptr = std::malloc(bytes))
60 return ptr;
61 throw std::bad_alloc();
62 }
63
64 void do_deallocate(void *p, size_t bytes, size_t alignment)
65 {
66 dalloc_count += bytes;
67 std::free(p);
68 }
69
70 bool do_is_equal(const memory_resource& __other) const noexcept
71 { return this == &__other; }
72 };
73
74 size_t CountedResource::alloc_count = 0;
75 size_t CountedResource::dalloc_count = 0;
76
77 void clear()
78 {
79 CountedResource::alloc_count = 0;
80 CountedResource::dalloc_count = 0;
81 A::ctor_count = 0;
82 A::dtor_count = 0;
83 }
84
85 // memory resource
86 void
87 test01()
88 {
89 memory_resource* r = new_delete_resource();
90 VERIFY(get_default_resource() == r);
91 void *p = get_default_resource()->allocate(5);
92 VERIFY(p);
93 get_default_resource()->deallocate(p, 5);
94
95 clear();
96 CountedResource* cr = new CountedResource();
97 set_default_resource(cr);
98 VERIFY(get_default_resource() == cr);
99 void *pc = get_default_resource()->allocate(5);
100 VERIFY(pc);
101 get_default_resource()->deallocate(pc, 5);
102 VERIFY(CountedResource::get_alloc_count() == 5);
103 VERIFY(CountedResource::get_dalloc_count() == 5);
104 }
105
106 // polymorphic_allocator
107 void
108 test02()
109 {
110 clear();
111 {
112 CountedResource cr;
113 polymorphic_allocator<A> pa(&cr);
114 std::vector<A, polymorphic_allocator<A>> v(5, A(), pa);
115 }
116 VERIFY(A::ctor_count == 1);
117 VERIFY(A::dtor_count == 6);
118 VERIFY(CountedResource::get_alloc_count() == 5);
119 VERIFY(CountedResource::get_dalloc_count() == 5);
120 }
121
122 void
123 test03()
124 {
125 clear();
126 CountedResource cr;
127 polymorphic_allocator<A> pa(&cr);
128 A* p = pa.allocate(1);
129 pa.construct(p);
130 pa.destroy(p);
131 pa.deallocate(p, 1);
132 VERIFY(A::ctor_count == 1);
133 VERIFY(A::dtor_count == 1);
134 VERIFY(CountedResource::get_alloc_count() == 1);
135 VERIFY(CountedResource::get_dalloc_count() == 1);
136 }
137
138 void
139 test04()
140 {
141 polymorphic_allocator<A> pa1(get_default_resource());
142 polymorphic_allocator<A> pa2(get_default_resource());
143 VERIFY(pa1 == pa2);
144 polymorphic_allocator<A> pa3 = pa2.select_on_container_copy_construction();
145 VERIFY(pa1 == pa3);
146 }
147
148 int main()
149 {
150 test01();
151 test02();
152 test03();
153 test04();
154 }