]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/experimental/memory_resource/new_delete_resource.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / experimental / memory_resource / new_delete_resource.cc
1 // Copyright (C) 2018-2019 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++14 } }
19 // { dg-require-cstdint "" }
20 // { dg-xfail-run-if "PR libstdc++/77691" { { i?86-*-solaris2.* x86_64-*-solaris2.* } && ilp32 } }
21
22 #include <experimental/memory_resource>
23 #include <cstdlib>
24 #include <testsuite_hooks.h>
25
26 bool new_called = false;
27 bool delete_called = false;
28 std::size_t bytes_allocated = 0;
29
30 void* operator new(std::size_t n)
31 {
32 new_called = true;
33 if (void* p = malloc(n))
34 {
35 bytes_allocated += n;
36 return p;
37 }
38 throw std::bad_alloc();
39 }
40
41 void operator delete(void* p)
42 {
43 delete_called = true;
44 std::free(p);
45 bytes_allocated = 0; // assume everything getting deleted
46 }
47
48 void operator delete(void* p, std::size_t n)
49 {
50 delete_called = true;
51 std::free(p);
52 bytes_allocated -= n;
53 }
54
55
56 template<std::size_t A>
57 bool aligned(void* p)
58 {
59 return (reinterpret_cast<std::uintptr_t>(p) % A) == 0;
60 }
61
62 template<typename T>
63 bool aligned(void* p)
64 { return aligned<alignof(T)>(p); }
65
66 // Extended alignments:
67 constexpr std::size_t al6 = (1ul << 6), al12 = (1ul << 12), al18 = (1ul << 18);
68
69 using std::experimental::pmr::memory_resource;
70 using std::experimental::pmr::new_delete_resource;
71
72 memory_resource* global = nullptr;
73
74 void
75 test01()
76 {
77 memory_resource* r1 = new_delete_resource();
78 VERIFY( *r1 == *r1 );
79 memory_resource* r2 = new_delete_resource();
80 VERIFY( r1 == r2 );
81 VERIFY( *r1 == *r2 );
82 global = r1;
83 }
84
85 void
86 test02()
87 {
88 memory_resource* r1 = new_delete_resource();
89 VERIFY( r1 == global );
90 VERIFY( *r1 == *global );
91
92 new_called = false;
93 delete_called = false;
94 void* p = r1->allocate(1);
95 VERIFY( new_called );
96 VERIFY( ! delete_called );
97
98 new_called = false;
99 r1->deallocate(p, 1);
100 VERIFY( ! new_called );
101 VERIFY( delete_called );
102 }
103
104 void
105 test03()
106
107 {
108 using std::max_align_t;
109 using std::size_t;
110 void* p = nullptr;
111
112 auto max = [](int n, int a) { return n > a ? n : a; };
113
114 bytes_allocated = 0;
115
116 memory_resource* r1 = new_delete_resource();
117 p = r1->allocate(1); // uses alignment = alignof(max_align_t)
118 VERIFY( bytes_allocated <= alignof(max_align_t) );
119 VERIFY( aligned<max_align_t>(p) );
120 r1->deallocate(p, 1);
121 VERIFY( bytes_allocated == 0 );
122
123 p = r1->allocate(2, alignof(char));
124 VERIFY( bytes_allocated == 2 );
125 VERIFY( aligned<max_align_t>(p) );
126 r1->deallocate(p, 2);
127 VERIFY( bytes_allocated == 0 );
128
129 p = r1->allocate(3, alignof(short));
130 VERIFY( bytes_allocated == max(3, alignof(short)) );
131 VERIFY( aligned<short>(p) );
132 r1->deallocate(p, 3, alignof(short));
133 VERIFY( bytes_allocated == 0 );
134
135 p = r1->allocate(4, alignof(long));
136 VERIFY( bytes_allocated == max(4, alignof(long)) );
137 VERIFY( aligned<long>(p) );
138 r1->deallocate(p, 4, alignof(long));
139 VERIFY( bytes_allocated == 0 );
140
141 // Test extended aligments:
142 p = r1->allocate(777, al6);
143 VERIFY( bytes_allocated >= 777 );
144 VERIFY( bytes_allocated < (777 + al6 + 8) ); // reasonable upper bound
145 VERIFY( aligned<al6>(p) );
146 r1->deallocate(p, 777, al6);
147 VERIFY( bytes_allocated == 0 );
148
149 p = r1->allocate(888, al12);
150 VERIFY( bytes_allocated >= 888 );
151 VERIFY( bytes_allocated < (888 + al12 + 8) ); // reasonable upper bound
152 VERIFY( aligned<al12>(p) );
153 r1->deallocate(p, 888, al12);
154 VERIFY( bytes_allocated == 0 );
155
156 p = r1->allocate(999, al18);
157 VERIFY( bytes_allocated >= 999 );
158 VERIFY( bytes_allocated < (999 + al18 + 8) ); // reasonable upper bound
159 VERIFY( aligned<al18>(p) );
160 r1->deallocate(p, 999, al18);
161 VERIFY( bytes_allocated == 0 );
162 }
163
164 int main()
165 {
166 test01();
167 test02();
168 test03();
169 }