blob: b3c1a8642a49ea7f76e59dc199e5946a5992a5bc [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>
668#include <type_traits>
669#include <typeinfo>
Arthur O'Dwyer7deec122021-03-24 18:19:12 -0400670#include <compare>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000671#include <cstddef>
672#include <cstdint>
673#include <new>
674#include <utility>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000675#include <iterator>
676#include <__functional_base>
Howard Hinnantdc095972011-07-18 15:51:59 +0000677#include <iosfwd>
Howard Hinnant83b1c052011-12-19 17:58:44 +0000678#include <tuple>
Eric Fiselier2db2bd52016-05-07 03:12:24 +0000679#include <stdexcept>
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000680#include <cstring>
Louis Dionne735bc462021-04-14 13:59:03 -0400681#include <__memory/addressof.h>
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 Dionne556fcd02021-04-09 14:43:01 -0400685#include <__memory/compressed_pair.h>
Louis Dionne735bc462021-04-14 13:59:03 -0400686#include <__memory/construct_at.h>
Louis Dionne25b7f2f2021-04-09 15:57:21 -0400687#include <__memory/pointer_safety.h>
Louis Dionnefa621d72020-12-10 18:28:13 -0500688#include <__memory/pointer_traits.h>
Louis Dionnee0103192021-04-09 14:45:18 -0400689#include <__memory/raw_storage_iterator.h>
Louis Dionnec7166582021-04-09 15:16:14 -0400690#include <__memory/shared_ptr.h>
Louis Dionne00180872021-04-09 12:58:00 -0400691#include <__memory/temporary_buffer.h>
Louis Dionnec7ef68c2021-04-09 14:47:46 -0400692#include <__memory/uninitialized_algorithms.h>
Louis Dionne7eadb032021-04-09 15:00:57 -0400693#include <__memory/unique_ptr.h>
Louis Dionne74aef9f2020-12-11 12:20:06 -0500694#include <__memory/utilities.h>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000695#include <version>
Howard Hinnant9fa30202012-07-30 01:40:57 +0000696
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000697#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000698#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000699#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000700
Eric Fiselierf4433a32017-05-31 22:07:49 +0000701_LIBCPP_PUSH_MACROS
702#include <__undef_macros>
703
704
Howard Hinnantc51e1022010-05-11 19:42:16 +0000705_LIBCPP_BEGIN_NAMESPACE_STD
706
Louis Dionned6651542020-11-03 12:05:55 -0500707template <class _Alloc, class _Ptr>
708_LIBCPP_INLINE_VISIBILITY
709void __construct_forward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2) {
710 static_assert(__is_cpp17_move_insertable<_Alloc>::value,
Arthur O'Dwyerf2016bb2020-12-12 11:43:15 -0500711 "The specified type does not meet the requirements of Cpp17MoveInsertable");
Louis Dionned6651542020-11-03 12:05:55 -0500712 typedef allocator_traits<_Alloc> _Traits;
713 for (; __begin1 != __end1; ++__begin1, (void)++__begin2) {
714 _Traits::construct(__a, _VSTD::__to_address(__begin2),
715#ifdef _LIBCPP_NO_EXCEPTIONS
716 _VSTD::move(*__begin1)
717#else
718 _VSTD::move_if_noexcept(*__begin1)
719#endif
720 );
721 }
722}
723
724template <class _Alloc, class _Tp, typename enable_if<
725 (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) &&
726 is_trivially_move_constructible<_Tp>::value
727>::type>
728_LIBCPP_INLINE_VISIBILITY
729void __construct_forward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2) {
730 ptrdiff_t _Np = __end1 - __begin1;
731 if (_Np > 0) {
732 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
733 __begin2 += _Np;
734 }
735}
736
737template <class _Alloc, class _Iter, class _Ptr>
738_LIBCPP_INLINE_VISIBILITY
739void __construct_range_forward(_Alloc& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2) {
740 typedef allocator_traits<_Alloc> _Traits;
741 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2) {
742 _Traits::construct(__a, _VSTD::__to_address(__begin2), *__begin1);
743 }
744}
745
746template <class _Alloc, class _Source, class _Dest,
747 class _RawSource = typename remove_const<_Source>::type,
748 class _RawDest = typename remove_const<_Dest>::type,
749 class =
750 typename enable_if<
751 is_trivially_copy_constructible<_Dest>::value &&
752 is_same<_RawSource, _RawDest>::value &&
753 (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Dest*, _Source&>::value)
754 >::type>
755_LIBCPP_INLINE_VISIBILITY
756void __construct_range_forward(_Alloc&, _Source* __begin1, _Source* __end1, _Dest*& __begin2) {
757 ptrdiff_t _Np = __end1 - __begin1;
758 if (_Np > 0) {
759 _VSTD::memcpy(const_cast<_RawDest*>(__begin2), __begin1, _Np * sizeof(_Dest));
760 __begin2 += _Np;
761 }
762}
763
764template <class _Alloc, class _Ptr>
765_LIBCPP_INLINE_VISIBILITY
766void __construct_backward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2) {
767 static_assert(__is_cpp17_move_insertable<_Alloc>::value,
768 "The specified type does not meet the requirements of Cpp17MoveInsertable");
769 typedef allocator_traits<_Alloc> _Traits;
770 while (__end1 != __begin1) {
771 _Traits::construct(__a, _VSTD::__to_address(__end2 - 1),
772#ifdef _LIBCPP_NO_EXCEPTIONS
773 _VSTD::move(*--__end1)
774#else
775 _VSTD::move_if_noexcept(*--__end1)
776#endif
777 );
778 --__end2;
779 }
780}
781
782template <class _Alloc, class _Tp, class = typename enable_if<
783 (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) &&
784 is_trivially_move_constructible<_Tp>::value
785>::type>
786_LIBCPP_INLINE_VISIBILITY
787void __construct_backward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2) {
788 ptrdiff_t _Np = __end1 - __begin1;
789 __end2 -= _Np;
790 if (_Np > 0)
791 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
792}
793
Howard Hinnantc51e1022010-05-11 19:42:16 +0000794struct __destruct_n
795{
796private:
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +0000797 size_t __size_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000798
799 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +0000800 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +0000801 {for (size_t __i = 0; __i < __size_; ++__i, ++__p) __p->~_Tp();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000802
803 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +0000804 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000805 {}
806
Howard Hinnant719bda32011-05-28 14:41:13 +0000807 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +0000808 {++__size_;}
Howard Hinnant719bda32011-05-28 14:41:13 +0000809 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000810 {}
811
Howard Hinnant719bda32011-05-28 14:41:13 +0000812 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +0000813 {__size_ = __s;}
Howard Hinnant719bda32011-05-28 14:41:13 +0000814 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000815 {}
816public:
Howard Hinnant719bda32011-05-28 14:41:13 +0000817 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +0000818 : __size_(__s) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000819
820 template <class _Tp>
Bruce Mitchener170d8972020-11-24 12:53:53 -0500821 _LIBCPP_INLINE_VISIBILITY void __incr() _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +0000822 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000823
824 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +0000825 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +0000826 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000827
828 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +0000829 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +0000830 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000831};
832
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000833_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000834
Marshall Clow8982dcd2015-07-13 20:04:56 +0000835// --- Helper for container swap --
836template <typename _Alloc>
Marshall Clow8982dcd2015-07-13 20:04:56 +0000837_LIBCPP_INLINE_VISIBILITY
838void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
839#if _LIBCPP_STD_VER >= 14
840 _NOEXCEPT
841#else
842 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
843#endif
844{
845 using _VSTD::swap;
846 swap(__a1, __a2);
847}
848
849template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +0000850inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +0000851void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
852
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -0500853template <typename _Alloc>
854inline _LIBCPP_INLINE_VISIBILITY
855void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
856#if _LIBCPP_STD_VER >= 14
857 _NOEXCEPT
858#else
859 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
860#endif
861{
862 _VSTD::__swap_allocator(__a1, __a2,
863 integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
864}
865
Marshall Clowff91de82015-08-18 19:51:37 +0000866template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
Aditya Kumar7c5db692017-08-20 10:38:55 +0000867struct __noexcept_move_assign_container : public integral_constant<bool,
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000868 _Traits::propagate_on_container_move_assignment::value
869#if _LIBCPP_STD_VER > 14
870 || _Traits::is_always_equal::value
871#else
872 && is_nothrow_move_assignable<_Alloc>::value
873#endif
874 > {};
Marshall Clow8982dcd2015-07-13 20:04:56 +0000875
Marshall Clowa591b9a2016-07-11 21:38:08 +0000876
Marshall Clowa591b9a2016-07-11 21:38:08 +0000877template <class _Tp, class _Alloc>
878struct __temp_value {
879 typedef allocator_traits<_Alloc> _Traits;
Aditya Kumar7c5db692017-08-20 10:38:55 +0000880
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +0000881 typename aligned_storage<sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)>::type __v;
Marshall Clowa591b9a2016-07-11 21:38:08 +0000882 _Alloc &__a;
883
884 _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
885 _Tp & get() { return *__addr(); }
Aditya Kumar7c5db692017-08-20 10:38:55 +0000886
Marshall Clowa591b9a2016-07-11 21:38:08 +0000887 template<class... _Args>
Peter Collingbournecb126152018-08-15 17:49:30 +0000888 _LIBCPP_NO_CFI
889 __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc) {
890 _Traits::construct(__a, reinterpret_cast<_Tp*>(addressof(__v)),
891 _VSTD::forward<_Args>(__args)...);
892 }
Aditya Kumar7c5db692017-08-20 10:38:55 +0000893
Marshall Clowa591b9a2016-07-11 21:38:08 +0000894 ~__temp_value() { _Traits::destroy(__a, __addr()); }
895 };
Marshall Clowa591b9a2016-07-11 21:38:08 +0000896
Marshall Clowe46031a2018-07-02 18:41:15 +0000897template<typename _Alloc, typename = void, typename = void>
Marshall Clow82c90aa2018-01-11 19:36:22 +0000898struct __is_allocator : false_type {};
899
900template<typename _Alloc>
901struct __is_allocator<_Alloc,
Marshall Clowe46031a2018-07-02 18:41:15 +0000902 typename __void_t<typename _Alloc::value_type>::type,
903 typename __void_t<decltype(_VSTD::declval<_Alloc&>().allocate(size_t(0)))>::type
904 >
Marshall Clow82c90aa2018-01-11 19:36:22 +0000905 : true_type {};
Marshall Clow82c90aa2018-01-11 19:36:22 +0000906
Eric Fiselier74ebee62019-06-08 01:31:19 +0000907// __builtin_new_allocator -- A non-templated helper for allocating and
908// deallocating memory using __builtin_operator_new and
909// __builtin_operator_delete. It should be used in preference to
910// `std::allocator<T>` to avoid additional instantiations.
911struct __builtin_new_allocator {
912 struct __builtin_new_deleter {
913 typedef void* pointer_type;
914
915 _LIBCPP_CONSTEXPR explicit __builtin_new_deleter(size_t __size, size_t __align)
916 : __size_(__size), __align_(__align) {}
917
918 void operator()(void* p) const _NOEXCEPT {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -0500919 _VSTD::__libcpp_deallocate(p, __size_, __align_);
Eric Fiselier74ebee62019-06-08 01:31:19 +0000920 }
921
922 private:
923 size_t __size_;
924 size_t __align_;
925 };
926
927 typedef unique_ptr<void, __builtin_new_deleter> __holder_t;
928
929 static __holder_t __allocate_bytes(size_t __s, size_t __align) {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -0500930 return __holder_t(_VSTD::__libcpp_allocate(__s, __align),
Eric Fiselier74ebee62019-06-08 01:31:19 +0000931 __builtin_new_deleter(__s, __align));
932 }
933
934 static void __deallocate_bytes(void* __p, size_t __s,
935 size_t __align) _NOEXCEPT {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -0500936 _VSTD::__libcpp_deallocate(__p, __s, __align);
Eric Fiselier74ebee62019-06-08 01:31:19 +0000937 }
938
939 template <class _Tp>
940 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
941 static __holder_t __allocate_type(size_t __n) {
942 return __allocate_bytes(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
943 }
944
945 template <class _Tp>
946 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
947 static void __deallocate_type(void* __p, size_t __n) _NOEXCEPT {
948 __deallocate_bytes(__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
949 }
950};
951
952
Howard Hinnantc51e1022010-05-11 19:42:16 +0000953_LIBCPP_END_NAMESPACE_STD
954
Eric Fiselierf4433a32017-05-31 22:07:49 +0000955_LIBCPP_POP_MACROS
956
Louis Dionne59d0b3c2019-08-05 18:29:14 +0000957#if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
Louis Dionned53d8c02019-08-06 21:11:24 +0000958# include <__pstl_memory>
Louis Dionne59d0b3c2019-08-05 18:29:14 +0000959#endif
960
Howard Hinnantc51e1022010-05-11 19:42:16 +0000961#endif // _LIBCPP_MEMORY