1 //===----------------------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
13 // template<class T, class A, class... Args>
14 // shared_ptr<T> allocate_shared(const A& a, Args&&... args); // T is not an array
21 #include "min_allocator.h"
22 #include "operator_hijacker.h"
23 #include "test_allocator.h"
24 #include "test_macros.h"
32 A(int i
, char c
) : int_(i
), char_(c
) {++count
;}
34 : int_(a
.int_
), char_(a
.char_
)
38 int get_int() const {return int_
;}
39 char get_char() const {return char_
;}
41 A
* operator& () = delete;
53 Zero(Zero
const &) {++count
;}
63 explicit One(int v
) : value(v
) {++count
;}
64 One(One
const & o
) : value(o
.value
) {++count
;}
75 Two(int v
, int) : value(v
) {++count
;}
76 Two(Two
const & o
) : value(o
.value
) {++count
;}
86 Three(int v
, int, int) : value(v
) {++count
;}
87 Three(Three
const & o
) : value(o
.value
) {++count
;}
93 template <class Alloc
>
98 std::shared_ptr
<Zero
> p
= std::allocate_shared
<Zero
>(Alloc());
99 assert(Zero::count
== 1);
101 assert(Zero::count
== 0);
104 std::shared_ptr
<One
> p
= std::allocate_shared
<One
>(Alloc(), i
);
105 assert(One::count
== 1);
106 assert(p
->value
== i
);
108 assert(One::count
== 0);
111 std::shared_ptr
<Two
> p
= std::allocate_shared
<Two
>(Alloc(), i
, bad
);
112 assert(Two::count
== 1);
113 assert(p
->value
== i
);
115 assert(Two::count
== 0);
118 std::shared_ptr
<Three
> p
= std::allocate_shared
<Three
>(Alloc(), i
, bad
, bad
);
119 assert(Three::count
== 1);
120 assert(p
->value
== i
);
122 assert(Three::count
== 0);
125 int main(int, char**)
127 test
<bare_allocator
<void> >();
128 test
<test_allocator
<void> >();
130 test_allocator_statistics alloc_stats
;
134 std::shared_ptr
<A
> p
= std::allocate_shared
<A
>(test_allocator
<A
>(54, &alloc_stats
), i
, c
);
135 assert(alloc_stats
.alloc_count
== 1);
136 assert(A::count
== 1);
137 assert(p
->get_int() == 67);
138 assert(p
->get_char() == 'e');
140 assert(A::count
== 0);
141 assert(alloc_stats
.alloc_count
== 0);
145 std::shared_ptr
<A
> p
= std::allocate_shared
<A
>(min_allocator
<void>(), i
, c
);
146 assert(A::count
== 1);
147 assert(p
->get_int() == 67);
148 assert(p
->get_char() == 'e');
150 assert(A::count
== 0);
154 std::shared_ptr
<A
> p
= std::allocate_shared
<A
>(bare_allocator
<void>(), i
, c
);
155 assert(A::count
== 1);
156 assert(p
->get_int() == 68);
157 assert(p
->get_char() == 'f');
159 assert(A::count
== 0);
161 // Make sure std::allocate_shared handles badly-behaved types properly
163 std::shared_ptr
<operator_hijacker
> p1
= std::allocate_shared
<operator_hijacker
>(min_allocator
<operator_hijacker
>());
164 std::shared_ptr
<operator_hijacker
> p2
= std::allocate_shared
<operator_hijacker
>(min_allocator
<operator_hijacker
>(), operator_hijacker());
165 assert(p1
!= nullptr);
166 assert(p2
!= nullptr);