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