Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
| 2 | //===-------------------------- memory ------------------------------------===// |
| 3 | // |
Chandler Carruth | d201210 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 4 | // 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 Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef _LIBCPP_MEMORY |
| 11 | #define _LIBCPP_MEMORY |
| 12 | |
| 13 | /* |
| 14 | memory synopsis |
| 15 | |
| 16 | namespace std |
| 17 | { |
| 18 | |
| 19 | struct allocator_arg_t { }; |
Marshall Clow | f1bf62f | 2018-01-02 17:17:01 +0000 | [diff] [blame] | 20 | inline constexpr allocator_arg_t allocator_arg = allocator_arg_t(); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 21 | |
| 22 | template <class T, class Alloc> struct uses_allocator; |
| 23 | |
| 24 | template <class Ptr> |
| 25 | struct pointer_traits |
| 26 | { |
| 27 | typedef Ptr pointer; |
| 28 | typedef <details> element_type; |
| 29 | typedef <details> difference_type; |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 30 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 31 | template <class U> using rebind = <details>; |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 32 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 33 | static pointer pointer_to(<details>); |
| 34 | }; |
| 35 | |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 36 | template <class T> |
| 37 | struct 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 Dionne | 44e1ea8 | 2018-11-13 17:04:05 +0000 | [diff] [blame] | 45 | static pointer pointer_to(<details>) noexcept; // constexpr in C++20 |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 46 | }; |
| 47 | |
Eric Fiselier | 45c9aac | 2017-11-22 19:49:21 +0000 | [diff] [blame] | 48 | template <class T> constexpr T* to_address(T* p) noexcept; // C++20 |
Marek Kurdej | 1f0cde9 | 2020-12-06 15:23:46 +0100 | [diff] [blame] | 49 | template <class Ptr> constexpr auto to_address(const Ptr& p) noexcept; // C++20 |
Eric Fiselier | 45c9aac | 2017-11-22 19:49:21 +0000 | [diff] [blame] | 50 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 51 | template <class Alloc> |
| 52 | struct 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 Hinnant | 8e4e600 | 2010-11-18 01:40:00 +0000 | [diff] [blame] | 69 | | pointer_traits<pointer>::difference_type |
| 70 | difference_type; |
| 71 | typedef Alloc::size_type |
| 72 | | make_unsigned<difference_type>::type |
| 73 | size_type; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 74 | 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 Clow | 0b58756 | 2015-06-02 16:34:03 +0000 | [diff] [blame] | 80 | typedef Alloc::is_always_equal |
| 81 | | is_empty is_always_equal; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 82 | |
Michael Park | 99f9e91 | 2020-03-04 11:27:14 -0500 | [diff] [blame] | 83 | template <class T> using rebind_alloc = Alloc::rebind<T>::other | Alloc<T, Args...>; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 84 | template <class T> using rebind_traits = allocator_traits<rebind_alloc<T>>; |
| 85 | |
Louis Dionne | 99f5943 | 2020-09-17 12:06:13 -0400 | [diff] [blame] | 86 | 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 Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 88 | |
Louis Dionne | 99f5943 | 2020-09-17 12:06:13 -0400 | [diff] [blame] | 89 | static void deallocate(allocator_type& a, pointer p, size_type n) noexcept; // constexpr in C++20 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 90 | |
| 91 | template <class T, class... Args> |
Louis Dionne | 99f5943 | 2020-09-17 12:06:13 -0400 | [diff] [blame] | 92 | static void construct(allocator_type& a, T* p, Args&&... args); // constexpr in C++20 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 93 | |
| 94 | template <class T> |
Louis Dionne | 99f5943 | 2020-09-17 12:06:13 -0400 | [diff] [blame] | 95 | static void destroy(allocator_type& a, T* p); // constexpr in C++20 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 96 | |
Louis Dionne | 99f5943 | 2020-09-17 12:06:13 -0400 | [diff] [blame] | 97 | 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 Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | template <> |
Louis Dionne | 482a04b | 2021-06-15 16:08:38 -0400 | [diff] [blame] | 102 | class allocator<void> // removed in C++20 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 103 | { |
| 104 | public: |
| 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 | |
| 112 | template <class T> |
| 113 | class allocator |
| 114 | { |
| 115 | public: |
Louis Dionne | c0056af | 2020-08-28 12:31:16 -0400 | [diff] [blame] | 116 | typedef size_t size_type; |
| 117 | typedef ptrdiff_t difference_type; |
Michael Park | 99f9e91 | 2020-03-04 11:27:14 -0500 | [diff] [blame] | 118 | 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 Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 124 | |
Michael Park | 99f9e91 | 2020-03-04 11:27:14 -0500 | [diff] [blame] | 125 | 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 Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 131 | |
Marshall Clow | bc75976 | 2018-03-20 23:02:53 +0000 | [diff] [blame] | 132 | 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 Dionne | 99f5943 | 2020-09-17 12:06:13 -0400 | [diff] [blame] | 136 | ~allocator(); // constexpr in C++20 |
Michael Park | 99f9e91 | 2020-03-04 11:27:14 -0500 | [diff] [blame] | 137 | 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 Dionne | 99f5943 | 2020-09-17 12:06:13 -0400 | [diff] [blame] | 140 | T* allocate(size_t n); // constexpr in C++20 |
| 141 | void deallocate(T* p, size_t n) noexcept; // constexpr in C++20 |
Michael Park | 99f9e91 | 2020-03-04 11:27:14 -0500 | [diff] [blame] | 142 | size_type max_size() const noexcept; // deprecated in C++17, removed in C++20 |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 143 | template<class U, class... Args> |
Michael Park | 99f9e91 | 2020-03-04 11:27:14 -0500 | [diff] [blame] | 144 | void construct(U* p, Args&&... args); // deprecated in C++17, removed in C++20 |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 145 | template <class U> |
Michael Park | 99f9e91 | 2020-03-04 11:27:14 -0500 | [diff] [blame] | 146 | void destroy(U* p); // deprecated in C++17, removed in C++20 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | template <class T, class U> |
Louis Dionne | 99f5943 | 2020-09-17 12:06:13 -0400 | [diff] [blame] | 150 | bool operator==(const allocator<T>&, const allocator<U>&) noexcept; // constexpr in C++20 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 151 | |
| 152 | template <class T, class U> |
Louis Dionne | 99f5943 | 2020-09-17 12:06:13 -0400 | [diff] [blame] | 153 | bool operator!=(const allocator<T>&, const allocator<U>&) noexcept; // constexpr in C++20 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 154 | |
| 155 | template <class OutputIterator, class T> |
Louis Dionne | 3402c3c | 2021-05-27 12:56:12 -0400 | [diff] [blame] | 156 | class raw_storage_iterator // deprecated in C++17, removed in C++20 |
Louis Dionne | 8762e2b | 2021-05-25 18:15:58 -0400 | [diff] [blame] | 157 | : public iterator<output_iterator_tag, void, void, void, void> // until C++17 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 158 | { |
| 159 | public: |
Louis Dionne | 3402c3c | 2021-05-27 12:56:12 -0400 | [diff] [blame] | 160 | 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 Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 167 | 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 Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 174 | template <class T> pair<T*,ptrdiff_t> get_temporary_buffer(ptrdiff_t n) noexcept; |
| 175 | template <class T> void return_temporary_buffer(T* p) noexcept; |
| 176 | |
| 177 | template <class T> T* addressof(T& r) noexcept; |
Marshall Clow | 78dbe46 | 2016-11-14 18:22:19 +0000 | [diff] [blame] | 178 | template <class T> T* addressof(const T&& r) noexcept = delete; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 179 | |
| 180 | template <class InputIterator, class ForwardIterator> |
| 181 | ForwardIterator |
| 182 | uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator result); |
| 183 | |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 184 | template <class InputIterator, class Size, class ForwardIterator> |
| 185 | ForwardIterator |
| 186 | uninitialized_copy_n(InputIterator first, Size n, ForwardIterator result); |
| 187 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 188 | template <class ForwardIterator, class T> |
| 189 | void uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& x); |
| 190 | |
| 191 | template <class ForwardIterator, class Size, class T> |
Howard Hinnant | 3c81109 | 2010-11-18 16:13:03 +0000 | [diff] [blame] | 192 | ForwardIterator |
| 193 | uninitialized_fill_n(ForwardIterator first, Size n, const T& x); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 194 | |
Louis Dionne | 99f5943 | 2020-09-17 12:06:13 -0400 | [diff] [blame] | 195 | template <class T, class ...Args> |
| 196 | constexpr T* construct_at(T* location, Args&& ...args); // since C++20 |
| 197 | |
Eric Fiselier | 383f6cf | 2016-07-24 03:51:39 +0000 | [diff] [blame] | 198 | template <class T> |
Louis Dionne | 99f5943 | 2020-09-17 12:06:13 -0400 | [diff] [blame] | 199 | void destroy_at(T* location); // constexpr in C++20 |
Eric Fiselier | 383f6cf | 2016-07-24 03:51:39 +0000 | [diff] [blame] | 200 | |
| 201 | template <class ForwardIterator> |
Louis Dionne | 99f5943 | 2020-09-17 12:06:13 -0400 | [diff] [blame] | 202 | void destroy(ForwardIterator first, ForwardIterator last); // constexpr in C++20 |
Eric Fiselier | 383f6cf | 2016-07-24 03:51:39 +0000 | [diff] [blame] | 203 | |
| 204 | template <class ForwardIterator, class Size> |
Louis Dionne | 99f5943 | 2020-09-17 12:06:13 -0400 | [diff] [blame] | 205 | ForwardIterator destroy_n(ForwardIterator first, Size n); // constexpr in C++20 |
Eric Fiselier | 383f6cf | 2016-07-24 03:51:39 +0000 | [diff] [blame] | 206 | |
| 207 | template <class InputIterator, class ForwardIterator> |
| 208 | ForwardIterator uninitialized_move(InputIterator first, InputIterator last, ForwardIterator result); |
| 209 | |
| 210 | template <class InputIterator, class Size, class ForwardIterator> |
| 211 | pair<InputIterator,ForwardIterator> uninitialized_move_n(InputIterator first, Size n, ForwardIterator result); |
| 212 | |
| 213 | template <class ForwardIterator> |
| 214 | void uninitialized_value_construct(ForwardIterator first, ForwardIterator last); |
| 215 | |
| 216 | template <class ForwardIterator, class Size> |
| 217 | ForwardIterator uninitialized_value_construct_n(ForwardIterator first, Size n); |
| 218 | |
| 219 | template <class ForwardIterator> |
| 220 | void uninitialized_default_construct(ForwardIterator first, ForwardIterator last); |
| 221 | |
| 222 | template <class ForwardIterator, class Size> |
| 223 | ForwardIterator uninitialized_default_construct_n(ForwardIterator first, Size n); |
| 224 | |
Louis Dionne | 481a266 | 2018-09-23 18:35:00 +0000 | [diff] [blame] | 225 | template <class Y> struct auto_ptr_ref {}; // deprecated in C++11, removed in C++17 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 226 | |
| 227 | template<class X> |
Louis Dionne | 481a266 | 2018-09-23 18:35:00 +0000 | [diff] [blame] | 228 | class auto_ptr // deprecated in C++11, removed in C++17 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 229 | { |
| 230 | public: |
| 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 Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 252 | template <class T> |
| 253 | struct default_delete |
| 254 | { |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 255 | constexpr default_delete() noexcept = default; |
| 256 | template <class U> default_delete(const default_delete<U>&) noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 257 | |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 258 | void operator()(T*) const noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 259 | }; |
| 260 | |
| 261 | template <class T> |
| 262 | struct default_delete<T[]> |
| 263 | { |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 264 | constexpr default_delete() noexcept = default; |
| 265 | void operator()(T*) const noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 266 | template <class U> void operator()(U*) const = delete; |
| 267 | }; |
| 268 | |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 269 | template <class T, class D = default_delete<T>> |
| 270 | class unique_ptr |
| 271 | { |
| 272 | public: |
| 273 | typedef see below pointer; |
| 274 | typedef T element_type; |
| 275 | typedef D deleter_type; |
| 276 | |
| 277 | // constructors |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 278 | 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 Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 284 | template <class U, class E> |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 285 | unique_ptr(unique_ptr<U, E>&& u) noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 286 | template <class U> |
Marshall Clow | b22274f | 2017-01-24 22:22:33 +0000 | [diff] [blame] | 287 | unique_ptr(auto_ptr<U>&& u) noexcept; // removed in C++17 |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 288 | |
| 289 | // destructor |
| 290 | ~unique_ptr(); |
| 291 | |
| 292 | // assignment |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 293 | 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 Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 296 | |
| 297 | // observers |
| 298 | typename add_lvalue_reference<T>::type operator*() const; |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 299 | 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 Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 304 | |
| 305 | // modifiers |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 306 | pointer release() noexcept; |
| 307 | void reset(pointer p = pointer()) noexcept; |
| 308 | void swap(unique_ptr& u) noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 309 | }; |
| 310 | |
| 311 | template <class T, class D> |
| 312 | class unique_ptr<T[], D> |
| 313 | { |
| 314 | public: |
| 315 | typedef implementation-defined pointer; |
| 316 | typedef T element_type; |
| 317 | typedef D deleter_type; |
| 318 | |
| 319 | // constructors |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 320 | 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 Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 326 | |
| 327 | // destructor |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 328 | ~unique_ptr(); |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 329 | |
| 330 | // assignment |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 331 | unique_ptr& operator=(unique_ptr&& u) noexcept; |
| 332 | unique_ptr& operator=(nullptr_t) noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 333 | |
| 334 | // observers |
| 335 | T& operator[](size_t i) const; |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 336 | 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 Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 340 | |
| 341 | // modifiers |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 342 | pointer release() noexcept; |
| 343 | void reset(pointer p = pointer()) noexcept; |
| 344 | void reset(nullptr_t) noexcept; |
Vy Nguyen | e369bd9 | 2020-07-13 12:34:37 -0400 | [diff] [blame] | 345 | template <class U> void reset(U) = delete; |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 346 | void swap(unique_ptr& u) noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 347 | }; |
| 348 | |
| 349 | template <class T, class D> |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 350 | void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y) noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 351 | |
| 352 | template <class T1, class D1, class T2, class D2> |
| 353 | bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y); |
| 354 | template <class T1, class D1, class T2, class D2> |
| 355 | bool operator!=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y); |
| 356 | template <class T1, class D1, class T2, class D2> |
| 357 | bool operator<(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y); |
| 358 | template <class T1, class D1, class T2, class D2> |
| 359 | bool operator<=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y); |
| 360 | template <class T1, class D1, class T2, class D2> |
| 361 | bool operator>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y); |
| 362 | template <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 Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 365 | template <class T, class D> |
| 366 | bool operator==(const unique_ptr<T, D>& x, nullptr_t) noexcept; |
| 367 | template <class T, class D> |
| 368 | bool operator==(nullptr_t, const unique_ptr<T, D>& y) noexcept; |
| 369 | template <class T, class D> |
| 370 | bool operator!=(const unique_ptr<T, D>& x, nullptr_t) noexcept; |
| 371 | template <class T, class D> |
| 372 | bool operator!=(nullptr_t, const unique_ptr<T, D>& y) noexcept; |
| 373 | |
| 374 | template <class T, class D> |
| 375 | bool operator<(const unique_ptr<T, D>& x, nullptr_t); |
| 376 | template <class T, class D> |
| 377 | bool operator<(nullptr_t, const unique_ptr<T, D>& y); |
| 378 | template <class T, class D> |
| 379 | bool operator<=(const unique_ptr<T, D>& x, nullptr_t); |
| 380 | template <class T, class D> |
| 381 | bool operator<=(nullptr_t, const unique_ptr<T, D>& y); |
| 382 | template <class T, class D> |
| 383 | bool operator>(const unique_ptr<T, D>& x, nullptr_t); |
| 384 | template <class T, class D> |
| 385 | bool operator>(nullptr_t, const unique_ptr<T, D>& y); |
| 386 | template <class T, class D> |
| 387 | bool operator>=(const unique_ptr<T, D>& x, nullptr_t); |
| 388 | template <class T, class D> |
| 389 | bool operator>=(nullptr_t, const unique_ptr<T, D>& y); |
| 390 | |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 391 | class bad_weak_ptr |
| 392 | : public std::exception |
| 393 | { |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 394 | bad_weak_ptr() noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 395 | }; |
| 396 | |
Marshall Clow | 9e8f4a9 | 2013-07-01 18:16:03 +0000 | [diff] [blame] | 397 | template<class T, class... Args> unique_ptr<T> make_unique(Args&&... args); // C++14 |
| 398 | template<class T> unique_ptr<T> make_unique(size_t n); // C++14 |
| 399 | template<class T, class... Args> unspecified make_unique(Args&&...) = delete; // C++14, T == U[N] |
| 400 | |
Marshall Clow | e52a324 | 2017-11-27 15:51:36 +0000 | [diff] [blame] | 401 | template<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 Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 404 | template<class T> |
| 405 | class shared_ptr |
| 406 | { |
| 407 | public: |
| 408 | typedef T element_type; |
Eric Fiselier | ae5b667 | 2016-06-27 01:02:43 +0000 | [diff] [blame] | 409 | typedef weak_ptr<T> weak_type; // C++17 |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 410 | |
| 411 | // constructors: |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 412 | constexpr shared_ptr() noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 413 | template<class Y> explicit shared_ptr(Y* p); |
| 414 | template<class Y, class D> shared_ptr(Y* p, D d); |
| 415 | template<class Y, class D, class A> shared_ptr(Y* p, D d, A a); |
| 416 | template <class D> shared_ptr(nullptr_t p, D d); |
| 417 | template <class D, class A> shared_ptr(nullptr_t p, D d, A a); |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 418 | template<class Y> shared_ptr(const shared_ptr<Y>& r, T *p) noexcept; |
| 419 | shared_ptr(const shared_ptr& r) noexcept; |
| 420 | template<class Y> shared_ptr(const shared_ptr<Y>& r) noexcept; |
| 421 | shared_ptr(shared_ptr&& r) noexcept; |
| 422 | template<class Y> shared_ptr(shared_ptr<Y>&& r) noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 423 | template<class Y> explicit shared_ptr(const weak_ptr<Y>& r); |
Marshall Clow | b22274f | 2017-01-24 22:22:33 +0000 | [diff] [blame] | 424 | template<class Y> shared_ptr(auto_ptr<Y>&& r); // removed in C++17 |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 425 | template <class Y, class D> shared_ptr(unique_ptr<Y, D>&& r); |
| 426 | shared_ptr(nullptr_t) : shared_ptr() { } |
| 427 | |
| 428 | // destructor: |
| 429 | ~shared_ptr(); |
| 430 | |
| 431 | // assignment: |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 432 | shared_ptr& operator=(const shared_ptr& r) noexcept; |
| 433 | template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r) noexcept; |
| 434 | shared_ptr& operator=(shared_ptr&& r) noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 435 | template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r); |
Marshall Clow | b22274f | 2017-01-24 22:22:33 +0000 | [diff] [blame] | 436 | template<class Y> shared_ptr& operator=(auto_ptr<Y>&& r); // removed in C++17 |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 437 | template <class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r); |
| 438 | |
| 439 | // modifiers: |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 440 | void swap(shared_ptr& r) noexcept; |
| 441 | void reset() noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 442 | template<class Y> void reset(Y* p); |
| 443 | template<class Y, class D> void reset(Y* p, D d); |
| 444 | template<class Y, class D, class A> void reset(Y* p, D d, A a); |
| 445 | |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 446 | // observers: |
| 447 | T* get() const noexcept; |
| 448 | T& operator*() const noexcept; |
| 449 | T* operator->() const noexcept; |
| 450 | long use_count() const noexcept; |
| 451 | bool unique() const noexcept; |
| 452 | explicit operator bool() const noexcept; |
Marshall Clow | 18a7cd5 | 2017-04-11 17:08:53 +0000 | [diff] [blame] | 453 | template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept; |
| 454 | template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 455 | }; |
| 456 | |
Logan Smith | a5e4d7e | 2020-05-07 12:07:01 -0400 | [diff] [blame] | 457 | template<class T> |
| 458 | shared_ptr(weak_ptr<T>) -> shared_ptr<T>; |
| 459 | template<class T, class D> |
| 460 | shared_ptr(unique_ptr<T, D>) -> shared_ptr<T>; |
| 461 | |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 462 | // shared_ptr comparisons: |
| 463 | template<class T, class U> |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 464 | bool operator==(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 465 | template<class T, class U> |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 466 | bool operator!=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 467 | template<class T, class U> |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 468 | bool operator<(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 469 | template<class T, class U> |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 470 | bool operator>(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 471 | template<class T, class U> |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 472 | bool operator<=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 473 | template<class T, class U> |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 474 | bool operator>=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept; |
| 475 | |
| 476 | template <class T> |
| 477 | bool operator==(const shared_ptr<T>& x, nullptr_t) noexcept; |
| 478 | template <class T> |
| 479 | bool operator==(nullptr_t, const shared_ptr<T>& y) noexcept; |
| 480 | template <class T> |
| 481 | bool operator!=(const shared_ptr<T>& x, nullptr_t) noexcept; |
| 482 | template <class T> |
| 483 | bool operator!=(nullptr_t, const shared_ptr<T>& y) noexcept; |
| 484 | template <class T> |
| 485 | bool operator<(const shared_ptr<T>& x, nullptr_t) noexcept; |
| 486 | template <class T> |
| 487 | bool operator<(nullptr_t, const shared_ptr<T>& y) noexcept; |
| 488 | template <class T> |
| 489 | bool operator<=(const shared_ptr<T>& x, nullptr_t) noexcept; |
| 490 | template <class T> |
| 491 | bool operator<=(nullptr_t, const shared_ptr<T>& y) noexcept; |
| 492 | template <class T> |
| 493 | bool operator>(const shared_ptr<T>& x, nullptr_t) noexcept; |
| 494 | template <class T> |
| 495 | bool operator>(nullptr_t, const shared_ptr<T>& y) noexcept; |
| 496 | template <class T> |
| 497 | bool operator>=(const shared_ptr<T>& x, nullptr_t) noexcept; |
| 498 | template <class T> |
| 499 | bool operator>=(nullptr_t, const shared_ptr<T>& y) noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 500 | |
| 501 | // shared_ptr specialized algorithms: |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 502 | template<class T> void swap(shared_ptr<T>& a, shared_ptr<T>& b) noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 503 | |
| 504 | // shared_ptr casts: |
| 505 | template<class T, class U> |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 506 | shared_ptr<T> static_pointer_cast(shared_ptr<U> const& r) noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 507 | template<class T, class U> |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 508 | shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const& r) noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 509 | template<class T, class U> |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 510 | shared_ptr<T> const_pointer_cast(shared_ptr<U> const& r) noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 511 | |
| 512 | // shared_ptr I/O: |
| 513 | template<class E, class T, class Y> |
| 514 | basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, shared_ptr<Y> const& p); |
| 515 | |
| 516 | // shared_ptr get_deleter: |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 517 | template<class D, class T> D* get_deleter(shared_ptr<T> const& p) noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 518 | |
| 519 | template<class T, class... Args> |
| 520 | shared_ptr<T> make_shared(Args&&... args); |
| 521 | template<class T, class A, class... Args> |
| 522 | shared_ptr<T> allocate_shared(const A& a, Args&&... args); |
| 523 | |
| 524 | template<class T> |
| 525 | class weak_ptr |
| 526 | { |
| 527 | public: |
| 528 | typedef T element_type; |
| 529 | |
| 530 | // constructors |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 531 | constexpr weak_ptr() noexcept; |
| 532 | template<class Y> weak_ptr(shared_ptr<Y> const& r) noexcept; |
| 533 | weak_ptr(weak_ptr const& r) noexcept; |
| 534 | template<class Y> weak_ptr(weak_ptr<Y> const& r) noexcept; |
Marshall Clow | e9b0a5c | 2014-03-05 03:12:04 +0000 | [diff] [blame] | 535 | weak_ptr(weak_ptr&& r) noexcept; // C++14 |
| 536 | template<class Y> weak_ptr(weak_ptr<Y>&& r) noexcept; // C++14 |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 537 | |
| 538 | // destructor |
| 539 | ~weak_ptr(); |
| 540 | |
| 541 | // assignment |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 542 | weak_ptr& operator=(weak_ptr const& r) noexcept; |
| 543 | template<class Y> weak_ptr& operator=(weak_ptr<Y> const& r) noexcept; |
| 544 | template<class Y> weak_ptr& operator=(shared_ptr<Y> const& r) noexcept; |
Marshall Clow | e9b0a5c | 2014-03-05 03:12:04 +0000 | [diff] [blame] | 545 | weak_ptr& operator=(weak_ptr&& r) noexcept; // C++14 |
| 546 | template<class Y> weak_ptr& operator=(weak_ptr<Y>&& r) noexcept; // C++14 |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 547 | |
| 548 | // modifiers |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 549 | void swap(weak_ptr& r) noexcept; |
| 550 | void reset() noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 551 | |
| 552 | // observers |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 553 | long use_count() const noexcept; |
| 554 | bool expired() const noexcept; |
| 555 | shared_ptr<T> lock() const noexcept; |
Marshall Clow | 18a7cd5 | 2017-04-11 17:08:53 +0000 | [diff] [blame] | 556 | template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept; |
| 557 | template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 558 | }; |
| 559 | |
Logan Smith | a5e4d7e | 2020-05-07 12:07:01 -0400 | [diff] [blame] | 560 | template<class T> |
| 561 | weak_ptr(shared_ptr<T>) -> weak_ptr<T>; |
| 562 | |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 563 | // weak_ptr specialized algorithms: |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 564 | template<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 565 | |
| 566 | // class owner_less: |
| 567 | template<class T> struct owner_less; |
| 568 | |
| 569 | template<class T> |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 570 | struct owner_less<shared_ptr<T> > |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 571 | : binary_function<shared_ptr<T>, shared_ptr<T>, bool> |
| 572 | { |
| 573 | typedef bool result_type; |
Marshall Clow | 18a7cd5 | 2017-04-11 17:08:53 +0000 | [diff] [blame] | 574 | bool operator()(shared_ptr<T> const&, shared_ptr<T> const&) const noexcept; |
| 575 | bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept; |
| 576 | bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 577 | }; |
| 578 | |
| 579 | template<class T> |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 580 | struct owner_less<weak_ptr<T> > |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 581 | : binary_function<weak_ptr<T>, weak_ptr<T>, bool> |
| 582 | { |
| 583 | typedef bool result_type; |
Marshall Clow | 18a7cd5 | 2017-04-11 17:08:53 +0000 | [diff] [blame] | 584 | bool operator()(weak_ptr<T> const&, weak_ptr<T> const&) const noexcept; |
| 585 | bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept; |
| 586 | bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept; |
| 587 | }; |
| 588 | |
| 589 | template <> // Added in C++14 |
| 590 | struct owner_less<void> |
| 591 | { |
| 592 | template <class _Tp, class _Up> |
| 593 | bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept; |
| 594 | template <class _Tp, class _Up> |
| 595 | bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept; |
| 596 | template <class _Tp, class _Up> |
| 597 | bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept; |
| 598 | template <class _Tp, class _Up> |
| 599 | bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept; |
| 600 | |
| 601 | typedef void is_transparent; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 602 | }; |
| 603 | |
| 604 | template<class T> |
| 605 | class enable_shared_from_this |
| 606 | { |
| 607 | protected: |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 608 | constexpr enable_shared_from_this() noexcept; |
| 609 | enable_shared_from_this(enable_shared_from_this const&) noexcept; |
| 610 | enable_shared_from_this& operator=(enable_shared_from_this const&) noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 611 | ~enable_shared_from_this(); |
| 612 | public: |
| 613 | shared_ptr<T> shared_from_this(); |
| 614 | shared_ptr<T const> shared_from_this() const; |
| 615 | }; |
| 616 | |
| 617 | template<class T> |
| 618 | bool atomic_is_lock_free(const shared_ptr<T>* p); |
| 619 | template<class T> |
| 620 | shared_ptr<T> atomic_load(const shared_ptr<T>* p); |
| 621 | template<class T> |
| 622 | shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order mo); |
| 623 | template<class T> |
| 624 | void atomic_store(shared_ptr<T>* p, shared_ptr<T> r); |
| 625 | template<class T> |
| 626 | void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo); |
| 627 | template<class T> |
| 628 | shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r); |
| 629 | template<class T> |
| 630 | shared_ptr<T> |
| 631 | atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo); |
| 632 | template<class T> |
| 633 | bool |
| 634 | atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w); |
| 635 | template<class T> |
| 636 | bool |
| 637 | atomic_compare_exchange_strong( shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w); |
| 638 | template<class T> |
| 639 | bool |
| 640 | atomic_compare_exchange_weak_explicit(shared_ptr<T>* p, shared_ptr<T>* v, |
| 641 | shared_ptr<T> w, memory_order success, |
| 642 | memory_order failure); |
| 643 | template<class T> |
| 644 | bool |
| 645 | atomic_compare_exchange_strong_explicit(shared_ptr<T>* p, shared_ptr<T>* v, |
| 646 | shared_ptr<T> w, memory_order success, |
| 647 | memory_order failure); |
| 648 | // Hash support |
| 649 | template <class T> struct hash; |
| 650 | template <class T, class D> struct hash<unique_ptr<T, D> >; |
| 651 | template <class T> struct hash<shared_ptr<T> >; |
| 652 | |
Marshall Clow | f1bf62f | 2018-01-02 17:17:01 +0000 | [diff] [blame] | 653 | template <class T, class Alloc> |
| 654 | inline constexpr bool uses_allocator_v = uses_allocator<T, Alloc>::value; |
| 655 | |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 656 | // Pointer safety |
Louis Dionne | e48b28f | 2021-04-13 16:43:42 -0400 | [diff] [blame] | 657 | enum class pointer_safety { relaxed, preferred, strict }; // since C++11 |
| 658 | void declare_reachable(void *p); // since C++11 |
| 659 | template <class T> T *undeclare_reachable(T *p); // since C++11 |
| 660 | void declare_no_pointers(char *p, size_t n); // since C++11 |
| 661 | void undeclare_no_pointers(char *p, size_t n); // since C++11 |
| 662 | pointer_safety get_pointer_safety() noexcept; // since C++11 |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 663 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 664 | void* align(size_t alignment, size_t size, void*& ptr, size_t& space); |
| 665 | |
| 666 | } // std |
| 667 | |
| 668 | */ |
| 669 | |
| 670 | #include <__config> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 671 | #include <__functional_base> |
Louis Dionne | 735bc46 | 2021-04-14 13:59:03 -0400 | [diff] [blame] | 672 | #include <__memory/addressof.h> |
Louis Dionne | 174abd4 | 2021-04-14 14:00:48 -0400 | [diff] [blame] | 673 | #include <__memory/allocation_guard.h> |
Louis Dionne | 1e38faa | 2021-04-09 12:48:34 -0400 | [diff] [blame] | 674 | #include <__memory/allocator.h> |
Christopher Di Bella | 55d7a82 | 2021-07-01 09:25:35 -0400 | [diff] [blame] | 675 | #include <__memory/allocator_arg_t.h> |
Louis Dionne | fa621d7 | 2020-12-10 18:28:13 -0500 | [diff] [blame] | 676 | #include <__memory/allocator_traits.h> |
Louis Dionne | 556fcd0 | 2021-04-09 14:43:01 -0400 | [diff] [blame] | 677 | #include <__memory/compressed_pair.h> |
Louis Dionne | 735bc46 | 2021-04-14 13:59:03 -0400 | [diff] [blame] | 678 | #include <__memory/construct_at.h> |
Louis Dionne | 25b7f2f | 2021-04-09 15:57:21 -0400 | [diff] [blame] | 679 | #include <__memory/pointer_safety.h> |
Louis Dionne | fa621d7 | 2020-12-10 18:28:13 -0500 | [diff] [blame] | 680 | #include <__memory/pointer_traits.h> |
Louis Dionne | e010319 | 2021-04-09 14:45:18 -0400 | [diff] [blame] | 681 | #include <__memory/raw_storage_iterator.h> |
Louis Dionne | c716658 | 2021-04-09 15:16:14 -0400 | [diff] [blame] | 682 | #include <__memory/shared_ptr.h> |
Louis Dionne | 0018087 | 2021-04-09 12:58:00 -0400 | [diff] [blame] | 683 | #include <__memory/temporary_buffer.h> |
Louis Dionne | c7ef68c | 2021-04-09 14:47:46 -0400 | [diff] [blame] | 684 | #include <__memory/uninitialized_algorithms.h> |
Louis Dionne | 7eadb03 | 2021-04-09 15:00:57 -0400 | [diff] [blame] | 685 | #include <__memory/unique_ptr.h> |
Christopher Di Bella | 55d7a82 | 2021-07-01 09:25:35 -0400 | [diff] [blame] | 686 | #include <__memory/uses_allocator.h> |
Arthur O'Dwyer | ef18160 | 2021-05-19 11:57:04 -0400 | [diff] [blame] | 687 | #include <compare> |
| 688 | #include <cstddef> |
| 689 | #include <cstdint> |
| 690 | #include <cstring> |
| 691 | #include <iosfwd> |
| 692 | #include <iterator> |
| 693 | #include <new> |
| 694 | #include <stdexcept> |
| 695 | #include <tuple> |
| 696 | #include <type_traits> |
| 697 | #include <typeinfo> |
| 698 | #include <utility> |
Marshall Clow | 0a1e750 | 2018-09-12 19:41:40 +0000 | [diff] [blame] | 699 | #include <version> |
Howard Hinnant | 9fa3020 | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 700 | |
Louis Dionne | d9c3641 | 2021-04-14 14:06:55 -0400 | [diff] [blame] | 701 | #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR) |
| 702 | # include <__memory/auto_ptr.h> |
| 703 | #endif |
| 704 | |
Howard Hinnant | aaaa52b | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 705 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 706 | #pragma GCC system_header |
Howard Hinnant | aaaa52b | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 707 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 708 | |
| 709 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 710 | |
Louis Dionne | d665154 | 2020-11-03 12:05:55 -0500 | [diff] [blame] | 711 | template <class _Alloc, class _Ptr> |
| 712 | _LIBCPP_INLINE_VISIBILITY |
| 713 | void __construct_forward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2) { |
| 714 | static_assert(__is_cpp17_move_insertable<_Alloc>::value, |
Arthur O'Dwyer | f2016bb | 2020-12-12 11:43:15 -0500 | [diff] [blame] | 715 | "The specified type does not meet the requirements of Cpp17MoveInsertable"); |
Louis Dionne | d665154 | 2020-11-03 12:05:55 -0500 | [diff] [blame] | 716 | typedef allocator_traits<_Alloc> _Traits; |
| 717 | for (; __begin1 != __end1; ++__begin1, (void)++__begin2) { |
| 718 | _Traits::construct(__a, _VSTD::__to_address(__begin2), |
| 719 | #ifdef _LIBCPP_NO_EXCEPTIONS |
| 720 | _VSTD::move(*__begin1) |
| 721 | #else |
| 722 | _VSTD::move_if_noexcept(*__begin1) |
| 723 | #endif |
| 724 | ); |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | template <class _Alloc, class _Tp, typename enable_if< |
| 729 | (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) && |
| 730 | is_trivially_move_constructible<_Tp>::value |
| 731 | >::type> |
| 732 | _LIBCPP_INLINE_VISIBILITY |
| 733 | void __construct_forward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2) { |
| 734 | ptrdiff_t _Np = __end1 - __begin1; |
| 735 | if (_Np > 0) { |
| 736 | _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp)); |
| 737 | __begin2 += _Np; |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | template <class _Alloc, class _Iter, class _Ptr> |
| 742 | _LIBCPP_INLINE_VISIBILITY |
| 743 | void __construct_range_forward(_Alloc& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2) { |
| 744 | typedef allocator_traits<_Alloc> _Traits; |
| 745 | for (; __begin1 != __end1; ++__begin1, (void) ++__begin2) { |
| 746 | _Traits::construct(__a, _VSTD::__to_address(__begin2), *__begin1); |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | template <class _Alloc, class _Source, class _Dest, |
| 751 | class _RawSource = typename remove_const<_Source>::type, |
| 752 | class _RawDest = typename remove_const<_Dest>::type, |
| 753 | class = |
| 754 | typename enable_if< |
| 755 | is_trivially_copy_constructible<_Dest>::value && |
| 756 | is_same<_RawSource, _RawDest>::value && |
| 757 | (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Dest*, _Source&>::value) |
| 758 | >::type> |
| 759 | _LIBCPP_INLINE_VISIBILITY |
| 760 | void __construct_range_forward(_Alloc&, _Source* __begin1, _Source* __end1, _Dest*& __begin2) { |
| 761 | ptrdiff_t _Np = __end1 - __begin1; |
| 762 | if (_Np > 0) { |
| 763 | _VSTD::memcpy(const_cast<_RawDest*>(__begin2), __begin1, _Np * sizeof(_Dest)); |
| 764 | __begin2 += _Np; |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | template <class _Alloc, class _Ptr> |
| 769 | _LIBCPP_INLINE_VISIBILITY |
| 770 | void __construct_backward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2) { |
| 771 | static_assert(__is_cpp17_move_insertable<_Alloc>::value, |
| 772 | "The specified type does not meet the requirements of Cpp17MoveInsertable"); |
| 773 | typedef allocator_traits<_Alloc> _Traits; |
| 774 | while (__end1 != __begin1) { |
| 775 | _Traits::construct(__a, _VSTD::__to_address(__end2 - 1), |
| 776 | #ifdef _LIBCPP_NO_EXCEPTIONS |
| 777 | _VSTD::move(*--__end1) |
| 778 | #else |
| 779 | _VSTD::move_if_noexcept(*--__end1) |
| 780 | #endif |
| 781 | ); |
| 782 | --__end2; |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | template <class _Alloc, class _Tp, class = typename enable_if< |
| 787 | (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) && |
| 788 | is_trivially_move_constructible<_Tp>::value |
| 789 | >::type> |
| 790 | _LIBCPP_INLINE_VISIBILITY |
| 791 | void __construct_backward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2) { |
| 792 | ptrdiff_t _Np = __end1 - __begin1; |
| 793 | __end2 -= _Np; |
| 794 | if (_Np > 0) |
Louis Dionne | af6be62 | 2021-07-27 17:30:47 -0400 | [diff] [blame] | 795 | _VSTD::memcpy(static_cast<void*>(__end2), static_cast<void const*>(__begin1), _Np * sizeof(_Tp)); |
Louis Dionne | d665154 | 2020-11-03 12:05:55 -0500 | [diff] [blame] | 796 | } |
| 797 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 798 | struct __destruct_n |
| 799 | { |
| 800 | private: |
Eric Fiselier | c5ea1ae | 2017-06-01 02:29:37 +0000 | [diff] [blame] | 801 | size_t __size_; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 802 | |
| 803 | template <class _Tp> |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 804 | _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT |
Eric Fiselier | c5ea1ae | 2017-06-01 02:29:37 +0000 | [diff] [blame] | 805 | {for (size_t __i = 0; __i < __size_; ++__i, ++__p) __p->~_Tp();} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 806 | |
| 807 | template <class _Tp> |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 808 | _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 809 | {} |
| 810 | |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 811 | _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT |
Eric Fiselier | c5ea1ae | 2017-06-01 02:29:37 +0000 | [diff] [blame] | 812 | {++__size_;} |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 813 | _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 814 | {} |
| 815 | |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 816 | _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT |
Eric Fiselier | c5ea1ae | 2017-06-01 02:29:37 +0000 | [diff] [blame] | 817 | {__size_ = __s;} |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 818 | _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 819 | {} |
| 820 | public: |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 821 | _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT |
Eric Fiselier | c5ea1ae | 2017-06-01 02:29:37 +0000 | [diff] [blame] | 822 | : __size_(__s) {} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 823 | |
| 824 | template <class _Tp> |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 825 | _LIBCPP_INLINE_VISIBILITY void __incr() _NOEXCEPT |
Howard Hinnant | a9a897e | 2010-11-19 22:17:28 +0000 | [diff] [blame] | 826 | {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 827 | |
| 828 | template <class _Tp> |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 829 | _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT |
Howard Hinnant | a9a897e | 2010-11-19 22:17:28 +0000 | [diff] [blame] | 830 | {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 831 | |
| 832 | template <class _Tp> |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 833 | _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT |
Howard Hinnant | a9a897e | 2010-11-19 22:17:28 +0000 | [diff] [blame] | 834 | {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 835 | }; |
| 836 | |
Howard Hinnant | a37d3cf | 2013-08-12 18:38:34 +0000 | [diff] [blame] | 837 | _LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 838 | |
Marshall Clow | 8982dcd | 2015-07-13 20:04:56 +0000 | [diff] [blame] | 839 | // --- Helper for container swap -- |
| 840 | template <typename _Alloc> |
Marshall Clow | 8982dcd | 2015-07-13 20:04:56 +0000 | [diff] [blame] | 841 | _LIBCPP_INLINE_VISIBILITY |
| 842 | void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type) |
| 843 | #if _LIBCPP_STD_VER >= 14 |
| 844 | _NOEXCEPT |
| 845 | #else |
| 846 | _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value) |
| 847 | #endif |
| 848 | { |
| 849 | using _VSTD::swap; |
| 850 | swap(__a1, __a2); |
| 851 | } |
| 852 | |
| 853 | template <typename _Alloc> |
Eric Fiselier | 6585c75 | 2016-04-21 22:54:21 +0000 | [diff] [blame] | 854 | inline _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 8982dcd | 2015-07-13 20:04:56 +0000 | [diff] [blame] | 855 | void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {} |
| 856 | |
Arthur O'Dwyer | 4e0de31 | 2020-11-18 18:54:38 -0500 | [diff] [blame] | 857 | template <typename _Alloc> |
| 858 | inline _LIBCPP_INLINE_VISIBILITY |
| 859 | void __swap_allocator(_Alloc & __a1, _Alloc & __a2) |
| 860 | #if _LIBCPP_STD_VER >= 14 |
| 861 | _NOEXCEPT |
| 862 | #else |
| 863 | _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value) |
| 864 | #endif |
| 865 | { |
| 866 | _VSTD::__swap_allocator(__a1, __a2, |
Arthur O'Dwyer | d8dddf5 | 2021-05-10 13:13:04 -0400 | [diff] [blame] | 867 | integral_constant<bool, allocator_traits<_Alloc>::propagate_on_container_swap::value>()); |
Arthur O'Dwyer | 4e0de31 | 2020-11-18 18:54:38 -0500 | [diff] [blame] | 868 | } |
| 869 | |
Marshall Clow | ff91de8 | 2015-08-18 19:51:37 +0000 | [diff] [blame] | 870 | template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> > |
Aditya Kumar | 7c5db69 | 2017-08-20 10:38:55 +0000 | [diff] [blame] | 871 | struct __noexcept_move_assign_container : public integral_constant<bool, |
Marshall Clow | 2fe8a8d | 2015-08-18 18:57:00 +0000 | [diff] [blame] | 872 | _Traits::propagate_on_container_move_assignment::value |
| 873 | #if _LIBCPP_STD_VER > 14 |
| 874 | || _Traits::is_always_equal::value |
| 875 | #else |
| 876 | && is_nothrow_move_assignable<_Alloc>::value |
| 877 | #endif |
| 878 | > {}; |
Marshall Clow | 8982dcd | 2015-07-13 20:04:56 +0000 | [diff] [blame] | 879 | |
Marshall Clow | a591b9a | 2016-07-11 21:38:08 +0000 | [diff] [blame] | 880 | |
Marshall Clow | a591b9a | 2016-07-11 21:38:08 +0000 | [diff] [blame] | 881 | template <class _Tp, class _Alloc> |
| 882 | struct __temp_value { |
| 883 | typedef allocator_traits<_Alloc> _Traits; |
Aditya Kumar | 7c5db69 | 2017-08-20 10:38:55 +0000 | [diff] [blame] | 884 | |
Eric Fiselier | 0f0ed9d | 2019-01-16 01:51:12 +0000 | [diff] [blame] | 885 | typename aligned_storage<sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)>::type __v; |
Marshall Clow | a591b9a | 2016-07-11 21:38:08 +0000 | [diff] [blame] | 886 | _Alloc &__a; |
| 887 | |
| 888 | _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); } |
| 889 | _Tp & get() { return *__addr(); } |
Aditya Kumar | 7c5db69 | 2017-08-20 10:38:55 +0000 | [diff] [blame] | 890 | |
Marshall Clow | a591b9a | 2016-07-11 21:38:08 +0000 | [diff] [blame] | 891 | template<class... _Args> |
Peter Collingbourne | cb12615 | 2018-08-15 17:49:30 +0000 | [diff] [blame] | 892 | _LIBCPP_NO_CFI |
| 893 | __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc) { |
| 894 | _Traits::construct(__a, reinterpret_cast<_Tp*>(addressof(__v)), |
| 895 | _VSTD::forward<_Args>(__args)...); |
| 896 | } |
Aditya Kumar | 7c5db69 | 2017-08-20 10:38:55 +0000 | [diff] [blame] | 897 | |
Marshall Clow | a591b9a | 2016-07-11 21:38:08 +0000 | [diff] [blame] | 898 | ~__temp_value() { _Traits::destroy(__a, __addr()); } |
| 899 | }; |
Marshall Clow | a591b9a | 2016-07-11 21:38:08 +0000 | [diff] [blame] | 900 | |
Marshall Clow | e46031a | 2018-07-02 18:41:15 +0000 | [diff] [blame] | 901 | template<typename _Alloc, typename = void, typename = void> |
Marshall Clow | 82c90aa | 2018-01-11 19:36:22 +0000 | [diff] [blame] | 902 | struct __is_allocator : false_type {}; |
| 903 | |
| 904 | template<typename _Alloc> |
| 905 | struct __is_allocator<_Alloc, |
Marshall Clow | e46031a | 2018-07-02 18:41:15 +0000 | [diff] [blame] | 906 | typename __void_t<typename _Alloc::value_type>::type, |
Arthur O'Dwyer | 3285c34 | 2021-05-10 13:04:16 -0400 | [diff] [blame] | 907 | typename __void_t<decltype(declval<_Alloc&>().allocate(size_t(0)))>::type |
Marshall Clow | e46031a | 2018-07-02 18:41:15 +0000 | [diff] [blame] | 908 | > |
Marshall Clow | 82c90aa | 2018-01-11 19:36:22 +0000 | [diff] [blame] | 909 | : true_type {}; |
Marshall Clow | 82c90aa | 2018-01-11 19:36:22 +0000 | [diff] [blame] | 910 | |
Eric Fiselier | 74ebee6 | 2019-06-08 01:31:19 +0000 | [diff] [blame] | 911 | // __builtin_new_allocator -- A non-templated helper for allocating and |
| 912 | // deallocating memory using __builtin_operator_new and |
| 913 | // __builtin_operator_delete. It should be used in preference to |
| 914 | // `std::allocator<T>` to avoid additional instantiations. |
| 915 | struct __builtin_new_allocator { |
| 916 | struct __builtin_new_deleter { |
| 917 | typedef void* pointer_type; |
| 918 | |
| 919 | _LIBCPP_CONSTEXPR explicit __builtin_new_deleter(size_t __size, size_t __align) |
| 920 | : __size_(__size), __align_(__align) {} |
| 921 | |
| 922 | void operator()(void* p) const _NOEXCEPT { |
Arthur O'Dwyer | 07b2249 | 2020-11-27 11:02:06 -0500 | [diff] [blame] | 923 | _VSTD::__libcpp_deallocate(p, __size_, __align_); |
Eric Fiselier | 74ebee6 | 2019-06-08 01:31:19 +0000 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | private: |
| 927 | size_t __size_; |
| 928 | size_t __align_; |
| 929 | }; |
| 930 | |
| 931 | typedef unique_ptr<void, __builtin_new_deleter> __holder_t; |
| 932 | |
| 933 | static __holder_t __allocate_bytes(size_t __s, size_t __align) { |
Arthur O'Dwyer | 07b2249 | 2020-11-27 11:02:06 -0500 | [diff] [blame] | 934 | return __holder_t(_VSTD::__libcpp_allocate(__s, __align), |
Eric Fiselier | 74ebee6 | 2019-06-08 01:31:19 +0000 | [diff] [blame] | 935 | __builtin_new_deleter(__s, __align)); |
| 936 | } |
| 937 | |
| 938 | static void __deallocate_bytes(void* __p, size_t __s, |
| 939 | size_t __align) _NOEXCEPT { |
Arthur O'Dwyer | 07b2249 | 2020-11-27 11:02:06 -0500 | [diff] [blame] | 940 | _VSTD::__libcpp_deallocate(__p, __s, __align); |
Eric Fiselier | 74ebee6 | 2019-06-08 01:31:19 +0000 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | template <class _Tp> |
| 944 | _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE |
| 945 | static __holder_t __allocate_type(size_t __n) { |
| 946 | return __allocate_bytes(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)); |
| 947 | } |
| 948 | |
| 949 | template <class _Tp> |
| 950 | _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE |
| 951 | static void __deallocate_type(void* __p, size_t __n) _NOEXCEPT { |
| 952 | __deallocate_bytes(__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)); |
| 953 | } |
| 954 | }; |
| 955 | |
| 956 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 957 | _LIBCPP_END_NAMESPACE_STD |
| 958 | |
Louis Dionne | 59d0b3c | 2019-08-05 18:29:14 +0000 | [diff] [blame] | 959 | #if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17 |
Louis Dionne | d53d8c0 | 2019-08-06 21:11:24 +0000 | [diff] [blame] | 960 | # include <__pstl_memory> |
Louis Dionne | 59d0b3c | 2019-08-05 18:29:14 +0000 | [diff] [blame] | 961 | #endif |
| 962 | |
Louis Dionne | 2b1ceaa | 2021-04-20 12:03:32 -0400 | [diff] [blame] | 963 | #endif // _LIBCPP_MEMORY |