]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/experimental/memory_resource/1.cc
1 // { dg-do run { target c++14 } }
2 // { dg-require-atomic-builtins "" }
4 // Copyright (C) 2015-2020 Free Software Foundation, Inc.
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)
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.
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/>.
21 #include <experimental/memory_resource>
24 #include <testsuite_hooks.h>
25 #include <testsuite_allocator.h>
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
;
36 ~A() { ++dtor_count
; }
37 static int ctor_count
;
38 static int dtor_count
;
41 int A::ctor_count
= 0;
42 int A::dtor_count
= 0;
44 struct CountedResource
: public memory_resource
47 CountedResource() = default;
48 ~CountedResource() = default;
50 static size_t get_alloc_count() { return alloc_count
; }
51 static size_t get_dalloc_count() { return dalloc_count
; }
53 static size_t alloc_count
;
54 static size_t dalloc_count
;
56 void* do_allocate(size_t bytes
, size_t alignment
)
59 if (auto ptr
= std::malloc(bytes
))
61 throw std::bad_alloc();
64 void do_deallocate(void *p
, size_t bytes
, size_t alignment
)
66 dalloc_count
+= bytes
;
70 bool do_is_equal(const memory_resource
& __other
) const noexcept
71 { return this == &__other
; }
74 size_t CountedResource::alloc_count
= 0;
75 size_t CountedResource::dalloc_count
= 0;
79 CountedResource::alloc_count
= 0;
80 CountedResource::dalloc_count
= 0;
89 memory_resource
* r
= new_delete_resource();
90 VERIFY(get_default_resource() == r
);
91 void *p
= get_default_resource()->allocate(5);
93 get_default_resource()->deallocate(p
, 5);
96 CountedResource
* cr
= new CountedResource();
97 set_default_resource(cr
);
98 VERIFY(get_default_resource() == cr
);
99 void *pc
= get_default_resource()->allocate(5);
101 get_default_resource()->deallocate(pc
, 5);
102 VERIFY(CountedResource::get_alloc_count() == 5);
103 VERIFY(CountedResource::get_dalloc_count() == 5);
106 // polymorphic_allocator
113 polymorphic_allocator
<A
> pa(&cr
);
114 std::vector
<A
, polymorphic_allocator
<A
>> v(5, A(), pa
);
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);
127 polymorphic_allocator
<A
> pa(&cr
);
128 A
* p
= pa
.allocate(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);
141 polymorphic_allocator
<A
> pa1(get_default_resource());
142 polymorphic_allocator
<A
> pa2(get_default_resource());
144 polymorphic_allocator
<A
> pa3
= pa2
.select_on_container_copy_construction();