blob: f12f3c70eadd257f48fdcfc9da308e2fe3ebd3ee [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
Louis Dionne9bd93882021-11-17 16:25:01 -05002//===----------------------------------------------------------------------===//
Howard Hinnantc51e1022010-05-11 19:42:16 +00003//
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 <>
Louis Dionne482a04b2021-06-15 16:08:38 -0400102class allocator<void> // 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>
Louis Dionne3402c3c2021-05-27 12:56:12 -0400156class raw_storage_iterator // deprecated in C++17, removed in C++20
Louis Dionne8762e2b2021-05-25 18:15:58 -0400157 : public iterator<output_iterator_tag, void, void, void, void> // until C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000158{
159public:
Louis Dionne3402c3c2021-05-27 12:56:12 -0400160 typedef output_iterator_tag iterator_category;
161 typedef void value_type;
162 typedef void difference_type; // until C++20
163 typedef ptrdiff_t difference_type; // since C++20
164 typedef void pointer;
165 typedef void reference;
166
Howard Hinnantc51e1022010-05-11 19:42:16 +0000167 explicit raw_storage_iterator(OutputIterator x);
168 raw_storage_iterator& operator*();
169 raw_storage_iterator& operator=(const T& element);
170 raw_storage_iterator& operator++();
171 raw_storage_iterator operator++(int);
172};
173
Howard Hinnant719bda32011-05-28 14:41:13 +0000174template <class T> pair<T*,ptrdiff_t> get_temporary_buffer(ptrdiff_t n) noexcept;
175template <class T> void return_temporary_buffer(T* p) noexcept;
176
177template <class T> T* addressof(T& r) noexcept;
Marshall Clow78dbe462016-11-14 18:22:19 +0000178template <class T> T* addressof(const T&& r) noexcept = delete;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000179
180template <class InputIterator, class ForwardIterator>
181ForwardIterator
182uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator result);
183
Howard Hinnant719bda32011-05-28 14:41:13 +0000184template <class InputIterator, class Size, class ForwardIterator>
185ForwardIterator
186uninitialized_copy_n(InputIterator first, Size n, ForwardIterator result);
187
Howard Hinnantc51e1022010-05-11 19:42:16 +0000188template <class ForwardIterator, class T>
189void uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& x);
190
191template <class ForwardIterator, class Size, class T>
Howard Hinnant3c811092010-11-18 16:13:03 +0000192ForwardIterator
193uninitialized_fill_n(ForwardIterator first, Size n, const T& x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000194
Louis Dionne99f59432020-09-17 12:06:13 -0400195template <class T, class ...Args>
196constexpr T* construct_at(T* location, Args&& ...args); // since C++20
197
Eric Fiselier383f6cf2016-07-24 03:51:39 +0000198template <class T>
Louis Dionne99f59432020-09-17 12:06:13 -0400199void destroy_at(T* location); // constexpr in C++20
Eric Fiselier383f6cf2016-07-24 03:51:39 +0000200
201template <class ForwardIterator>
Louis Dionne99f59432020-09-17 12:06:13 -0400202void destroy(ForwardIterator first, ForwardIterator last); // constexpr in C++20
Eric Fiselier383f6cf2016-07-24 03:51:39 +0000203
204template <class ForwardIterator, class Size>
Louis Dionne99f59432020-09-17 12:06:13 -0400205ForwardIterator destroy_n(ForwardIterator first, Size n); // constexpr in C++20
Eric Fiselier383f6cf2016-07-24 03:51:39 +0000206
207template <class InputIterator, class ForwardIterator>
208 ForwardIterator uninitialized_move(InputIterator first, InputIterator last, ForwardIterator result);
209
210template <class InputIterator, class Size, class ForwardIterator>
211 pair<InputIterator,ForwardIterator> uninitialized_move_n(InputIterator first, Size n, ForwardIterator result);
212
213template <class ForwardIterator>
214 void uninitialized_value_construct(ForwardIterator first, ForwardIterator last);
215
216template <class ForwardIterator, class Size>
217 ForwardIterator uninitialized_value_construct_n(ForwardIterator first, Size n);
218
219template <class ForwardIterator>
220 void uninitialized_default_construct(ForwardIterator first, ForwardIterator last);
221
222template <class ForwardIterator, class Size>
223 ForwardIterator uninitialized_default_construct_n(ForwardIterator first, Size n);
224
Louis Dionne481a2662018-09-23 18:35:00 +0000225template <class Y> struct auto_ptr_ref {}; // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000226
227template<class X>
Louis Dionne481a2662018-09-23 18:35:00 +0000228class auto_ptr // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000229{
230public:
231 typedef X element_type;
232
233 explicit auto_ptr(X* p =0) throw();
234 auto_ptr(auto_ptr&) throw();
235 template<class Y> auto_ptr(auto_ptr<Y>&) throw();
236 auto_ptr& operator=(auto_ptr&) throw();
237 template<class Y> auto_ptr& operator=(auto_ptr<Y>&) throw();
238 auto_ptr& operator=(auto_ptr_ref<X> r) throw();
239 ~auto_ptr() throw();
240
241 typename add_lvalue_reference<X>::type operator*() const throw();
242 X* operator->() const throw();
243 X* get() const throw();
244 X* release() throw();
245 void reset(X* p =0) throw();
246
247 auto_ptr(auto_ptr_ref<X>) throw();
248 template<class Y> operator auto_ptr_ref<Y>() throw();
249 template<class Y> operator auto_ptr<Y>() throw();
250};
251
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000252template <class T>
253struct default_delete
254{
Howard Hinnant719bda32011-05-28 14:41:13 +0000255 constexpr default_delete() noexcept = default;
256 template <class U> default_delete(const default_delete<U>&) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000257
Howard Hinnant719bda32011-05-28 14:41:13 +0000258 void operator()(T*) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000259};
260
261template <class T>
262struct default_delete<T[]>
263{
Howard Hinnant719bda32011-05-28 14:41:13 +0000264 constexpr default_delete() noexcept = default;
265 void operator()(T*) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000266 template <class U> void operator()(U*) const = delete;
267};
268
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000269template <class T, class D = default_delete<T>>
270class unique_ptr
271{
272public:
273 typedef see below pointer;
274 typedef T element_type;
275 typedef D deleter_type;
276
277 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000278 constexpr unique_ptr() noexcept;
279 explicit unique_ptr(pointer p) noexcept;
280 unique_ptr(pointer p, see below d1) noexcept;
281 unique_ptr(pointer p, see below d2) noexcept;
282 unique_ptr(unique_ptr&& u) noexcept;
283 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000284 template <class U, class E>
Howard Hinnant719bda32011-05-28 14:41:13 +0000285 unique_ptr(unique_ptr<U, E>&& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000286 template <class U>
Marshall Clowb22274f2017-01-24 22:22:33 +0000287 unique_ptr(auto_ptr<U>&& u) noexcept; // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000288
289 // destructor
290 ~unique_ptr();
291
292 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000293 unique_ptr& operator=(unique_ptr&& u) noexcept;
294 template <class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;
295 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000296
297 // observers
298 typename add_lvalue_reference<T>::type operator*() const;
Howard Hinnant719bda32011-05-28 14:41:13 +0000299 pointer operator->() const noexcept;
300 pointer get() const noexcept;
301 deleter_type& get_deleter() noexcept;
302 const deleter_type& get_deleter() const noexcept;
303 explicit operator bool() const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000304
305 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000306 pointer release() noexcept;
307 void reset(pointer p = pointer()) noexcept;
308 void swap(unique_ptr& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000309};
310
311template <class T, class D>
312class unique_ptr<T[], D>
313{
314public:
315 typedef implementation-defined pointer;
316 typedef T element_type;
317 typedef D deleter_type;
318
319 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000320 constexpr unique_ptr() noexcept;
321 explicit unique_ptr(pointer p) noexcept;
322 unique_ptr(pointer p, see below d) noexcept;
323 unique_ptr(pointer p, see below d) noexcept;
324 unique_ptr(unique_ptr&& u) noexcept;
325 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000326
327 // destructor
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000328 ~unique_ptr();
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000329
330 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000331 unique_ptr& operator=(unique_ptr&& u) noexcept;
332 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000333
334 // observers
335 T& operator[](size_t i) const;
Howard Hinnant719bda32011-05-28 14:41:13 +0000336 pointer get() const noexcept;
337 deleter_type& get_deleter() noexcept;
338 const deleter_type& get_deleter() const noexcept;
339 explicit operator bool() const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000340
341 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000342 pointer release() noexcept;
343 void reset(pointer p = pointer()) noexcept;
344 void reset(nullptr_t) noexcept;
Vy Nguyene369bd92020-07-13 12:34:37 -0400345 template <class U> void reset(U) = delete;
Howard Hinnant719bda32011-05-28 14:41:13 +0000346 void swap(unique_ptr& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000347};
348
349template <class T, class D>
Howard Hinnant719bda32011-05-28 14:41:13 +0000350 void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000351
352template <class T1, class D1, class T2, class D2>
353 bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
354template <class T1, class D1, class T2, class D2>
355 bool operator!=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
356template <class T1, class D1, class T2, class D2>
357 bool operator<(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
358template <class T1, class D1, class T2, class D2>
359 bool operator<=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
360template <class T1, class D1, class T2, class D2>
361 bool operator>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
362template <class T1, class D1, class T2, class D2>
363 bool operator>=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
364
Howard Hinnant719bda32011-05-28 14:41:13 +0000365template <class T, class D>
366 bool operator==(const unique_ptr<T, D>& x, nullptr_t) noexcept;
367template <class T, class D>
368 bool operator==(nullptr_t, const unique_ptr<T, D>& y) noexcept;
369template <class T, class D>
370 bool operator!=(const unique_ptr<T, D>& x, nullptr_t) noexcept;
371template <class T, class D>
372 bool operator!=(nullptr_t, const unique_ptr<T, D>& y) noexcept;
373
374template <class T, class D>
375 bool operator<(const unique_ptr<T, D>& x, nullptr_t);
376template <class T, class D>
377 bool operator<(nullptr_t, const unique_ptr<T, D>& y);
378template <class T, class D>
379 bool operator<=(const unique_ptr<T, D>& x, nullptr_t);
380template <class T, class D>
381 bool operator<=(nullptr_t, const unique_ptr<T, D>& y);
382template <class T, class D>
383 bool operator>(const unique_ptr<T, D>& x, nullptr_t);
384template <class T, class D>
385 bool operator>(nullptr_t, const unique_ptr<T, D>& y);
386template <class T, class D>
387 bool operator>=(const unique_ptr<T, D>& x, nullptr_t);
388template <class T, class D>
389 bool operator>=(nullptr_t, const unique_ptr<T, D>& y);
390
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000391class bad_weak_ptr
392 : public std::exception
393{
Howard Hinnant719bda32011-05-28 14:41:13 +0000394 bad_weak_ptr() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000395};
396
Marshall Clow9e8f4a92013-07-01 18:16:03 +0000397template<class T, class... Args> unique_ptr<T> make_unique(Args&&... args); // C++14
398template<class T> unique_ptr<T> make_unique(size_t n); // C++14
399template<class T, class... Args> unspecified make_unique(Args&&...) = delete; // C++14, T == U[N]
400
Marshall Clowe52a3242017-11-27 15:51:36 +0000401template<class E, class T, class Y, class D>
402 basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, unique_ptr<Y, D> const& p);
403
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000404template<class T>
405class shared_ptr
406{
407public:
Konstantin Varlamovd15d1c72021-10-25 11:15:38 -0400408 typedef T element_type; // until C++17
409 typedef remove_extent_t<T> element_type; // since C++17
Eric Fiselierae5b6672016-06-27 01:02:43 +0000410 typedef weak_ptr<T> weak_type; // C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000411
412 // constructors:
Howard Hinnant719bda32011-05-28 14:41:13 +0000413 constexpr shared_ptr() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000414 template<class Y> explicit shared_ptr(Y* p);
415 template<class Y, class D> shared_ptr(Y* p, D d);
416 template<class Y, class D, class A> shared_ptr(Y* p, D d, A a);
417 template <class D> shared_ptr(nullptr_t p, D d);
418 template <class D, class A> shared_ptr(nullptr_t p, D d, A a);
Howard Hinnant719bda32011-05-28 14:41:13 +0000419 template<class Y> shared_ptr(const shared_ptr<Y>& r, T *p) noexcept;
420 shared_ptr(const shared_ptr& r) noexcept;
421 template<class Y> shared_ptr(const shared_ptr<Y>& r) noexcept;
422 shared_ptr(shared_ptr&& r) noexcept;
423 template<class Y> shared_ptr(shared_ptr<Y>&& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000424 template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
Marshall Clowb22274f2017-01-24 22:22:33 +0000425 template<class Y> shared_ptr(auto_ptr<Y>&& r); // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000426 template <class Y, class D> shared_ptr(unique_ptr<Y, D>&& r);
427 shared_ptr(nullptr_t) : shared_ptr() { }
428
429 // destructor:
430 ~shared_ptr();
431
432 // assignment:
Howard Hinnant719bda32011-05-28 14:41:13 +0000433 shared_ptr& operator=(const shared_ptr& r) noexcept;
434 template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r) noexcept;
435 shared_ptr& operator=(shared_ptr&& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000436 template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r);
Marshall Clowb22274f2017-01-24 22:22:33 +0000437 template<class Y> shared_ptr& operator=(auto_ptr<Y>&& r); // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000438 template <class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r);
439
440 // modifiers:
Howard Hinnant719bda32011-05-28 14:41:13 +0000441 void swap(shared_ptr& r) noexcept;
442 void reset() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000443 template<class Y> void reset(Y* p);
444 template<class Y, class D> void reset(Y* p, D d);
445 template<class Y, class D, class A> void reset(Y* p, D d, A a);
446
Howard Hinnant719bda32011-05-28 14:41:13 +0000447 // observers:
448 T* get() const noexcept;
449 T& operator*() const noexcept;
450 T* operator->() const noexcept;
451 long use_count() const noexcept;
452 bool unique() const noexcept;
453 explicit operator bool() const noexcept;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000454 template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
455 template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000456};
457
Logan Smitha5e4d7e2020-05-07 12:07:01 -0400458template<class T>
459shared_ptr(weak_ptr<T>) -> shared_ptr<T>;
460template<class T, class D>
461shared_ptr(unique_ptr<T, D>) -> shared_ptr<T>;
462
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000463// shared_ptr comparisons:
464template<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;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000472template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000473 bool operator<=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000474template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000475 bool operator>=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
476
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>
484 bool 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>
488bool 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;
497template <class T>
498 bool operator>=(const shared_ptr<T>& x, nullptr_t) noexcept;
499template <class T>
500 bool operator>=(nullptr_t, const shared_ptr<T>& y) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000501
502// shared_ptr specialized algorithms:
Howard Hinnant719bda32011-05-28 14:41:13 +0000503template<class T> void swap(shared_ptr<T>& a, shared_ptr<T>& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000504
505// shared_ptr casts:
506template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000507 shared_ptr<T> static_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000508template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000509 shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000510template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000511 shared_ptr<T> const_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000512
513// shared_ptr I/O:
514template<class E, class T, class Y>
515 basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, shared_ptr<Y> const& p);
516
517// shared_ptr get_deleter:
Howard Hinnant719bda32011-05-28 14:41:13 +0000518template<class D, class T> D* get_deleter(shared_ptr<T> const& p) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000519
520template<class T, class... Args>
521 shared_ptr<T> make_shared(Args&&... args);
522template<class T, class A, class... Args>
523 shared_ptr<T> allocate_shared(const A& a, Args&&... args);
524
525template<class T>
526class weak_ptr
527{
528public:
Konstantin Varlamovd15d1c72021-10-25 11:15:38 -0400529 typedef T element_type; // until C++17
530 typedef remove_extent_t<T> element_type; // since C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000531
532 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000533 constexpr weak_ptr() noexcept;
534 template<class Y> weak_ptr(shared_ptr<Y> const& r) noexcept;
535 weak_ptr(weak_ptr const& r) noexcept;
536 template<class Y> weak_ptr(weak_ptr<Y> const& r) noexcept;
Marshall Clowe9b0a5c2014-03-05 03:12:04 +0000537 weak_ptr(weak_ptr&& r) noexcept; // C++14
538 template<class Y> weak_ptr(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000539
540 // destructor
541 ~weak_ptr();
542
543 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000544 weak_ptr& operator=(weak_ptr const& r) noexcept;
545 template<class Y> weak_ptr& operator=(weak_ptr<Y> const& r) noexcept;
546 template<class Y> weak_ptr& operator=(shared_ptr<Y> const& r) noexcept;
Marshall Clowe9b0a5c2014-03-05 03:12:04 +0000547 weak_ptr& operator=(weak_ptr&& r) noexcept; // C++14
548 template<class Y> weak_ptr& operator=(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000549
550 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000551 void swap(weak_ptr& r) noexcept;
552 void reset() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000553
554 // observers
Howard Hinnant719bda32011-05-28 14:41:13 +0000555 long use_count() const noexcept;
556 bool expired() const noexcept;
557 shared_ptr<T> lock() const noexcept;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000558 template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
559 template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000560};
561
Logan Smitha5e4d7e2020-05-07 12:07:01 -0400562template<class T>
563weak_ptr(shared_ptr<T>) -> weak_ptr<T>;
564
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000565// weak_ptr specialized algorithms:
Howard Hinnant719bda32011-05-28 14:41:13 +0000566template<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000567
568// class owner_less:
569template<class T> struct owner_less;
570
571template<class T>
Eric Fiselierea6fdf62019-06-23 20:47:21 +0000572struct owner_less<shared_ptr<T> >
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000573 : binary_function<shared_ptr<T>, shared_ptr<T>, bool>
574{
575 typedef bool result_type;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000576 bool operator()(shared_ptr<T> const&, shared_ptr<T> const&) const noexcept;
577 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
578 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000579};
580
581template<class T>
Eric Fiselierea6fdf62019-06-23 20:47:21 +0000582struct owner_less<weak_ptr<T> >
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000583 : binary_function<weak_ptr<T>, weak_ptr<T>, bool>
584{
585 typedef bool result_type;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000586 bool operator()(weak_ptr<T> const&, weak_ptr<T> const&) const noexcept;
587 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
588 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
589};
590
591template <> // Added in C++14
592struct owner_less<void>
593{
594 template <class _Tp, class _Up>
595 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
596 template <class _Tp, class _Up>
597 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept;
598 template <class _Tp, class _Up>
599 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
600 template <class _Tp, class _Up>
601 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept;
602
603 typedef void is_transparent;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000604};
605
606template<class T>
607class enable_shared_from_this
608{
609protected:
Howard Hinnant719bda32011-05-28 14:41:13 +0000610 constexpr enable_shared_from_this() noexcept;
611 enable_shared_from_this(enable_shared_from_this const&) noexcept;
612 enable_shared_from_this& operator=(enable_shared_from_this const&) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000613 ~enable_shared_from_this();
614public:
615 shared_ptr<T> shared_from_this();
616 shared_ptr<T const> shared_from_this() const;
617};
618
619template<class T>
620 bool atomic_is_lock_free(const shared_ptr<T>* p);
621template<class T>
622 shared_ptr<T> atomic_load(const shared_ptr<T>* p);
623template<class T>
624 shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order mo);
625template<class T>
626 void atomic_store(shared_ptr<T>* p, shared_ptr<T> r);
627template<class T>
628 void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
629template<class T>
630 shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r);
631template<class T>
632 shared_ptr<T>
633 atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
634template<class T>
635 bool
636 atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
637template<class T>
638 bool
639 atomic_compare_exchange_strong( shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
640template<class T>
641 bool
642 atomic_compare_exchange_weak_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
643 shared_ptr<T> w, memory_order success,
644 memory_order failure);
645template<class T>
646 bool
647 atomic_compare_exchange_strong_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
648 shared_ptr<T> w, memory_order success,
649 memory_order failure);
650// Hash support
651template <class T> struct hash;
652template <class T, class D> struct hash<unique_ptr<T, D> >;
653template <class T> struct hash<shared_ptr<T> >;
654
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000655template <class T, class Alloc>
656 inline constexpr bool uses_allocator_v = uses_allocator<T, Alloc>::value;
657
Howard Hinnantc51e1022010-05-11 19:42:16 +0000658void* align(size_t alignment, size_t size, void*& ptr, size_t& space);
659
660} // std
661
662*/
663
664#include <__config>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000665#include <__functional_base>
Louis Dionne735bc462021-04-14 13:59:03 -0400666#include <__memory/addressof.h>
Louis Dionne174abd42021-04-14 14:00:48 -0400667#include <__memory/allocation_guard.h>
Louis Dionne1e38faa2021-04-09 12:48:34 -0400668#include <__memory/allocator.h>
Christopher Di Bella55d7a822021-07-01 09:25:35 -0400669#include <__memory/allocator_arg_t.h>
Louis Dionnefa621d72020-12-10 18:28:13 -0500670#include <__memory/allocator_traits.h>
Louis Dionne556fcd02021-04-09 14:43:01 -0400671#include <__memory/compressed_pair.h>
Louis Dionne735bc462021-04-14 13:59:03 -0400672#include <__memory/construct_at.h>
Louis Dionnefa621d72020-12-10 18:28:13 -0500673#include <__memory/pointer_traits.h>
Louis Dionnee0103192021-04-09 14:45:18 -0400674#include <__memory/raw_storage_iterator.h>
Louis Dionnec7166582021-04-09 15:16:14 -0400675#include <__memory/shared_ptr.h>
Louis Dionne00180872021-04-09 12:58:00 -0400676#include <__memory/temporary_buffer.h>
Louis Dionnec7ef68c2021-04-09 14:47:46 -0400677#include <__memory/uninitialized_algorithms.h>
Louis Dionne7eadb032021-04-09 15:00:57 -0400678#include <__memory/unique_ptr.h>
Christopher Di Bella55d7a822021-07-01 09:25:35 -0400679#include <__memory/uses_allocator.h>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400680#include <compare>
681#include <cstddef>
682#include <cstdint>
683#include <cstring>
684#include <iosfwd>
685#include <iterator>
686#include <new>
687#include <stdexcept>
688#include <tuple>
689#include <type_traits>
690#include <typeinfo>
691#include <utility>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000692#include <version>
Howard Hinnant9fa30202012-07-30 01:40:57 +0000693
Louis Dionned9c36412021-04-14 14:06:55 -0400694#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
695# include <__memory/auto_ptr.h>
696#endif
697
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000698#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000699#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000700#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000701
702_LIBCPP_BEGIN_NAMESPACE_STD
703
Louis Dionned6651542020-11-03 12:05:55 -0500704template <class _Alloc, class _Ptr>
705_LIBCPP_INLINE_VISIBILITY
706void __construct_forward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2) {
707 static_assert(__is_cpp17_move_insertable<_Alloc>::value,
Arthur O'Dwyerf2016bb2020-12-12 11:43:15 -0500708 "The specified type does not meet the requirements of Cpp17MoveInsertable");
Louis Dionned6651542020-11-03 12:05:55 -0500709 typedef allocator_traits<_Alloc> _Traits;
710 for (; __begin1 != __end1; ++__begin1, (void)++__begin2) {
711 _Traits::construct(__a, _VSTD::__to_address(__begin2),
712#ifdef _LIBCPP_NO_EXCEPTIONS
713 _VSTD::move(*__begin1)
714#else
715 _VSTD::move_if_noexcept(*__begin1)
716#endif
717 );
718 }
719}
720
721template <class _Alloc, class _Tp, typename enable_if<
722 (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) &&
723 is_trivially_move_constructible<_Tp>::value
724>::type>
725_LIBCPP_INLINE_VISIBILITY
726void __construct_forward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2) {
727 ptrdiff_t _Np = __end1 - __begin1;
728 if (_Np > 0) {
729 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
730 __begin2 += _Np;
731 }
732}
733
734template <class _Alloc, class _Iter, class _Ptr>
735_LIBCPP_INLINE_VISIBILITY
736void __construct_range_forward(_Alloc& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2) {
737 typedef allocator_traits<_Alloc> _Traits;
738 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2) {
739 _Traits::construct(__a, _VSTD::__to_address(__begin2), *__begin1);
740 }
741}
742
743template <class _Alloc, class _Source, class _Dest,
744 class _RawSource = typename remove_const<_Source>::type,
745 class _RawDest = typename remove_const<_Dest>::type,
746 class =
747 typename enable_if<
748 is_trivially_copy_constructible<_Dest>::value &&
749 is_same<_RawSource, _RawDest>::value &&
750 (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Dest*, _Source&>::value)
751 >::type>
752_LIBCPP_INLINE_VISIBILITY
753void __construct_range_forward(_Alloc&, _Source* __begin1, _Source* __end1, _Dest*& __begin2) {
754 ptrdiff_t _Np = __end1 - __begin1;
755 if (_Np > 0) {
756 _VSTD::memcpy(const_cast<_RawDest*>(__begin2), __begin1, _Np * sizeof(_Dest));
757 __begin2 += _Np;
758 }
759}
760
761template <class _Alloc, class _Ptr>
762_LIBCPP_INLINE_VISIBILITY
763void __construct_backward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2) {
764 static_assert(__is_cpp17_move_insertable<_Alloc>::value,
765 "The specified type does not meet the requirements of Cpp17MoveInsertable");
766 typedef allocator_traits<_Alloc> _Traits;
767 while (__end1 != __begin1) {
768 _Traits::construct(__a, _VSTD::__to_address(__end2 - 1),
769#ifdef _LIBCPP_NO_EXCEPTIONS
770 _VSTD::move(*--__end1)
771#else
772 _VSTD::move_if_noexcept(*--__end1)
773#endif
774 );
775 --__end2;
776 }
777}
778
779template <class _Alloc, class _Tp, class = typename enable_if<
780 (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) &&
781 is_trivially_move_constructible<_Tp>::value
782>::type>
783_LIBCPP_INLINE_VISIBILITY
784void __construct_backward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2) {
785 ptrdiff_t _Np = __end1 - __begin1;
786 __end2 -= _Np;
787 if (_Np > 0)
Louis Dionneaf6be622021-07-27 17:30:47 -0400788 _VSTD::memcpy(static_cast<void*>(__end2), static_cast<void const*>(__begin1), _Np * sizeof(_Tp));
Louis Dionned6651542020-11-03 12:05:55 -0500789}
790
Howard Hinnantc51e1022010-05-11 19:42:16 +0000791struct __destruct_n
792{
793private:
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +0000794 size_t __size_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000795
796 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +0000797 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +0000798 {for (size_t __i = 0; __i < __size_; ++__i, ++__p) __p->~_Tp();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000799
800 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +0000801 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000802 {}
803
Howard Hinnant719bda32011-05-28 14:41:13 +0000804 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +0000805 {++__size_;}
Howard Hinnant719bda32011-05-28 14:41:13 +0000806 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000807 {}
808
Howard Hinnant719bda32011-05-28 14:41:13 +0000809 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +0000810 {__size_ = __s;}
Howard Hinnant719bda32011-05-28 14:41:13 +0000811 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000812 {}
813public:
Howard Hinnant719bda32011-05-28 14:41:13 +0000814 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +0000815 : __size_(__s) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000816
817 template <class _Tp>
Bruce Mitchener170d8972020-11-24 12:53:53 -0500818 _LIBCPP_INLINE_VISIBILITY void __incr() _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +0000819 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000820
821 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +0000822 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +0000823 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000824
825 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +0000826 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +0000827 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000828};
829
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000830_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000831
Marshall Clow8982dcd2015-07-13 20:04:56 +0000832// --- Helper for container swap --
833template <typename _Alloc>
Marshall Clow8982dcd2015-07-13 20:04:56 +0000834_LIBCPP_INLINE_VISIBILITY
835void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
836#if _LIBCPP_STD_VER >= 14
837 _NOEXCEPT
838#else
839 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
840#endif
841{
842 using _VSTD::swap;
843 swap(__a1, __a2);
844}
845
846template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +0000847inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +0000848void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
849
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -0500850template <typename _Alloc>
851inline _LIBCPP_INLINE_VISIBILITY
852void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
853#if _LIBCPP_STD_VER >= 14
854 _NOEXCEPT
855#else
856 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
857#endif
858{
859 _VSTD::__swap_allocator(__a1, __a2,
Arthur O'Dwyerd8dddf52021-05-10 13:13:04 -0400860 integral_constant<bool, allocator_traits<_Alloc>::propagate_on_container_swap::value>());
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -0500861}
862
Marshall Clowff91de82015-08-18 19:51:37 +0000863template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
Aditya Kumar7c5db692017-08-20 10:38:55 +0000864struct __noexcept_move_assign_container : public integral_constant<bool,
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000865 _Traits::propagate_on_container_move_assignment::value
866#if _LIBCPP_STD_VER > 14
867 || _Traits::is_always_equal::value
868#else
869 && is_nothrow_move_assignable<_Alloc>::value
870#endif
871 > {};
Marshall Clow8982dcd2015-07-13 20:04:56 +0000872
Marshall Clowa591b9a2016-07-11 21:38:08 +0000873
Marshall Clowa591b9a2016-07-11 21:38:08 +0000874template <class _Tp, class _Alloc>
875struct __temp_value {
876 typedef allocator_traits<_Alloc> _Traits;
Aditya Kumar7c5db692017-08-20 10:38:55 +0000877
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +0000878 typename aligned_storage<sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)>::type __v;
Marshall Clowa591b9a2016-07-11 21:38:08 +0000879 _Alloc &__a;
880
881 _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
882 _Tp & get() { return *__addr(); }
Aditya Kumar7c5db692017-08-20 10:38:55 +0000883
Marshall Clowa591b9a2016-07-11 21:38:08 +0000884 template<class... _Args>
Peter Collingbournecb126152018-08-15 17:49:30 +0000885 _LIBCPP_NO_CFI
886 __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc) {
887 _Traits::construct(__a, reinterpret_cast<_Tp*>(addressof(__v)),
888 _VSTD::forward<_Args>(__args)...);
889 }
Aditya Kumar7c5db692017-08-20 10:38:55 +0000890
Marshall Clowa591b9a2016-07-11 21:38:08 +0000891 ~__temp_value() { _Traits::destroy(__a, __addr()); }
892 };
Marshall Clowa591b9a2016-07-11 21:38:08 +0000893
Marshall Clowe46031a2018-07-02 18:41:15 +0000894template<typename _Alloc, typename = void, typename = void>
Marshall Clow82c90aa2018-01-11 19:36:22 +0000895struct __is_allocator : false_type {};
896
897template<typename _Alloc>
898struct __is_allocator<_Alloc,
Marshall Clowe46031a2018-07-02 18:41:15 +0000899 typename __void_t<typename _Alloc::value_type>::type,
Arthur O'Dwyer3285c342021-05-10 13:04:16 -0400900 typename __void_t<decltype(declval<_Alloc&>().allocate(size_t(0)))>::type
Marshall Clowe46031a2018-07-02 18:41:15 +0000901 >
Marshall Clow82c90aa2018-01-11 19:36:22 +0000902 : true_type {};
Marshall Clow82c90aa2018-01-11 19:36:22 +0000903
Eric Fiselier74ebee62019-06-08 01:31:19 +0000904// __builtin_new_allocator -- A non-templated helper for allocating and
905// deallocating memory using __builtin_operator_new and
906// __builtin_operator_delete. It should be used in preference to
907// `std::allocator<T>` to avoid additional instantiations.
908struct __builtin_new_allocator {
909 struct __builtin_new_deleter {
910 typedef void* pointer_type;
911
912 _LIBCPP_CONSTEXPR explicit __builtin_new_deleter(size_t __size, size_t __align)
913 : __size_(__size), __align_(__align) {}
914
915 void operator()(void* p) const _NOEXCEPT {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -0500916 _VSTD::__libcpp_deallocate(p, __size_, __align_);
Eric Fiselier74ebee62019-06-08 01:31:19 +0000917 }
918
919 private:
920 size_t __size_;
921 size_t __align_;
922 };
923
924 typedef unique_ptr<void, __builtin_new_deleter> __holder_t;
925
926 static __holder_t __allocate_bytes(size_t __s, size_t __align) {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -0500927 return __holder_t(_VSTD::__libcpp_allocate(__s, __align),
Eric Fiselier74ebee62019-06-08 01:31:19 +0000928 __builtin_new_deleter(__s, __align));
929 }
930
931 static void __deallocate_bytes(void* __p, size_t __s,
932 size_t __align) _NOEXCEPT {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -0500933 _VSTD::__libcpp_deallocate(__p, __s, __align);
Eric Fiselier74ebee62019-06-08 01:31:19 +0000934 }
935
936 template <class _Tp>
937 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
938 static __holder_t __allocate_type(size_t __n) {
939 return __allocate_bytes(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
940 }
941
942 template <class _Tp>
943 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
944 static void __deallocate_type(void* __p, size_t __n) _NOEXCEPT {
945 __deallocate_bytes(__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
946 }
947};
948
949
Howard Hinnantc51e1022010-05-11 19:42:16 +0000950_LIBCPP_END_NAMESPACE_STD
951
Louis Dionne59d0b3c2019-08-05 18:29:14 +0000952#if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
Louis Dionned53d8c02019-08-06 21:11:24 +0000953# include <__pstl_memory>
Louis Dionne59d0b3c2019-08-05 18:29:14 +0000954#endif
955
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400956#endif // _LIBCPP_MEMORY