blob: fcfc10b01315df0be20f68bf03569e44f103afde [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 Dionne00180872021-04-09 12:58:00 -0400688#include <__memory/temporary_buffer.h>
Louis Dionne74aef9f2020-12-11 12:20:06 -0500689#include <__memory/utilities.h>
Eric Fiselier8020b6c2015-08-19 17:21:46 +0000690#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +0000691# include <atomic>
692#endif
Marshall Clow0a1e7502018-09-12 19:41:40 +0000693#include <version>
Howard Hinnant9fa30202012-07-30 01:40:57 +0000694
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000695#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000696#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000697#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000698
Eric Fiselierf4433a32017-05-31 22:07:49 +0000699_LIBCPP_PUSH_MACROS
700#include <__undef_macros>
701
702
Howard Hinnantc51e1022010-05-11 19:42:16 +0000703_LIBCPP_BEGIN_NAMESPACE_STD
704
Eric Fiselier89659d12015-07-07 00:27:16 +0000705template <class _ValueType>
Louis Dionne16fe2952018-07-11 23:14:33 +0000706inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +0000707_ValueType __libcpp_relaxed_load(_ValueType const* __value) {
708#if !defined(_LIBCPP_HAS_NO_THREADS) && \
709 defined(__ATOMIC_RELAXED) && \
Richard Smithca47d0f2019-04-25 20:02:10 +0000710 (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
Eric Fiselier89659d12015-07-07 00:27:16 +0000711 return __atomic_load_n(__value, __ATOMIC_RELAXED);
712#else
713 return *__value;
714#endif
715}
716
Kuba Breckade9d6792016-09-04 09:55:12 +0000717template <class _ValueType>
Louis Dionne16fe2952018-07-11 23:14:33 +0000718inline _LIBCPP_INLINE_VISIBILITY
Kuba Breckade9d6792016-09-04 09:55:12 +0000719_ValueType __libcpp_acquire_load(_ValueType const* __value) {
720#if !defined(_LIBCPP_HAS_NO_THREADS) && \
721 defined(__ATOMIC_ACQUIRE) && \
Richard Smithca47d0f2019-04-25 20:02:10 +0000722 (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
Kuba Breckade9d6792016-09-04 09:55:12 +0000723 return __atomic_load_n(__value, __ATOMIC_ACQUIRE);
724#else
725 return *__value;
726#endif
727}
728
Louis Dionned6651542020-11-03 12:05:55 -0500729template <class _Alloc, class _Ptr>
730_LIBCPP_INLINE_VISIBILITY
731void __construct_forward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2) {
732 static_assert(__is_cpp17_move_insertable<_Alloc>::value,
Arthur O'Dwyerf2016bb2020-12-12 11:43:15 -0500733 "The specified type does not meet the requirements of Cpp17MoveInsertable");
Louis Dionned6651542020-11-03 12:05:55 -0500734 typedef allocator_traits<_Alloc> _Traits;
735 for (; __begin1 != __end1; ++__begin1, (void)++__begin2) {
736 _Traits::construct(__a, _VSTD::__to_address(__begin2),
737#ifdef _LIBCPP_NO_EXCEPTIONS
738 _VSTD::move(*__begin1)
739#else
740 _VSTD::move_if_noexcept(*__begin1)
741#endif
742 );
743 }
744}
745
746template <class _Alloc, class _Tp, typename enable_if<
747 (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) &&
748 is_trivially_move_constructible<_Tp>::value
749>::type>
750_LIBCPP_INLINE_VISIBILITY
751void __construct_forward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2) {
752 ptrdiff_t _Np = __end1 - __begin1;
753 if (_Np > 0) {
754 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
755 __begin2 += _Np;
756 }
757}
758
759template <class _Alloc, class _Iter, class _Ptr>
760_LIBCPP_INLINE_VISIBILITY
761void __construct_range_forward(_Alloc& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2) {
762 typedef allocator_traits<_Alloc> _Traits;
763 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2) {
764 _Traits::construct(__a, _VSTD::__to_address(__begin2), *__begin1);
765 }
766}
767
768template <class _Alloc, class _Source, class _Dest,
769 class _RawSource = typename remove_const<_Source>::type,
770 class _RawDest = typename remove_const<_Dest>::type,
771 class =
772 typename enable_if<
773 is_trivially_copy_constructible<_Dest>::value &&
774 is_same<_RawSource, _RawDest>::value &&
775 (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Dest*, _Source&>::value)
776 >::type>
777_LIBCPP_INLINE_VISIBILITY
778void __construct_range_forward(_Alloc&, _Source* __begin1, _Source* __end1, _Dest*& __begin2) {
779 ptrdiff_t _Np = __end1 - __begin1;
780 if (_Np > 0) {
781 _VSTD::memcpy(const_cast<_RawDest*>(__begin2), __begin1, _Np * sizeof(_Dest));
782 __begin2 += _Np;
783 }
784}
785
786template <class _Alloc, class _Ptr>
787_LIBCPP_INLINE_VISIBILITY
788void __construct_backward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2) {
789 static_assert(__is_cpp17_move_insertable<_Alloc>::value,
790 "The specified type does not meet the requirements of Cpp17MoveInsertable");
791 typedef allocator_traits<_Alloc> _Traits;
792 while (__end1 != __begin1) {
793 _Traits::construct(__a, _VSTD::__to_address(__end2 - 1),
794#ifdef _LIBCPP_NO_EXCEPTIONS
795 _VSTD::move(*--__end1)
796#else
797 _VSTD::move_if_noexcept(*--__end1)
798#endif
799 );
800 --__end2;
801 }
802}
803
804template <class _Alloc, class _Tp, class = typename enable_if<
805 (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) &&
806 is_trivially_move_constructible<_Tp>::value
807>::type>
808_LIBCPP_INLINE_VISIBILITY
809void __construct_backward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2) {
810 ptrdiff_t _Np = __end1 - __begin1;
811 __end2 -= _Np;
812 if (_Np > 0)
813 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
814}
815
Howard Hinnantc51e1022010-05-11 19:42:16 +0000816template <class _OutputIterator, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000817class _LIBCPP_TEMPLATE_VIS raw_storage_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000818 : public iterator<output_iterator_tag,
819 _Tp, // purposefully not C++03
820 ptrdiff_t, // purposefully not C++03
821 _Tp*, // purposefully not C++03
822 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
823{
824private:
825 _OutputIterator __x_;
826public:
827 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
828 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
829 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -0500830 {::new ((void*)_VSTD::addressof(*__x_)) _Tp(__element); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +0000831#if _LIBCPP_STD_VER >= 14
832 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(_Tp&& __element)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -0500833 {::new ((void*)_VSTD::addressof(*__x_)) _Tp(_VSTD::move(__element)); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +0000834#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000835 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
836 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
837 {raw_storage_iterator __t(*this); ++__x_; return __t;}
Marshall Clowb949f1b2015-05-10 13:14:08 +0000838#if _LIBCPP_STD_VER >= 14
Aditya Kumar7c5db692017-08-20 10:38:55 +0000839 _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
Marshall Clowb949f1b2015-05-10 13:14:08 +0000840#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000841};
842
Howard Hinnant741c8fa2012-01-02 17:56:02 +0000843// default_delete
844
Howard Hinnantc51e1022010-05-11 19:42:16 +0000845template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +0000846struct _LIBCPP_TEMPLATE_VIS default_delete {
Erik Pilkington2a398762017-05-25 15:43:31 +0000847 static_assert(!is_function<_Tp>::value,
848 "default_delete cannot be instantiated for function types");
Eric Fiselier6779b062017-04-16 02:06:25 +0000849#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +0000850 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +0000851#else
Eric Fiselier6779b062017-04-16 02:06:25 +0000852 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +0000853#endif
Eric Fiselier6779b062017-04-16 02:06:25 +0000854 template <class _Up>
855 _LIBCPP_INLINE_VISIBILITY
856 default_delete(const default_delete<_Up>&,
857 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* =
858 0) _NOEXCEPT {}
859
860 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __ptr) const _NOEXCEPT {
861 static_assert(sizeof(_Tp) > 0,
862 "default_delete can not delete incomplete type");
863 static_assert(!is_void<_Tp>::value,
864 "default_delete can not delete incomplete type");
865 delete __ptr;
866 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000867};
868
869template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +0000870struct _LIBCPP_TEMPLATE_VIS default_delete<_Tp[]> {
871private:
872 template <class _Up>
873 struct _EnableIfConvertible
874 : enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value> {};
875
Howard Hinnant4500ca52011-12-18 21:19:44 +0000876public:
Eric Fiselier6779b062017-04-16 02:06:25 +0000877#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +0000878 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +0000879#else
Eric Fiselier6779b062017-04-16 02:06:25 +0000880 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +0000881#endif
Eric Fiselier6779b062017-04-16 02:06:25 +0000882
883 template <class _Up>
884 _LIBCPP_INLINE_VISIBILITY
885 default_delete(const default_delete<_Up[]>&,
886 typename _EnableIfConvertible<_Up>::type* = 0) _NOEXCEPT {}
887
888 template <class _Up>
889 _LIBCPP_INLINE_VISIBILITY
890 typename _EnableIfConvertible<_Up>::type
891 operator()(_Up* __ptr) const _NOEXCEPT {
892 static_assert(sizeof(_Tp) > 0,
893 "default_delete can not delete incomplete type");
894 static_assert(!is_void<_Tp>::value,
895 "default_delete can not delete void type");
896 delete[] __ptr;
897 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000898};
899
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000900template <class _Deleter>
901struct __unique_ptr_deleter_sfinae {
902 static_assert(!is_reference<_Deleter>::value, "incorrect specialization");
903 typedef const _Deleter& __lval_ref_type;
904 typedef _Deleter&& __good_rval_ref_type;
905 typedef true_type __enable_rval_overload;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000906};
907
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000908template <class _Deleter>
909struct __unique_ptr_deleter_sfinae<_Deleter const&> {
910 typedef const _Deleter& __lval_ref_type;
911 typedef const _Deleter&& __bad_rval_ref_type;
912 typedef false_type __enable_rval_overload;
913};
914
915template <class _Deleter>
916struct __unique_ptr_deleter_sfinae<_Deleter&> {
917 typedef _Deleter& __lval_ref_type;
918 typedef _Deleter&& __bad_rval_ref_type;
919 typedef false_type __enable_rval_overload;
920};
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000921
Vy Nguyene369bd92020-07-13 12:34:37 -0400922#if defined(_LIBCPP_ABI_ENABLE_UNIQUE_PTR_TRIVIAL_ABI)
923# define _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI __attribute__((trivial_abi))
924#else
925# define _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI
926#endif
927
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000928template <class _Tp, class _Dp = default_delete<_Tp> >
Vy Nguyene369bd92020-07-13 12:34:37 -0400929class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000930public:
931 typedef _Tp element_type;
932 typedef _Dp deleter_type;
Louis Dionne88978f62021-01-12 11:53:24 -0500933 typedef _LIBCPP_NODEBUG_TYPE typename __pointer<_Tp, deleter_type>::type pointer;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000934
935 static_assert(!is_rvalue_reference<deleter_type>::value,
936 "the specified deleter type cannot be an rvalue reference");
937
938private:
939 __compressed_pair<pointer, deleter_type> __ptr_;
940
941 struct __nat { int __for_bool_; };
942
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000943 typedef _LIBCPP_NODEBUG_TYPE __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000944
945 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000946 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +0000947 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000948
949 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000950 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +0000951 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000952
953 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000954 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +0000955 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000956
957 template <bool _Dummy, class _Deleter = typename __dependent_type<
958 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000959 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000960 typename enable_if<is_default_constructible<_Deleter>::value &&
961 !is_pointer<_Deleter>::value>::type;
962
963 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000964 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000965 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
966
967 template <class _UPtr, class _Up>
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000968 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000969 is_convertible<typename _UPtr::pointer, pointer>::value &&
970 !is_array<_Up>::value
971 >::type;
972
973 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000974 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000975 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
976 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
977 >::type;
978
979 template <class _UDel>
980 using _EnableIfDeleterAssignable = typename enable_if<
981 is_assignable<_Dp&, _UDel&&>::value
982 >::type;
983
984public:
985 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +0000986 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000987 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -0500988 _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000989
990 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +0000991 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000992 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -0500993 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000994
995 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +0000996 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000997 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -0500998 explicit unique_ptr(pointer __p) _NOEXCEPT : __ptr_(__p, __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +0000999
1000 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001001 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001002 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001003 unique_ptr(pointer __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001004 : __ptr_(__p, __d) {}
1005
1006 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001007 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001008 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001009 unique_ptr(pointer __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001010 : __ptr_(__p, _VSTD::move(__d)) {
1011 static_assert(!is_reference<deleter_type>::value,
1012 "rvalue deleter bound to reference");
1013 }
1014
1015 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001016 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001017 _LIBCPP_INLINE_VISIBILITY
1018 unique_ptr(pointer __p, _BadRValRefType<_Dummy> __d) = delete;
1019
1020 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001021 unique_ptr(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001022 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
1023 }
1024
1025 template <class _Up, class _Ep,
1026 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
1027 class = _EnableIfDeleterConvertible<_Ep>
1028 >
1029 _LIBCPP_INLINE_VISIBILITY
1030 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
1031 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
1032
1033#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
1034 template <class _Up>
1035 _LIBCPP_INLINE_VISIBILITY
1036 unique_ptr(auto_ptr<_Up>&& __p,
1037 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001038 is_same<_Dp, default_delete<_Tp> >::value,
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001039 __nat>::type = __nat()) _NOEXCEPT
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001040 : __ptr_(__p.release(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001041#endif
1042
1043 _LIBCPP_INLINE_VISIBILITY
1044 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
1045 reset(__u.release());
1046 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
1047 return *this;
1048 }
1049
1050 template <class _Up, class _Ep,
1051 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
1052 class = _EnableIfDeleterAssignable<_Ep>
1053 >
1054 _LIBCPP_INLINE_VISIBILITY
1055 unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
1056 reset(__u.release());
1057 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
1058 return *this;
1059 }
1060
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001061#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
1062 template <class _Up>
1063 _LIBCPP_INLINE_VISIBILITY
1064 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
1065 is_same<_Dp, default_delete<_Tp> >::value,
1066 unique_ptr&>::type
1067 operator=(auto_ptr<_Up> __p) {
1068 reset(__p.release());
1069 return *this;
1070 }
1071#endif
1072
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001073#ifdef _LIBCPP_CXX03_LANG
1074 unique_ptr(unique_ptr const&) = delete;
1075 unique_ptr& operator=(unique_ptr const&) = delete;
1076#endif
1077
1078
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001079 _LIBCPP_INLINE_VISIBILITY
1080 ~unique_ptr() { reset(); }
1081
1082 _LIBCPP_INLINE_VISIBILITY
1083 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
1084 reset();
1085 return *this;
1086 }
1087
1088 _LIBCPP_INLINE_VISIBILITY
1089 typename add_lvalue_reference<_Tp>::type
1090 operator*() const {
1091 return *__ptr_.first();
1092 }
1093 _LIBCPP_INLINE_VISIBILITY
1094 pointer operator->() const _NOEXCEPT {
1095 return __ptr_.first();
1096 }
1097 _LIBCPP_INLINE_VISIBILITY
1098 pointer get() const _NOEXCEPT {
1099 return __ptr_.first();
1100 }
1101 _LIBCPP_INLINE_VISIBILITY
1102 deleter_type& get_deleter() _NOEXCEPT {
1103 return __ptr_.second();
1104 }
1105 _LIBCPP_INLINE_VISIBILITY
1106 const deleter_type& get_deleter() const _NOEXCEPT {
1107 return __ptr_.second();
1108 }
1109 _LIBCPP_INLINE_VISIBILITY
1110 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
1111 return __ptr_.first() != nullptr;
1112 }
1113
1114 _LIBCPP_INLINE_VISIBILITY
1115 pointer release() _NOEXCEPT {
1116 pointer __t = __ptr_.first();
1117 __ptr_.first() = pointer();
1118 return __t;
1119 }
1120
1121 _LIBCPP_INLINE_VISIBILITY
1122 void reset(pointer __p = pointer()) _NOEXCEPT {
1123 pointer __tmp = __ptr_.first();
1124 __ptr_.first() = __p;
1125 if (__tmp)
1126 __ptr_.second()(__tmp);
1127 }
1128
1129 _LIBCPP_INLINE_VISIBILITY
1130 void swap(unique_ptr& __u) _NOEXCEPT {
1131 __ptr_.swap(__u.__ptr_);
1132 }
1133};
1134
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001135
Howard Hinnantc51e1022010-05-11 19:42:16 +00001136template <class _Tp, class _Dp>
Vy Nguyene369bd92020-07-13 12:34:37 -04001137class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp> {
Howard Hinnantc51e1022010-05-11 19:42:16 +00001138public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001139 typedef _Tp element_type;
1140 typedef _Dp deleter_type;
Louis Dionne88978f62021-01-12 11:53:24 -05001141 typedef typename __pointer<_Tp, deleter_type>::type pointer;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001142
Howard Hinnantc51e1022010-05-11 19:42:16 +00001143private:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001144 __compressed_pair<pointer, deleter_type> __ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001145
Eric Fiselier31127cd2017-04-16 02:14:31 +00001146 template <class _From>
1147 struct _CheckArrayPointerConversion : is_same<_From, pointer> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001148
Eric Fiselier31127cd2017-04-16 02:14:31 +00001149 template <class _FromElem>
1150 struct _CheckArrayPointerConversion<_FromElem*>
1151 : integral_constant<bool,
1152 is_same<_FromElem*, pointer>::value ||
1153 (is_same<pointer, element_type*>::value &&
1154 is_convertible<_FromElem(*)[], element_type(*)[]>::value)
1155 >
1156 {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001157
Eric Fiselier31127cd2017-04-16 02:14:31 +00001158 typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001159
1160 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001161 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001162 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001163
1164 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001165 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001166 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001167
1168 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001169 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001170 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001171
1172 template <bool _Dummy, class _Deleter = typename __dependent_type<
1173 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001174 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001175 typename enable_if<is_default_constructible<_Deleter>::value &&
1176 !is_pointer<_Deleter>::value>::type;
1177
1178 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001179 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001180 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
1181
1182 template <class _Pp>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001183 using _EnableIfPointerConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00001184 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001185 >::type;
1186
1187 template <class _UPtr, class _Up,
1188 class _ElemT = typename _UPtr::element_type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001189 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001190 is_array<_Up>::value &&
1191 is_same<pointer, element_type*>::value &&
1192 is_same<typename _UPtr::pointer, _ElemT*>::value &&
1193 is_convertible<_ElemT(*)[], element_type(*)[]>::value
1194 >::type;
1195
1196 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001197 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001198 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
1199 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
1200 >::type;
1201
1202 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001203 using _EnableIfDeleterAssignable _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001204 is_assignable<_Dp&, _UDel&&>::value
1205 >::type;
1206
Howard Hinnantc51e1022010-05-11 19:42:16 +00001207public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001208 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001209 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001210 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001211 _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001212
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001213 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001214 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001215 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001216 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001217
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001218 template <class _Pp, bool _Dummy = true,
1219 class = _EnableIfDeleterDefaultConstructible<_Dummy>,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001220 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001221 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001222 explicit unique_ptr(_Pp __p) _NOEXCEPT
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001223 : __ptr_(__p, __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001224
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001225 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001226 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> >,
1227 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001228 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001229 unique_ptr(_Pp __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001230 : __ptr_(__p, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001231
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001232 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001233 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001234 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001235 unique_ptr(nullptr_t, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001236 : __ptr_(nullptr, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001237
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001238 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001239 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> >,
1240 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001241 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001242 unique_ptr(_Pp __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001243 : __ptr_(__p, _VSTD::move(__d)) {
1244 static_assert(!is_reference<deleter_type>::value,
1245 "rvalue deleter bound to reference");
1246 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001247
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001248 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001249 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001250 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001251 unique_ptr(nullptr_t, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001252 : __ptr_(nullptr, _VSTD::move(__d)) {
1253 static_assert(!is_reference<deleter_type>::value,
1254 "rvalue deleter bound to reference");
1255 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00001256
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001257 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001258 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> >,
1259 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001260 _LIBCPP_INLINE_VISIBILITY
1261 unique_ptr(_Pp __p, _BadRValRefType<_Dummy> __d) = delete;
Howard Hinnant4500ca52011-12-18 21:19:44 +00001262
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001263 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001264 unique_ptr(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001265 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
1266 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00001267
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001268 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001269 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001270 reset(__u.release());
1271 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
1272 return *this;
1273 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001274
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001275 template <class _Up, class _Ep,
1276 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
1277 class = _EnableIfDeleterConvertible<_Ep>
1278 >
1279 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001280 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Eric Fiselier3827e6a2017-04-17 20:20:27 +00001281 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001282 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001283
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001284 template <class _Up, class _Ep,
1285 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
1286 class = _EnableIfDeleterAssignable<_Ep>
1287 >
1288 _LIBCPP_INLINE_VISIBILITY
1289 unique_ptr&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001290 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001291 reset(__u.release());
1292 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
1293 return *this;
1294 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001295
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001296#ifdef _LIBCPP_CXX03_LANG
1297 unique_ptr(unique_ptr const&) = delete;
1298 unique_ptr& operator=(unique_ptr const&) = delete;
1299#endif
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001300
1301public:
1302 _LIBCPP_INLINE_VISIBILITY
1303 ~unique_ptr() { reset(); }
1304
1305 _LIBCPP_INLINE_VISIBILITY
1306 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
1307 reset();
1308 return *this;
1309 }
1310
1311 _LIBCPP_INLINE_VISIBILITY
1312 typename add_lvalue_reference<_Tp>::type
1313 operator[](size_t __i) const {
1314 return __ptr_.first()[__i];
1315 }
1316 _LIBCPP_INLINE_VISIBILITY
1317 pointer get() const _NOEXCEPT {
1318 return __ptr_.first();
1319 }
1320
1321 _LIBCPP_INLINE_VISIBILITY
1322 deleter_type& get_deleter() _NOEXCEPT {
1323 return __ptr_.second();
1324 }
1325
1326 _LIBCPP_INLINE_VISIBILITY
1327 const deleter_type& get_deleter() const _NOEXCEPT {
1328 return __ptr_.second();
1329 }
1330 _LIBCPP_INLINE_VISIBILITY
1331 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
1332 return __ptr_.first() != nullptr;
1333 }
1334
1335 _LIBCPP_INLINE_VISIBILITY
1336 pointer release() _NOEXCEPT {
1337 pointer __t = __ptr_.first();
1338 __ptr_.first() = pointer();
1339 return __t;
1340 }
1341
1342 template <class _Pp>
1343 _LIBCPP_INLINE_VISIBILITY
1344 typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00001345 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001346 >::type
1347 reset(_Pp __p) _NOEXCEPT {
1348 pointer __tmp = __ptr_.first();
1349 __ptr_.first() = __p;
1350 if (__tmp)
1351 __ptr_.second()(__tmp);
1352 }
1353
1354 _LIBCPP_INLINE_VISIBILITY
1355 void reset(nullptr_t = nullptr) _NOEXCEPT {
1356 pointer __tmp = __ptr_.first();
1357 __ptr_.first() = nullptr;
1358 if (__tmp)
1359 __ptr_.second()(__tmp);
1360 }
1361
1362 _LIBCPP_INLINE_VISIBILITY
1363 void swap(unique_ptr& __u) _NOEXCEPT {
1364 __ptr_.swap(__u.__ptr_);
1365 }
1366
Howard Hinnantc51e1022010-05-11 19:42:16 +00001367};
1368
1369template <class _Tp, class _Dp>
1370inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6bfed252016-04-21 23:38:59 +00001371typename enable_if<
1372 __is_swappable<_Dp>::value,
1373 void
1374>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00001375swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001376
1377template <class _T1, class _D1, class _T2, class _D2>
1378inline _LIBCPP_INLINE_VISIBILITY
1379bool
1380operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
1381
1382template <class _T1, class _D1, class _T2, class _D2>
1383inline _LIBCPP_INLINE_VISIBILITY
1384bool
1385operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
1386
1387template <class _T1, class _D1, class _T2, class _D2>
1388inline _LIBCPP_INLINE_VISIBILITY
1389bool
Howard Hinnantb17caf92012-02-21 21:02:58 +00001390operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
1391{
1392 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
1393 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselierf8898c82015-02-05 23:01:40 +00001394 typedef typename common_type<_P1, _P2>::type _Vp;
1395 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00001396}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001397
1398template <class _T1, class _D1, class _T2, class _D2>
1399inline _LIBCPP_INLINE_VISIBILITY
1400bool
1401operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
1402
1403template <class _T1, class _D1, class _T2, class _D2>
1404inline _LIBCPP_INLINE_VISIBILITY
1405bool
1406operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
1407
1408template <class _T1, class _D1, class _T2, class _D2>
1409inline _LIBCPP_INLINE_VISIBILITY
1410bool
1411operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
1412
Howard Hinnantb17caf92012-02-21 21:02:58 +00001413template <class _T1, class _D1>
1414inline _LIBCPP_INLINE_VISIBILITY
1415bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001416operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00001417{
1418 return !__x;
1419}
1420
1421template <class _T1, class _D1>
1422inline _LIBCPP_INLINE_VISIBILITY
1423bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001424operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00001425{
1426 return !__x;
1427}
1428
1429template <class _T1, class _D1>
1430inline _LIBCPP_INLINE_VISIBILITY
1431bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001432operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00001433{
1434 return static_cast<bool>(__x);
1435}
1436
1437template <class _T1, class _D1>
1438inline _LIBCPP_INLINE_VISIBILITY
1439bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001440operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00001441{
1442 return static_cast<bool>(__x);
1443}
1444
1445template <class _T1, class _D1>
1446inline _LIBCPP_INLINE_VISIBILITY
1447bool
1448operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
1449{
1450 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
1451 return less<_P1>()(__x.get(), nullptr);
1452}
1453
1454template <class _T1, class _D1>
1455inline _LIBCPP_INLINE_VISIBILITY
1456bool
1457operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
1458{
1459 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
1460 return less<_P1>()(nullptr, __x.get());
1461}
1462
1463template <class _T1, class _D1>
1464inline _LIBCPP_INLINE_VISIBILITY
1465bool
1466operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
1467{
1468 return nullptr < __x;
1469}
1470
1471template <class _T1, class _D1>
1472inline _LIBCPP_INLINE_VISIBILITY
1473bool
1474operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
1475{
1476 return __x < nullptr;
1477}
1478
1479template <class _T1, class _D1>
1480inline _LIBCPP_INLINE_VISIBILITY
1481bool
1482operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
1483{
1484 return !(nullptr < __x);
1485}
1486
1487template <class _T1, class _D1>
1488inline _LIBCPP_INLINE_VISIBILITY
1489bool
1490operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
1491{
1492 return !(__x < nullptr);
1493}
1494
1495template <class _T1, class _D1>
1496inline _LIBCPP_INLINE_VISIBILITY
1497bool
1498operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
1499{
1500 return !(__x < nullptr);
1501}
1502
1503template <class _T1, class _D1>
1504inline _LIBCPP_INLINE_VISIBILITY
1505bool
1506operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
1507{
1508 return !(nullptr < __x);
1509}
1510
Marshall Clow9e8f4a92013-07-01 18:16:03 +00001511#if _LIBCPP_STD_VER > 11
1512
1513template<class _Tp>
1514struct __unique_if
1515{
1516 typedef unique_ptr<_Tp> __unique_single;
1517};
1518
1519template<class _Tp>
1520struct __unique_if<_Tp[]>
1521{
1522 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
1523};
1524
1525template<class _Tp, size_t _Np>
1526struct __unique_if<_Tp[_Np]>
1527{
1528 typedef void __unique_array_known_bound;
1529};
1530
1531template<class _Tp, class... _Args>
Marshall Clow724e3df2013-07-02 20:06:09 +00001532inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00001533typename __unique_if<_Tp>::__unique_single
1534make_unique(_Args&&... __args)
1535{
1536 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
1537}
1538
1539template<class _Tp>
Marshall Clow724e3df2013-07-02 20:06:09 +00001540inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00001541typename __unique_if<_Tp>::__unique_array_unknown_bound
1542make_unique(size_t __n)
1543{
1544 typedef typename remove_extent<_Tp>::type _Up;
1545 return unique_ptr<_Tp>(new _Up[__n]());
1546}
1547
1548template<class _Tp, class... _Args>
1549 typename __unique_if<_Tp>::__unique_array_known_bound
1550 make_unique(_Args&&...) = delete;
1551
1552#endif // _LIBCPP_STD_VER > 11
1553
Howard Hinnant36b31ae2010-06-03 16:42:57 +00001554template <class _Tp, class _Dp>
Eric Fiselier698a97b2017-01-21 00:02:12 +00001555#ifdef _LIBCPP_CXX03_LANG
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001556struct _LIBCPP_TEMPLATE_VIS hash<unique_ptr<_Tp, _Dp> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00001557#else
1558struct _LIBCPP_TEMPLATE_VIS hash<__enable_hash_helper<
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001559 unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00001560#endif
Howard Hinnant36b31ae2010-06-03 16:42:57 +00001561{
1562 typedef unique_ptr<_Tp, _Dp> argument_type;
1563 typedef size_t result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00001564 _LIBCPP_INLINE_VISIBILITY
Marshall Clow49036892017-03-23 02:40:28 +00001565 result_type operator()(const argument_type& __ptr) const
Howard Hinnant36b31ae2010-06-03 16:42:57 +00001566 {
1567 typedef typename argument_type::pointer pointer;
1568 return hash<pointer>()(__ptr.get());
1569 }
1570};
1571
Howard Hinnantc51e1022010-05-11 19:42:16 +00001572struct __destruct_n
1573{
1574private:
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001575 size_t __size_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001576
1577 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00001578 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001579 {for (size_t __i = 0; __i < __size_; ++__i, ++__p) __p->~_Tp();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001580
1581 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00001582 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001583 {}
1584
Howard Hinnant719bda32011-05-28 14:41:13 +00001585 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001586 {++__size_;}
Howard Hinnant719bda32011-05-28 14:41:13 +00001587 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001588 {}
1589
Howard Hinnant719bda32011-05-28 14:41:13 +00001590 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001591 {__size_ = __s;}
Howard Hinnant719bda32011-05-28 14:41:13 +00001592 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001593 {}
1594public:
Howard Hinnant719bda32011-05-28 14:41:13 +00001595 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001596 : __size_(__s) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001597
1598 template <class _Tp>
Bruce Mitchener170d8972020-11-24 12:53:53 -05001599 _LIBCPP_INLINE_VISIBILITY void __incr() _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00001600 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001601
1602 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00001603 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00001604 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001605
1606 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00001607 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00001608 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001609};
1610
1611template <class _Alloc>
1612class __allocator_destructor
1613{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001614 typedef _LIBCPP_NODEBUG_TYPE allocator_traits<_Alloc> __alloc_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001615public:
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001616 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::pointer pointer;
1617 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::size_type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001618private:
1619 _Alloc& __alloc_;
1620 size_type __s_;
1621public:
1622 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant719bda32011-05-28 14:41:13 +00001623 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001624 : __alloc_(__a), __s_(__s) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001625 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001626 void operator()(pointer __p) _NOEXCEPT
1627 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001628};
1629
1630template <class _InputIterator, class _ForwardIterator>
1631_ForwardIterator
1632uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
1633{
Howard Hinnantc51e1022010-05-11 19:42:16 +00001634 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00001635#ifndef _LIBCPP_NO_EXCEPTIONS
1636 _ForwardIterator __s = __r;
1637 try
1638 {
1639#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00001640 for (; __f != __l; ++__f, (void) ++__r)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001641 ::new ((void*)_VSTD::addressof(*__r)) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00001642#ifndef _LIBCPP_NO_EXCEPTIONS
1643 }
1644 catch (...)
1645 {
1646 for (; __s != __r; ++__s)
1647 __s->~value_type();
1648 throw;
1649 }
1650#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001651 return __r;
1652}
1653
1654template <class _InputIterator, class _Size, class _ForwardIterator>
1655_ForwardIterator
1656uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
1657{
Howard Hinnantc51e1022010-05-11 19:42:16 +00001658 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00001659#ifndef _LIBCPP_NO_EXCEPTIONS
1660 _ForwardIterator __s = __r;
1661 try
1662 {
1663#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00001664 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001665 ::new ((void*)_VSTD::addressof(*__r)) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00001666#ifndef _LIBCPP_NO_EXCEPTIONS
1667 }
1668 catch (...)
1669 {
1670 for (; __s != __r; ++__s)
1671 __s->~value_type();
1672 throw;
1673 }
1674#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001675 return __r;
1676}
1677
1678template <class _ForwardIterator, class _Tp>
1679void
1680uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
1681{
Howard Hinnantc51e1022010-05-11 19:42:16 +00001682 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00001683#ifndef _LIBCPP_NO_EXCEPTIONS
1684 _ForwardIterator __s = __f;
1685 try
1686 {
1687#endif
1688 for (; __f != __l; ++__f)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001689 ::new ((void*)_VSTD::addressof(*__f)) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00001690#ifndef _LIBCPP_NO_EXCEPTIONS
1691 }
1692 catch (...)
1693 {
1694 for (; __s != __f; ++__s)
1695 __s->~value_type();
1696 throw;
1697 }
1698#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001699}
1700
1701template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant3c811092010-11-18 16:13:03 +00001702_ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001703uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
1704{
Howard Hinnantc51e1022010-05-11 19:42:16 +00001705 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00001706#ifndef _LIBCPP_NO_EXCEPTIONS
1707 _ForwardIterator __s = __f;
1708 try
1709 {
1710#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00001711 for (; __n > 0; ++__f, (void) --__n)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001712 ::new ((void*)_VSTD::addressof(*__f)) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00001713#ifndef _LIBCPP_NO_EXCEPTIONS
1714 }
1715 catch (...)
1716 {
1717 for (; __s != __f; ++__s)
1718 __s->~value_type();
1719 throw;
1720 }
1721#endif
Howard Hinnant3c811092010-11-18 16:13:03 +00001722 return __f;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001723}
1724
Eric Fiselier383f6cf2016-07-24 03:51:39 +00001725#if _LIBCPP_STD_VER > 14
1726
Eric Fiselier383f6cf2016-07-24 03:51:39 +00001727template <class _ForwardIterator>
Louis Dionne99f59432020-09-17 12:06:13 -04001728inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier383f6cf2016-07-24 03:51:39 +00001729void destroy(_ForwardIterator __first, _ForwardIterator __last) {
1730 for (; __first != __last; ++__first)
1731 _VSTD::destroy_at(_VSTD::addressof(*__first));
1732}
1733
1734template <class _ForwardIterator, class _Size>
Louis Dionne99f59432020-09-17 12:06:13 -04001735inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier383f6cf2016-07-24 03:51:39 +00001736_ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
1737 for (; __n > 0; (void)++__first, --__n)
1738 _VSTD::destroy_at(_VSTD::addressof(*__first));
1739 return __first;
1740}
1741
Eric Fiselier290c07c2016-10-11 21:13:44 +00001742template <class _ForwardIterator>
1743inline _LIBCPP_INLINE_VISIBILITY
1744void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) {
1745 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
1746 auto __idx = __first;
1747#ifndef _LIBCPP_NO_EXCEPTIONS
1748 try {
1749#endif
1750 for (; __idx != __last; ++__idx)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001751 ::new ((void*)_VSTD::addressof(*__idx)) _Vt;
Eric Fiselier290c07c2016-10-11 21:13:44 +00001752#ifndef _LIBCPP_NO_EXCEPTIONS
1753 } catch (...) {
1754 _VSTD::destroy(__first, __idx);
1755 throw;
1756 }
1757#endif
1758}
1759
1760template <class _ForwardIterator, class _Size>
1761inline _LIBCPP_INLINE_VISIBILITY
1762_ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
1763 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
1764 auto __idx = __first;
1765#ifndef _LIBCPP_NO_EXCEPTIONS
1766 try {
1767#endif
1768 for (; __n > 0; (void)++__idx, --__n)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001769 ::new ((void*)_VSTD::addressof(*__idx)) _Vt;
Eric Fiselier290c07c2016-10-11 21:13:44 +00001770 return __idx;
1771#ifndef _LIBCPP_NO_EXCEPTIONS
1772 } catch (...) {
1773 _VSTD::destroy(__first, __idx);
1774 throw;
1775 }
1776#endif
1777}
1778
1779
1780template <class _ForwardIterator>
1781inline _LIBCPP_INLINE_VISIBILITY
1782void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) {
1783 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
1784 auto __idx = __first;
1785#ifndef _LIBCPP_NO_EXCEPTIONS
1786 try {
1787#endif
1788 for (; __idx != __last; ++__idx)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001789 ::new ((void*)_VSTD::addressof(*__idx)) _Vt();
Eric Fiselier290c07c2016-10-11 21:13:44 +00001790#ifndef _LIBCPP_NO_EXCEPTIONS
1791 } catch (...) {
1792 _VSTD::destroy(__first, __idx);
1793 throw;
1794 }
1795#endif
1796}
1797
1798template <class _ForwardIterator, class _Size>
1799inline _LIBCPP_INLINE_VISIBILITY
1800_ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
1801 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
1802 auto __idx = __first;
1803#ifndef _LIBCPP_NO_EXCEPTIONS
1804 try {
1805#endif
1806 for (; __n > 0; (void)++__idx, --__n)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001807 ::new ((void*)_VSTD::addressof(*__idx)) _Vt();
Eric Fiselier290c07c2016-10-11 21:13:44 +00001808 return __idx;
1809#ifndef _LIBCPP_NO_EXCEPTIONS
1810 } catch (...) {
1811 _VSTD::destroy(__first, __idx);
1812 throw;
1813 }
1814#endif
1815}
1816
1817
1818template <class _InputIt, class _ForwardIt>
1819inline _LIBCPP_INLINE_VISIBILITY
1820_ForwardIt uninitialized_move(_InputIt __first, _InputIt __last, _ForwardIt __first_res) {
1821 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
1822 auto __idx = __first_res;
1823#ifndef _LIBCPP_NO_EXCEPTIONS
1824 try {
1825#endif
1826 for (; __first != __last; (void)++__idx, ++__first)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001827 ::new ((void*)_VSTD::addressof(*__idx)) _Vt(_VSTD::move(*__first));
Eric Fiselier290c07c2016-10-11 21:13:44 +00001828 return __idx;
1829#ifndef _LIBCPP_NO_EXCEPTIONS
1830 } catch (...) {
1831 _VSTD::destroy(__first_res, __idx);
1832 throw;
1833 }
1834#endif
1835}
1836
1837template <class _InputIt, class _Size, class _ForwardIt>
1838inline _LIBCPP_INLINE_VISIBILITY
1839pair<_InputIt, _ForwardIt>
1840uninitialized_move_n(_InputIt __first, _Size __n, _ForwardIt __first_res) {
1841 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
1842 auto __idx = __first_res;
1843#ifndef _LIBCPP_NO_EXCEPTIONS
1844 try {
1845#endif
1846 for (; __n > 0; ++__idx, (void)++__first, --__n)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001847 ::new ((void*)_VSTD::addressof(*__idx)) _Vt(_VSTD::move(*__first));
Eric Fiselier290c07c2016-10-11 21:13:44 +00001848 return {__first, __idx};
1849#ifndef _LIBCPP_NO_EXCEPTIONS
1850 } catch (...) {
1851 _VSTD::destroy(__first_res, __idx);
1852 throw;
1853 }
1854#endif
1855}
1856
1857
Eric Fiselier383f6cf2016-07-24 03:51:39 +00001858#endif // _LIBCPP_STD_VER > 14
1859
Kevin Hu4bdc8a02017-01-17 02:46:33 +00001860// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
1861// should be sufficient for thread safety.
Louis Dionne38437402021-03-03 13:45:06 -05001862// See https://llvm.org/PR22803
Kevin Hu4bdc8a02017-01-17 02:46:33 +00001863#if defined(__clang__) && __has_builtin(__atomic_add_fetch) \
1864 && defined(__ATOMIC_RELAXED) \
1865 && defined(__ATOMIC_ACQ_REL)
1866# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
Richard Smithca47d0f2019-04-25 20:02:10 +00001867#elif defined(_LIBCPP_COMPILER_GCC)
Kevin Hu4bdc8a02017-01-17 02:46:33 +00001868# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
1869#endif
1870
1871template <class _Tp>
1872inline _LIBCPP_INLINE_VISIBILITY _Tp
1873__libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT
1874{
1875#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
1876 return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
1877#else
1878 return __t += 1;
1879#endif
1880}
1881
1882template <class _Tp>
1883inline _LIBCPP_INLINE_VISIBILITY _Tp
1884__libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT
1885{
1886#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
1887 return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
1888#else
1889 return __t -= 1;
1890#endif
1891}
1892
Howard Hinnant756c69b2010-09-22 16:48:34 +00001893class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00001894 : public std::exception
1895{
1896public:
Dimitry Andric47269ce2020-03-13 19:36:26 +01001897 bad_weak_ptr() _NOEXCEPT = default;
1898 bad_weak_ptr(const bad_weak_ptr&) _NOEXCEPT = default;
Howard Hinnant719bda32011-05-28 14:41:13 +00001899 virtual ~bad_weak_ptr() _NOEXCEPT;
1900 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001901};
1902
Louis Dionne16fe2952018-07-11 23:14:33 +00001903_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +00001904void __throw_bad_weak_ptr()
1905{
1906#ifndef _LIBCPP_NO_EXCEPTIONS
1907 throw bad_weak_ptr();
1908#else
1909 _VSTD::abort();
1910#endif
1911}
1912
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001913template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001914
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00001915class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00001916{
1917 __shared_count(const __shared_count&);
1918 __shared_count& operator=(const __shared_count&);
1919
1920protected:
1921 long __shared_owners_;
1922 virtual ~__shared_count();
1923private:
Howard Hinnant719bda32011-05-28 14:41:13 +00001924 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001925
1926public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00001927 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001928 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001929 : __shared_owners_(__refs) {}
1930
Louis Dionne5e0eadd2018-08-01 02:08:59 +00001931#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00001932 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00001933 void __add_shared() _NOEXCEPT;
1934 bool __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00001935#else
1936 _LIBCPP_INLINE_VISIBILITY
1937 void __add_shared() _NOEXCEPT {
1938 __libcpp_atomic_refcount_increment(__shared_owners_);
1939 }
1940 _LIBCPP_INLINE_VISIBILITY
1941 bool __release_shared() _NOEXCEPT {
1942 if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
1943 __on_zero_shared();
1944 return true;
1945 }
1946 return false;
1947 }
1948#endif
Howard Hinnant756c69b2010-09-22 16:48:34 +00001949 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +00001950 long use_count() const _NOEXCEPT {
1951 return __libcpp_relaxed_load(&__shared_owners_) + 1;
1952 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001953};
1954
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00001955class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00001956 : private __shared_count
1957{
1958 long __shared_weak_owners_;
1959
1960public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00001961 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001962 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001963 : __shared_count(__refs),
1964 __shared_weak_owners_(__refs) {}
1965protected:
1966 virtual ~__shared_weak_count();
1967
1968public:
Louis Dionne5e0eadd2018-08-01 02:08:59 +00001969#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00001970 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00001971 void __add_shared() _NOEXCEPT;
1972 void __add_weak() _NOEXCEPT;
1973 void __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00001974#else
1975 _LIBCPP_INLINE_VISIBILITY
1976 void __add_shared() _NOEXCEPT {
1977 __shared_count::__add_shared();
1978 }
1979 _LIBCPP_INLINE_VISIBILITY
1980 void __add_weak() _NOEXCEPT {
1981 __libcpp_atomic_refcount_increment(__shared_weak_owners_);
1982 }
1983 _LIBCPP_INLINE_VISIBILITY
1984 void __release_shared() _NOEXCEPT {
1985 if (__shared_count::__release_shared())
1986 __release_weak();
1987 }
1988#endif
Howard Hinnant719bda32011-05-28 14:41:13 +00001989 void __release_weak() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00001990 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001991 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
1992 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001993
Howard Hinnant719bda32011-05-28 14:41:13 +00001994 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001995private:
Howard Hinnant719bda32011-05-28 14:41:13 +00001996 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001997};
1998
1999template <class _Tp, class _Dp, class _Alloc>
2000class __shared_ptr_pointer
2001 : public __shared_weak_count
2002{
2003 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
2004public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00002005 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002006 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002007 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002008
Howard Hinnant72f73582010-08-11 17:04:31 +00002009#ifndef _LIBCPP_NO_RTTI
Howard Hinnant719bda32011-05-28 14:41:13 +00002010 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant72f73582010-08-11 17:04:31 +00002011#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002012
2013private:
Howard Hinnant719bda32011-05-28 14:41:13 +00002014 virtual void __on_zero_shared() _NOEXCEPT;
2015 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002016};
2017
Howard Hinnant72f73582010-08-11 17:04:31 +00002018#ifndef _LIBCPP_NO_RTTI
2019
Howard Hinnantc51e1022010-05-11 19:42:16 +00002020template <class _Tp, class _Dp, class _Alloc>
2021const void*
Howard Hinnant719bda32011-05-28 14:41:13 +00002022__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002023{
Eric Fiselierc7490d02017-05-04 01:06:56 +00002024 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002025}
2026
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002027#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00002028
Howard Hinnantc51e1022010-05-11 19:42:16 +00002029template <class _Tp, class _Dp, class _Alloc>
2030void
Howard Hinnant719bda32011-05-28 14:41:13 +00002031__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002032{
2033 __data_.first().second()(__data_.first().first());
2034 __data_.first().second().~_Dp();
2035}
2036
2037template <class _Tp, class _Dp, class _Alloc>
2038void
Howard Hinnant719bda32011-05-28 14:41:13 +00002039__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002040{
Eric Fiselierf8898c82015-02-05 23:01:40 +00002041 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
2042 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00002043 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
2044
Eric Fiselierf8898c82015-02-05 23:01:40 +00002045 _Al __a(__data_.second());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002046 __data_.second().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00002047 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002048}
2049
2050template <class _Tp, class _Alloc>
Louis Dionne86549d72020-12-09 16:22:17 -05002051struct __shared_ptr_emplace
2052 : __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00002053{
Louis Dionneda463cb2020-12-11 12:22:16 -05002054 template<class ..._Args>
Louis Dionne86549d72020-12-09 16:22:17 -05002055 _LIBCPP_HIDE_FROM_ABI
2056 explicit __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Louis Dionneda463cb2020-12-11 12:22:16 -05002057 : __storage_(_VSTD::move(__a))
2058 {
2059#if _LIBCPP_STD_VER > 17
2060 using _TpAlloc = typename __allocator_traits_rebind<_Alloc, _Tp>::type;
2061 _TpAlloc __tmp(*__get_alloc());
2062 allocator_traits<_TpAlloc>::construct(__tmp, __get_elem(), _VSTD::forward<_Args>(__args)...);
Louis Dionne1a1fc9d2020-07-30 10:00:53 -04002063#else
Louis Dionneda463cb2020-12-11 12:22:16 -05002064 ::new ((void*)__get_elem()) _Tp(_VSTD::forward<_Args>(__args)...);
Louis Dionne1a1fc9d2020-07-30 10:00:53 -04002065#endif
Louis Dionneda463cb2020-12-11 12:22:16 -05002066 }
Louis Dionne86549d72020-12-09 16:22:17 -05002067
2068 _LIBCPP_HIDE_FROM_ABI
Louis Dionneda463cb2020-12-11 12:22:16 -05002069 _Alloc* __get_alloc() _NOEXCEPT { return __storage_.__get_alloc(); }
Louis Dionne86549d72020-12-09 16:22:17 -05002070
2071 _LIBCPP_HIDE_FROM_ABI
Louis Dionneda463cb2020-12-11 12:22:16 -05002072 _Tp* __get_elem() _NOEXCEPT { return __storage_.__get_elem(); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002073
2074private:
Louis Dionne86549d72020-12-09 16:22:17 -05002075 virtual void __on_zero_shared() _NOEXCEPT {
Louis Dionneda463cb2020-12-11 12:22:16 -05002076#if _LIBCPP_STD_VER > 17
2077 using _TpAlloc = typename __allocator_traits_rebind<_Alloc, _Tp>::type;
2078 _TpAlloc __tmp(*__get_alloc());
2079 allocator_traits<_TpAlloc>::destroy(__tmp, __get_elem());
2080#else
Louis Dionne86549d72020-12-09 16:22:17 -05002081 __get_elem()->~_Tp();
Louis Dionneda463cb2020-12-11 12:22:16 -05002082#endif
Louis Dionne86549d72020-12-09 16:22:17 -05002083 }
2084
2085 virtual void __on_zero_shared_weak() _NOEXCEPT {
2086 using _ControlBlockAlloc = typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type;
2087 using _ControlBlockPointer = typename allocator_traits<_ControlBlockAlloc>::pointer;
Louis Dionneca117a22020-12-14 17:40:56 -05002088 _ControlBlockAlloc __tmp(*__get_alloc());
Louis Dionneda463cb2020-12-11 12:22:16 -05002089 __storage_.~_Storage();
Louis Dionne86549d72020-12-09 16:22:17 -05002090 allocator_traits<_ControlBlockAlloc>::deallocate(__tmp,
2091 pointer_traits<_ControlBlockPointer>::pointer_to(*this), 1);
2092 }
2093
Louis Dionneda463cb2020-12-11 12:22:16 -05002094 // This class implements the control block for non-array shared pointers created
2095 // through `std::allocate_shared` and `std::make_shared`.
2096 //
2097 // In previous versions of the library, we used a compressed pair to store
2098 // both the _Alloc and the _Tp. This implies using EBO, which is incompatible
2099 // with Allocator construction for _Tp. To allow implementing P0674 in C++20,
2100 // we now use a properly aligned char buffer while making sure that we maintain
2101 // the same layout that we had when we used a compressed pair.
2102 using _CompressedPair = __compressed_pair<_Alloc, _Tp>;
2103 struct _ALIGNAS_TYPE(_CompressedPair) _Storage {
2104 char __blob_[sizeof(_CompressedPair)];
2105
2106 _LIBCPP_HIDE_FROM_ABI explicit _Storage(_Alloc&& __a) {
2107 ::new ((void*)__get_alloc()) _Alloc(_VSTD::move(__a));
2108 }
2109 _LIBCPP_HIDE_FROM_ABI ~_Storage() {
2110 __get_alloc()->~_Alloc();
2111 }
2112 _Alloc* __get_alloc() _NOEXCEPT {
2113 _CompressedPair *__as_pair = reinterpret_cast<_CompressedPair*>(__blob_);
2114 typename _CompressedPair::_Base1* __first = _CompressedPair::__get_first_base(__as_pair);
2115 _Alloc *__alloc = reinterpret_cast<_Alloc*>(__first);
2116 return __alloc;
2117 }
Reid Klecknera43e87e2021-02-01 15:18:42 -08002118 _LIBCPP_NO_CFI _Tp* __get_elem() _NOEXCEPT {
Louis Dionneda463cb2020-12-11 12:22:16 -05002119 _CompressedPair *__as_pair = reinterpret_cast<_CompressedPair*>(__blob_);
2120 typename _CompressedPair::_Base2* __second = _CompressedPair::__get_second_base(__as_pair);
2121 _Tp *__elem = reinterpret_cast<_Tp*>(__second);
2122 return __elem;
2123 }
2124 };
2125
2126 static_assert(_LIBCPP_ALIGNOF(_Storage) == _LIBCPP_ALIGNOF(_CompressedPair), "");
2127 static_assert(sizeof(_Storage) == sizeof(_CompressedPair), "");
2128 _Storage __storage_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002129};
2130
Erik Pilkington2a398762017-05-25 15:43:31 +00002131struct __shared_ptr_dummy_rebind_allocator_type;
2132template <>
2133class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type>
2134{
2135public:
2136 template <class _Other>
2137 struct rebind
2138 {
2139 typedef allocator<_Other> other;
2140 };
2141};
2142
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002143template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002144
zoecarverd73f42a2020-05-11 18:42:50 -07002145template<class _Tp, class _Up>
2146struct __compatible_with
2147#if _LIBCPP_STD_VER > 14
2148 : is_convertible<remove_extent_t<_Tp>*, remove_extent_t<_Up>*> {};
2149#else
2150 : is_convertible<_Tp*, _Up*> {};
2151#endif // _LIBCPP_STD_VER > 14
2152
zoecarver9bc39102021-02-19 11:10:36 -08002153template <class _Dp, class _Pt,
2154 class = decltype(_VSTD::declval<_Dp>()(_VSTD::declval<_Pt>()))>
2155static true_type __well_formed_deleter_test(int);
2156
2157template <class, class>
2158static false_type __well_formed_deleter_test(...);
2159
2160template <class _Dp, class _Pt>
2161struct __well_formed_deleter : decltype(__well_formed_deleter_test<_Dp, _Pt>(0)) {};
2162
2163template<class _Dp, class _Tp, class _Yp>
2164struct __shared_ptr_deleter_ctor_reqs
2165{
2166 static const bool value = __compatible_with<_Tp, _Yp>::value &&
2167 is_move_constructible<_Dp>::value &&
2168 __well_formed_deleter<_Dp, _Tp*>::value;
2169};
2170
Vy Nguyene369bd92020-07-13 12:34:37 -04002171#if defined(_LIBCPP_ABI_ENABLE_SHARED_PTR_TRIVIAL_ABI)
2172# define _LIBCPP_SHARED_PTR_TRIVIAL_ABI __attribute__((trivial_abi))
2173#else
2174# define _LIBCPP_SHARED_PTR_TRIVIAL_ABI
2175#endif
2176
Howard Hinnantc51e1022010-05-11 19:42:16 +00002177template<class _Tp>
Vy Nguyene369bd92020-07-13 12:34:37 -04002178class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS shared_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002179{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002180public:
Eric Fiselierae5b6672016-06-27 01:02:43 +00002181#if _LIBCPP_STD_VER > 14
2182 typedef weak_ptr<_Tp> weak_type;
zoecarverd73f42a2020-05-11 18:42:50 -07002183 typedef remove_extent_t<_Tp> element_type;
2184#else
2185 typedef _Tp element_type;
Eric Fiselierae5b6672016-06-27 01:02:43 +00002186#endif
zoecarverd73f42a2020-05-11 18:42:50 -07002187
Howard Hinnantc51e1022010-05-11 19:42:16 +00002188private:
2189 element_type* __ptr_;
2190 __shared_weak_count* __cntrl_;
2191
2192 struct __nat {int __for_bool_;};
2193public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002194 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002195 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002196 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002197 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiend435f8b2014-01-31 09:30:46 +00002198 template<class _Yp>
2199 explicit shared_ptr(_Yp* __p,
zoecarverd73f42a2020-05-11 18:42:50 -07002200 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
Logan Chiend435f8b2014-01-31 09:30:46 +00002201 template<class _Yp, class _Dp>
2202 shared_ptr(_Yp* __p, _Dp __d,
zoecarver9bc39102021-02-19 11:10:36 -08002203 typename enable_if<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, element_type>::value, __nat>::type = __nat());
Logan Chiend435f8b2014-01-31 09:30:46 +00002204 template<class _Yp, class _Dp, class _Alloc>
2205 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
zoecarver9bc39102021-02-19 11:10:36 -08002206 typename enable_if<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, element_type>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002207 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
2208 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002209 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
2210 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002211 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002212 template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002213 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002214 shared_ptr(const shared_ptr<_Yp>& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07002215 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00002216 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002217 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002218 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002219 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07002220 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00002221 _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002222 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00002223 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type= __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00002224#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Logan Chiend435f8b2014-01-31 09:30:46 +00002225 template<class _Yp>
2226 shared_ptr(auto_ptr<_Yp>&& __r,
2227 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00002228#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00002229 template <class _Yp, class _Dp>
2230 shared_ptr(unique_ptr<_Yp, _Dp>&&,
2231 typename enable_if
2232 <
2233 !is_lvalue_reference<_Dp>::value &&
Logan Chiend435f8b2014-01-31 09:30:46 +00002234 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
2235 __nat
2236 >::type = __nat());
2237 template <class _Yp, class _Dp>
2238 shared_ptr(unique_ptr<_Yp, _Dp>&&,
2239 typename enable_if
2240 <
2241 is_lvalue_reference<_Dp>::value &&
Logan Chiend435f8b2014-01-31 09:30:46 +00002242 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
2243 __nat
2244 >::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002245
2246 ~shared_ptr();
2247
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002248 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002249 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002250 template<class _Yp>
2251 typename enable_if
2252 <
zoecarverd73f42a2020-05-11 18:42:50 -07002253 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002254 shared_ptr&
2255 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002256 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002257 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002258 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002259 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002260 template<class _Yp>
2261 typename enable_if
2262 <
zoecarverd73f42a2020-05-11 18:42:50 -07002263 __compatible_with<_Yp, element_type>::value,
2264 shared_ptr&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002265 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002266 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002267 operator=(shared_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00002268#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002269 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00002270 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002271 typename enable_if
2272 <
2273 !is_array<_Yp>::value &&
2274 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00002275 shared_ptr
2276 >::type&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002277 operator=(auto_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00002278#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002279 template <class _Yp, class _Dp>
2280 typename enable_if
2281 <
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002282 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
2283 shared_ptr&
2284 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002285 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002286 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002287
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002288 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002289 void swap(shared_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002290 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002291 void reset() _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002292 template<class _Yp>
2293 typename enable_if
2294 <
zoecarverd73f42a2020-05-11 18:42:50 -07002295 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002296 void
2297 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002298 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002299 reset(_Yp* __p);
2300 template<class _Yp, class _Dp>
2301 typename enable_if
2302 <
zoecarverd73f42a2020-05-11 18:42:50 -07002303 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002304 void
2305 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002306 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002307 reset(_Yp* __p, _Dp __d);
2308 template<class _Yp, class _Dp, class _Alloc>
2309 typename enable_if
2310 <
zoecarverd73f42a2020-05-11 18:42:50 -07002311 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002312 void
2313 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002314 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002315 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002316
Howard Hinnant756c69b2010-09-22 16:48:34 +00002317 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002318 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002319 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002320 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
2321 {return *__ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002322 _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07002323 element_type* operator->() const _NOEXCEPT
2324 {
2325 static_assert(!_VSTD::is_array<_Tp>::value,
2326 "std::shared_ptr<T>::operator-> is only valid when T is not an array type.");
2327 return __ptr_;
2328 }
Howard Hinnant756c69b2010-09-22 16:48:34 +00002329 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002330 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002331 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002332 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002333 _LIBCPP_INLINE_VISIBILITY
Bruce Mitchener170d8972020-11-24 12:53:53 -05002334 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != nullptr;}
Howard Hinnantc834c512011-11-29 18:15:50 +00002335 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00002336 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00002337 bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002338 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnantc834c512011-11-29 18:15:50 +00002339 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00002340 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00002341 bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002342 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant9fa30202012-07-30 01:40:57 +00002343 _LIBCPP_INLINE_VISIBILITY
2344 bool
2345 __owner_equivalent(const shared_ptr& __p) const
2346 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002347
zoecarverd73f42a2020-05-11 18:42:50 -07002348#if _LIBCPP_STD_VER > 14
2349 typename add_lvalue_reference<element_type>::type
2350 _LIBCPP_INLINE_VISIBILITY
2351 operator[](ptrdiff_t __i) const
2352 {
2353 static_assert(_VSTD::is_array<_Tp>::value,
2354 "std::shared_ptr<T>::operator[] is only valid when T is an array type.");
2355 return __ptr_[__i];
2356 }
2357#endif
2358
Howard Hinnant72f73582010-08-11 17:04:31 +00002359#ifndef _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00002360 template <class _Dp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00002361 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002362 _Dp* __get_deleter() const _NOEXCEPT
Marshall Clow31350ab2017-06-14 16:54:43 +00002363 {return static_cast<_Dp*>(__cntrl_
Aditya Kumar7c5db692017-08-20 10:38:55 +00002364 ? const_cast<void *>(__cntrl_->__get_deleter(typeid(_Dp)))
Marshall Clow31350ab2017-06-14 16:54:43 +00002365 : nullptr);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002366#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00002367
Zoe Carverd9040c72019-10-22 15:16:49 +00002368 template<class _Yp, class _CntrlBlk>
2369 static shared_ptr<_Tp>
zoecarver867d3112020-05-19 17:17:16 -07002370 __create_with_control_block(_Yp* __p, _CntrlBlk* __cntrl) _NOEXCEPT
Zoe Carverd9040c72019-10-22 15:16:49 +00002371 {
2372 shared_ptr<_Tp> __r;
2373 __r.__ptr_ = __p;
2374 __r.__cntrl_ = __cntrl;
2375 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
2376 return __r;
2377 }
Zoe Carver6cd05c32019-08-19 15:47:16 +00002378
Howard Hinnantc51e1022010-05-11 19:42:16 +00002379private:
Erik Pilkington2a398762017-05-25 15:43:31 +00002380 template <class _Yp, bool = is_function<_Yp>::value>
2381 struct __shared_ptr_default_allocator
2382 {
2383 typedef allocator<_Yp> type;
2384 };
2385
2386 template <class _Yp>
2387 struct __shared_ptr_default_allocator<_Yp, true>
2388 {
2389 typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type;
2390 };
Howard Hinnantc51e1022010-05-11 19:42:16 +00002391
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002392 template <class _Yp, class _OrigPtr>
Howard Hinnant756c69b2010-09-22 16:48:34 +00002393 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier30d5ac62017-05-10 19:35:49 +00002394 typename enable_if<is_convertible<_OrigPtr*,
2395 const enable_shared_from_this<_Yp>*
2396 >::value,
2397 void>::type
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002398 __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
2399 _OrigPtr* __ptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002400 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002401 typedef typename remove_cv<_Yp>::type _RawYp;
Eric Fiselier84006862016-06-02 00:15:35 +00002402 if (__e && __e->__weak_this_.expired())
Marshall Clow99442fc2015-06-19 15:54:13 +00002403 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002404 __e->__weak_this_ = shared_ptr<_RawYp>(*this,
2405 const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
Marshall Clow99442fc2015-06-19 15:54:13 +00002406 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002407 }
2408
Erik Pilkington2a398762017-05-25 15:43:31 +00002409 _LIBCPP_INLINE_VISIBILITY void __enable_weak_this(...) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002410
zoecarverd73f42a2020-05-11 18:42:50 -07002411 template <class, class _Yp>
2412 struct __shared_ptr_default_delete
2413 : default_delete<_Yp> {};
2414
2415 template <class _Yp, class _Un, size_t _Sz>
2416 struct __shared_ptr_default_delete<_Yp[_Sz], _Un>
2417 : default_delete<_Yp[]> {};
2418
2419 template <class _Yp, class _Un>
2420 struct __shared_ptr_default_delete<_Yp[], _Un>
2421 : default_delete<_Yp[]> {};
2422
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002423 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
2424 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002425};
2426
Logan Smitha5e4d7e2020-05-07 12:07:01 -04002427#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
2428template<class _Tp>
2429shared_ptr(weak_ptr<_Tp>) -> shared_ptr<_Tp>;
2430template<class _Tp, class _Dp>
2431shared_ptr(unique_ptr<_Tp, _Dp>) -> shared_ptr<_Tp>;
2432#endif
Eric Fiselier30d5ac62017-05-10 19:35:49 +00002433
Howard Hinnantc51e1022010-05-11 19:42:16 +00002434template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002435inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002436_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00002437shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05002438 : __ptr_(nullptr),
2439 __cntrl_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002440{
2441}
2442
2443template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002444inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002445_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00002446shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05002447 : __ptr_(nullptr),
2448 __cntrl_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002449{
2450}
2451
2452template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00002453template<class _Yp>
2454shared_ptr<_Tp>::shared_ptr(_Yp* __p,
zoecarverd73f42a2020-05-11 18:42:50 -07002455 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002456 : __ptr_(__p)
2457{
2458 unique_ptr<_Yp> __hold(__p);
Erik Pilkington2a398762017-05-25 15:43:31 +00002459 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
zoecarverd73f42a2020-05-11 18:42:50 -07002460 typedef __shared_ptr_pointer<_Yp*, __shared_ptr_default_delete<_Tp, _Yp>, _AllocT > _CntrlBlk;
2461 __cntrl_ = new _CntrlBlk(__p, __shared_ptr_default_delete<_Tp, _Yp>(), _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002462 __hold.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002463 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002464}
2465
2466template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00002467template<class _Yp, class _Dp>
2468shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
zoecarver9bc39102021-02-19 11:10:36 -08002469 typename enable_if<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002470 : __ptr_(__p)
2471{
2472#ifndef _LIBCPP_NO_EXCEPTIONS
2473 try
2474 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002475#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00002476 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
2477 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
zoecarverbec93cf2021-02-18 21:31:07 -08002478#ifndef _LIBCPP_CXX03_LANG
2479 __cntrl_ = new _CntrlBlk(__p, _VSTD::move(__d), _AllocT());
2480#else
Erik Pilkington2a398762017-05-25 15:43:31 +00002481 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
zoecarverbec93cf2021-02-18 21:31:07 -08002482#endif // not _LIBCPP_CXX03_LANG
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002483 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002484#ifndef _LIBCPP_NO_EXCEPTIONS
2485 }
2486 catch (...)
2487 {
2488 __d(__p);
2489 throw;
2490 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002491#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002492}
2493
2494template<class _Tp>
2495template<class _Dp>
2496shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
Bruce Mitchener170d8972020-11-24 12:53:53 -05002497 : __ptr_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002498{
2499#ifndef _LIBCPP_NO_EXCEPTIONS
2500 try
2501 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002502#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00002503 typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
2504 typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT > _CntrlBlk;
zoecarverbec93cf2021-02-18 21:31:07 -08002505#ifndef _LIBCPP_CXX03_LANG
2506 __cntrl_ = new _CntrlBlk(__p, _VSTD::move(__d), _AllocT());
2507#else
Erik Pilkington2a398762017-05-25 15:43:31 +00002508 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
zoecarverbec93cf2021-02-18 21:31:07 -08002509#endif // not _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002510#ifndef _LIBCPP_NO_EXCEPTIONS
2511 }
2512 catch (...)
2513 {
2514 __d(__p);
2515 throw;
2516 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002517#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002518}
2519
2520template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00002521template<class _Yp, class _Dp, class _Alloc>
2522shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
zoecarver9bc39102021-02-19 11:10:36 -08002523 typename enable_if<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002524 : __ptr_(__p)
2525{
2526#ifndef _LIBCPP_NO_EXCEPTIONS
2527 try
2528 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002529#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002530 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00002531 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002532 typedef __allocator_destructor<_A2> _D2;
2533 _A2 __a2(__a);
2534 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
zoecarverbec93cf2021-02-18 21:31:07 -08002535 ::new ((void*)_VSTD::addressof(*__hold2.get()))
2536#ifndef _LIBCPP_CXX03_LANG
2537 _CntrlBlk(__p, _VSTD::move(__d), __a);
2538#else
2539 _CntrlBlk(__p, __d, __a);
2540#endif // not _LIBCPP_CXX03_LANG
Eric Fiselier6bd814f2014-10-23 04:12:28 +00002541 __cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002542 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002543#ifndef _LIBCPP_NO_EXCEPTIONS
2544 }
2545 catch (...)
2546 {
2547 __d(__p);
2548 throw;
2549 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002550#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002551}
2552
2553template<class _Tp>
2554template<class _Dp, class _Alloc>
2555shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
Bruce Mitchener170d8972020-11-24 12:53:53 -05002556 : __ptr_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002557{
2558#ifndef _LIBCPP_NO_EXCEPTIONS
2559 try
2560 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002561#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002562 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00002563 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002564 typedef __allocator_destructor<_A2> _D2;
2565 _A2 __a2(__a);
2566 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
zoecarverbec93cf2021-02-18 21:31:07 -08002567 ::new ((void*)_VSTD::addressof(*__hold2.get()))
2568#ifndef _LIBCPP_CXX03_LANG
2569 _CntrlBlk(__p, _VSTD::move(__d), __a);
2570#else
2571 _CntrlBlk(__p, __d, __a);
2572#endif // not _LIBCPP_CXX03_LANG
Eric Fiselier6bd814f2014-10-23 04:12:28 +00002573 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002574#ifndef _LIBCPP_NO_EXCEPTIONS
2575 }
2576 catch (...)
2577 {
2578 __d(__p);
2579 throw;
2580 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002581#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002582}
2583
2584template<class _Tp>
2585template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002586inline
Howard Hinnant719bda32011-05-28 14:41:13 +00002587shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002588 : __ptr_(__p),
2589 __cntrl_(__r.__cntrl_)
2590{
2591 if (__cntrl_)
2592 __cntrl_->__add_shared();
2593}
2594
2595template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002596inline
Howard Hinnant719bda32011-05-28 14:41:13 +00002597shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002598 : __ptr_(__r.__ptr_),
2599 __cntrl_(__r.__cntrl_)
2600{
2601 if (__cntrl_)
2602 __cntrl_->__add_shared();
2603}
2604
2605template<class _Tp>
2606template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002607inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002608shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07002609 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00002610 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002611 : __ptr_(__r.__ptr_),
2612 __cntrl_(__r.__cntrl_)
2613{
2614 if (__cntrl_)
2615 __cntrl_->__add_shared();
2616}
2617
Howard Hinnantc51e1022010-05-11 19:42:16 +00002618template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002619inline
Howard Hinnant719bda32011-05-28 14:41:13 +00002620shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002621 : __ptr_(__r.__ptr_),
2622 __cntrl_(__r.__cntrl_)
2623{
Bruce Mitchener170d8972020-11-24 12:53:53 -05002624 __r.__ptr_ = nullptr;
2625 __r.__cntrl_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002626}
2627
2628template<class _Tp>
2629template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002630inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002631shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07002632 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00002633 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002634 : __ptr_(__r.__ptr_),
2635 __cntrl_(__r.__cntrl_)
2636{
Bruce Mitchener170d8972020-11-24 12:53:53 -05002637 __r.__ptr_ = nullptr;
2638 __r.__cntrl_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002639}
2640
Marshall Clowb22274f2017-01-24 22:22:33 +00002641#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002642template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00002643template<class _Yp>
Logan Chiend435f8b2014-01-31 09:30:46 +00002644shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Logan Chiend435f8b2014-01-31 09:30:46 +00002645 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002646 : __ptr_(__r.get())
2647{
2648 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
2649 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002650 __enable_weak_this(__r.get(), __r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002651 __r.release();
2652}
Marshall Clowb22274f2017-01-24 22:22:33 +00002653#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002654
2655template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00002656template <class _Yp, class _Dp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002657shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
Logan Chiend435f8b2014-01-31 09:30:46 +00002658 typename enable_if
2659 <
2660 !is_lvalue_reference<_Dp>::value &&
Logan Chiend435f8b2014-01-31 09:30:46 +00002661 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
2662 __nat
2663 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002664 : __ptr_(__r.get())
2665{
Marshall Clow35cde742015-05-10 13:59:45 +00002666#if _LIBCPP_STD_VER > 11
2667 if (__ptr_ == nullptr)
2668 __cntrl_ = nullptr;
2669 else
2670#endif
2671 {
Erik Pilkington2a398762017-05-25 15:43:31 +00002672 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
zoecarvercaf89ec2021-04-08 22:01:35 -07002673 typedef __shared_ptr_pointer<typename unique_ptr<_Yp, _Dp>::pointer, _Dp, _AllocT > _CntrlBlk;
Erik Pilkington2a398762017-05-25 15:43:31 +00002674 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002675 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00002676 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002677 __r.release();
2678}
2679
2680template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00002681template <class _Yp, class _Dp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002682shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
Logan Chiend435f8b2014-01-31 09:30:46 +00002683 typename enable_if
2684 <
2685 is_lvalue_reference<_Dp>::value &&
Logan Chiend435f8b2014-01-31 09:30:46 +00002686 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
2687 __nat
2688 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002689 : __ptr_(__r.get())
2690{
Marshall Clow35cde742015-05-10 13:59:45 +00002691#if _LIBCPP_STD_VER > 11
2692 if (__ptr_ == nullptr)
2693 __cntrl_ = nullptr;
2694 else
2695#endif
2696 {
Erik Pilkington2a398762017-05-25 15:43:31 +00002697 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
zoecarvercaf89ec2021-04-08 22:01:35 -07002698 typedef __shared_ptr_pointer<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clow35cde742015-05-10 13:59:45 +00002699 reference_wrapper<typename remove_reference<_Dp>::type>,
Erik Pilkington2a398762017-05-25 15:43:31 +00002700 _AllocT > _CntrlBlk;
Logan Smith85cb0812020-02-20 12:23:36 -05002701 __cntrl_ = new _CntrlBlk(__r.get(), _VSTD::ref(__r.get_deleter()), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002702 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00002703 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002704 __r.release();
2705}
2706
Zoe Carver6cd05c32019-08-19 15:47:16 +00002707template<class _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002708shared_ptr<_Tp>::~shared_ptr()
2709{
2710 if (__cntrl_)
2711 __cntrl_->__release_shared();
2712}
2713
2714template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002715inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002716shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00002717shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002718{
2719 shared_ptr(__r).swap(*this);
2720 return *this;
2721}
2722
2723template<class _Tp>
2724template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002725inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002726typename enable_if
2727<
zoecarverd73f42a2020-05-11 18:42:50 -07002728 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002729 shared_ptr<_Tp>&
2730>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00002731shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002732{
2733 shared_ptr(__r).swap(*this);
2734 return *this;
2735}
2736
Howard Hinnantc51e1022010-05-11 19:42:16 +00002737template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002738inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002739shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00002740shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002741{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002742 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002743 return *this;
2744}
2745
2746template<class _Tp>
2747template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002748inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002749typename enable_if
2750<
zoecarverd73f42a2020-05-11 18:42:50 -07002751 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002752 shared_ptr<_Tp>&
2753>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002754shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
2755{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002756 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002757 return *this;
2758}
2759
Marshall Clowb22274f2017-01-24 22:22:33 +00002760#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002761template<class _Tp>
2762template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002763inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002764typename enable_if
2765<
2766 !is_array<_Yp>::value &&
Marshall Clow7e384b72017-01-10 16:59:33 +00002767 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00002768 shared_ptr<_Tp>
2769>::type&
Howard Hinnantc51e1022010-05-11 19:42:16 +00002770shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
2771{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002772 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002773 return *this;
2774}
Marshall Clowb22274f2017-01-24 22:22:33 +00002775#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002776
2777template<class _Tp>
2778template <class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002779inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002780typename enable_if
2781<
Aditya Kumar7c5db692017-08-20 10:38:55 +00002782 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clow7e384b72017-01-10 16:59:33 +00002783 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002784 shared_ptr<_Tp>&
2785>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002786shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
2787{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002788 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002789 return *this;
2790}
2791
Howard Hinnantc51e1022010-05-11 19:42:16 +00002792template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002793inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002794void
Howard Hinnant719bda32011-05-28 14:41:13 +00002795shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002796{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002797 _VSTD::swap(__ptr_, __r.__ptr_);
2798 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002799}
2800
2801template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002802inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002803void
Howard Hinnant719bda32011-05-28 14:41:13 +00002804shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002805{
2806 shared_ptr().swap(*this);
2807}
2808
2809template<class _Tp>
2810template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002811inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002812typename enable_if
2813<
zoecarverd73f42a2020-05-11 18:42:50 -07002814 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002815 void
2816>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002817shared_ptr<_Tp>::reset(_Yp* __p)
2818{
2819 shared_ptr(__p).swap(*this);
2820}
2821
2822template<class _Tp>
2823template<class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002824inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002825typename enable_if
2826<
zoecarverd73f42a2020-05-11 18:42:50 -07002827 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002828 void
2829>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002830shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
2831{
2832 shared_ptr(__p, __d).swap(*this);
2833}
2834
2835template<class _Tp>
2836template<class _Yp, class _Dp, class _Alloc>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002837inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002838typename enable_if
2839<
zoecarverd73f42a2020-05-11 18:42:50 -07002840 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002841 void
2842>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002843shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
2844{
2845 shared_ptr(__p, __d, __a).swap(*this);
2846}
2847
Louis Dionne74aef9f2020-12-11 12:20:06 -05002848//
2849// std::allocate_shared and std::make_shared
2850//
2851template<class _Tp, class _Alloc, class ..._Args, class = _EnableIf<!is_array<_Tp>::value> >
2852_LIBCPP_HIDE_FROM_ABI
2853shared_ptr<_Tp> allocate_shared(const _Alloc& __a, _Args&& ...__args)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002854{
Louis Dionne74aef9f2020-12-11 12:20:06 -05002855 using _ControlBlock = __shared_ptr_emplace<_Tp, _Alloc>;
2856 using _ControlBlockAllocator = typename __allocator_traits_rebind<_Alloc, _ControlBlock>::type;
2857 __allocation_guard<_ControlBlockAllocator> __guard(__a, 1);
2858 ::new ((void*)_VSTD::addressof(*__guard.__get())) _ControlBlock(__a, _VSTD::forward<_Args>(__args)...);
2859 auto __control_block = __guard.__release_ptr();
2860 return shared_ptr<_Tp>::__create_with_control_block((*__control_block).__get_elem(), _VSTD::addressof(*__control_block));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002861}
2862
Louis Dionne43c9f8f2020-12-09 16:57:28 -05002863template<class _Tp, class ..._Args, class = _EnableIf<!is_array<_Tp>::value> >
2864_LIBCPP_HIDE_FROM_ABI
2865shared_ptr<_Tp> make_shared(_Args&& ...__args)
2866{
2867 return _VSTD::allocate_shared<_Tp>(allocator<_Tp>(), _VSTD::forward<_Args>(__args)...);
2868}
2869
Howard Hinnantc51e1022010-05-11 19:42:16 +00002870template<class _Tp, class _Up>
2871inline _LIBCPP_INLINE_VISIBILITY
2872bool
Howard Hinnant719bda32011-05-28 14:41:13 +00002873operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002874{
2875 return __x.get() == __y.get();
2876}
2877
2878template<class _Tp, class _Up>
2879inline _LIBCPP_INLINE_VISIBILITY
2880bool
Howard Hinnant719bda32011-05-28 14:41:13 +00002881operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002882{
2883 return !(__x == __y);
2884}
2885
2886template<class _Tp, class _Up>
2887inline _LIBCPP_INLINE_VISIBILITY
2888bool
Howard Hinnant719bda32011-05-28 14:41:13 +00002889operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002890{
Marshall Clow8afdf7a2018-02-12 17:26:40 +00002891#if _LIBCPP_STD_VER <= 11
Eric Fiselierf8898c82015-02-05 23:01:40 +00002892 typedef typename common_type<_Tp*, _Up*>::type _Vp;
2893 return less<_Vp>()(__x.get(), __y.get());
Marshall Clow8afdf7a2018-02-12 17:26:40 +00002894#else
2895 return less<>()(__x.get(), __y.get());
2896#endif
2897
Howard Hinnantb17caf92012-02-21 21:02:58 +00002898}
2899
2900template<class _Tp, class _Up>
2901inline _LIBCPP_INLINE_VISIBILITY
2902bool
2903operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
2904{
2905 return __y < __x;
2906}
2907
2908template<class _Tp, class _Up>
2909inline _LIBCPP_INLINE_VISIBILITY
2910bool
2911operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
2912{
2913 return !(__y < __x);
2914}
2915
2916template<class _Tp, class _Up>
2917inline _LIBCPP_INLINE_VISIBILITY
2918bool
2919operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
2920{
2921 return !(__x < __y);
2922}
2923
2924template<class _Tp>
2925inline _LIBCPP_INLINE_VISIBILITY
2926bool
2927operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
2928{
2929 return !__x;
2930}
2931
2932template<class _Tp>
2933inline _LIBCPP_INLINE_VISIBILITY
2934bool
2935operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
2936{
2937 return !__x;
2938}
2939
2940template<class _Tp>
2941inline _LIBCPP_INLINE_VISIBILITY
2942bool
2943operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
2944{
2945 return static_cast<bool>(__x);
2946}
2947
2948template<class _Tp>
2949inline _LIBCPP_INLINE_VISIBILITY
2950bool
2951operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
2952{
2953 return static_cast<bool>(__x);
2954}
2955
2956template<class _Tp>
2957inline _LIBCPP_INLINE_VISIBILITY
2958bool
2959operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
2960{
2961 return less<_Tp*>()(__x.get(), nullptr);
2962}
2963
2964template<class _Tp>
2965inline _LIBCPP_INLINE_VISIBILITY
2966bool
2967operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
2968{
2969 return less<_Tp*>()(nullptr, __x.get());
2970}
2971
2972template<class _Tp>
2973inline _LIBCPP_INLINE_VISIBILITY
2974bool
2975operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
2976{
2977 return nullptr < __x;
2978}
2979
2980template<class _Tp>
2981inline _LIBCPP_INLINE_VISIBILITY
2982bool
2983operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
2984{
2985 return __x < nullptr;
2986}
2987
2988template<class _Tp>
2989inline _LIBCPP_INLINE_VISIBILITY
2990bool
2991operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
2992{
2993 return !(nullptr < __x);
2994}
2995
2996template<class _Tp>
2997inline _LIBCPP_INLINE_VISIBILITY
2998bool
2999operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3000{
3001 return !(__x < nullptr);
3002}
3003
3004template<class _Tp>
3005inline _LIBCPP_INLINE_VISIBILITY
3006bool
3007operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3008{
3009 return !(__x < nullptr);
3010}
3011
3012template<class _Tp>
3013inline _LIBCPP_INLINE_VISIBILITY
3014bool
3015operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3016{
3017 return !(nullptr < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003018}
3019
3020template<class _Tp>
3021inline _LIBCPP_INLINE_VISIBILITY
3022void
Howard Hinnant719bda32011-05-28 14:41:13 +00003023swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003024{
3025 __x.swap(__y);
3026}
3027
3028template<class _Tp, class _Up>
3029inline _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07003030shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003031static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003032{
zoecarverd73f42a2020-05-11 18:42:50 -07003033 return shared_ptr<_Tp>(__r,
3034 static_cast<
3035 typename shared_ptr<_Tp>::element_type*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003036}
3037
3038template<class _Tp, class _Up>
3039inline _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07003040shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003041dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003042{
zoecarverd73f42a2020-05-11 18:42:50 -07003043 typedef typename shared_ptr<_Tp>::element_type _ET;
3044 _ET* __p = dynamic_cast<_ET*>(__r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003045 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
3046}
3047
3048template<class _Tp, class _Up>
zoecarverd73f42a2020-05-11 18:42:50 -07003049shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003050const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003051{
zoecarverd73f42a2020-05-11 18:42:50 -07003052 typedef typename shared_ptr<_Tp>::element_type _RTp;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003053 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003054}
3055
zoecarverd73f42a2020-05-11 18:42:50 -07003056template<class _Tp, class _Up>
3057shared_ptr<_Tp>
3058reinterpret_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
3059{
3060 return shared_ptr<_Tp>(__r,
3061 reinterpret_cast<
3062 typename shared_ptr<_Tp>::element_type*>(__r.get()));
3063}
3064
Howard Hinnant72f73582010-08-11 17:04:31 +00003065#ifndef _LIBCPP_NO_RTTI
3066
Howard Hinnantc51e1022010-05-11 19:42:16 +00003067template<class _Dp, class _Tp>
3068inline _LIBCPP_INLINE_VISIBILITY
3069_Dp*
Howard Hinnant719bda32011-05-28 14:41:13 +00003070get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003071{
3072 return __p.template __get_deleter<_Dp>();
3073}
3074
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003075#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00003076
Howard Hinnantc51e1022010-05-11 19:42:16 +00003077template<class _Tp>
Vy Nguyene369bd92020-07-13 12:34:37 -04003078class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003079{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003080public:
3081 typedef _Tp element_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003082private:
3083 element_type* __ptr_;
3084 __shared_weak_count* __cntrl_;
3085
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003086public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003087 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003088 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003089 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00003090 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
3091 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003092 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003093 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003094 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00003095 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
3096 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003097
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003098 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003099 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003100 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003101 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
3102 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003103 ~weak_ptr();
3104
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003105 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003106 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003107 template<class _Yp>
3108 typename enable_if
3109 <
3110 is_convertible<_Yp*, element_type*>::value,
3111 weak_ptr&
3112 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003113 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003114 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
3115
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003116 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003117 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
3118 template<class _Yp>
3119 typename enable_if
3120 <
3121 is_convertible<_Yp*, element_type*>::value,
3122 weak_ptr&
3123 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003124 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003125 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
3126
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003127 template<class _Yp>
3128 typename enable_if
3129 <
3130 is_convertible<_Yp*, element_type*>::value,
3131 weak_ptr&
3132 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003133 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003134 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003135
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003136 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003137 void swap(weak_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003138 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003139 void reset() _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003140
Howard Hinnant756c69b2010-09-22 16:48:34 +00003141 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003142 long use_count() const _NOEXCEPT
3143 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003144 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003145 bool expired() const _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05003146 {return __cntrl_ == nullptr || __cntrl_->use_count() == 0;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003147 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003148 template<class _Up>
3149 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003150 bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003151 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003152 template<class _Up>
3153 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003154 bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003155 {return __cntrl_ < __r.__cntrl_;}
3156
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003157 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
3158 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003159};
3160
Logan Smitha5e4d7e2020-05-07 12:07:01 -04003161#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
3162template<class _Tp>
3163weak_ptr(shared_ptr<_Tp>) -> weak_ptr<_Tp>;
3164#endif
3165
Howard Hinnantc51e1022010-05-11 19:42:16 +00003166template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003167inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003168_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003169weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05003170 : __ptr_(nullptr),
3171 __cntrl_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003172{
3173}
3174
3175template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003176inline
Howard Hinnant719bda32011-05-28 14:41:13 +00003177weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003178 : __ptr_(__r.__ptr_),
3179 __cntrl_(__r.__cntrl_)
3180{
3181 if (__cntrl_)
3182 __cntrl_->__add_weak();
3183}
3184
3185template<class _Tp>
3186template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003187inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003188weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00003189 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00003190 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003191 : __ptr_(__r.__ptr_),
3192 __cntrl_(__r.__cntrl_)
3193{
3194 if (__cntrl_)
3195 __cntrl_->__add_weak();
3196}
3197
3198template<class _Tp>
3199template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003200inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003201weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00003202 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00003203 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003204 : __ptr_(__r.__ptr_),
3205 __cntrl_(__r.__cntrl_)
3206{
3207 if (__cntrl_)
3208 __cntrl_->__add_weak();
3209}
3210
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003211template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003212inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003213weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
3214 : __ptr_(__r.__ptr_),
3215 __cntrl_(__r.__cntrl_)
3216{
Bruce Mitchener170d8972020-11-24 12:53:53 -05003217 __r.__ptr_ = nullptr;
3218 __r.__cntrl_ = nullptr;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003219}
3220
3221template<class _Tp>
3222template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003223inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003224weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
3225 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
3226 _NOEXCEPT
3227 : __ptr_(__r.__ptr_),
3228 __cntrl_(__r.__cntrl_)
3229{
Bruce Mitchener170d8972020-11-24 12:53:53 -05003230 __r.__ptr_ = nullptr;
3231 __r.__cntrl_ = nullptr;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003232}
3233
Howard Hinnantc51e1022010-05-11 19:42:16 +00003234template<class _Tp>
3235weak_ptr<_Tp>::~weak_ptr()
3236{
3237 if (__cntrl_)
3238 __cntrl_->__release_weak();
3239}
3240
3241template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003242inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003243weak_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00003244weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003245{
3246 weak_ptr(__r).swap(*this);
3247 return *this;
3248}
3249
3250template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003251template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003252inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003253typename enable_if
3254<
3255 is_convertible<_Yp*, _Tp*>::value,
3256 weak_ptr<_Tp>&
3257>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00003258weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003259{
3260 weak_ptr(__r).swap(*this);
3261 return *this;
3262}
3263
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003264template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003265inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003266weak_ptr<_Tp>&
3267weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
3268{
3269 weak_ptr(_VSTD::move(__r)).swap(*this);
3270 return *this;
3271}
3272
Howard Hinnantc51e1022010-05-11 19:42:16 +00003273template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003274template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003275inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003276typename enable_if
3277<
3278 is_convertible<_Yp*, _Tp*>::value,
3279 weak_ptr<_Tp>&
3280>::type
3281weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
3282{
3283 weak_ptr(_VSTD::move(__r)).swap(*this);
3284 return *this;
3285}
3286
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003287template<class _Tp>
3288template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003289inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003290typename enable_if
3291<
3292 is_convertible<_Yp*, _Tp*>::value,
3293 weak_ptr<_Tp>&
3294>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00003295weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003296{
3297 weak_ptr(__r).swap(*this);
3298 return *this;
3299}
3300
3301template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003302inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003303void
Howard Hinnant719bda32011-05-28 14:41:13 +00003304weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003305{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003306 _VSTD::swap(__ptr_, __r.__ptr_);
3307 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003308}
3309
3310template<class _Tp>
3311inline _LIBCPP_INLINE_VISIBILITY
3312void
Howard Hinnant719bda32011-05-28 14:41:13 +00003313swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003314{
3315 __x.swap(__y);
3316}
3317
3318template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003319inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003320void
Howard Hinnant719bda32011-05-28 14:41:13 +00003321weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003322{
3323 weak_ptr().swap(*this);
3324}
3325
3326template<class _Tp>
3327template<class _Yp>
3328shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003329 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003330 : __ptr_(__r.__ptr_),
3331 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
3332{
Bruce Mitchener170d8972020-11-24 12:53:53 -05003333 if (__cntrl_ == nullptr)
Marshall Clow8fea1612016-08-25 15:09:01 +00003334 __throw_bad_weak_ptr();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003335}
3336
3337template<class _Tp>
3338shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003339weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003340{
3341 shared_ptr<_Tp> __r;
3342 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
3343 if (__r.__cntrl_)
3344 __r.__ptr_ = __ptr_;
3345 return __r;
3346}
3347
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003348#if _LIBCPP_STD_VER > 14
3349template <class _Tp = void> struct owner_less;
3350#else
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003351template <class _Tp> struct owner_less;
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003352#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003353
3354template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003355struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00003356 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003357{
3358 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003359 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003360 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003361 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003362 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003363 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003364 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003365 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003366 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003367 {return __x.owner_before(__y);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003368};
Howard Hinnantc51e1022010-05-11 19:42:16 +00003369
3370template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003371struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00003372 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
3373{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003374 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003375 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003376 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003377 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003378 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003379 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003380 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003381 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003382 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003383 {return __x.owner_before(__y);}
3384};
3385
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003386#if _LIBCPP_STD_VER > 14
3387template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003388struct _LIBCPP_TEMPLATE_VIS owner_less<void>
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003389{
3390 template <class _Tp, class _Up>
3391 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003392 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003393 {return __x.owner_before(__y);}
3394 template <class _Tp, class _Up>
3395 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003396 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003397 {return __x.owner_before(__y);}
3398 template <class _Tp, class _Up>
3399 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003400 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003401 {return __x.owner_before(__y);}
3402 template <class _Tp, class _Up>
3403 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003404 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003405 {return __x.owner_before(__y);}
3406 typedef void is_transparent;
3407};
3408#endif
3409
Howard Hinnantc51e1022010-05-11 19:42:16 +00003410template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003411class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
Howard Hinnantc51e1022010-05-11 19:42:16 +00003412{
3413 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003414protected:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003415 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003416 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003417 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003418 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003419 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003420 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
3421 {return *this;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003422 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003423 ~enable_shared_from_this() {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003424public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003425 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003426 shared_ptr<_Tp> shared_from_this()
3427 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003428 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003429 shared_ptr<_Tp const> shared_from_this() const
3430 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003431
Eric Fiselier84006862016-06-02 00:15:35 +00003432#if _LIBCPP_STD_VER > 14
3433 _LIBCPP_INLINE_VISIBILITY
3434 weak_ptr<_Tp> weak_from_this() _NOEXCEPT
3435 { return __weak_this_; }
3436
3437 _LIBCPP_INLINE_VISIBILITY
3438 weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
3439 { return __weak_this_; }
3440#endif // _LIBCPP_STD_VER > 14
3441
Howard Hinnantc51e1022010-05-11 19:42:16 +00003442 template <class _Up> friend class shared_ptr;
3443};
3444
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003445template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003446struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003447{
3448 typedef shared_ptr<_Tp> argument_type;
3449 typedef size_t result_type;
Eric Fiselier698a97b2017-01-21 00:02:12 +00003450
Howard Hinnant756c69b2010-09-22 16:48:34 +00003451 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003452 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003453 {
zoecarverd73f42a2020-05-11 18:42:50 -07003454 return hash<typename shared_ptr<_Tp>::element_type*>()(__ptr.get());
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003455 }
3456};
3457
Howard Hinnantc834c512011-11-29 18:15:50 +00003458template<class _CharT, class _Traits, class _Yp>
Howard Hinnantdc095972011-07-18 15:51:59 +00003459inline _LIBCPP_INLINE_VISIBILITY
3460basic_ostream<_CharT, _Traits>&
Howard Hinnantc834c512011-11-29 18:15:50 +00003461operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnantdc095972011-07-18 15:51:59 +00003462
Eric Fiselier9b492672016-06-18 02:12:53 +00003463
3464#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00003465
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003466class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant9fa30202012-07-30 01:40:57 +00003467{
Howard Hinnant49e145e2012-10-30 19:06:59 +00003468 void* __lx;
Howard Hinnant9fa30202012-07-30 01:40:57 +00003469public:
3470 void lock() _NOEXCEPT;
3471 void unlock() _NOEXCEPT;
3472
3473private:
3474 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
3475 __sp_mut(const __sp_mut&);
3476 __sp_mut& operator=(const __sp_mut&);
3477
Howard Hinnant8331b762013-03-06 23:30:19 +00003478 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00003479};
3480
Mehdi Amini228053d2017-05-04 17:08:54 +00003481_LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
3482__sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00003483
3484template <class _Tp>
3485inline _LIBCPP_INLINE_VISIBILITY
3486bool
3487atomic_is_lock_free(const shared_ptr<_Tp>*)
3488{
3489 return false;
3490}
3491
3492template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00003493_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003494shared_ptr<_Tp>
3495atomic_load(const shared_ptr<_Tp>* __p)
3496{
3497 __sp_mut& __m = __get_sp_mut(__p);
3498 __m.lock();
3499 shared_ptr<_Tp> __q = *__p;
3500 __m.unlock();
3501 return __q;
3502}
Aditya Kumar7c5db692017-08-20 10:38:55 +00003503
Howard Hinnant9fa30202012-07-30 01:40:57 +00003504template <class _Tp>
3505inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00003506_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003507shared_ptr<_Tp>
3508atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
3509{
3510 return atomic_load(__p);
3511}
3512
3513template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00003514_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003515void
3516atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
3517{
3518 __sp_mut& __m = __get_sp_mut(__p);
3519 __m.lock();
3520 __p->swap(__r);
3521 __m.unlock();
3522}
3523
3524template <class _Tp>
3525inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00003526_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003527void
3528atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
3529{
3530 atomic_store(__p, __r);
3531}
3532
3533template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00003534_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003535shared_ptr<_Tp>
3536atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
3537{
3538 __sp_mut& __m = __get_sp_mut(__p);
3539 __m.lock();
3540 __p->swap(__r);
3541 __m.unlock();
3542 return __r;
3543}
Aditya Kumar7c5db692017-08-20 10:38:55 +00003544
Howard Hinnant9fa30202012-07-30 01:40:57 +00003545template <class _Tp>
3546inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00003547_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003548shared_ptr<_Tp>
3549atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
3550{
3551 return atomic_exchange(__p, __r);
3552}
3553
3554template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00003555_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003556bool
3557atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
3558{
Marshall Clow4201ee82016-05-18 17:50:13 +00003559 shared_ptr<_Tp> __temp;
Howard Hinnant9fa30202012-07-30 01:40:57 +00003560 __sp_mut& __m = __get_sp_mut(__p);
3561 __m.lock();
3562 if (__p->__owner_equivalent(*__v))
3563 {
Marshall Clow4201ee82016-05-18 17:50:13 +00003564 _VSTD::swap(__temp, *__p);
Howard Hinnant9fa30202012-07-30 01:40:57 +00003565 *__p = __w;
3566 __m.unlock();
3567 return true;
3568 }
Marshall Clow4201ee82016-05-18 17:50:13 +00003569 _VSTD::swap(__temp, *__v);
Howard Hinnant9fa30202012-07-30 01:40:57 +00003570 *__v = *__p;
3571 __m.unlock();
3572 return false;
3573}
3574
3575template <class _Tp>
3576inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00003577_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003578bool
3579atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
3580{
3581 return atomic_compare_exchange_strong(__p, __v, __w);
3582}
3583
3584template <class _Tp>
3585inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00003586_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003587bool
3588atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
3589 shared_ptr<_Tp> __w, memory_order, memory_order)
3590{
3591 return atomic_compare_exchange_strong(__p, __v, __w);
3592}
3593
3594template <class _Tp>
3595inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00003596_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003597bool
3598atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
3599 shared_ptr<_Tp> __w, memory_order, memory_order)
3600{
3601 return atomic_compare_exchange_weak(__p, __v, __w);
3602}
3603
Eric Fiselier9b492672016-06-18 02:12:53 +00003604#endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00003605
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003606//enum class
Eric Fiselierab6bb302017-01-05 01:15:42 +00003607#if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE)
3608# ifndef _LIBCPP_CXX03_LANG
3609enum class pointer_safety : unsigned char {
3610 relaxed,
3611 preferred,
3612 strict
3613};
3614# endif
3615#else
Howard Hinnant8331b762013-03-06 23:30:19 +00003616struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantc51e1022010-05-11 19:42:16 +00003617{
Howard Hinnant49e145e2012-10-30 19:06:59 +00003618 enum __lx
Howard Hinnantc51e1022010-05-11 19:42:16 +00003619 {
3620 relaxed,
3621 preferred,
3622 strict
3623 };
3624
Howard Hinnant49e145e2012-10-30 19:06:59 +00003625 __lx __v_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003626
Howard Hinnant756c69b2010-09-22 16:48:34 +00003627 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2fdd22b2017-01-05 01:28:40 +00003628 pointer_safety() : __v_() {}
3629
3630 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant49e145e2012-10-30 19:06:59 +00003631 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003632 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003633 operator int() const {return __v_;}
3634};
Eric Fiselierab6bb302017-01-05 01:15:42 +00003635#endif
3636
3637#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \
Louis Dionne5e0eadd2018-08-01 02:08:59 +00003638 defined(_LIBCPP_BUILDING_LIBRARY)
Eric Fiselierab6bb302017-01-05 01:15:42 +00003639_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
3640#else
3641// This function is only offered in C++03 under ABI v1.
3642# if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG)
3643inline _LIBCPP_INLINE_VISIBILITY
3644pointer_safety get_pointer_safety() _NOEXCEPT {
3645 return pointer_safety::relaxed;
3646}
3647# endif
3648#endif
3649
Howard Hinnantc51e1022010-05-11 19:42:16 +00003650
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003651_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
3652_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
3653_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003654_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003655
3656template <class _Tp>
3657inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003658_Tp*
Howard Hinnantc51e1022010-05-11 19:42:16 +00003659undeclare_reachable(_Tp* __p)
3660{
3661 return static_cast<_Tp*>(__undeclare_reachable(__p));
3662}
3663
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003664_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003665
Marshall Clow8982dcd2015-07-13 20:04:56 +00003666// --- Helper for container swap --
3667template <typename _Alloc>
Marshall Clow8982dcd2015-07-13 20:04:56 +00003668_LIBCPP_INLINE_VISIBILITY
3669void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
3670#if _LIBCPP_STD_VER >= 14
3671 _NOEXCEPT
3672#else
3673 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
3674#endif
3675{
3676 using _VSTD::swap;
3677 swap(__a1, __a2);
3678}
3679
3680template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00003681inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00003682void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
3683
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05003684template <typename _Alloc>
3685inline _LIBCPP_INLINE_VISIBILITY
3686void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
3687#if _LIBCPP_STD_VER >= 14
3688 _NOEXCEPT
3689#else
3690 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
3691#endif
3692{
3693 _VSTD::__swap_allocator(__a1, __a2,
3694 integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
3695}
3696
Marshall Clowff91de82015-08-18 19:51:37 +00003697template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
Aditya Kumar7c5db692017-08-20 10:38:55 +00003698struct __noexcept_move_assign_container : public integral_constant<bool,
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00003699 _Traits::propagate_on_container_move_assignment::value
3700#if _LIBCPP_STD_VER > 14
3701 || _Traits::is_always_equal::value
3702#else
3703 && is_nothrow_move_assignable<_Alloc>::value
3704#endif
3705 > {};
Marshall Clow8982dcd2015-07-13 20:04:56 +00003706
Marshall Clowa591b9a2016-07-11 21:38:08 +00003707
Marshall Clowa591b9a2016-07-11 21:38:08 +00003708template <class _Tp, class _Alloc>
3709struct __temp_value {
3710 typedef allocator_traits<_Alloc> _Traits;
Aditya Kumar7c5db692017-08-20 10:38:55 +00003711
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00003712 typename aligned_storage<sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)>::type __v;
Marshall Clowa591b9a2016-07-11 21:38:08 +00003713 _Alloc &__a;
3714
3715 _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
3716 _Tp & get() { return *__addr(); }
Aditya Kumar7c5db692017-08-20 10:38:55 +00003717
Marshall Clowa591b9a2016-07-11 21:38:08 +00003718 template<class... _Args>
Peter Collingbournecb126152018-08-15 17:49:30 +00003719 _LIBCPP_NO_CFI
3720 __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc) {
3721 _Traits::construct(__a, reinterpret_cast<_Tp*>(addressof(__v)),
3722 _VSTD::forward<_Args>(__args)...);
3723 }
Aditya Kumar7c5db692017-08-20 10:38:55 +00003724
Marshall Clowa591b9a2016-07-11 21:38:08 +00003725 ~__temp_value() { _Traits::destroy(__a, __addr()); }
3726 };
Marshall Clowa591b9a2016-07-11 21:38:08 +00003727
Marshall Clowe46031a2018-07-02 18:41:15 +00003728template<typename _Alloc, typename = void, typename = void>
Marshall Clow82c90aa2018-01-11 19:36:22 +00003729struct __is_allocator : false_type {};
3730
3731template<typename _Alloc>
3732struct __is_allocator<_Alloc,
Marshall Clowe46031a2018-07-02 18:41:15 +00003733 typename __void_t<typename _Alloc::value_type>::type,
3734 typename __void_t<decltype(_VSTD::declval<_Alloc&>().allocate(size_t(0)))>::type
3735 >
Marshall Clow82c90aa2018-01-11 19:36:22 +00003736 : true_type {};
Marshall Clow82c90aa2018-01-11 19:36:22 +00003737
Eric Fiselier74ebee62019-06-08 01:31:19 +00003738// __builtin_new_allocator -- A non-templated helper for allocating and
3739// deallocating memory using __builtin_operator_new and
3740// __builtin_operator_delete. It should be used in preference to
3741// `std::allocator<T>` to avoid additional instantiations.
3742struct __builtin_new_allocator {
3743 struct __builtin_new_deleter {
3744 typedef void* pointer_type;
3745
3746 _LIBCPP_CONSTEXPR explicit __builtin_new_deleter(size_t __size, size_t __align)
3747 : __size_(__size), __align_(__align) {}
3748
3749 void operator()(void* p) const _NOEXCEPT {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05003750 _VSTD::__libcpp_deallocate(p, __size_, __align_);
Eric Fiselier74ebee62019-06-08 01:31:19 +00003751 }
3752
3753 private:
3754 size_t __size_;
3755 size_t __align_;
3756 };
3757
3758 typedef unique_ptr<void, __builtin_new_deleter> __holder_t;
3759
3760 static __holder_t __allocate_bytes(size_t __s, size_t __align) {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05003761 return __holder_t(_VSTD::__libcpp_allocate(__s, __align),
Eric Fiselier74ebee62019-06-08 01:31:19 +00003762 __builtin_new_deleter(__s, __align));
3763 }
3764
3765 static void __deallocate_bytes(void* __p, size_t __s,
3766 size_t __align) _NOEXCEPT {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05003767 _VSTD::__libcpp_deallocate(__p, __s, __align);
Eric Fiselier74ebee62019-06-08 01:31:19 +00003768 }
3769
3770 template <class _Tp>
3771 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
3772 static __holder_t __allocate_type(size_t __n) {
3773 return __allocate_bytes(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
3774 }
3775
3776 template <class _Tp>
3777 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
3778 static void __deallocate_type(void* __p, size_t __n) _NOEXCEPT {
3779 __deallocate_bytes(__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
3780 }
3781};
3782
3783
Howard Hinnantc51e1022010-05-11 19:42:16 +00003784_LIBCPP_END_NAMESPACE_STD
3785
Eric Fiselierf4433a32017-05-31 22:07:49 +00003786_LIBCPP_POP_MACROS
3787
Louis Dionne59d0b3c2019-08-05 18:29:14 +00003788#if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
Louis Dionned53d8c02019-08-06 21:11:24 +00003789# include <__pstl_memory>
Louis Dionne59d0b3c2019-08-05 18:29:14 +00003790#endif
3791
Howard Hinnantc51e1022010-05-11 19:42:16 +00003792#endif // _LIBCPP_MEMORY