1 // Copyright (C) 2018 Free Software Foundation, Inc.
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)
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.
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/>.
18 // { dg-options "-std=gnu++17" }
19 // { dg-do run { target c++17 } }
20 // { dg-require-cstdint "" }
22 #include <memory_resource>
23 #include <testsuite_allocator.h>
28 __gnu_test::memory_resource r
;
30 // test that it's possible to allocate after each of the constructors
32 std::pmr::monotonic_buffer_resource
mr(&r
);
33 auto p
= mr
.allocate(1024);
34 VERIFY( p
!= nullptr );
35 auto q
= mr
.allocate(1024);
36 VERIFY( q
!= nullptr );
39 VERIFY( r
.number_of_active_allocations() == 0 );
41 std::pmr::monotonic_buffer_resource
mr(128, &r
);
42 auto p
= mr
.allocate(1024);
43 VERIFY( p
!= nullptr );
44 auto q
= mr
.allocate(1024);
45 VERIFY( q
!= nullptr );
48 VERIFY( r
.number_of_active_allocations() == 0 );
50 unsigned char buf
[64];
51 std::pmr::monotonic_buffer_resource
mr((void*)buf
, sizeof(buf
), &r
);
52 auto p
= mr
.allocate(1024);
53 VERIFY( p
!= nullptr );
54 auto q
= mr
.allocate(1024);
55 VERIFY( q
!= nullptr );
58 VERIFY( r
.number_of_active_allocations() == 0 );
60 std::pmr::monotonic_buffer_resource mr
;
61 auto p
= mr
.allocate(1024);
62 VERIFY( p
!= nullptr );
63 auto q
= mr
.allocate(1024);
64 VERIFY( q
!= nullptr );
68 std::pmr::monotonic_buffer_resource
mr(64);
69 auto p
= mr
.allocate(1024);
70 VERIFY( p
!= nullptr );
71 auto q
= mr
.allocate(1024);
72 VERIFY( q
!= nullptr );
76 unsigned char buf
[64];
77 std::pmr::monotonic_buffer_resource
mr((void*)buf
, sizeof(buf
));
78 auto p
= mr
.allocate(1024);
79 VERIFY( p
!= nullptr );
80 auto q
= mr
.allocate(1024);
81 VERIFY( q
!= nullptr );
89 unsigned char buf
[64];
90 std::pmr::monotonic_buffer_resource
mr(buf
, sizeof(buf
));
92 auto p
= mr
.allocate(0);
93 VERIFY( p
!= nullptr );
94 auto q
= mr
.allocate(0);
95 VERIFY( q
!= nullptr );
98 p
= mr
.allocate(0, 1);
99 VERIFY( p
!= nullptr );
100 q
= mr
.allocate(0, 1);
101 VERIFY( q
!= nullptr );
110 std::pmr::monotonic_buffer_resource
mr(std::pmr::null_memory_resource());
114 (void) mr
.allocate(1, 1);
116 catch (const std::bad_alloc
&)
123 unsigned char buf
[16];
124 std::pmr::monotonic_buffer_resource
mr(buf
, sizeof(buf
),
125 std::pmr::null_memory_resource());
126 (void) mr
.allocate(16, 1);
130 (void) mr
.allocate(1, 1);
132 catch (const std::bad_alloc
&)
144 auto buf
= new unsigned char[512];
145 std::pmr::monotonic_buffer_resource
mr(buf
, 512,
146 std::pmr::null_memory_resource());
147 std::size_t prev_size
= 1;
148 void* prev_ptr
= mr
.allocate(prev_size
, 1);
149 for (int i
= 0; i
< 9; ++i
)
151 std::size_t size
= 1 << i
;
152 void* ptr
= mr
.allocate(size
, 1);
153 VERIFY( ((char*)ptr
- (char*)prev_ptr
) == prev_size
);
162 // test that returned pointer is correctly aligned
163 auto is_aligned
= [](void* p
, size_t alignment
) -> bool {
164 return (reinterpret_cast<std::uintptr_t>(p
) % alignment
) == 0;
167 auto buf
= new unsigned char[2048];
168 std::pmr::monotonic_buffer_resource
mr(buf
+1, 2047);
169 for (int i
= 0; i
< 9; ++i
)
171 auto p
= mr
.allocate(1, 1 << i
);
172 VERIFY( is_aligned(p
, 1 << i
) );
173 // Make next available byte misaligned:
174 (void) mr
.allocate(1 << i
, 1);
181 // check for geometric progression in buffer sizes from upstream
183 struct resource
: __gnu_test::memory_resource
185 bool allocated
= false;
186 std::size_t last_size
= 0;
189 do_allocate(size_t bytes
, size_t align
) override
193 return __gnu_test::memory_resource::do_allocate(bytes
, align
);
198 std::pmr::monotonic_buffer_resource
mr(32, &r
);
199 std::size_t last_size
= 0;
201 for (int i
= 0; i
< 100; ++i
)
203 (void) mr
.allocate(16);
206 VERIFY(r
.last_size
>= last_size
);
207 last_size
= r
.last_size
;