[24/46] Make stmt_info_for_cost use a stmt_vec_info
[official-gcc.git] / libstdc++-v3 / testsuite / experimental / memory_resource / 1.cc
blob69869792ab35c7622572d209ed72e7f2505d5191
1 // { dg-do run { target c++14 } }
2 // { dg-require-atomic-builtins "" }
4 // Copyright (C) 2015-2018 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.
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>
22 #include <vector>
23 #include <cstdlib>
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;
33 struct A
35 A() { ++ctor_count; }
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
46 public:
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;
55 protected:
56 void* do_allocate(size_t bytes, size_t alignment)
58 alloc_count += bytes;
59 if (auto ptr = std::malloc(bytes))
60 return ptr;
61 throw std::bad_alloc();
64 void do_deallocate(void *p, size_t bytes, size_t alignment)
66 dalloc_count += bytes;
67 std::free(p);
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;
77 void clear()
79 CountedResource::alloc_count = 0;
80 CountedResource::dalloc_count = 0;
81 A::ctor_count = 0;
82 A::dtor_count = 0;
85 // memory resource
86 void
87 test01()
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);
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);
106 // polymorphic_allocator
107 void
108 test02()
110 clear();
112 CountedResource cr;
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);
122 void
123 test03()
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);
138 void
139 test04()
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);
148 int main()
150 test01();
151 test02();
152 test03();
153 test04();