blob: 8ce8517a71d9195fc3de9bec3be6aedc71b73853 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===-------------------------- memory ------------------------------------===//
3//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_MEMORY
11#define _LIBCPP_MEMORY
12
13/*
14 memory synopsis
15
16namespace std
17{
18
19struct allocator_arg_t { };
Marshall Clowf1bf62f2018-01-02 17:17:01 +000020inline constexpr allocator_arg_t allocator_arg = allocator_arg_t();
Howard Hinnantc51e1022010-05-11 19:42:16 +000021
22template <class T, class Alloc> struct uses_allocator;
23
24template <class Ptr>
25struct pointer_traits
26{
27 typedef Ptr pointer;
28 typedef <details> element_type;
29 typedef <details> difference_type;
Howard Hinnant3b6579a2010-08-22 00:02:43 +000030
Howard Hinnantc51e1022010-05-11 19:42:16 +000031 template <class U> using rebind = <details>;
Howard Hinnant3b6579a2010-08-22 00:02:43 +000032
Howard Hinnantc51e1022010-05-11 19:42:16 +000033 static pointer pointer_to(<details>);
34};
35
Howard Hinnant719bda32011-05-28 14:41:13 +000036template <class T>
37struct pointer_traits<T*>
38{
39 typedef T* pointer;
40 typedef T element_type;
41 typedef ptrdiff_t difference_type;
42
43 template <class U> using rebind = U*;
44
Louis Dionne44e1ea82018-11-13 17:04:05 +000045 static pointer pointer_to(<details>) noexcept; // constexpr in C++20
Howard Hinnant719bda32011-05-28 14:41:13 +000046};
47
Eric Fiselier45c9aac2017-11-22 19:49:21 +000048template <class T> constexpr T* to_address(T* p) noexcept; // C++20
Marek Kurdej1f0cde92020-12-06 15:23:46 +010049template <class Ptr> constexpr auto to_address(const Ptr& p) noexcept; // C++20
Eric Fiselier45c9aac2017-11-22 19:49:21 +000050
Howard Hinnantc51e1022010-05-11 19:42:16 +000051template <class Alloc>
52struct allocator_traits
53{
54 typedef Alloc allocator_type;
55 typedef typename allocator_type::value_type
56 value_type;
57
58 typedef Alloc::pointer | value_type* pointer;
59 typedef Alloc::const_pointer
60 | pointer_traits<pointer>::rebind<const value_type>
61 const_pointer;
62 typedef Alloc::void_pointer
63 | pointer_traits<pointer>::rebind<void>
64 void_pointer;
65 typedef Alloc::const_void_pointer
66 | pointer_traits<pointer>::rebind<const void>
67 const_void_pointer;
68 typedef Alloc::difference_type
Howard Hinnant8e4e6002010-11-18 01:40:00 +000069 | pointer_traits<pointer>::difference_type
70 difference_type;
71 typedef Alloc::size_type
72 | make_unsigned<difference_type>::type
73 size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +000074 typedef Alloc::propagate_on_container_copy_assignment
75 | false_type propagate_on_container_copy_assignment;
76 typedef Alloc::propagate_on_container_move_assignment
77 | false_type propagate_on_container_move_assignment;
78 typedef Alloc::propagate_on_container_swap
79 | false_type propagate_on_container_swap;
Marshall Clow0b587562015-06-02 16:34:03 +000080 typedef Alloc::is_always_equal
81 | is_empty is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +000082
Michael Park99f9e912020-03-04 11:27:14 -050083 template <class T> using rebind_alloc = Alloc::rebind<T>::other | Alloc<T, Args...>;
Howard Hinnantc51e1022010-05-11 19:42:16 +000084 template <class T> using rebind_traits = allocator_traits<rebind_alloc<T>>;
85
Louis Dionne99f59432020-09-17 12:06:13 -040086 static pointer allocate(allocator_type& a, size_type n); // constexpr and [[nodiscard]] in C++20
87 static pointer allocate(allocator_type& a, size_type n, const_void_pointer hint); // constexpr and [[nodiscard]] in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000088
Louis Dionne99f59432020-09-17 12:06:13 -040089 static void deallocate(allocator_type& a, pointer p, size_type n) noexcept; // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000090
91 template <class T, class... Args>
Louis Dionne99f59432020-09-17 12:06:13 -040092 static void construct(allocator_type& a, T* p, Args&&... args); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000093
94 template <class T>
Louis Dionne99f59432020-09-17 12:06:13 -040095 static void destroy(allocator_type& a, T* p); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000096
Louis Dionne99f59432020-09-17 12:06:13 -040097 static size_type max_size(const allocator_type& a); // noexcept in C++14, constexpr in C++20
98 static allocator_type select_on_container_copy_construction(const allocator_type& a); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000099};
100
101template <>
Michael Park99f9e912020-03-04 11:27:14 -0500102class allocator<void> // deprecated in C++17, removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000103{
104public:
105 typedef void* pointer;
106 typedef const void* const_pointer;
107 typedef void value_type;
108
109 template <class _Up> struct rebind {typedef allocator<_Up> other;};
110};
111
112template <class T>
113class allocator
114{
115public:
Louis Dionnec0056af2020-08-28 12:31:16 -0400116 typedef size_t size_type;
117 typedef ptrdiff_t difference_type;
Michael Park99f9e912020-03-04 11:27:14 -0500118 typedef T* pointer; // deprecated in C++17, removed in C++20
119 typedef const T* const_pointer; // deprecated in C++17, removed in C++20
120 typedef typename add_lvalue_reference<T>::type
121 reference; // deprecated in C++17, removed in C++20
122 typedef typename add_lvalue_reference<const T>::type
123 const_reference; // deprecated in C++17, removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000124
Michael Park99f9e912020-03-04 11:27:14 -0500125 typedef T value_type;
126
127 template <class U> struct rebind {typedef allocator<U> other;}; // deprecated in C++17, removed in C++20
128
129 typedef true_type propagate_on_container_move_assignment;
130 typedef true_type is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000131
Marshall Clowbc759762018-03-20 23:02:53 +0000132 constexpr allocator() noexcept; // constexpr in C++20
133 constexpr allocator(const allocator&) noexcept; // constexpr in C++20
134 template <class U>
135 constexpr allocator(const allocator<U>&) noexcept; // constexpr in C++20
Louis Dionne99f59432020-09-17 12:06:13 -0400136 ~allocator(); // constexpr in C++20
Michael Park99f9e912020-03-04 11:27:14 -0500137 pointer address(reference x) const noexcept; // deprecated in C++17, removed in C++20
138 const_pointer address(const_reference x) const noexcept; // deprecated in C++17, removed in C++20
139 T* allocate(size_t n, const void* hint); // deprecated in C++17, removed in C++20
Louis Dionne99f59432020-09-17 12:06:13 -0400140 T* allocate(size_t n); // constexpr in C++20
141 void deallocate(T* p, size_t n) noexcept; // constexpr in C++20
Michael Park99f9e912020-03-04 11:27:14 -0500142 size_type max_size() const noexcept; // deprecated in C++17, removed in C++20
Howard Hinnant719bda32011-05-28 14:41:13 +0000143 template<class U, class... Args>
Michael Park99f9e912020-03-04 11:27:14 -0500144 void construct(U* p, Args&&... args); // deprecated in C++17, removed in C++20
Howard Hinnant719bda32011-05-28 14:41:13 +0000145 template <class U>
Michael Park99f9e912020-03-04 11:27:14 -0500146 void destroy(U* p); // deprecated in C++17, removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000147};
148
149template <class T, class U>
Louis Dionne99f59432020-09-17 12:06:13 -0400150bool operator==(const allocator<T>&, const allocator<U>&) noexcept; // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000151
152template <class T, class U>
Louis Dionne99f59432020-09-17 12:06:13 -0400153bool operator!=(const allocator<T>&, const allocator<U>&) noexcept; // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000154
155template <class OutputIterator, class T>
156class raw_storage_iterator
157 : public iterator<output_iterator_tag,
158 T, // purposefully not C++03
159 ptrdiff_t, // purposefully not C++03
160 T*, // purposefully not C++03
161 raw_storage_iterator&> // purposefully not C++03
162{
163public:
164 explicit raw_storage_iterator(OutputIterator x);
165 raw_storage_iterator& operator*();
166 raw_storage_iterator& operator=(const T& element);
167 raw_storage_iterator& operator++();
168 raw_storage_iterator operator++(int);
169};
170
Howard Hinnant719bda32011-05-28 14:41:13 +0000171template <class T> pair<T*,ptrdiff_t> get_temporary_buffer(ptrdiff_t n) noexcept;
172template <class T> void return_temporary_buffer(T* p) noexcept;
173
174template <class T> T* addressof(T& r) noexcept;
Marshall Clow78dbe462016-11-14 18:22:19 +0000175template <class T> T* addressof(const T&& r) noexcept = delete;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000176
177template <class InputIterator, class ForwardIterator>
178ForwardIterator
179uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator result);
180
Howard Hinnant719bda32011-05-28 14:41:13 +0000181template <class InputIterator, class Size, class ForwardIterator>
182ForwardIterator
183uninitialized_copy_n(InputIterator first, Size n, ForwardIterator result);
184
Howard Hinnantc51e1022010-05-11 19:42:16 +0000185template <class ForwardIterator, class T>
186void uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& x);
187
188template <class ForwardIterator, class Size, class T>
Howard Hinnant3c811092010-11-18 16:13:03 +0000189ForwardIterator
190uninitialized_fill_n(ForwardIterator first, Size n, const T& x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000191
Louis Dionne99f59432020-09-17 12:06:13 -0400192template <class T, class ...Args>
193constexpr T* construct_at(T* location, Args&& ...args); // since C++20
194
Eric Fiselier383f6cf2016-07-24 03:51:39 +0000195template <class T>
Louis Dionne99f59432020-09-17 12:06:13 -0400196void destroy_at(T* location); // constexpr in C++20
Eric Fiselier383f6cf2016-07-24 03:51:39 +0000197
198template <class ForwardIterator>
Louis Dionne99f59432020-09-17 12:06:13 -0400199void destroy(ForwardIterator first, ForwardIterator last); // constexpr in C++20
Eric Fiselier383f6cf2016-07-24 03:51:39 +0000200
201template <class ForwardIterator, class Size>
Louis Dionne99f59432020-09-17 12:06:13 -0400202ForwardIterator destroy_n(ForwardIterator first, Size n); // constexpr in C++20
Eric Fiselier383f6cf2016-07-24 03:51:39 +0000203
204template <class InputIterator, class ForwardIterator>
205 ForwardIterator uninitialized_move(InputIterator first, InputIterator last, ForwardIterator result);
206
207template <class InputIterator, class Size, class ForwardIterator>
208 pair<InputIterator,ForwardIterator> uninitialized_move_n(InputIterator first, Size n, ForwardIterator result);
209
210template <class ForwardIterator>
211 void uninitialized_value_construct(ForwardIterator first, ForwardIterator last);
212
213template <class ForwardIterator, class Size>
214 ForwardIterator uninitialized_value_construct_n(ForwardIterator first, Size n);
215
216template <class ForwardIterator>
217 void uninitialized_default_construct(ForwardIterator first, ForwardIterator last);
218
219template <class ForwardIterator, class Size>
220 ForwardIterator uninitialized_default_construct_n(ForwardIterator first, Size n);
221
Louis Dionne481a2662018-09-23 18:35:00 +0000222template <class Y> struct auto_ptr_ref {}; // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000223
224template<class X>
Louis Dionne481a2662018-09-23 18:35:00 +0000225class auto_ptr // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000226{
227public:
228 typedef X element_type;
229
230 explicit auto_ptr(X* p =0) throw();
231 auto_ptr(auto_ptr&) throw();
232 template<class Y> auto_ptr(auto_ptr<Y>&) throw();
233 auto_ptr& operator=(auto_ptr&) throw();
234 template<class Y> auto_ptr& operator=(auto_ptr<Y>&) throw();
235 auto_ptr& operator=(auto_ptr_ref<X> r) throw();
236 ~auto_ptr() throw();
237
238 typename add_lvalue_reference<X>::type operator*() const throw();
239 X* operator->() const throw();
240 X* get() const throw();
241 X* release() throw();
242 void reset(X* p =0) throw();
243
244 auto_ptr(auto_ptr_ref<X>) throw();
245 template<class Y> operator auto_ptr_ref<Y>() throw();
246 template<class Y> operator auto_ptr<Y>() throw();
247};
248
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000249template <class T>
250struct default_delete
251{
Howard Hinnant719bda32011-05-28 14:41:13 +0000252 constexpr default_delete() noexcept = default;
253 template <class U> default_delete(const default_delete<U>&) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000254
Howard Hinnant719bda32011-05-28 14:41:13 +0000255 void operator()(T*) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000256};
257
258template <class T>
259struct default_delete<T[]>
260{
Howard Hinnant719bda32011-05-28 14:41:13 +0000261 constexpr default_delete() noexcept = default;
262 void operator()(T*) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000263 template <class U> void operator()(U*) const = delete;
264};
265
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000266template <class T, class D = default_delete<T>>
267class unique_ptr
268{
269public:
270 typedef see below pointer;
271 typedef T element_type;
272 typedef D deleter_type;
273
274 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000275 constexpr unique_ptr() noexcept;
276 explicit unique_ptr(pointer p) noexcept;
277 unique_ptr(pointer p, see below d1) noexcept;
278 unique_ptr(pointer p, see below d2) noexcept;
279 unique_ptr(unique_ptr&& u) noexcept;
280 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000281 template <class U, class E>
Howard Hinnant719bda32011-05-28 14:41:13 +0000282 unique_ptr(unique_ptr<U, E>&& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000283 template <class U>
Marshall Clowb22274f2017-01-24 22:22:33 +0000284 unique_ptr(auto_ptr<U>&& u) noexcept; // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000285
286 // destructor
287 ~unique_ptr();
288
289 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000290 unique_ptr& operator=(unique_ptr&& u) noexcept;
291 template <class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;
292 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000293
294 // observers
295 typename add_lvalue_reference<T>::type operator*() const;
Howard Hinnant719bda32011-05-28 14:41:13 +0000296 pointer operator->() const noexcept;
297 pointer get() const noexcept;
298 deleter_type& get_deleter() noexcept;
299 const deleter_type& get_deleter() const noexcept;
300 explicit operator bool() const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000301
302 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000303 pointer release() noexcept;
304 void reset(pointer p = pointer()) noexcept;
305 void swap(unique_ptr& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000306};
307
308template <class T, class D>
309class unique_ptr<T[], D>
310{
311public:
312 typedef implementation-defined pointer;
313 typedef T element_type;
314 typedef D deleter_type;
315
316 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000317 constexpr unique_ptr() noexcept;
318 explicit unique_ptr(pointer p) noexcept;
319 unique_ptr(pointer p, see below d) noexcept;
320 unique_ptr(pointer p, see below d) noexcept;
321 unique_ptr(unique_ptr&& u) noexcept;
322 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000323
324 // destructor
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000325 ~unique_ptr();
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000326
327 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000328 unique_ptr& operator=(unique_ptr&& u) noexcept;
329 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000330
331 // observers
332 T& operator[](size_t i) const;
Howard Hinnant719bda32011-05-28 14:41:13 +0000333 pointer get() const noexcept;
334 deleter_type& get_deleter() noexcept;
335 const deleter_type& get_deleter() const noexcept;
336 explicit operator bool() const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000337
338 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000339 pointer release() noexcept;
340 void reset(pointer p = pointer()) noexcept;
341 void reset(nullptr_t) noexcept;
Vy Nguyene369bd92020-07-13 12:34:37 -0400342 template <class U> void reset(U) = delete;
Howard Hinnant719bda32011-05-28 14:41:13 +0000343 void swap(unique_ptr& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000344};
345
346template <class T, class D>
Howard Hinnant719bda32011-05-28 14:41:13 +0000347 void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000348
349template <class T1, class D1, class T2, class D2>
350 bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
351template <class T1, class D1, class T2, class D2>
352 bool operator!=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
353template <class T1, class D1, class T2, class D2>
354 bool operator<(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
355template <class T1, class D1, class T2, class D2>
356 bool operator<=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
357template <class T1, class D1, class T2, class D2>
358 bool operator>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
359template <class T1, class D1, class T2, class D2>
360 bool operator>=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
361
Howard Hinnant719bda32011-05-28 14:41:13 +0000362template <class T, class D>
363 bool operator==(const unique_ptr<T, D>& x, nullptr_t) noexcept;
364template <class T, class D>
365 bool operator==(nullptr_t, const unique_ptr<T, D>& y) noexcept;
366template <class T, class D>
367 bool operator!=(const unique_ptr<T, D>& x, nullptr_t) noexcept;
368template <class T, class D>
369 bool operator!=(nullptr_t, const unique_ptr<T, D>& y) noexcept;
370
371template <class T, class D>
372 bool operator<(const unique_ptr<T, D>& x, nullptr_t);
373template <class T, class D>
374 bool operator<(nullptr_t, const unique_ptr<T, D>& y);
375template <class T, class D>
376 bool operator<=(const unique_ptr<T, D>& x, nullptr_t);
377template <class T, class D>
378 bool operator<=(nullptr_t, const unique_ptr<T, D>& y);
379template <class T, class D>
380 bool operator>(const unique_ptr<T, D>& x, nullptr_t);
381template <class T, class D>
382 bool operator>(nullptr_t, const unique_ptr<T, D>& y);
383template <class T, class D>
384 bool operator>=(const unique_ptr<T, D>& x, nullptr_t);
385template <class T, class D>
386 bool operator>=(nullptr_t, const unique_ptr<T, D>& y);
387
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000388class bad_weak_ptr
389 : public std::exception
390{
Howard Hinnant719bda32011-05-28 14:41:13 +0000391 bad_weak_ptr() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000392};
393
Marshall Clow9e8f4a92013-07-01 18:16:03 +0000394template<class T, class... Args> unique_ptr<T> make_unique(Args&&... args); // C++14
395template<class T> unique_ptr<T> make_unique(size_t n); // C++14
396template<class T, class... Args> unspecified make_unique(Args&&...) = delete; // C++14, T == U[N]
397
Marshall Clowe52a3242017-11-27 15:51:36 +0000398template<class E, class T, class Y, class D>
399 basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, unique_ptr<Y, D> const& p);
400
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000401template<class T>
402class shared_ptr
403{
404public:
405 typedef T element_type;
Eric Fiselierae5b6672016-06-27 01:02:43 +0000406 typedef weak_ptr<T> weak_type; // C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000407
408 // constructors:
Howard Hinnant719bda32011-05-28 14:41:13 +0000409 constexpr shared_ptr() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000410 template<class Y> explicit shared_ptr(Y* p);
411 template<class Y, class D> shared_ptr(Y* p, D d);
412 template<class Y, class D, class A> shared_ptr(Y* p, D d, A a);
413 template <class D> shared_ptr(nullptr_t p, D d);
414 template <class D, class A> shared_ptr(nullptr_t p, D d, A a);
Howard Hinnant719bda32011-05-28 14:41:13 +0000415 template<class Y> shared_ptr(const shared_ptr<Y>& r, T *p) noexcept;
416 shared_ptr(const shared_ptr& r) noexcept;
417 template<class Y> shared_ptr(const shared_ptr<Y>& r) noexcept;
418 shared_ptr(shared_ptr&& r) noexcept;
419 template<class Y> shared_ptr(shared_ptr<Y>&& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000420 template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
Marshall Clowb22274f2017-01-24 22:22:33 +0000421 template<class Y> shared_ptr(auto_ptr<Y>&& r); // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000422 template <class Y, class D> shared_ptr(unique_ptr<Y, D>&& r);
423 shared_ptr(nullptr_t) : shared_ptr() { }
424
425 // destructor:
426 ~shared_ptr();
427
428 // assignment:
Howard Hinnant719bda32011-05-28 14:41:13 +0000429 shared_ptr& operator=(const shared_ptr& r) noexcept;
430 template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r) noexcept;
431 shared_ptr& operator=(shared_ptr&& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000432 template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r);
Marshall Clowb22274f2017-01-24 22:22:33 +0000433 template<class Y> shared_ptr& operator=(auto_ptr<Y>&& r); // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000434 template <class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r);
435
436 // modifiers:
Howard Hinnant719bda32011-05-28 14:41:13 +0000437 void swap(shared_ptr& r) noexcept;
438 void reset() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000439 template<class Y> void reset(Y* p);
440 template<class Y, class D> void reset(Y* p, D d);
441 template<class Y, class D, class A> void reset(Y* p, D d, A a);
442
Howard Hinnant719bda32011-05-28 14:41:13 +0000443 // observers:
444 T* get() const noexcept;
445 T& operator*() const noexcept;
446 T* operator->() const noexcept;
447 long use_count() const noexcept;
448 bool unique() const noexcept;
449 explicit operator bool() const noexcept;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000450 template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
451 template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000452};
453
Logan Smitha5e4d7e2020-05-07 12:07:01 -0400454template<class T>
455shared_ptr(weak_ptr<T>) -> shared_ptr<T>;
456template<class T, class D>
457shared_ptr(unique_ptr<T, D>) -> shared_ptr<T>;
458
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000459// shared_ptr comparisons:
460template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000461 bool operator==(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000462template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000463 bool operator!=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000464template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000465 bool operator<(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000466template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000467 bool operator>(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000468template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000469 bool operator<=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000470template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000471 bool operator>=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
472
473template <class T>
474 bool operator==(const shared_ptr<T>& x, nullptr_t) noexcept;
475template <class T>
476 bool operator==(nullptr_t, const shared_ptr<T>& y) noexcept;
477template <class T>
478 bool operator!=(const shared_ptr<T>& x, nullptr_t) noexcept;
479template <class T>
480 bool operator!=(nullptr_t, const shared_ptr<T>& y) noexcept;
481template <class T>
482 bool operator<(const shared_ptr<T>& x, nullptr_t) noexcept;
483template <class T>
484bool operator<(nullptr_t, const shared_ptr<T>& y) noexcept;
485template <class T>
486 bool operator<=(const shared_ptr<T>& x, nullptr_t) noexcept;
487template <class T>
488 bool operator<=(nullptr_t, const shared_ptr<T>& y) noexcept;
489template <class T>
490 bool operator>(const shared_ptr<T>& x, nullptr_t) noexcept;
491template <class T>
492 bool operator>(nullptr_t, const shared_ptr<T>& y) noexcept;
493template <class T>
494 bool operator>=(const shared_ptr<T>& x, nullptr_t) noexcept;
495template <class T>
496 bool operator>=(nullptr_t, const shared_ptr<T>& y) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000497
498// shared_ptr specialized algorithms:
Howard Hinnant719bda32011-05-28 14:41:13 +0000499template<class T> void swap(shared_ptr<T>& a, shared_ptr<T>& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000500
501// shared_ptr casts:
502template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000503 shared_ptr<T> static_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000504template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000505 shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000506template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000507 shared_ptr<T> const_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000508
509// shared_ptr I/O:
510template<class E, class T, class Y>
511 basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, shared_ptr<Y> const& p);
512
513// shared_ptr get_deleter:
Howard Hinnant719bda32011-05-28 14:41:13 +0000514template<class D, class T> D* get_deleter(shared_ptr<T> const& p) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000515
516template<class T, class... Args>
517 shared_ptr<T> make_shared(Args&&... args);
518template<class T, class A, class... Args>
519 shared_ptr<T> allocate_shared(const A& a, Args&&... args);
520
521template<class T>
522class weak_ptr
523{
524public:
525 typedef T element_type;
526
527 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000528 constexpr weak_ptr() noexcept;
529 template<class Y> weak_ptr(shared_ptr<Y> const& r) noexcept;
530 weak_ptr(weak_ptr const& r) noexcept;
531 template<class Y> weak_ptr(weak_ptr<Y> const& r) noexcept;
Marshall Clowe9b0a5c2014-03-05 03:12:04 +0000532 weak_ptr(weak_ptr&& r) noexcept; // C++14
533 template<class Y> weak_ptr(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000534
535 // destructor
536 ~weak_ptr();
537
538 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000539 weak_ptr& operator=(weak_ptr const& r) noexcept;
540 template<class Y> weak_ptr& operator=(weak_ptr<Y> const& r) noexcept;
541 template<class Y> weak_ptr& operator=(shared_ptr<Y> const& r) noexcept;
Marshall Clowe9b0a5c2014-03-05 03:12:04 +0000542 weak_ptr& operator=(weak_ptr&& r) noexcept; // C++14
543 template<class Y> weak_ptr& operator=(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000544
545 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000546 void swap(weak_ptr& r) noexcept;
547 void reset() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000548
549 // observers
Howard Hinnant719bda32011-05-28 14:41:13 +0000550 long use_count() const noexcept;
551 bool expired() const noexcept;
552 shared_ptr<T> lock() const noexcept;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000553 template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
554 template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000555};
556
Logan Smitha5e4d7e2020-05-07 12:07:01 -0400557template<class T>
558weak_ptr(shared_ptr<T>) -> weak_ptr<T>;
559
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000560// weak_ptr specialized algorithms:
Howard Hinnant719bda32011-05-28 14:41:13 +0000561template<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000562
563// class owner_less:
564template<class T> struct owner_less;
565
566template<class T>
Eric Fiselierea6fdf62019-06-23 20:47:21 +0000567struct owner_less<shared_ptr<T> >
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000568 : binary_function<shared_ptr<T>, shared_ptr<T>, bool>
569{
570 typedef bool result_type;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000571 bool operator()(shared_ptr<T> const&, shared_ptr<T> const&) const noexcept;
572 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
573 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000574};
575
576template<class T>
Eric Fiselierea6fdf62019-06-23 20:47:21 +0000577struct owner_less<weak_ptr<T> >
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000578 : binary_function<weak_ptr<T>, weak_ptr<T>, bool>
579{
580 typedef bool result_type;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000581 bool operator()(weak_ptr<T> const&, weak_ptr<T> const&) const noexcept;
582 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
583 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
584};
585
586template <> // Added in C++14
587struct owner_less<void>
588{
589 template <class _Tp, class _Up>
590 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
591 template <class _Tp, class _Up>
592 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept;
593 template <class _Tp, class _Up>
594 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
595 template <class _Tp, class _Up>
596 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept;
597
598 typedef void is_transparent;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000599};
600
601template<class T>
602class enable_shared_from_this
603{
604protected:
Howard Hinnant719bda32011-05-28 14:41:13 +0000605 constexpr enable_shared_from_this() noexcept;
606 enable_shared_from_this(enable_shared_from_this const&) noexcept;
607 enable_shared_from_this& operator=(enable_shared_from_this const&) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000608 ~enable_shared_from_this();
609public:
610 shared_ptr<T> shared_from_this();
611 shared_ptr<T const> shared_from_this() const;
612};
613
614template<class T>
615 bool atomic_is_lock_free(const shared_ptr<T>* p);
616template<class T>
617 shared_ptr<T> atomic_load(const shared_ptr<T>* p);
618template<class T>
619 shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order mo);
620template<class T>
621 void atomic_store(shared_ptr<T>* p, shared_ptr<T> r);
622template<class T>
623 void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
624template<class T>
625 shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r);
626template<class T>
627 shared_ptr<T>
628 atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
629template<class T>
630 bool
631 atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
632template<class T>
633 bool
634 atomic_compare_exchange_strong( shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
635template<class T>
636 bool
637 atomic_compare_exchange_weak_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
638 shared_ptr<T> w, memory_order success,
639 memory_order failure);
640template<class T>
641 bool
642 atomic_compare_exchange_strong_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
643 shared_ptr<T> w, memory_order success,
644 memory_order failure);
645// Hash support
646template <class T> struct hash;
647template <class T, class D> struct hash<unique_ptr<T, D> >;
648template <class T> struct hash<shared_ptr<T> >;
649
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000650template <class T, class Alloc>
651 inline constexpr bool uses_allocator_v = uses_allocator<T, Alloc>::value;
652
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000653// Pointer safety
654enum class pointer_safety { relaxed, preferred, strict };
655void declare_reachable(void *p);
656template <class T> T *undeclare_reachable(T *p);
657void declare_no_pointers(char *p, size_t n);
658void undeclare_no_pointers(char *p, size_t n);
Howard Hinnant719bda32011-05-28 14:41:13 +0000659pointer_safety get_pointer_safety() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000660
Howard Hinnantc51e1022010-05-11 19:42:16 +0000661void* align(size_t alignment, size_t size, void*& ptr, size_t& space);
662
663} // std
664
665*/
666
667#include <__config>
Louis Dionne73912b22020-11-04 15:01:25 -0500668#include <__availability>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000669#include <type_traits>
670#include <typeinfo>
Arthur O'Dwyer7deec122021-03-24 18:19:12 -0400671#include <compare>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000672#include <cstddef>
673#include <cstdint>
674#include <new>
675#include <utility>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000676#include <iterator>
677#include <__functional_base>
Howard Hinnantdc095972011-07-18 15:51:59 +0000678#include <iosfwd>
Howard Hinnant83b1c052011-12-19 17:58:44 +0000679#include <tuple>
Eric Fiselier2db2bd52016-05-07 03:12:24 +0000680#include <stdexcept>
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000681#include <cstring>
Louis Dionne1e38faa2021-04-09 12:48:34 -0400682#include <__memory/allocator.h>
Louis Dionnefa621d72020-12-10 18:28:13 -0500683#include <__memory/allocator_traits.h>
Louis Dionne26bc98e2021-04-09 12:44:26 -0400684#include <__memory/auto_ptr.h>
Louis Dionnefa621d72020-12-10 18:28:13 -0500685#include <__memory/base.h>
Louis Dionne556fcd02021-04-09 14:43:01 -0400686#include <__memory/compressed_pair.h>
Louis Dionnefa621d72020-12-10 18:28:13 -0500687#include <__memory/pointer_traits.h>
Louis Dionnee0103192021-04-09 14:45:18 -0400688#include <__memory/raw_storage_iterator.h>
Louis Dionne00180872021-04-09 12:58:00 -0400689#include <__memory/temporary_buffer.h>
Louis Dionnec7ef68c2021-04-09 14:47:46 -0400690#include <__memory/uninitialized_algorithms.h>
Louis Dionne7eadb032021-04-09 15:00:57 -0400691#include <__memory/unique_ptr.h>
Louis Dionne74aef9f2020-12-11 12:20:06 -0500692#include <__memory/utilities.h>
Eric Fiselier8020b6c2015-08-19 17:21:46 +0000693#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +0000694# include <atomic>
695#endif
Marshall Clow0a1e7502018-09-12 19:41:40 +0000696#include <version>
Howard Hinnant9fa30202012-07-30 01:40:57 +0000697
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000698#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000699#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000700#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000701
Eric Fiselierf4433a32017-05-31 22:07:49 +0000702_LIBCPP_PUSH_MACROS
703#include <__undef_macros>
704
705
Howard Hinnantc51e1022010-05-11 19:42:16 +0000706_LIBCPP_BEGIN_NAMESPACE_STD
707
Eric Fiselier89659d12015-07-07 00:27:16 +0000708template <class _ValueType>
Louis Dionne16fe2952018-07-11 23:14:33 +0000709inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +0000710_ValueType __libcpp_relaxed_load(_ValueType const* __value) {
711#if !defined(_LIBCPP_HAS_NO_THREADS) && \
712 defined(__ATOMIC_RELAXED) && \
Richard Smithca47d0f2019-04-25 20:02:10 +0000713 (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
Eric Fiselier89659d12015-07-07 00:27:16 +0000714 return __atomic_load_n(__value, __ATOMIC_RELAXED);
715#else
716 return *__value;
717#endif
718}
719
Kuba Breckade9d6792016-09-04 09:55:12 +0000720template <class _ValueType>
Louis Dionne16fe2952018-07-11 23:14:33 +0000721inline _LIBCPP_INLINE_VISIBILITY
Kuba Breckade9d6792016-09-04 09:55:12 +0000722_ValueType __libcpp_acquire_load(_ValueType const* __value) {
723#if !defined(_LIBCPP_HAS_NO_THREADS) && \
724 defined(__ATOMIC_ACQUIRE) && \
Richard Smithca47d0f2019-04-25 20:02:10 +0000725 (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
Kuba Breckade9d6792016-09-04 09:55:12 +0000726 return __atomic_load_n(__value, __ATOMIC_ACQUIRE);
727#else
728 return *__value;
729#endif
730}
731
Louis Dionned6651542020-11-03 12:05:55 -0500732template <class _Alloc, class _Ptr>
733_LIBCPP_INLINE_VISIBILITY
734void __construct_forward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2) {
735 static_assert(__is_cpp17_move_insertable<_Alloc>::value,
Arthur O'Dwyerf2016bb2020-12-12 11:43:15 -0500736 "The specified type does not meet the requirements of Cpp17MoveInsertable");
Louis Dionned6651542020-11-03 12:05:55 -0500737 typedef allocator_traits<_Alloc> _Traits;
738 for (; __begin1 != __end1; ++__begin1, (void)++__begin2) {
739 _Traits::construct(__a, _VSTD::__to_address(__begin2),
740#ifdef _LIBCPP_NO_EXCEPTIONS
741 _VSTD::move(*__begin1)
742#else
743 _VSTD::move_if_noexcept(*__begin1)
744#endif
745 );
746 }
747}
748
749template <class _Alloc, class _Tp, typename enable_if<
750 (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) &&
751 is_trivially_move_constructible<_Tp>::value
752>::type>
753_LIBCPP_INLINE_VISIBILITY
754void __construct_forward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2) {
755 ptrdiff_t _Np = __end1 - __begin1;
756 if (_Np > 0) {
757 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
758 __begin2 += _Np;
759 }
760}
761
762template <class _Alloc, class _Iter, class _Ptr>
763_LIBCPP_INLINE_VISIBILITY
764void __construct_range_forward(_Alloc& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2) {
765 typedef allocator_traits<_Alloc> _Traits;
766 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2) {
767 _Traits::construct(__a, _VSTD::__to_address(__begin2), *__begin1);
768 }
769}
770
771template <class _Alloc, class _Source, class _Dest,
772 class _RawSource = typename remove_const<_Source>::type,
773 class _RawDest = typename remove_const<_Dest>::type,
774 class =
775 typename enable_if<
776 is_trivially_copy_constructible<_Dest>::value &&
777 is_same<_RawSource, _RawDest>::value &&
778 (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Dest*, _Source&>::value)
779 >::type>
780_LIBCPP_INLINE_VISIBILITY
781void __construct_range_forward(_Alloc&, _Source* __begin1, _Source* __end1, _Dest*& __begin2) {
782 ptrdiff_t _Np = __end1 - __begin1;
783 if (_Np > 0) {
784 _VSTD::memcpy(const_cast<_RawDest*>(__begin2), __begin1, _Np * sizeof(_Dest));
785 __begin2 += _Np;
786 }
787}
788
789template <class _Alloc, class _Ptr>
790_LIBCPP_INLINE_VISIBILITY
791void __construct_backward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2) {
792 static_assert(__is_cpp17_move_insertable<_Alloc>::value,
793 "The specified type does not meet the requirements of Cpp17MoveInsertable");
794 typedef allocator_traits<_Alloc> _Traits;
795 while (__end1 != __begin1) {
796 _Traits::construct(__a, _VSTD::__to_address(__end2 - 1),
797#ifdef _LIBCPP_NO_EXCEPTIONS
798 _VSTD::move(*--__end1)
799#else
800 _VSTD::move_if_noexcept(*--__end1)
801#endif
802 );
803 --__end2;
804 }
805}
806
807template <class _Alloc, class _Tp, class = typename enable_if<
808 (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) &&
809 is_trivially_move_constructible<_Tp>::value
810>::type>
811_LIBCPP_INLINE_VISIBILITY
812void __construct_backward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2) {
813 ptrdiff_t _Np = __end1 - __begin1;
814 __end2 -= _Np;
815 if (_Np > 0)
816 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
817}
818
Howard Hinnantc51e1022010-05-11 19:42:16 +0000819struct __destruct_n
820{
821private:
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +0000822 size_t __size_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000823
824 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +0000825 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +0000826 {for (size_t __i = 0; __i < __size_; ++__i, ++__p) __p->~_Tp();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000827
828 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +0000829 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000830 {}
831
Howard Hinnant719bda32011-05-28 14:41:13 +0000832 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +0000833 {++__size_;}
Howard Hinnant719bda32011-05-28 14:41:13 +0000834 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000835 {}
836
Howard Hinnant719bda32011-05-28 14:41:13 +0000837 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +0000838 {__size_ = __s;}
Howard Hinnant719bda32011-05-28 14:41:13 +0000839 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000840 {}
841public:
Howard Hinnant719bda32011-05-28 14:41:13 +0000842 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +0000843 : __size_(__s) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000844
845 template <class _Tp>
Bruce Mitchener170d8972020-11-24 12:53:53 -0500846 _LIBCPP_INLINE_VISIBILITY void __incr() _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +0000847 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000848
849 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +0000850 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +0000851 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000852
853 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +0000854 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +0000855 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000856};
857
858template <class _Alloc>
859class __allocator_destructor
860{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000861 typedef _LIBCPP_NODEBUG_TYPE allocator_traits<_Alloc> __alloc_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000862public:
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000863 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::pointer pointer;
864 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::size_type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000865private:
866 _Alloc& __alloc_;
867 size_type __s_;
868public:
869 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant719bda32011-05-28 14:41:13 +0000870 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000871 : __alloc_(__a), __s_(__s) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +0000872 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +0000873 void operator()(pointer __p) _NOEXCEPT
874 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000875};
876
Kevin Hu4bdc8a02017-01-17 02:46:33 +0000877// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
878// should be sufficient for thread safety.
Louis Dionne38437402021-03-03 13:45:06 -0500879// See https://llvm.org/PR22803
Kevin Hu4bdc8a02017-01-17 02:46:33 +0000880#if defined(__clang__) && __has_builtin(__atomic_add_fetch) \
881 && defined(__ATOMIC_RELAXED) \
882 && defined(__ATOMIC_ACQ_REL)
883# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
Richard Smithca47d0f2019-04-25 20:02:10 +0000884#elif defined(_LIBCPP_COMPILER_GCC)
Kevin Hu4bdc8a02017-01-17 02:46:33 +0000885# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
886#endif
887
888template <class _Tp>
889inline _LIBCPP_INLINE_VISIBILITY _Tp
890__libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT
891{
892#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
893 return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
894#else
895 return __t += 1;
896#endif
897}
898
899template <class _Tp>
900inline _LIBCPP_INLINE_VISIBILITY _Tp
901__libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT
902{
903#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
904 return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
905#else
906 return __t -= 1;
907#endif
908}
909
Howard Hinnant756c69b2010-09-22 16:48:34 +0000910class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +0000911 : public std::exception
912{
913public:
Dimitry Andric47269ce2020-03-13 19:36:26 +0100914 bad_weak_ptr() _NOEXCEPT = default;
915 bad_weak_ptr(const bad_weak_ptr&) _NOEXCEPT = default;
Howard Hinnant719bda32011-05-28 14:41:13 +0000916 virtual ~bad_weak_ptr() _NOEXCEPT;
917 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000918};
919
Louis Dionne16fe2952018-07-11 23:14:33 +0000920_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +0000921void __throw_bad_weak_ptr()
922{
923#ifndef _LIBCPP_NO_EXCEPTIONS
924 throw bad_weak_ptr();
925#else
926 _VSTD::abort();
927#endif
928}
929
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000930template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000931
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000932class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantc51e1022010-05-11 19:42:16 +0000933{
934 __shared_count(const __shared_count&);
935 __shared_count& operator=(const __shared_count&);
936
937protected:
938 long __shared_owners_;
939 virtual ~__shared_count();
940private:
Howard Hinnant719bda32011-05-28 14:41:13 +0000941 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000942
943public:
Howard Hinnant756c69b2010-09-22 16:48:34 +0000944 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +0000945 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000946 : __shared_owners_(__refs) {}
947
Louis Dionne5e0eadd2018-08-01 02:08:59 +0000948#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +0000949 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +0000950 void __add_shared() _NOEXCEPT;
951 bool __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +0000952#else
953 _LIBCPP_INLINE_VISIBILITY
954 void __add_shared() _NOEXCEPT {
955 __libcpp_atomic_refcount_increment(__shared_owners_);
956 }
957 _LIBCPP_INLINE_VISIBILITY
958 bool __release_shared() _NOEXCEPT {
959 if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
960 __on_zero_shared();
961 return true;
962 }
963 return false;
964 }
965#endif
Howard Hinnant756c69b2010-09-22 16:48:34 +0000966 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +0000967 long use_count() const _NOEXCEPT {
968 return __libcpp_relaxed_load(&__shared_owners_) + 1;
969 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000970};
971
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000972class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +0000973 : private __shared_count
974{
975 long __shared_weak_owners_;
976
977public:
Howard Hinnant756c69b2010-09-22 16:48:34 +0000978 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +0000979 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000980 : __shared_count(__refs),
981 __shared_weak_owners_(__refs) {}
982protected:
983 virtual ~__shared_weak_count();
984
985public:
Louis Dionne5e0eadd2018-08-01 02:08:59 +0000986#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +0000987 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +0000988 void __add_shared() _NOEXCEPT;
989 void __add_weak() _NOEXCEPT;
990 void __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +0000991#else
992 _LIBCPP_INLINE_VISIBILITY
993 void __add_shared() _NOEXCEPT {
994 __shared_count::__add_shared();
995 }
996 _LIBCPP_INLINE_VISIBILITY
997 void __add_weak() _NOEXCEPT {
998 __libcpp_atomic_refcount_increment(__shared_weak_owners_);
999 }
1000 _LIBCPP_INLINE_VISIBILITY
1001 void __release_shared() _NOEXCEPT {
1002 if (__shared_count::__release_shared())
1003 __release_weak();
1004 }
1005#endif
Howard Hinnant719bda32011-05-28 14:41:13 +00001006 void __release_weak() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00001007 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001008 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
1009 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001010
Howard Hinnant719bda32011-05-28 14:41:13 +00001011 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001012private:
Howard Hinnant719bda32011-05-28 14:41:13 +00001013 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001014};
1015
1016template <class _Tp, class _Dp, class _Alloc>
1017class __shared_ptr_pointer
1018 : public __shared_weak_count
1019{
1020 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
1021public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00001022 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001023 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001024 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001025
Howard Hinnant72f73582010-08-11 17:04:31 +00001026#ifndef _LIBCPP_NO_RTTI
Howard Hinnant719bda32011-05-28 14:41:13 +00001027 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant72f73582010-08-11 17:04:31 +00001028#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001029
1030private:
Howard Hinnant719bda32011-05-28 14:41:13 +00001031 virtual void __on_zero_shared() _NOEXCEPT;
1032 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001033};
1034
Howard Hinnant72f73582010-08-11 17:04:31 +00001035#ifndef _LIBCPP_NO_RTTI
1036
Howard Hinnantc51e1022010-05-11 19:42:16 +00001037template <class _Tp, class _Dp, class _Alloc>
1038const void*
Howard Hinnant719bda32011-05-28 14:41:13 +00001039__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001040{
Eric Fiselierc7490d02017-05-04 01:06:56 +00001041 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001042}
1043
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001044#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00001045
Howard Hinnantc51e1022010-05-11 19:42:16 +00001046template <class _Tp, class _Dp, class _Alloc>
1047void
Howard Hinnant719bda32011-05-28 14:41:13 +00001048__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001049{
1050 __data_.first().second()(__data_.first().first());
1051 __data_.first().second().~_Dp();
1052}
1053
1054template <class _Tp, class _Dp, class _Alloc>
1055void
Howard Hinnant719bda32011-05-28 14:41:13 +00001056__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001057{
Eric Fiselierf8898c82015-02-05 23:01:40 +00001058 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
1059 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00001060 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
1061
Eric Fiselierf8898c82015-02-05 23:01:40 +00001062 _Al __a(__data_.second());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001063 __data_.second().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00001064 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001065}
1066
1067template <class _Tp, class _Alloc>
Louis Dionne86549d72020-12-09 16:22:17 -05001068struct __shared_ptr_emplace
1069 : __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00001070{
Louis Dionneda463cb2020-12-11 12:22:16 -05001071 template<class ..._Args>
Louis Dionne86549d72020-12-09 16:22:17 -05001072 _LIBCPP_HIDE_FROM_ABI
1073 explicit __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Louis Dionneda463cb2020-12-11 12:22:16 -05001074 : __storage_(_VSTD::move(__a))
1075 {
1076#if _LIBCPP_STD_VER > 17
1077 using _TpAlloc = typename __allocator_traits_rebind<_Alloc, _Tp>::type;
1078 _TpAlloc __tmp(*__get_alloc());
1079 allocator_traits<_TpAlloc>::construct(__tmp, __get_elem(), _VSTD::forward<_Args>(__args)...);
Louis Dionne1a1fc9d2020-07-30 10:00:53 -04001080#else
Louis Dionneda463cb2020-12-11 12:22:16 -05001081 ::new ((void*)__get_elem()) _Tp(_VSTD::forward<_Args>(__args)...);
Louis Dionne1a1fc9d2020-07-30 10:00:53 -04001082#endif
Louis Dionneda463cb2020-12-11 12:22:16 -05001083 }
Louis Dionne86549d72020-12-09 16:22:17 -05001084
1085 _LIBCPP_HIDE_FROM_ABI
Louis Dionneda463cb2020-12-11 12:22:16 -05001086 _Alloc* __get_alloc() _NOEXCEPT { return __storage_.__get_alloc(); }
Louis Dionne86549d72020-12-09 16:22:17 -05001087
1088 _LIBCPP_HIDE_FROM_ABI
Louis Dionneda463cb2020-12-11 12:22:16 -05001089 _Tp* __get_elem() _NOEXCEPT { return __storage_.__get_elem(); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001090
1091private:
Louis Dionne86549d72020-12-09 16:22:17 -05001092 virtual void __on_zero_shared() _NOEXCEPT {
Louis Dionneda463cb2020-12-11 12:22:16 -05001093#if _LIBCPP_STD_VER > 17
1094 using _TpAlloc = typename __allocator_traits_rebind<_Alloc, _Tp>::type;
1095 _TpAlloc __tmp(*__get_alloc());
1096 allocator_traits<_TpAlloc>::destroy(__tmp, __get_elem());
1097#else
Louis Dionne86549d72020-12-09 16:22:17 -05001098 __get_elem()->~_Tp();
Louis Dionneda463cb2020-12-11 12:22:16 -05001099#endif
Louis Dionne86549d72020-12-09 16:22:17 -05001100 }
1101
1102 virtual void __on_zero_shared_weak() _NOEXCEPT {
1103 using _ControlBlockAlloc = typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type;
1104 using _ControlBlockPointer = typename allocator_traits<_ControlBlockAlloc>::pointer;
Louis Dionneca117a22020-12-14 17:40:56 -05001105 _ControlBlockAlloc __tmp(*__get_alloc());
Louis Dionneda463cb2020-12-11 12:22:16 -05001106 __storage_.~_Storage();
Louis Dionne86549d72020-12-09 16:22:17 -05001107 allocator_traits<_ControlBlockAlloc>::deallocate(__tmp,
1108 pointer_traits<_ControlBlockPointer>::pointer_to(*this), 1);
1109 }
1110
Louis Dionneda463cb2020-12-11 12:22:16 -05001111 // This class implements the control block for non-array shared pointers created
1112 // through `std::allocate_shared` and `std::make_shared`.
1113 //
1114 // In previous versions of the library, we used a compressed pair to store
1115 // both the _Alloc and the _Tp. This implies using EBO, which is incompatible
1116 // with Allocator construction for _Tp. To allow implementing P0674 in C++20,
1117 // we now use a properly aligned char buffer while making sure that we maintain
1118 // the same layout that we had when we used a compressed pair.
1119 using _CompressedPair = __compressed_pair<_Alloc, _Tp>;
1120 struct _ALIGNAS_TYPE(_CompressedPair) _Storage {
1121 char __blob_[sizeof(_CompressedPair)];
1122
1123 _LIBCPP_HIDE_FROM_ABI explicit _Storage(_Alloc&& __a) {
1124 ::new ((void*)__get_alloc()) _Alloc(_VSTD::move(__a));
1125 }
1126 _LIBCPP_HIDE_FROM_ABI ~_Storage() {
1127 __get_alloc()->~_Alloc();
1128 }
1129 _Alloc* __get_alloc() _NOEXCEPT {
1130 _CompressedPair *__as_pair = reinterpret_cast<_CompressedPair*>(__blob_);
1131 typename _CompressedPair::_Base1* __first = _CompressedPair::__get_first_base(__as_pair);
1132 _Alloc *__alloc = reinterpret_cast<_Alloc*>(__first);
1133 return __alloc;
1134 }
Reid Klecknera43e87e2021-02-01 15:18:42 -08001135 _LIBCPP_NO_CFI _Tp* __get_elem() _NOEXCEPT {
Louis Dionneda463cb2020-12-11 12:22:16 -05001136 _CompressedPair *__as_pair = reinterpret_cast<_CompressedPair*>(__blob_);
1137 typename _CompressedPair::_Base2* __second = _CompressedPair::__get_second_base(__as_pair);
1138 _Tp *__elem = reinterpret_cast<_Tp*>(__second);
1139 return __elem;
1140 }
1141 };
1142
1143 static_assert(_LIBCPP_ALIGNOF(_Storage) == _LIBCPP_ALIGNOF(_CompressedPair), "");
1144 static_assert(sizeof(_Storage) == sizeof(_CompressedPair), "");
1145 _Storage __storage_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001146};
1147
Erik Pilkington2a398762017-05-25 15:43:31 +00001148struct __shared_ptr_dummy_rebind_allocator_type;
1149template <>
1150class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type>
1151{
1152public:
1153 template <class _Other>
1154 struct rebind
1155 {
1156 typedef allocator<_Other> other;
1157 };
1158};
1159
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001160template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001161
zoecarverd73f42a2020-05-11 18:42:50 -07001162template<class _Tp, class _Up>
1163struct __compatible_with
1164#if _LIBCPP_STD_VER > 14
1165 : is_convertible<remove_extent_t<_Tp>*, remove_extent_t<_Up>*> {};
1166#else
1167 : is_convertible<_Tp*, _Up*> {};
1168#endif // _LIBCPP_STD_VER > 14
1169
zoecarver9bc39102021-02-19 11:10:36 -08001170template <class _Dp, class _Pt,
1171 class = decltype(_VSTD::declval<_Dp>()(_VSTD::declval<_Pt>()))>
1172static true_type __well_formed_deleter_test(int);
1173
1174template <class, class>
1175static false_type __well_formed_deleter_test(...);
1176
1177template <class _Dp, class _Pt>
1178struct __well_formed_deleter : decltype(__well_formed_deleter_test<_Dp, _Pt>(0)) {};
1179
1180template<class _Dp, class _Tp, class _Yp>
1181struct __shared_ptr_deleter_ctor_reqs
1182{
1183 static const bool value = __compatible_with<_Tp, _Yp>::value &&
1184 is_move_constructible<_Dp>::value &&
1185 __well_formed_deleter<_Dp, _Tp*>::value;
1186};
1187
Vy Nguyene369bd92020-07-13 12:34:37 -04001188#if defined(_LIBCPP_ABI_ENABLE_SHARED_PTR_TRIVIAL_ABI)
1189# define _LIBCPP_SHARED_PTR_TRIVIAL_ABI __attribute__((trivial_abi))
1190#else
1191# define _LIBCPP_SHARED_PTR_TRIVIAL_ABI
1192#endif
1193
Howard Hinnantc51e1022010-05-11 19:42:16 +00001194template<class _Tp>
Vy Nguyene369bd92020-07-13 12:34:37 -04001195class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS shared_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00001196{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001197public:
Eric Fiselierae5b6672016-06-27 01:02:43 +00001198#if _LIBCPP_STD_VER > 14
1199 typedef weak_ptr<_Tp> weak_type;
zoecarverd73f42a2020-05-11 18:42:50 -07001200 typedef remove_extent_t<_Tp> element_type;
1201#else
1202 typedef _Tp element_type;
Eric Fiselierae5b6672016-06-27 01:02:43 +00001203#endif
zoecarverd73f42a2020-05-11 18:42:50 -07001204
Howard Hinnantc51e1022010-05-11 19:42:16 +00001205private:
1206 element_type* __ptr_;
1207 __shared_weak_count* __cntrl_;
1208
1209 struct __nat {int __for_bool_;};
1210public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001211 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001212 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001213 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001214 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiend435f8b2014-01-31 09:30:46 +00001215 template<class _Yp>
1216 explicit shared_ptr(_Yp* __p,
zoecarverd73f42a2020-05-11 18:42:50 -07001217 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
Logan Chiend435f8b2014-01-31 09:30:46 +00001218 template<class _Yp, class _Dp>
1219 shared_ptr(_Yp* __p, _Dp __d,
zoecarver9bc39102021-02-19 11:10:36 -08001220 typename enable_if<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, element_type>::value, __nat>::type = __nat());
Logan Chiend435f8b2014-01-31 09:30:46 +00001221 template<class _Yp, class _Dp, class _Alloc>
1222 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
zoecarver9bc39102021-02-19 11:10:36 -08001223 typename enable_if<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, element_type>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001224 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
1225 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001226 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
1227 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001228 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001229 template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001230 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001231 shared_ptr(const shared_ptr<_Yp>& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07001232 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00001233 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001234 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001235 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001236 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07001237 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00001238 _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001239 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00001240 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type= __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00001241#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Logan Chiend435f8b2014-01-31 09:30:46 +00001242 template<class _Yp>
1243 shared_ptr(auto_ptr<_Yp>&& __r,
1244 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00001245#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00001246 template <class _Yp, class _Dp>
1247 shared_ptr(unique_ptr<_Yp, _Dp>&&,
1248 typename enable_if
1249 <
1250 !is_lvalue_reference<_Dp>::value &&
Logan Chiend435f8b2014-01-31 09:30:46 +00001251 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
1252 __nat
1253 >::type = __nat());
1254 template <class _Yp, class _Dp>
1255 shared_ptr(unique_ptr<_Yp, _Dp>&&,
1256 typename enable_if
1257 <
1258 is_lvalue_reference<_Dp>::value &&
Logan Chiend435f8b2014-01-31 09:30:46 +00001259 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
1260 __nat
1261 >::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001262
1263 ~shared_ptr();
1264
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001265 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001266 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001267 template<class _Yp>
1268 typename enable_if
1269 <
zoecarverd73f42a2020-05-11 18:42:50 -07001270 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001271 shared_ptr&
1272 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001273 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001274 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001275 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001276 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001277 template<class _Yp>
1278 typename enable_if
1279 <
zoecarverd73f42a2020-05-11 18:42:50 -07001280 __compatible_with<_Yp, element_type>::value,
1281 shared_ptr&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001282 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001283 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001284 operator=(shared_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00001285#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001286 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00001287 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001288 typename enable_if
1289 <
1290 !is_array<_Yp>::value &&
1291 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00001292 shared_ptr
1293 >::type&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001294 operator=(auto_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00001295#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001296 template <class _Yp, class _Dp>
1297 typename enable_if
1298 <
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001299 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
1300 shared_ptr&
1301 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001302 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001303 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001304
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001305 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001306 void swap(shared_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001307 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001308 void reset() _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001309 template<class _Yp>
1310 typename enable_if
1311 <
zoecarverd73f42a2020-05-11 18:42:50 -07001312 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001313 void
1314 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001315 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001316 reset(_Yp* __p);
1317 template<class _Yp, class _Dp>
1318 typename enable_if
1319 <
zoecarverd73f42a2020-05-11 18:42:50 -07001320 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001321 void
1322 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001323 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001324 reset(_Yp* __p, _Dp __d);
1325 template<class _Yp, class _Dp, class _Alloc>
1326 typename enable_if
1327 <
zoecarverd73f42a2020-05-11 18:42:50 -07001328 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001329 void
1330 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001331 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001332 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001333
Howard Hinnant756c69b2010-09-22 16:48:34 +00001334 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001335 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001336 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001337 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
1338 {return *__ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001339 _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07001340 element_type* operator->() const _NOEXCEPT
1341 {
1342 static_assert(!_VSTD::is_array<_Tp>::value,
1343 "std::shared_ptr<T>::operator-> is only valid when T is not an array type.");
1344 return __ptr_;
1345 }
Howard Hinnant756c69b2010-09-22 16:48:34 +00001346 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001347 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001348 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001349 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001350 _LIBCPP_INLINE_VISIBILITY
Bruce Mitchener170d8972020-11-24 12:53:53 -05001351 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != nullptr;}
Howard Hinnantc834c512011-11-29 18:15:50 +00001352 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001353 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00001354 bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001355 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnantc834c512011-11-29 18:15:50 +00001356 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001357 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00001358 bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001359 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant9fa30202012-07-30 01:40:57 +00001360 _LIBCPP_INLINE_VISIBILITY
1361 bool
1362 __owner_equivalent(const shared_ptr& __p) const
1363 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001364
zoecarverd73f42a2020-05-11 18:42:50 -07001365#if _LIBCPP_STD_VER > 14
1366 typename add_lvalue_reference<element_type>::type
1367 _LIBCPP_INLINE_VISIBILITY
1368 operator[](ptrdiff_t __i) const
1369 {
1370 static_assert(_VSTD::is_array<_Tp>::value,
1371 "std::shared_ptr<T>::operator[] is only valid when T is an array type.");
1372 return __ptr_[__i];
1373 }
1374#endif
1375
Howard Hinnant72f73582010-08-11 17:04:31 +00001376#ifndef _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00001377 template <class _Dp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001378 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001379 _Dp* __get_deleter() const _NOEXCEPT
Marshall Clow31350ab2017-06-14 16:54:43 +00001380 {return static_cast<_Dp*>(__cntrl_
Aditya Kumar7c5db692017-08-20 10:38:55 +00001381 ? const_cast<void *>(__cntrl_->__get_deleter(typeid(_Dp)))
Marshall Clow31350ab2017-06-14 16:54:43 +00001382 : nullptr);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001383#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00001384
Zoe Carverd9040c72019-10-22 15:16:49 +00001385 template<class _Yp, class _CntrlBlk>
1386 static shared_ptr<_Tp>
zoecarver867d3112020-05-19 17:17:16 -07001387 __create_with_control_block(_Yp* __p, _CntrlBlk* __cntrl) _NOEXCEPT
Zoe Carverd9040c72019-10-22 15:16:49 +00001388 {
1389 shared_ptr<_Tp> __r;
1390 __r.__ptr_ = __p;
1391 __r.__cntrl_ = __cntrl;
1392 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
1393 return __r;
1394 }
Zoe Carver6cd05c32019-08-19 15:47:16 +00001395
Howard Hinnantc51e1022010-05-11 19:42:16 +00001396private:
Erik Pilkington2a398762017-05-25 15:43:31 +00001397 template <class _Yp, bool = is_function<_Yp>::value>
1398 struct __shared_ptr_default_allocator
1399 {
1400 typedef allocator<_Yp> type;
1401 };
1402
1403 template <class _Yp>
1404 struct __shared_ptr_default_allocator<_Yp, true>
1405 {
1406 typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type;
1407 };
Howard Hinnantc51e1022010-05-11 19:42:16 +00001408
Eric Fiselierf16c93f2016-06-26 23:56:32 +00001409 template <class _Yp, class _OrigPtr>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001410 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier30d5ac62017-05-10 19:35:49 +00001411 typename enable_if<is_convertible<_OrigPtr*,
1412 const enable_shared_from_this<_Yp>*
1413 >::value,
1414 void>::type
Eric Fiselierf16c93f2016-06-26 23:56:32 +00001415 __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
1416 _OrigPtr* __ptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001417 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00001418 typedef typename remove_cv<_Yp>::type _RawYp;
Eric Fiselier84006862016-06-02 00:15:35 +00001419 if (__e && __e->__weak_this_.expired())
Marshall Clow99442fc2015-06-19 15:54:13 +00001420 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00001421 __e->__weak_this_ = shared_ptr<_RawYp>(*this,
1422 const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
Marshall Clow99442fc2015-06-19 15:54:13 +00001423 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001424 }
1425
Erik Pilkington2a398762017-05-25 15:43:31 +00001426 _LIBCPP_INLINE_VISIBILITY void __enable_weak_this(...) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001427
zoecarverd73f42a2020-05-11 18:42:50 -07001428 template <class, class _Yp>
1429 struct __shared_ptr_default_delete
1430 : default_delete<_Yp> {};
1431
1432 template <class _Yp, class _Un, size_t _Sz>
1433 struct __shared_ptr_default_delete<_Yp[_Sz], _Un>
1434 : default_delete<_Yp[]> {};
1435
1436 template <class _Yp, class _Un>
1437 struct __shared_ptr_default_delete<_Yp[], _Un>
1438 : default_delete<_Yp[]> {};
1439
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001440 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
1441 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001442};
1443
Logan Smitha5e4d7e2020-05-07 12:07:01 -04001444#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
1445template<class _Tp>
1446shared_ptr(weak_ptr<_Tp>) -> shared_ptr<_Tp>;
1447template<class _Tp, class _Dp>
1448shared_ptr(unique_ptr<_Tp, _Dp>) -> shared_ptr<_Tp>;
1449#endif
Eric Fiselier30d5ac62017-05-10 19:35:49 +00001450
Howard Hinnantc51e1022010-05-11 19:42:16 +00001451template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001452inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001453_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00001454shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05001455 : __ptr_(nullptr),
1456 __cntrl_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001457{
1458}
1459
1460template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001461inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001462_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00001463shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05001464 : __ptr_(nullptr),
1465 __cntrl_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001466{
1467}
1468
1469template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00001470template<class _Yp>
1471shared_ptr<_Tp>::shared_ptr(_Yp* __p,
zoecarverd73f42a2020-05-11 18:42:50 -07001472 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001473 : __ptr_(__p)
1474{
1475 unique_ptr<_Yp> __hold(__p);
Erik Pilkington2a398762017-05-25 15:43:31 +00001476 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
zoecarverd73f42a2020-05-11 18:42:50 -07001477 typedef __shared_ptr_pointer<_Yp*, __shared_ptr_default_delete<_Tp, _Yp>, _AllocT > _CntrlBlk;
1478 __cntrl_ = new _CntrlBlk(__p, __shared_ptr_default_delete<_Tp, _Yp>(), _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001479 __hold.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00001480 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001481}
1482
1483template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00001484template<class _Yp, class _Dp>
1485shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
zoecarver9bc39102021-02-19 11:10:36 -08001486 typename enable_if<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001487 : __ptr_(__p)
1488{
1489#ifndef _LIBCPP_NO_EXCEPTIONS
1490 try
1491 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001492#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00001493 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
1494 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
zoecarverbec93cf2021-02-18 21:31:07 -08001495#ifndef _LIBCPP_CXX03_LANG
1496 __cntrl_ = new _CntrlBlk(__p, _VSTD::move(__d), _AllocT());
1497#else
Erik Pilkington2a398762017-05-25 15:43:31 +00001498 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
zoecarverbec93cf2021-02-18 21:31:07 -08001499#endif // not _LIBCPP_CXX03_LANG
Eric Fiselierf16c93f2016-06-26 23:56:32 +00001500 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001501#ifndef _LIBCPP_NO_EXCEPTIONS
1502 }
1503 catch (...)
1504 {
1505 __d(__p);
1506 throw;
1507 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001508#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001509}
1510
1511template<class _Tp>
1512template<class _Dp>
1513shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
Bruce Mitchener170d8972020-11-24 12:53:53 -05001514 : __ptr_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001515{
1516#ifndef _LIBCPP_NO_EXCEPTIONS
1517 try
1518 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001519#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00001520 typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
1521 typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT > _CntrlBlk;
zoecarverbec93cf2021-02-18 21:31:07 -08001522#ifndef _LIBCPP_CXX03_LANG
1523 __cntrl_ = new _CntrlBlk(__p, _VSTD::move(__d), _AllocT());
1524#else
Erik Pilkington2a398762017-05-25 15:43:31 +00001525 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
zoecarverbec93cf2021-02-18 21:31:07 -08001526#endif // not _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001527#ifndef _LIBCPP_NO_EXCEPTIONS
1528 }
1529 catch (...)
1530 {
1531 __d(__p);
1532 throw;
1533 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001534#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001535}
1536
1537template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00001538template<class _Yp, class _Dp, class _Alloc>
1539shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
zoecarver9bc39102021-02-19 11:10:36 -08001540 typename enable_if<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001541 : __ptr_(__p)
1542{
1543#ifndef _LIBCPP_NO_EXCEPTIONS
1544 try
1545 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001546#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001547 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00001548 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001549 typedef __allocator_destructor<_A2> _D2;
1550 _A2 __a2(__a);
1551 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
zoecarverbec93cf2021-02-18 21:31:07 -08001552 ::new ((void*)_VSTD::addressof(*__hold2.get()))
1553#ifndef _LIBCPP_CXX03_LANG
1554 _CntrlBlk(__p, _VSTD::move(__d), __a);
1555#else
1556 _CntrlBlk(__p, __d, __a);
1557#endif // not _LIBCPP_CXX03_LANG
Eric Fiselier6bd814f2014-10-23 04:12:28 +00001558 __cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00001559 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001560#ifndef _LIBCPP_NO_EXCEPTIONS
1561 }
1562 catch (...)
1563 {
1564 __d(__p);
1565 throw;
1566 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001567#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001568}
1569
1570template<class _Tp>
1571template<class _Dp, class _Alloc>
1572shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
Bruce Mitchener170d8972020-11-24 12:53:53 -05001573 : __ptr_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001574{
1575#ifndef _LIBCPP_NO_EXCEPTIONS
1576 try
1577 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001578#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001579 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00001580 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001581 typedef __allocator_destructor<_A2> _D2;
1582 _A2 __a2(__a);
1583 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
zoecarverbec93cf2021-02-18 21:31:07 -08001584 ::new ((void*)_VSTD::addressof(*__hold2.get()))
1585#ifndef _LIBCPP_CXX03_LANG
1586 _CntrlBlk(__p, _VSTD::move(__d), __a);
1587#else
1588 _CntrlBlk(__p, __d, __a);
1589#endif // not _LIBCPP_CXX03_LANG
Eric Fiselier6bd814f2014-10-23 04:12:28 +00001590 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001591#ifndef _LIBCPP_NO_EXCEPTIONS
1592 }
1593 catch (...)
1594 {
1595 __d(__p);
1596 throw;
1597 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001598#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001599}
1600
1601template<class _Tp>
1602template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001603inline
Howard Hinnant719bda32011-05-28 14:41:13 +00001604shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001605 : __ptr_(__p),
1606 __cntrl_(__r.__cntrl_)
1607{
1608 if (__cntrl_)
1609 __cntrl_->__add_shared();
1610}
1611
1612template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001613inline
Howard Hinnant719bda32011-05-28 14:41:13 +00001614shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001615 : __ptr_(__r.__ptr_),
1616 __cntrl_(__r.__cntrl_)
1617{
1618 if (__cntrl_)
1619 __cntrl_->__add_shared();
1620}
1621
1622template<class _Tp>
1623template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001624inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001625shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07001626 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00001627 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001628 : __ptr_(__r.__ptr_),
1629 __cntrl_(__r.__cntrl_)
1630{
1631 if (__cntrl_)
1632 __cntrl_->__add_shared();
1633}
1634
Howard Hinnantc51e1022010-05-11 19:42:16 +00001635template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001636inline
Howard Hinnant719bda32011-05-28 14:41:13 +00001637shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001638 : __ptr_(__r.__ptr_),
1639 __cntrl_(__r.__cntrl_)
1640{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001641 __r.__ptr_ = nullptr;
1642 __r.__cntrl_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001643}
1644
1645template<class _Tp>
1646template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001647inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001648shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07001649 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00001650 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001651 : __ptr_(__r.__ptr_),
1652 __cntrl_(__r.__cntrl_)
1653{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001654 __r.__ptr_ = nullptr;
1655 __r.__cntrl_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001656}
1657
Marshall Clowb22274f2017-01-24 22:22:33 +00001658#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001659template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00001660template<class _Yp>
Logan Chiend435f8b2014-01-31 09:30:46 +00001661shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Logan Chiend435f8b2014-01-31 09:30:46 +00001662 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001663 : __ptr_(__r.get())
1664{
1665 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
1666 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00001667 __enable_weak_this(__r.get(), __r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001668 __r.release();
1669}
Marshall Clowb22274f2017-01-24 22:22:33 +00001670#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001671
1672template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00001673template <class _Yp, class _Dp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001674shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
Logan Chiend435f8b2014-01-31 09:30:46 +00001675 typename enable_if
1676 <
1677 !is_lvalue_reference<_Dp>::value &&
Logan Chiend435f8b2014-01-31 09:30:46 +00001678 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
1679 __nat
1680 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001681 : __ptr_(__r.get())
1682{
Marshall Clow35cde742015-05-10 13:59:45 +00001683#if _LIBCPP_STD_VER > 11
1684 if (__ptr_ == nullptr)
1685 __cntrl_ = nullptr;
1686 else
1687#endif
1688 {
Erik Pilkington2a398762017-05-25 15:43:31 +00001689 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
zoecarvercaf89ec2021-04-08 22:01:35 -07001690 typedef __shared_ptr_pointer<typename unique_ptr<_Yp, _Dp>::pointer, _Dp, _AllocT > _CntrlBlk;
Erik Pilkington2a398762017-05-25 15:43:31 +00001691 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00001692 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00001693 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001694 __r.release();
1695}
1696
1697template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00001698template <class _Yp, class _Dp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001699shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
Logan Chiend435f8b2014-01-31 09:30:46 +00001700 typename enable_if
1701 <
1702 is_lvalue_reference<_Dp>::value &&
Logan Chiend435f8b2014-01-31 09:30:46 +00001703 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
1704 __nat
1705 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001706 : __ptr_(__r.get())
1707{
Marshall Clow35cde742015-05-10 13:59:45 +00001708#if _LIBCPP_STD_VER > 11
1709 if (__ptr_ == nullptr)
1710 __cntrl_ = nullptr;
1711 else
1712#endif
1713 {
Erik Pilkington2a398762017-05-25 15:43:31 +00001714 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
zoecarvercaf89ec2021-04-08 22:01:35 -07001715 typedef __shared_ptr_pointer<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clow35cde742015-05-10 13:59:45 +00001716 reference_wrapper<typename remove_reference<_Dp>::type>,
Erik Pilkington2a398762017-05-25 15:43:31 +00001717 _AllocT > _CntrlBlk;
Logan Smith85cb0812020-02-20 12:23:36 -05001718 __cntrl_ = new _CntrlBlk(__r.get(), _VSTD::ref(__r.get_deleter()), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00001719 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00001720 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001721 __r.release();
1722}
1723
Zoe Carver6cd05c32019-08-19 15:47:16 +00001724template<class _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001725shared_ptr<_Tp>::~shared_ptr()
1726{
1727 if (__cntrl_)
1728 __cntrl_->__release_shared();
1729}
1730
1731template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001732inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001733shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00001734shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001735{
1736 shared_ptr(__r).swap(*this);
1737 return *this;
1738}
1739
1740template<class _Tp>
1741template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001742inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001743typename enable_if
1744<
zoecarverd73f42a2020-05-11 18:42:50 -07001745 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001746 shared_ptr<_Tp>&
1747>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00001748shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001749{
1750 shared_ptr(__r).swap(*this);
1751 return *this;
1752}
1753
Howard Hinnantc51e1022010-05-11 19:42:16 +00001754template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001755inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001756shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00001757shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001758{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001759 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001760 return *this;
1761}
1762
1763template<class _Tp>
1764template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001765inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001766typename enable_if
1767<
zoecarverd73f42a2020-05-11 18:42:50 -07001768 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001769 shared_ptr<_Tp>&
1770>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00001771shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
1772{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001773 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001774 return *this;
1775}
1776
Marshall Clowb22274f2017-01-24 22:22:33 +00001777#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001778template<class _Tp>
1779template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001780inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001781typename enable_if
1782<
1783 !is_array<_Yp>::value &&
Marshall Clow7e384b72017-01-10 16:59:33 +00001784 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00001785 shared_ptr<_Tp>
1786>::type&
Howard Hinnantc51e1022010-05-11 19:42:16 +00001787shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
1788{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001789 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001790 return *this;
1791}
Marshall Clowb22274f2017-01-24 22:22:33 +00001792#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001793
1794template<class _Tp>
1795template <class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001796inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001797typename enable_if
1798<
Aditya Kumar7c5db692017-08-20 10:38:55 +00001799 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clow7e384b72017-01-10 16:59:33 +00001800 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001801 shared_ptr<_Tp>&
1802>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00001803shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
1804{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001805 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001806 return *this;
1807}
1808
Howard Hinnantc51e1022010-05-11 19:42:16 +00001809template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001810inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001811void
Howard Hinnant719bda32011-05-28 14:41:13 +00001812shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001813{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001814 _VSTD::swap(__ptr_, __r.__ptr_);
1815 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001816}
1817
1818template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001819inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001820void
Howard Hinnant719bda32011-05-28 14:41:13 +00001821shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001822{
1823 shared_ptr().swap(*this);
1824}
1825
1826template<class _Tp>
1827template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001828inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001829typename enable_if
1830<
zoecarverd73f42a2020-05-11 18:42:50 -07001831 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001832 void
1833>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00001834shared_ptr<_Tp>::reset(_Yp* __p)
1835{
1836 shared_ptr(__p).swap(*this);
1837}
1838
1839template<class _Tp>
1840template<class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001841inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001842typename enable_if
1843<
zoecarverd73f42a2020-05-11 18:42:50 -07001844 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001845 void
1846>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00001847shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
1848{
1849 shared_ptr(__p, __d).swap(*this);
1850}
1851
1852template<class _Tp>
1853template<class _Yp, class _Dp, class _Alloc>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001854inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001855typename enable_if
1856<
zoecarverd73f42a2020-05-11 18:42:50 -07001857 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001858 void
1859>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00001860shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
1861{
1862 shared_ptr(__p, __d, __a).swap(*this);
1863}
1864
Louis Dionne74aef9f2020-12-11 12:20:06 -05001865//
1866// std::allocate_shared and std::make_shared
1867//
1868template<class _Tp, class _Alloc, class ..._Args, class = _EnableIf<!is_array<_Tp>::value> >
1869_LIBCPP_HIDE_FROM_ABI
1870shared_ptr<_Tp> allocate_shared(const _Alloc& __a, _Args&& ...__args)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001871{
Louis Dionne74aef9f2020-12-11 12:20:06 -05001872 using _ControlBlock = __shared_ptr_emplace<_Tp, _Alloc>;
1873 using _ControlBlockAllocator = typename __allocator_traits_rebind<_Alloc, _ControlBlock>::type;
1874 __allocation_guard<_ControlBlockAllocator> __guard(__a, 1);
1875 ::new ((void*)_VSTD::addressof(*__guard.__get())) _ControlBlock(__a, _VSTD::forward<_Args>(__args)...);
1876 auto __control_block = __guard.__release_ptr();
1877 return shared_ptr<_Tp>::__create_with_control_block((*__control_block).__get_elem(), _VSTD::addressof(*__control_block));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001878}
1879
Louis Dionne43c9f8f2020-12-09 16:57:28 -05001880template<class _Tp, class ..._Args, class = _EnableIf<!is_array<_Tp>::value> >
1881_LIBCPP_HIDE_FROM_ABI
1882shared_ptr<_Tp> make_shared(_Args&& ...__args)
1883{
1884 return _VSTD::allocate_shared<_Tp>(allocator<_Tp>(), _VSTD::forward<_Args>(__args)...);
1885}
1886
Howard Hinnantc51e1022010-05-11 19:42:16 +00001887template<class _Tp, class _Up>
1888inline _LIBCPP_INLINE_VISIBILITY
1889bool
Howard Hinnant719bda32011-05-28 14:41:13 +00001890operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001891{
1892 return __x.get() == __y.get();
1893}
1894
1895template<class _Tp, class _Up>
1896inline _LIBCPP_INLINE_VISIBILITY
1897bool
Howard Hinnant719bda32011-05-28 14:41:13 +00001898operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001899{
1900 return !(__x == __y);
1901}
1902
1903template<class _Tp, class _Up>
1904inline _LIBCPP_INLINE_VISIBILITY
1905bool
Howard Hinnant719bda32011-05-28 14:41:13 +00001906operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001907{
Marshall Clow8afdf7a2018-02-12 17:26:40 +00001908#if _LIBCPP_STD_VER <= 11
Eric Fiselierf8898c82015-02-05 23:01:40 +00001909 typedef typename common_type<_Tp*, _Up*>::type _Vp;
1910 return less<_Vp>()(__x.get(), __y.get());
Marshall Clow8afdf7a2018-02-12 17:26:40 +00001911#else
1912 return less<>()(__x.get(), __y.get());
1913#endif
1914
Howard Hinnantb17caf92012-02-21 21:02:58 +00001915}
1916
1917template<class _Tp, class _Up>
1918inline _LIBCPP_INLINE_VISIBILITY
1919bool
1920operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
1921{
1922 return __y < __x;
1923}
1924
1925template<class _Tp, class _Up>
1926inline _LIBCPP_INLINE_VISIBILITY
1927bool
1928operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
1929{
1930 return !(__y < __x);
1931}
1932
1933template<class _Tp, class _Up>
1934inline _LIBCPP_INLINE_VISIBILITY
1935bool
1936operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
1937{
1938 return !(__x < __y);
1939}
1940
1941template<class _Tp>
1942inline _LIBCPP_INLINE_VISIBILITY
1943bool
1944operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
1945{
1946 return !__x;
1947}
1948
1949template<class _Tp>
1950inline _LIBCPP_INLINE_VISIBILITY
1951bool
1952operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
1953{
1954 return !__x;
1955}
1956
1957template<class _Tp>
1958inline _LIBCPP_INLINE_VISIBILITY
1959bool
1960operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
1961{
1962 return static_cast<bool>(__x);
1963}
1964
1965template<class _Tp>
1966inline _LIBCPP_INLINE_VISIBILITY
1967bool
1968operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
1969{
1970 return static_cast<bool>(__x);
1971}
1972
1973template<class _Tp>
1974inline _LIBCPP_INLINE_VISIBILITY
1975bool
1976operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
1977{
1978 return less<_Tp*>()(__x.get(), nullptr);
1979}
1980
1981template<class _Tp>
1982inline _LIBCPP_INLINE_VISIBILITY
1983bool
1984operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
1985{
1986 return less<_Tp*>()(nullptr, __x.get());
1987}
1988
1989template<class _Tp>
1990inline _LIBCPP_INLINE_VISIBILITY
1991bool
1992operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
1993{
1994 return nullptr < __x;
1995}
1996
1997template<class _Tp>
1998inline _LIBCPP_INLINE_VISIBILITY
1999bool
2000operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
2001{
2002 return __x < nullptr;
2003}
2004
2005template<class _Tp>
2006inline _LIBCPP_INLINE_VISIBILITY
2007bool
2008operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
2009{
2010 return !(nullptr < __x);
2011}
2012
2013template<class _Tp>
2014inline _LIBCPP_INLINE_VISIBILITY
2015bool
2016operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
2017{
2018 return !(__x < nullptr);
2019}
2020
2021template<class _Tp>
2022inline _LIBCPP_INLINE_VISIBILITY
2023bool
2024operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
2025{
2026 return !(__x < nullptr);
2027}
2028
2029template<class _Tp>
2030inline _LIBCPP_INLINE_VISIBILITY
2031bool
2032operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
2033{
2034 return !(nullptr < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002035}
2036
2037template<class _Tp>
2038inline _LIBCPP_INLINE_VISIBILITY
2039void
Howard Hinnant719bda32011-05-28 14:41:13 +00002040swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002041{
2042 __x.swap(__y);
2043}
2044
2045template<class _Tp, class _Up>
2046inline _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07002047shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002048static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002049{
zoecarverd73f42a2020-05-11 18:42:50 -07002050 return shared_ptr<_Tp>(__r,
2051 static_cast<
2052 typename shared_ptr<_Tp>::element_type*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002053}
2054
2055template<class _Tp, class _Up>
2056inline _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07002057shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002058dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002059{
zoecarverd73f42a2020-05-11 18:42:50 -07002060 typedef typename shared_ptr<_Tp>::element_type _ET;
2061 _ET* __p = dynamic_cast<_ET*>(__r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002062 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
2063}
2064
2065template<class _Tp, class _Up>
zoecarverd73f42a2020-05-11 18:42:50 -07002066shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002067const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002068{
zoecarverd73f42a2020-05-11 18:42:50 -07002069 typedef typename shared_ptr<_Tp>::element_type _RTp;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002070 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002071}
2072
zoecarverd73f42a2020-05-11 18:42:50 -07002073template<class _Tp, class _Up>
2074shared_ptr<_Tp>
2075reinterpret_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
2076{
2077 return shared_ptr<_Tp>(__r,
2078 reinterpret_cast<
2079 typename shared_ptr<_Tp>::element_type*>(__r.get()));
2080}
2081
Howard Hinnant72f73582010-08-11 17:04:31 +00002082#ifndef _LIBCPP_NO_RTTI
2083
Howard Hinnantc51e1022010-05-11 19:42:16 +00002084template<class _Dp, class _Tp>
2085inline _LIBCPP_INLINE_VISIBILITY
2086_Dp*
Howard Hinnant719bda32011-05-28 14:41:13 +00002087get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002088{
2089 return __p.template __get_deleter<_Dp>();
2090}
2091
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002092#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00002093
Howard Hinnantc51e1022010-05-11 19:42:16 +00002094template<class _Tp>
Vy Nguyene369bd92020-07-13 12:34:37 -04002095class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002096{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002097public:
2098 typedef _Tp element_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002099private:
2100 element_type* __ptr_;
2101 __shared_weak_count* __cntrl_;
2102
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002103public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002104 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002105 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002106 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00002107 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
2108 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002109 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002110 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002111 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00002112 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
2113 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002114
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002115 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002116 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002117 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002118 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
2119 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002120 ~weak_ptr();
2121
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002122 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002123 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002124 template<class _Yp>
2125 typename enable_if
2126 <
2127 is_convertible<_Yp*, element_type*>::value,
2128 weak_ptr&
2129 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002130 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002131 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
2132
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002133 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002134 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
2135 template<class _Yp>
2136 typename enable_if
2137 <
2138 is_convertible<_Yp*, element_type*>::value,
2139 weak_ptr&
2140 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002141 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002142 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
2143
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002144 template<class _Yp>
2145 typename enable_if
2146 <
2147 is_convertible<_Yp*, element_type*>::value,
2148 weak_ptr&
2149 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002150 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002151 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002152
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002153 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002154 void swap(weak_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002155 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002156 void reset() _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002157
Howard Hinnant756c69b2010-09-22 16:48:34 +00002158 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002159 long use_count() const _NOEXCEPT
2160 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002161 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002162 bool expired() const _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05002163 {return __cntrl_ == nullptr || __cntrl_->use_count() == 0;}
Howard Hinnant719bda32011-05-28 14:41:13 +00002164 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00002165 template<class _Up>
2166 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00002167 bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002168 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002169 template<class _Up>
2170 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00002171 bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002172 {return __cntrl_ < __r.__cntrl_;}
2173
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002174 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
2175 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002176};
2177
Logan Smitha5e4d7e2020-05-07 12:07:01 -04002178#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
2179template<class _Tp>
2180weak_ptr(shared_ptr<_Tp>) -> weak_ptr<_Tp>;
2181#endif
2182
Howard Hinnantc51e1022010-05-11 19:42:16 +00002183template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002184inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002185_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00002186weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05002187 : __ptr_(nullptr),
2188 __cntrl_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002189{
2190}
2191
2192template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002193inline
Howard Hinnant719bda32011-05-28 14:41:13 +00002194weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002195 : __ptr_(__r.__ptr_),
2196 __cntrl_(__r.__cntrl_)
2197{
2198 if (__cntrl_)
2199 __cntrl_->__add_weak();
2200}
2201
2202template<class _Tp>
2203template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002204inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002205weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00002206 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00002207 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002208 : __ptr_(__r.__ptr_),
2209 __cntrl_(__r.__cntrl_)
2210{
2211 if (__cntrl_)
2212 __cntrl_->__add_weak();
2213}
2214
2215template<class _Tp>
2216template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002217inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002218weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00002219 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00002220 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002221 : __ptr_(__r.__ptr_),
2222 __cntrl_(__r.__cntrl_)
2223{
2224 if (__cntrl_)
2225 __cntrl_->__add_weak();
2226}
2227
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002228template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002229inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002230weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
2231 : __ptr_(__r.__ptr_),
2232 __cntrl_(__r.__cntrl_)
2233{
Bruce Mitchener170d8972020-11-24 12:53:53 -05002234 __r.__ptr_ = nullptr;
2235 __r.__cntrl_ = nullptr;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002236}
2237
2238template<class _Tp>
2239template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002240inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002241weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
2242 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
2243 _NOEXCEPT
2244 : __ptr_(__r.__ptr_),
2245 __cntrl_(__r.__cntrl_)
2246{
Bruce Mitchener170d8972020-11-24 12:53:53 -05002247 __r.__ptr_ = nullptr;
2248 __r.__cntrl_ = nullptr;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002249}
2250
Howard Hinnantc51e1022010-05-11 19:42:16 +00002251template<class _Tp>
2252weak_ptr<_Tp>::~weak_ptr()
2253{
2254 if (__cntrl_)
2255 __cntrl_->__release_weak();
2256}
2257
2258template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002259inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002260weak_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00002261weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002262{
2263 weak_ptr(__r).swap(*this);
2264 return *this;
2265}
2266
2267template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002268template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002269inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002270typename enable_if
2271<
2272 is_convertible<_Yp*, _Tp*>::value,
2273 weak_ptr<_Tp>&
2274>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00002275weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002276{
2277 weak_ptr(__r).swap(*this);
2278 return *this;
2279}
2280
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002281template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002282inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002283weak_ptr<_Tp>&
2284weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
2285{
2286 weak_ptr(_VSTD::move(__r)).swap(*this);
2287 return *this;
2288}
2289
Howard Hinnantc51e1022010-05-11 19:42:16 +00002290template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002291template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002292inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002293typename enable_if
2294<
2295 is_convertible<_Yp*, _Tp*>::value,
2296 weak_ptr<_Tp>&
2297>::type
2298weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
2299{
2300 weak_ptr(_VSTD::move(__r)).swap(*this);
2301 return *this;
2302}
2303
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002304template<class _Tp>
2305template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002306inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002307typename enable_if
2308<
2309 is_convertible<_Yp*, _Tp*>::value,
2310 weak_ptr<_Tp>&
2311>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00002312weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002313{
2314 weak_ptr(__r).swap(*this);
2315 return *this;
2316}
2317
2318template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002319inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002320void
Howard Hinnant719bda32011-05-28 14:41:13 +00002321weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002322{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002323 _VSTD::swap(__ptr_, __r.__ptr_);
2324 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002325}
2326
2327template<class _Tp>
2328inline _LIBCPP_INLINE_VISIBILITY
2329void
Howard Hinnant719bda32011-05-28 14:41:13 +00002330swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002331{
2332 __x.swap(__y);
2333}
2334
2335template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002336inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002337void
Howard Hinnant719bda32011-05-28 14:41:13 +00002338weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002339{
2340 weak_ptr().swap(*this);
2341}
2342
2343template<class _Tp>
2344template<class _Yp>
2345shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00002346 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002347 : __ptr_(__r.__ptr_),
2348 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
2349{
Bruce Mitchener170d8972020-11-24 12:53:53 -05002350 if (__cntrl_ == nullptr)
Marshall Clow8fea1612016-08-25 15:09:01 +00002351 __throw_bad_weak_ptr();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002352}
2353
2354template<class _Tp>
2355shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002356weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002357{
2358 shared_ptr<_Tp> __r;
2359 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
2360 if (__r.__cntrl_)
2361 __r.__ptr_ = __ptr_;
2362 return __r;
2363}
2364
Marshall Clow3c2d8a42015-11-12 15:56:44 +00002365#if _LIBCPP_STD_VER > 14
2366template <class _Tp = void> struct owner_less;
2367#else
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002368template <class _Tp> struct owner_less;
Marshall Clow3c2d8a42015-11-12 15:56:44 +00002369#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002370
2371template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002372struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00002373 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002374{
2375 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00002376 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00002377 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002378 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002379 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00002380 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002381 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002382 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00002383 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002384 {return __x.owner_before(__y);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002385};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002386
2387template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002388struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00002389 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
2390{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002391 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00002392 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00002393 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002394 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002395 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00002396 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002397 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002398 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00002399 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002400 {return __x.owner_before(__y);}
2401};
2402
Marshall Clow3c2d8a42015-11-12 15:56:44 +00002403#if _LIBCPP_STD_VER > 14
2404template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002405struct _LIBCPP_TEMPLATE_VIS owner_less<void>
Marshall Clow3c2d8a42015-11-12 15:56:44 +00002406{
2407 template <class _Tp, class _Up>
2408 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00002409 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00002410 {return __x.owner_before(__y);}
2411 template <class _Tp, class _Up>
2412 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00002413 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00002414 {return __x.owner_before(__y);}
2415 template <class _Tp, class _Up>
2416 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00002417 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00002418 {return __x.owner_before(__y);}
2419 template <class _Tp, class _Up>
2420 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00002421 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00002422 {return __x.owner_before(__y);}
2423 typedef void is_transparent;
2424};
2425#endif
2426
Howard Hinnantc51e1022010-05-11 19:42:16 +00002427template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002428class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
Howard Hinnantc51e1022010-05-11 19:42:16 +00002429{
2430 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002431protected:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002432 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00002433 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002434 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002435 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002436 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002437 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
2438 {return *this;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002439 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002440 ~enable_shared_from_this() {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002441public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00002442 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002443 shared_ptr<_Tp> shared_from_this()
2444 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002445 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002446 shared_ptr<_Tp const> shared_from_this() const
2447 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002448
Eric Fiselier84006862016-06-02 00:15:35 +00002449#if _LIBCPP_STD_VER > 14
2450 _LIBCPP_INLINE_VISIBILITY
2451 weak_ptr<_Tp> weak_from_this() _NOEXCEPT
2452 { return __weak_this_; }
2453
2454 _LIBCPP_INLINE_VISIBILITY
2455 weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
2456 { return __weak_this_; }
2457#endif // _LIBCPP_STD_VER > 14
2458
Howard Hinnantc51e1022010-05-11 19:42:16 +00002459 template <class _Up> friend class shared_ptr;
2460};
2461
Howard Hinnant36b31ae2010-06-03 16:42:57 +00002462template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002463struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00002464{
2465 typedef shared_ptr<_Tp> argument_type;
2466 typedef size_t result_type;
Eric Fiselier698a97b2017-01-21 00:02:12 +00002467
Howard Hinnant756c69b2010-09-22 16:48:34 +00002468 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002469 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00002470 {
zoecarverd73f42a2020-05-11 18:42:50 -07002471 return hash<typename shared_ptr<_Tp>::element_type*>()(__ptr.get());
Howard Hinnant36b31ae2010-06-03 16:42:57 +00002472 }
2473};
2474
Howard Hinnantc834c512011-11-29 18:15:50 +00002475template<class _CharT, class _Traits, class _Yp>
Howard Hinnantdc095972011-07-18 15:51:59 +00002476inline _LIBCPP_INLINE_VISIBILITY
2477basic_ostream<_CharT, _Traits>&
Howard Hinnantc834c512011-11-29 18:15:50 +00002478operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnantdc095972011-07-18 15:51:59 +00002479
Eric Fiselier9b492672016-06-18 02:12:53 +00002480
2481#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00002482
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002483class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant9fa30202012-07-30 01:40:57 +00002484{
Howard Hinnant49e145e2012-10-30 19:06:59 +00002485 void* __lx;
Howard Hinnant9fa30202012-07-30 01:40:57 +00002486public:
2487 void lock() _NOEXCEPT;
2488 void unlock() _NOEXCEPT;
2489
2490private:
2491 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
2492 __sp_mut(const __sp_mut&);
2493 __sp_mut& operator=(const __sp_mut&);
2494
Howard Hinnant8331b762013-03-06 23:30:19 +00002495 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00002496};
2497
Mehdi Amini228053d2017-05-04 17:08:54 +00002498_LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
2499__sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00002500
2501template <class _Tp>
2502inline _LIBCPP_INLINE_VISIBILITY
2503bool
2504atomic_is_lock_free(const shared_ptr<_Tp>*)
2505{
2506 return false;
2507}
2508
2509template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00002510_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00002511shared_ptr<_Tp>
2512atomic_load(const shared_ptr<_Tp>* __p)
2513{
2514 __sp_mut& __m = __get_sp_mut(__p);
2515 __m.lock();
2516 shared_ptr<_Tp> __q = *__p;
2517 __m.unlock();
2518 return __q;
2519}
Aditya Kumar7c5db692017-08-20 10:38:55 +00002520
Howard Hinnant9fa30202012-07-30 01:40:57 +00002521template <class _Tp>
2522inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00002523_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00002524shared_ptr<_Tp>
2525atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
2526{
2527 return atomic_load(__p);
2528}
2529
2530template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00002531_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00002532void
2533atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
2534{
2535 __sp_mut& __m = __get_sp_mut(__p);
2536 __m.lock();
2537 __p->swap(__r);
2538 __m.unlock();
2539}
2540
2541template <class _Tp>
2542inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00002543_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00002544void
2545atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
2546{
2547 atomic_store(__p, __r);
2548}
2549
2550template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00002551_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00002552shared_ptr<_Tp>
2553atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
2554{
2555 __sp_mut& __m = __get_sp_mut(__p);
2556 __m.lock();
2557 __p->swap(__r);
2558 __m.unlock();
2559 return __r;
2560}
Aditya Kumar7c5db692017-08-20 10:38:55 +00002561
Howard Hinnant9fa30202012-07-30 01:40:57 +00002562template <class _Tp>
2563inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00002564_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00002565shared_ptr<_Tp>
2566atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
2567{
2568 return atomic_exchange(__p, __r);
2569}
2570
2571template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00002572_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00002573bool
2574atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
2575{
Marshall Clow4201ee82016-05-18 17:50:13 +00002576 shared_ptr<_Tp> __temp;
Howard Hinnant9fa30202012-07-30 01:40:57 +00002577 __sp_mut& __m = __get_sp_mut(__p);
2578 __m.lock();
2579 if (__p->__owner_equivalent(*__v))
2580 {
Marshall Clow4201ee82016-05-18 17:50:13 +00002581 _VSTD::swap(__temp, *__p);
Howard Hinnant9fa30202012-07-30 01:40:57 +00002582 *__p = __w;
2583 __m.unlock();
2584 return true;
2585 }
Marshall Clow4201ee82016-05-18 17:50:13 +00002586 _VSTD::swap(__temp, *__v);
Howard Hinnant9fa30202012-07-30 01:40:57 +00002587 *__v = *__p;
2588 __m.unlock();
2589 return false;
2590}
2591
2592template <class _Tp>
2593inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00002594_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00002595bool
2596atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
2597{
2598 return atomic_compare_exchange_strong(__p, __v, __w);
2599}
2600
2601template <class _Tp>
2602inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00002603_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00002604bool
2605atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
2606 shared_ptr<_Tp> __w, memory_order, memory_order)
2607{
2608 return atomic_compare_exchange_strong(__p, __v, __w);
2609}
2610
2611template <class _Tp>
2612inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00002613_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00002614bool
2615atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
2616 shared_ptr<_Tp> __w, memory_order, memory_order)
2617{
2618 return atomic_compare_exchange_weak(__p, __v, __w);
2619}
2620
Eric Fiselier9b492672016-06-18 02:12:53 +00002621#endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00002622
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002623//enum class
Eric Fiselierab6bb302017-01-05 01:15:42 +00002624#if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE)
2625# ifndef _LIBCPP_CXX03_LANG
2626enum class pointer_safety : unsigned char {
2627 relaxed,
2628 preferred,
2629 strict
2630};
2631# endif
2632#else
Howard Hinnant8331b762013-03-06 23:30:19 +00002633struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantc51e1022010-05-11 19:42:16 +00002634{
Howard Hinnant49e145e2012-10-30 19:06:59 +00002635 enum __lx
Howard Hinnantc51e1022010-05-11 19:42:16 +00002636 {
2637 relaxed,
2638 preferred,
2639 strict
2640 };
2641
Howard Hinnant49e145e2012-10-30 19:06:59 +00002642 __lx __v_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002643
Howard Hinnant756c69b2010-09-22 16:48:34 +00002644 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2fdd22b2017-01-05 01:28:40 +00002645 pointer_safety() : __v_() {}
2646
2647 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant49e145e2012-10-30 19:06:59 +00002648 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002649 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002650 operator int() const {return __v_;}
2651};
Eric Fiselierab6bb302017-01-05 01:15:42 +00002652#endif
2653
2654#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \
Louis Dionne5e0eadd2018-08-01 02:08:59 +00002655 defined(_LIBCPP_BUILDING_LIBRARY)
Eric Fiselierab6bb302017-01-05 01:15:42 +00002656_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
2657#else
2658// This function is only offered in C++03 under ABI v1.
2659# if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG)
2660inline _LIBCPP_INLINE_VISIBILITY
2661pointer_safety get_pointer_safety() _NOEXCEPT {
2662 return pointer_safety::relaxed;
2663}
2664# endif
2665#endif
2666
Howard Hinnantc51e1022010-05-11 19:42:16 +00002667
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002668_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
2669_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
2670_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002671_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002672
2673template <class _Tp>
2674inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002675_Tp*
Howard Hinnantc51e1022010-05-11 19:42:16 +00002676undeclare_reachable(_Tp* __p)
2677{
2678 return static_cast<_Tp*>(__undeclare_reachable(__p));
2679}
2680
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002681_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002682
Marshall Clow8982dcd2015-07-13 20:04:56 +00002683// --- Helper for container swap --
2684template <typename _Alloc>
Marshall Clow8982dcd2015-07-13 20:04:56 +00002685_LIBCPP_INLINE_VISIBILITY
2686void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
2687#if _LIBCPP_STD_VER >= 14
2688 _NOEXCEPT
2689#else
2690 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
2691#endif
2692{
2693 using _VSTD::swap;
2694 swap(__a1, __a2);
2695}
2696
2697template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00002698inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00002699void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
2700
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05002701template <typename _Alloc>
2702inline _LIBCPP_INLINE_VISIBILITY
2703void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
2704#if _LIBCPP_STD_VER >= 14
2705 _NOEXCEPT
2706#else
2707 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
2708#endif
2709{
2710 _VSTD::__swap_allocator(__a1, __a2,
2711 integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
2712}
2713
Marshall Clowff91de82015-08-18 19:51:37 +00002714template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
Aditya Kumar7c5db692017-08-20 10:38:55 +00002715struct __noexcept_move_assign_container : public integral_constant<bool,
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002716 _Traits::propagate_on_container_move_assignment::value
2717#if _LIBCPP_STD_VER > 14
2718 || _Traits::is_always_equal::value
2719#else
2720 && is_nothrow_move_assignable<_Alloc>::value
2721#endif
2722 > {};
Marshall Clow8982dcd2015-07-13 20:04:56 +00002723
Marshall Clowa591b9a2016-07-11 21:38:08 +00002724
Marshall Clowa591b9a2016-07-11 21:38:08 +00002725template <class _Tp, class _Alloc>
2726struct __temp_value {
2727 typedef allocator_traits<_Alloc> _Traits;
Aditya Kumar7c5db692017-08-20 10:38:55 +00002728
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00002729 typename aligned_storage<sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)>::type __v;
Marshall Clowa591b9a2016-07-11 21:38:08 +00002730 _Alloc &__a;
2731
2732 _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
2733 _Tp & get() { return *__addr(); }
Aditya Kumar7c5db692017-08-20 10:38:55 +00002734
Marshall Clowa591b9a2016-07-11 21:38:08 +00002735 template<class... _Args>
Peter Collingbournecb126152018-08-15 17:49:30 +00002736 _LIBCPP_NO_CFI
2737 __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc) {
2738 _Traits::construct(__a, reinterpret_cast<_Tp*>(addressof(__v)),
2739 _VSTD::forward<_Args>(__args)...);
2740 }
Aditya Kumar7c5db692017-08-20 10:38:55 +00002741
Marshall Clowa591b9a2016-07-11 21:38:08 +00002742 ~__temp_value() { _Traits::destroy(__a, __addr()); }
2743 };
Marshall Clowa591b9a2016-07-11 21:38:08 +00002744
Marshall Clowe46031a2018-07-02 18:41:15 +00002745template<typename _Alloc, typename = void, typename = void>
Marshall Clow82c90aa2018-01-11 19:36:22 +00002746struct __is_allocator : false_type {};
2747
2748template<typename _Alloc>
2749struct __is_allocator<_Alloc,
Marshall Clowe46031a2018-07-02 18:41:15 +00002750 typename __void_t<typename _Alloc::value_type>::type,
2751 typename __void_t<decltype(_VSTD::declval<_Alloc&>().allocate(size_t(0)))>::type
2752 >
Marshall Clow82c90aa2018-01-11 19:36:22 +00002753 : true_type {};
Marshall Clow82c90aa2018-01-11 19:36:22 +00002754
Eric Fiselier74ebee62019-06-08 01:31:19 +00002755// __builtin_new_allocator -- A non-templated helper for allocating and
2756// deallocating memory using __builtin_operator_new and
2757// __builtin_operator_delete. It should be used in preference to
2758// `std::allocator<T>` to avoid additional instantiations.
2759struct __builtin_new_allocator {
2760 struct __builtin_new_deleter {
2761 typedef void* pointer_type;
2762
2763 _LIBCPP_CONSTEXPR explicit __builtin_new_deleter(size_t __size, size_t __align)
2764 : __size_(__size), __align_(__align) {}
2765
2766 void operator()(void* p) const _NOEXCEPT {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05002767 _VSTD::__libcpp_deallocate(p, __size_, __align_);
Eric Fiselier74ebee62019-06-08 01:31:19 +00002768 }
2769
2770 private:
2771 size_t __size_;
2772 size_t __align_;
2773 };
2774
2775 typedef unique_ptr<void, __builtin_new_deleter> __holder_t;
2776
2777 static __holder_t __allocate_bytes(size_t __s, size_t __align) {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05002778 return __holder_t(_VSTD::__libcpp_allocate(__s, __align),
Eric Fiselier74ebee62019-06-08 01:31:19 +00002779 __builtin_new_deleter(__s, __align));
2780 }
2781
2782 static void __deallocate_bytes(void* __p, size_t __s,
2783 size_t __align) _NOEXCEPT {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05002784 _VSTD::__libcpp_deallocate(__p, __s, __align);
Eric Fiselier74ebee62019-06-08 01:31:19 +00002785 }
2786
2787 template <class _Tp>
2788 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
2789 static __holder_t __allocate_type(size_t __n) {
2790 return __allocate_bytes(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
2791 }
2792
2793 template <class _Tp>
2794 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
2795 static void __deallocate_type(void* __p, size_t __n) _NOEXCEPT {
2796 __deallocate_bytes(__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
2797 }
2798};
2799
2800
Howard Hinnantc51e1022010-05-11 19:42:16 +00002801_LIBCPP_END_NAMESPACE_STD
2802
Eric Fiselierf4433a32017-05-31 22:07:49 +00002803_LIBCPP_POP_MACROS
2804
Louis Dionne59d0b3c2019-08-05 18:29:14 +00002805#if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
Louis Dionned53d8c02019-08-06 21:11:24 +00002806# include <__pstl_memory>
Louis Dionne59d0b3c2019-08-05 18:29:14 +00002807#endif
2808
Howard Hinnantc51e1022010-05-11 19:42:16 +00002809#endif // _LIBCPP_MEMORY