memory 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  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
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP_MEMORY
  10. #define _LIBCPP_MEMORY
  11. /*
  12. memory synopsis
  13. namespace std
  14. {
  15. struct allocator_arg_t { };
  16. inline constexpr allocator_arg_t allocator_arg = allocator_arg_t();
  17. template <class T, class Alloc> struct uses_allocator;
  18. template <class Ptr>
  19. struct pointer_traits
  20. {
  21. typedef Ptr pointer;
  22. typedef <details> element_type;
  23. typedef <details> difference_type;
  24. template <class U> using rebind = <details>;
  25. static pointer pointer_to(<details>);
  26. };
  27. template <class T>
  28. struct pointer_traits<T*>
  29. {
  30. typedef T* pointer;
  31. typedef T element_type;
  32. typedef ptrdiff_t difference_type;
  33. template <class U> using rebind = U*;
  34. static pointer pointer_to(<details>) noexcept; // constexpr in C++20
  35. };
  36. template <class T> constexpr T* to_address(T* p) noexcept; // C++20
  37. template <class Ptr> constexpr auto to_address(const Ptr& p) noexcept; // C++20
  38. template <class Alloc>
  39. struct allocator_traits
  40. {
  41. typedef Alloc allocator_type;
  42. typedef typename allocator_type::value_type
  43. value_type;
  44. typedef Alloc::pointer | value_type* pointer;
  45. typedef Alloc::const_pointer
  46. | pointer_traits<pointer>::rebind<const value_type>
  47. const_pointer;
  48. typedef Alloc::void_pointer
  49. | pointer_traits<pointer>::rebind<void>
  50. void_pointer;
  51. typedef Alloc::const_void_pointer
  52. | pointer_traits<pointer>::rebind<const void>
  53. const_void_pointer;
  54. typedef Alloc::difference_type
  55. | pointer_traits<pointer>::difference_type
  56. difference_type;
  57. typedef Alloc::size_type
  58. | make_unsigned<difference_type>::type
  59. size_type;
  60. typedef Alloc::propagate_on_container_copy_assignment
  61. | false_type propagate_on_container_copy_assignment;
  62. typedef Alloc::propagate_on_container_move_assignment
  63. | false_type propagate_on_container_move_assignment;
  64. typedef Alloc::propagate_on_container_swap
  65. | false_type propagate_on_container_swap;
  66. typedef Alloc::is_always_equal
  67. | is_empty is_always_equal;
  68. template <class T> using rebind_alloc = Alloc::rebind<T>::other | Alloc<T, Args...>;
  69. template <class T> using rebind_traits = allocator_traits<rebind_alloc<T>>;
  70. static pointer allocate(allocator_type& a, size_type n); // constexpr and [[nodiscard]] in C++20
  71. static pointer allocate(allocator_type& a, size_type n, const_void_pointer hint); // constexpr and [[nodiscard]] in C++20
  72. static void deallocate(allocator_type& a, pointer p, size_type n) noexcept; // constexpr in C++20
  73. template <class T, class... Args>
  74. static void construct(allocator_type& a, T* p, Args&&... args); // constexpr in C++20
  75. template <class T>
  76. static void destroy(allocator_type& a, T* p); // constexpr in C++20
  77. static size_type max_size(const allocator_type& a); // noexcept in C++14, constexpr in C++20
  78. static allocator_type select_on_container_copy_construction(const allocator_type& a); // constexpr in C++20
  79. };
  80. template <>
  81. class allocator<void> // removed in C++20
  82. {
  83. public:
  84. typedef void* pointer;
  85. typedef const void* const_pointer;
  86. typedef void value_type;
  87. template <class _Up> struct rebind {typedef allocator<_Up> other;};
  88. };
  89. template <class T>
  90. class allocator
  91. {
  92. public:
  93. typedef size_t size_type;
  94. typedef ptrdiff_t difference_type;
  95. typedef T* pointer; // deprecated in C++17, removed in C++20
  96. typedef const T* const_pointer; // deprecated in C++17, removed in C++20
  97. typedef typename add_lvalue_reference<T>::type
  98. reference; // deprecated in C++17, removed in C++20
  99. typedef typename add_lvalue_reference<const T>::type
  100. const_reference; // deprecated in C++17, removed in C++20
  101. typedef T value_type;
  102. template <class U> struct rebind {typedef allocator<U> other;}; // deprecated in C++17, removed in C++20
  103. typedef true_type propagate_on_container_move_assignment;
  104. typedef true_type is_always_equal;
  105. constexpr allocator() noexcept; // constexpr in C++20
  106. constexpr allocator(const allocator&) noexcept; // constexpr in C++20
  107. template <class U>
  108. constexpr allocator(const allocator<U>&) noexcept; // constexpr in C++20
  109. ~allocator(); // constexpr in C++20
  110. pointer address(reference x) const noexcept; // deprecated in C++17, removed in C++20
  111. const_pointer address(const_reference x) const noexcept; // deprecated in C++17, removed in C++20
  112. T* allocate(size_t n, const void* hint); // deprecated in C++17, removed in C++20
  113. T* allocate(size_t n); // constexpr in C++20
  114. void deallocate(T* p, size_t n) noexcept; // constexpr in C++20
  115. size_type max_size() const noexcept; // deprecated in C++17, removed in C++20
  116. template<class U, class... Args>
  117. void construct(U* p, Args&&... args); // deprecated in C++17, removed in C++20
  118. template <class U>
  119. void destroy(U* p); // deprecated in C++17, removed in C++20
  120. };
  121. template <class T, class U>
  122. bool operator==(const allocator<T>&, const allocator<U>&) noexcept; // constexpr in C++20
  123. template <class T, class U>
  124. bool operator!=(const allocator<T>&, const allocator<U>&) noexcept; // constexpr in C++20
  125. template <class OutputIterator, class T>
  126. class raw_storage_iterator // deprecated in C++17, removed in C++20
  127. : public iterator<output_iterator_tag, void, void, void, void> // until C++17
  128. {
  129. public:
  130. typedef output_iterator_tag iterator_category;
  131. typedef void value_type;
  132. typedef void difference_type; // until C++20
  133. typedef ptrdiff_t difference_type; // since C++20
  134. typedef void pointer;
  135. typedef void reference;
  136. explicit raw_storage_iterator(OutputIterator x);
  137. raw_storage_iterator& operator*();
  138. raw_storage_iterator& operator=(const T& element);
  139. raw_storage_iterator& operator++();
  140. raw_storage_iterator operator++(int);
  141. };
  142. template <class T> pair<T*,ptrdiff_t> get_temporary_buffer(ptrdiff_t n) noexcept;
  143. template <class T> void return_temporary_buffer(T* p) noexcept;
  144. template <class T> T* addressof(T& r) noexcept;
  145. template <class T> T* addressof(const T&& r) noexcept = delete;
  146. template <class InputIterator, class ForwardIterator>
  147. ForwardIterator
  148. uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator result);
  149. namespace ranges {
  150. template<class InputIterator, class OutputIterator>
  151. using uninitialized_copy_result = in_out_result<InputIterator, OutputIterator>; // since C++20
  152. template<input_iterator InputIterator, sentinel-for<InputIterator> Sentinel1, nothrow-forward-iterator OutputIterator, nothrow-sentinel-for<OutputIterator> Sentinel2>
  153. requires constructible_from<iter_value_t<OutputIterator>, iter_reference_t<InputIterator>>
  154. uninitialized_copy_result<InputIterator, OutputIterator>
  155. uninitialized_copy(InputIterator ifirst, Sentinel1 ilast, OutputIterator ofirst, Sentinel2 olast); // since C++20
  156. template<input_range InputRange, nothrow-forward-range OutputRange>
  157. requires constructible_from<range_value_t<OutputRange>, range_reference_t<InputRange>>
  158. uninitialized_copy_result<borrowed_iterator_t<InputRange>, borrowed_iterator_t<OutputRange>>
  159. uninitialized_copy(InputRange&& in_range, OutputRange&& out_range); // since C++20
  160. }
  161. template <class InputIterator, class Size, class ForwardIterator>
  162. ForwardIterator
  163. uninitialized_copy_n(InputIterator first, Size n, ForwardIterator result);
  164. namespace ranges {
  165. template<class InputIterator, class OutputIterator>
  166. using uninitialized_copy_n_result = in_out_result<InputIterator, OutputIterator>; // since C++20
  167. template<input_iterator InputIterator, nothrow-forward-iterator OutputIterator, nothrow-sentinel-for<OutputIterator> Sentinel>
  168. requires constructible_from<iter_value_t<OutputIterator>, iter_reference_t<InputIterator>>
  169. uninitialized_copy_n_result<InputIterator, OutputIterator>
  170. uninitialized_copy_n(InputIterator ifirst, iter_difference_t<InputIterator> n, OutputIterator ofirst, Sentinel olast); // since C++20
  171. }
  172. template <class ForwardIterator, class T>
  173. void uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& x);
  174. namespace ranges {
  175. template <nothrow-forward-iterator ForwardIterator, nothrow-sentinel-for<ForwardIterator> Sentinel, class T>
  176. requires constructible_from<iter_value_t<ForwardIterator>, const T&>
  177. ForwardIterator uninitialized_fill(ForwardIterator first, Sentinel last, const T& x); // since C++20
  178. template <nothrow-forward-range ForwardRange, class T>
  179. requires constructible_from<range_value_t<ForwardRange>, const T&>
  180. borrowed_iterator_t<ForwardRange> uninitialized_fill(ForwardRange&& range, const T& x); // since C++20
  181. }
  182. template <class ForwardIterator, class Size, class T>
  183. ForwardIterator
  184. uninitialized_fill_n(ForwardIterator first, Size n, const T& x);
  185. namespace ranges {
  186. template <nothrow-forward-iterator ForwardIterator, class T>
  187. requires constructible_from<iter_value_t<ForwardIterator>, const T&>
  188. ForwardIterator uninitialized_fill_n(ForwardIterator first, iter_difference_t<ForwardIterator> n); // since C++20
  189. }
  190. template <class T, class ...Args>
  191. constexpr T* construct_at(T* location, Args&& ...args); // since C++20
  192. namespace ranges {
  193. template<class T, class... Args>
  194. constexpr T* construct_at(T* location, Args&&... args); // since C++20
  195. }
  196. template <class T>
  197. void destroy_at(T* location); // constexpr in C++20
  198. namespace ranges {
  199. template<destructible T>
  200. constexpr void destroy_at(T* location) noexcept; // since C++20
  201. }
  202. template <class ForwardIterator>
  203. void destroy(ForwardIterator first, ForwardIterator last); // constexpr in C++20
  204. namespace ranges {
  205. template<nothrow-input-iterator InputIterator, nothrow-sentinel-for<InputIterator> Sentinel>
  206. requires destructible<iter_value_t<InputIterator>>
  207. constexpr InputIterator destroy(InputIterator first, Sentinel last) noexcept; // since C++20
  208. template<nothrow-input-range InputRange>
  209. requires destructible<range_value_t<InputRange>>
  210. constexpr borrowed_iterator_t<InputRange> destroy(InputRange&& range) noexcept; // since C++20
  211. }
  212. template <class ForwardIterator, class Size>
  213. ForwardIterator destroy_n(ForwardIterator first, Size n); // constexpr in C++20
  214. namespace ranges {
  215. template<nothrow-input-iterator InputIterator>
  216. requires destructible<iter_value_t<InputIterator>>
  217. constexpr InputIterator destroy_n(InputIterator first, iter_difference_t<InputIterator> n) noexcept; // since C++20
  218. }
  219. template <class InputIterator, class ForwardIterator>
  220. ForwardIterator uninitialized_move(InputIterator first, InputIterator last, ForwardIterator result);
  221. namespace ranges {
  222. template<class InputIterator, class OutputIterator>
  223. using uninitialized_move_result = in_out_result<InputIterator, OutputIterator>; // since C++20
  224. template <input_iterator InputIterator, sentinel_for<InputIterator> Sentinel1, nothrow-forward-iterator OutputIterator, nothrow-sentinel-for<O> Sentinel2>
  225. requires constructible_from<iter_value_t<OutputIterator>, iter_rvalue_reference_t<InputIterator>>
  226. uninitialized_move_result<InputIterator, OutputIterator>
  227. uninitialized_move(InputIterator ifirst, Sentinel1 ilast, OutputIterator ofirst, Sentinel2 olast); // since C++20
  228. template<input_range InputRange, nothrow-forward-range OutputRange>
  229. requires constructible_from<range_value_t<OutputRange>, range_rvalue_reference_t<InputRange>>
  230. uninitialized_move_result<borrowed_iterator_t<InputRange>, borrowed_iterator_t<OutputRange>>
  231. uninitialized_move(InputRange&& in_range, OutputRange&& out_range); // since C++20
  232. }
  233. template <class InputIterator, class Size, class ForwardIterator>
  234. pair<InputIterator,ForwardIterator> uninitialized_move_n(InputIterator first, Size n, ForwardIterator result);
  235. namespace ranges {
  236. template<class InputIterator, class OutputIterator>
  237. using uninitialized_move_n_result = in_out_result<InputIterator, OutputIterator>; // since C++20
  238. template<input_iterator InputIterator, nothrow-forward-iterator OutputIterator, nothrow-sentinel-for<OutputIterator> Sentinel>
  239. requires constructible_from<iter_value_t<OutputIterator>, iter_rvalue_reference_t<InputIterator>>
  240. uninitialized_move_n_result<InputIterator, OutputIterator>
  241. uninitialized_move_n(InputIterator ifirst, iter_difference_t<InputIterator> n, OutputIterator ofirst, Sentinel olast); // since C++20
  242. }
  243. template <class ForwardIterator>
  244. void uninitialized_value_construct(ForwardIterator first, ForwardIterator last);
  245. namespace ranges {
  246. template <nothrow-forward-iterator ForwardIterator, nothrow-sentinel-for<ForwardIterator> Sentinel>
  247. requires default_initializable<iter_value_t<ForwardIterator>>
  248. ForwardIterator uninitialized_value_construct(ForwardIterator first, Sentinel last); // since C++20
  249. template <nothrow-forward-range ForwardRange>
  250. requires default_initializable<range_value_t<ForwardRange>>
  251. borrowed_iterator_t<ForwardRange> uninitialized_value_construct(ForwardRange&& r); // since C++20
  252. }
  253. template <class ForwardIterator, class Size>
  254. ForwardIterator uninitialized_value_construct_n(ForwardIterator first, Size n);
  255. namespace ranges {
  256. template <nothrow-forward-iterator ForwardIterator>
  257. requires default_initializable<iter_value_t<ForwardIterator>>
  258. ForwardIterator uninitialized_value_construct_n(ForwardIterator first, iter_difference_t<ForwardIterator> n); // since C++20
  259. }
  260. template <class ForwardIterator>
  261. void uninitialized_default_construct(ForwardIterator first, ForwardIterator last);
  262. namespace ranges {
  263. template <nothrow-forward-iterator ForwardIterator, nothrow-sentinel-for<ForwardIterator> Sentinel>
  264. requires default_initializable<iter_value_t<ForwardIterator>>
  265. ForwardIterator uninitialized_default_construct(ForwardIterator first, Sentinel last); // since C++20
  266. template <nothrow-forward-range ForwardRange>
  267. requires default_initializable<range_value_t<ForwardRange>>
  268. borrowed_iterator_t<ForwardRange> uninitialized_default_construct(ForwardRange&& r); // since C++20
  269. }
  270. template <class ForwardIterator, class Size>
  271. ForwardIterator uninitialized_default_construct_n(ForwardIterator first, Size n);
  272. namespace ranges {
  273. template <nothrow-forward-iterator ForwardIterator>
  274. requires default_initializable<iter_value_t<ForwardIterator>>
  275. ForwardIterator uninitialized_default_construct_n(ForwardIterator first, iter_difference_t<ForwardIterator> n); // since C++20
  276. }
  277. template <class Y> struct auto_ptr_ref {}; // deprecated in C++11, removed in C++17
  278. template<class X>
  279. class auto_ptr // deprecated in C++11, removed in C++17
  280. {
  281. public:
  282. typedef X element_type;
  283. explicit auto_ptr(X* p =0) throw();
  284. auto_ptr(auto_ptr&) throw();
  285. template<class Y> auto_ptr(auto_ptr<Y>&) throw();
  286. auto_ptr& operator=(auto_ptr&) throw();
  287. template<class Y> auto_ptr& operator=(auto_ptr<Y>&) throw();
  288. auto_ptr& operator=(auto_ptr_ref<X> r) throw();
  289. ~auto_ptr() throw();
  290. typename add_lvalue_reference<X>::type operator*() const throw();
  291. X* operator->() const throw();
  292. X* get() const throw();
  293. X* release() throw();
  294. void reset(X* p =0) throw();
  295. auto_ptr(auto_ptr_ref<X>) throw();
  296. template<class Y> operator auto_ptr_ref<Y>() throw();
  297. template<class Y> operator auto_ptr<Y>() throw();
  298. };
  299. template <class T>
  300. struct default_delete
  301. {
  302. constexpr default_delete() noexcept = default;
  303. template <class U> default_delete(const default_delete<U>&) noexcept;
  304. void operator()(T*) const noexcept;
  305. };
  306. template <class T>
  307. struct default_delete<T[]>
  308. {
  309. constexpr default_delete() noexcept = default;
  310. void operator()(T*) const noexcept;
  311. template <class U> void operator()(U*) const = delete;
  312. };
  313. template <class T, class D = default_delete<T>>
  314. class unique_ptr
  315. {
  316. public:
  317. typedef see below pointer;
  318. typedef T element_type;
  319. typedef D deleter_type;
  320. // constructors
  321. constexpr unique_ptr() noexcept;
  322. explicit unique_ptr(pointer p) noexcept;
  323. unique_ptr(pointer p, see below d1) noexcept;
  324. unique_ptr(pointer p, see below d2) noexcept;
  325. unique_ptr(unique_ptr&& u) noexcept;
  326. unique_ptr(nullptr_t) noexcept : unique_ptr() { }
  327. template <class U, class E>
  328. unique_ptr(unique_ptr<U, E>&& u) noexcept;
  329. template <class U>
  330. unique_ptr(auto_ptr<U>&& u) noexcept; // removed in C++17
  331. // destructor
  332. ~unique_ptr();
  333. // assignment
  334. unique_ptr& operator=(unique_ptr&& u) noexcept;
  335. template <class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;
  336. unique_ptr& operator=(nullptr_t) noexcept;
  337. // observers
  338. typename add_lvalue_reference<T>::type operator*() const;
  339. pointer operator->() const noexcept;
  340. pointer get() const noexcept;
  341. deleter_type& get_deleter() noexcept;
  342. const deleter_type& get_deleter() const noexcept;
  343. explicit operator bool() const noexcept;
  344. // modifiers
  345. pointer release() noexcept;
  346. void reset(pointer p = pointer()) noexcept;
  347. void swap(unique_ptr& u) noexcept;
  348. };
  349. template <class T, class D>
  350. class unique_ptr<T[], D>
  351. {
  352. public:
  353. typedef implementation-defined pointer;
  354. typedef T element_type;
  355. typedef D deleter_type;
  356. // constructors
  357. constexpr unique_ptr() noexcept;
  358. explicit unique_ptr(pointer p) noexcept;
  359. unique_ptr(pointer p, see below d) noexcept;
  360. unique_ptr(pointer p, see below d) noexcept;
  361. unique_ptr(unique_ptr&& u) noexcept;
  362. unique_ptr(nullptr_t) noexcept : unique_ptr() { }
  363. // destructor
  364. ~unique_ptr();
  365. // assignment
  366. unique_ptr& operator=(unique_ptr&& u) noexcept;
  367. unique_ptr& operator=(nullptr_t) noexcept;
  368. // observers
  369. T& operator[](size_t i) const;
  370. pointer get() const noexcept;
  371. deleter_type& get_deleter() noexcept;
  372. const deleter_type& get_deleter() const noexcept;
  373. explicit operator bool() const noexcept;
  374. // modifiers
  375. pointer release() noexcept;
  376. void reset(pointer p = pointer()) noexcept;
  377. void reset(nullptr_t) noexcept;
  378. template <class U> void reset(U) = delete;
  379. void swap(unique_ptr& u) noexcept;
  380. };
  381. template <class T, class D>
  382. void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y) noexcept;
  383. template <class T1, class D1, class T2, class D2>
  384. bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
  385. template <class T1, class D1, class T2, class D2>
  386. bool operator!=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
  387. template <class T1, class D1, class T2, class D2>
  388. bool operator<(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
  389. template <class T1, class D1, class T2, class D2>
  390. bool operator<=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
  391. template <class T1, class D1, class T2, class D2>
  392. bool operator>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
  393. template <class T1, class D1, class T2, class D2>
  394. bool operator>=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
  395. template <class T, class D>
  396. bool operator==(const unique_ptr<T, D>& x, nullptr_t) noexcept;
  397. template <class T, class D>
  398. bool operator==(nullptr_t, const unique_ptr<T, D>& y) noexcept;
  399. template <class T, class D>
  400. bool operator!=(const unique_ptr<T, D>& x, nullptr_t) noexcept;
  401. template <class T, class D>
  402. bool operator!=(nullptr_t, const unique_ptr<T, D>& y) noexcept;
  403. template <class T, class D>
  404. bool operator<(const unique_ptr<T, D>& x, nullptr_t);
  405. template <class T, class D>
  406. bool operator<(nullptr_t, const unique_ptr<T, D>& y);
  407. template <class T, class D>
  408. bool operator<=(const unique_ptr<T, D>& x, nullptr_t);
  409. template <class T, class D>
  410. bool operator<=(nullptr_t, const unique_ptr<T, D>& y);
  411. template <class T, class D>
  412. bool operator>(const unique_ptr<T, D>& x, nullptr_t);
  413. template <class T, class D>
  414. bool operator>(nullptr_t, const unique_ptr<T, D>& y);
  415. template <class T, class D>
  416. bool operator>=(const unique_ptr<T, D>& x, nullptr_t);
  417. template <class T, class D>
  418. bool operator>=(nullptr_t, const unique_ptr<T, D>& y);
  419. class bad_weak_ptr
  420. : public std::exception
  421. {
  422. bad_weak_ptr() noexcept;
  423. };
  424. template<class T, class... Args> unique_ptr<T> make_unique(Args&&... args); // C++14
  425. template<class T> unique_ptr<T> make_unique(size_t n); // C++14
  426. template<class T, class... Args> unspecified make_unique(Args&&...) = delete; // C++14, T == U[N]
  427. template<class E, class T, class Y, class D>
  428. basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, unique_ptr<Y, D> const& p);
  429. template<class T>
  430. class shared_ptr
  431. {
  432. public:
  433. typedef T element_type; // until C++17
  434. typedef remove_extent_t<T> element_type; // since C++17
  435. typedef weak_ptr<T> weak_type; // C++17
  436. // constructors:
  437. constexpr shared_ptr() noexcept;
  438. template<class Y> explicit shared_ptr(Y* p);
  439. template<class Y, class D> shared_ptr(Y* p, D d);
  440. template<class Y, class D, class A> shared_ptr(Y* p, D d, A a);
  441. template <class D> shared_ptr(nullptr_t p, D d);
  442. template <class D, class A> shared_ptr(nullptr_t p, D d, A a);
  443. template<class Y> shared_ptr(const shared_ptr<Y>& r, T *p) noexcept;
  444. shared_ptr(const shared_ptr& r) noexcept;
  445. template<class Y> shared_ptr(const shared_ptr<Y>& r) noexcept;
  446. shared_ptr(shared_ptr&& r) noexcept;
  447. template<class Y> shared_ptr(shared_ptr<Y>&& r) noexcept;
  448. template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
  449. template<class Y> shared_ptr(auto_ptr<Y>&& r); // removed in C++17
  450. template <class Y, class D> shared_ptr(unique_ptr<Y, D>&& r);
  451. shared_ptr(nullptr_t) : shared_ptr() { }
  452. // destructor:
  453. ~shared_ptr();
  454. // assignment:
  455. shared_ptr& operator=(const shared_ptr& r) noexcept;
  456. template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r) noexcept;
  457. shared_ptr& operator=(shared_ptr&& r) noexcept;
  458. template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r);
  459. template<class Y> shared_ptr& operator=(auto_ptr<Y>&& r); // removed in C++17
  460. template <class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r);
  461. // modifiers:
  462. void swap(shared_ptr& r) noexcept;
  463. void reset() noexcept;
  464. template<class Y> void reset(Y* p);
  465. template<class Y, class D> void reset(Y* p, D d);
  466. template<class Y, class D, class A> void reset(Y* p, D d, A a);
  467. // observers:
  468. T* get() const noexcept;
  469. T& operator*() const noexcept;
  470. T* operator->() const noexcept;
  471. long use_count() const noexcept;
  472. bool unique() const noexcept;
  473. explicit operator bool() const noexcept;
  474. template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
  475. template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
  476. };
  477. template<class T>
  478. shared_ptr(weak_ptr<T>) -> shared_ptr<T>;
  479. template<class T, class D>
  480. shared_ptr(unique_ptr<T, D>) -> shared_ptr<T>;
  481. // shared_ptr comparisons:
  482. template<class T, class U>
  483. bool operator==(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
  484. template<class T, class U>
  485. bool operator!=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
  486. template<class T, class U>
  487. bool operator<(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
  488. template<class T, class U>
  489. bool operator>(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
  490. template<class T, class U>
  491. bool operator<=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
  492. template<class T, class U>
  493. bool operator>=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
  494. template <class T>
  495. bool operator==(const shared_ptr<T>& x, nullptr_t) noexcept;
  496. template <class T>
  497. bool operator==(nullptr_t, const shared_ptr<T>& y) noexcept;
  498. template <class T>
  499. bool operator!=(const shared_ptr<T>& x, nullptr_t) noexcept;
  500. template <class T>
  501. bool operator!=(nullptr_t, const shared_ptr<T>& y) noexcept;
  502. template <class T>
  503. bool operator<(const shared_ptr<T>& x, nullptr_t) noexcept;
  504. template <class T>
  505. bool operator<(nullptr_t, const shared_ptr<T>& y) noexcept;
  506. template <class T>
  507. bool operator<=(const shared_ptr<T>& x, nullptr_t) noexcept;
  508. template <class T>
  509. bool operator<=(nullptr_t, const shared_ptr<T>& y) noexcept;
  510. template <class T>
  511. bool operator>(const shared_ptr<T>& x, nullptr_t) noexcept;
  512. template <class T>
  513. bool operator>(nullptr_t, const shared_ptr<T>& y) noexcept;
  514. template <class T>
  515. bool operator>=(const shared_ptr<T>& x, nullptr_t) noexcept;
  516. template <class T>
  517. bool operator>=(nullptr_t, const shared_ptr<T>& y) noexcept;
  518. // shared_ptr specialized algorithms:
  519. template<class T> void swap(shared_ptr<T>& a, shared_ptr<T>& b) noexcept;
  520. // shared_ptr casts:
  521. template<class T, class U>
  522. shared_ptr<T> static_pointer_cast(shared_ptr<U> const& r) noexcept;
  523. template<class T, class U>
  524. shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const& r) noexcept;
  525. template<class T, class U>
  526. shared_ptr<T> const_pointer_cast(shared_ptr<U> const& r) noexcept;
  527. // shared_ptr I/O:
  528. template<class E, class T, class Y>
  529. basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, shared_ptr<Y> const& p);
  530. // shared_ptr get_deleter:
  531. template<class D, class T> D* get_deleter(shared_ptr<T> const& p) noexcept;
  532. template<class T, class... Args>
  533. shared_ptr<T> make_shared(Args&&... args);
  534. template<class T, class A, class... Args>
  535. shared_ptr<T> allocate_shared(const A& a, Args&&... args);
  536. template<class T>
  537. class weak_ptr
  538. {
  539. public:
  540. typedef T element_type; // until C++17
  541. typedef remove_extent_t<T> element_type; // since C++17
  542. // constructors
  543. constexpr weak_ptr() noexcept;
  544. template<class Y> weak_ptr(shared_ptr<Y> const& r) noexcept;
  545. weak_ptr(weak_ptr const& r) noexcept;
  546. template<class Y> weak_ptr(weak_ptr<Y> const& r) noexcept;
  547. weak_ptr(weak_ptr&& r) noexcept; // C++14
  548. template<class Y> weak_ptr(weak_ptr<Y>&& r) noexcept; // C++14
  549. // destructor
  550. ~weak_ptr();
  551. // assignment
  552. weak_ptr& operator=(weak_ptr const& r) noexcept;
  553. template<class Y> weak_ptr& operator=(weak_ptr<Y> const& r) noexcept;
  554. template<class Y> weak_ptr& operator=(shared_ptr<Y> const& r) noexcept;
  555. weak_ptr& operator=(weak_ptr&& r) noexcept; // C++14
  556. template<class Y> weak_ptr& operator=(weak_ptr<Y>&& r) noexcept; // C++14
  557. // modifiers
  558. void swap(weak_ptr& r) noexcept;
  559. void reset() noexcept;
  560. // observers
  561. long use_count() const noexcept;
  562. bool expired() const noexcept;
  563. shared_ptr<T> lock() const noexcept;
  564. template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
  565. template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
  566. };
  567. template<class T>
  568. weak_ptr(shared_ptr<T>) -> weak_ptr<T>;
  569. // weak_ptr specialized algorithms:
  570. template<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept;
  571. // class owner_less:
  572. template<class T> struct owner_less;
  573. template<class T>
  574. struct owner_less<shared_ptr<T> >
  575. : binary_function<shared_ptr<T>, shared_ptr<T>, bool>
  576. {
  577. typedef bool result_type;
  578. bool operator()(shared_ptr<T> const&, shared_ptr<T> const&) const noexcept;
  579. bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
  580. bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
  581. };
  582. template<class T>
  583. struct owner_less<weak_ptr<T> >
  584. : binary_function<weak_ptr<T>, weak_ptr<T>, bool>
  585. {
  586. typedef bool result_type;
  587. bool operator()(weak_ptr<T> const&, weak_ptr<T> const&) const noexcept;
  588. bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
  589. bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
  590. };
  591. template <> // Added in C++14
  592. struct owner_less<void>
  593. {
  594. template <class _Tp, class _Up>
  595. bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
  596. template <class _Tp, class _Up>
  597. bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept;
  598. template <class _Tp, class _Up>
  599. bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
  600. template <class _Tp, class _Up>
  601. bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept;
  602. typedef void is_transparent;
  603. };
  604. template<class T>
  605. class enable_shared_from_this
  606. {
  607. protected:
  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;
  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. template<class T>
  617. bool atomic_is_lock_free(const shared_ptr<T>* p);
  618. template<class T>
  619. shared_ptr<T> atomic_load(const shared_ptr<T>* p);
  620. template<class T>
  621. shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order mo);
  622. template<class T>
  623. void atomic_store(shared_ptr<T>* p, shared_ptr<T> r);
  624. template<class T>
  625. void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
  626. template<class T>
  627. shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r);
  628. template<class T>
  629. shared_ptr<T>
  630. atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
  631. template<class T>
  632. bool
  633. atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
  634. template<class T>
  635. bool
  636. atomic_compare_exchange_strong( shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
  637. template<class T>
  638. bool
  639. atomic_compare_exchange_weak_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
  640. shared_ptr<T> w, memory_order success,
  641. memory_order failure);
  642. template<class T>
  643. bool
  644. atomic_compare_exchange_strong_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
  645. shared_ptr<T> w, memory_order success,
  646. memory_order failure);
  647. // Hash support
  648. template <class T> struct hash;
  649. template <class T, class D> struct hash<unique_ptr<T, D> >;
  650. template <class T> struct hash<shared_ptr<T> >;
  651. template <class T, class Alloc>
  652. inline constexpr bool uses_allocator_v = uses_allocator<T, Alloc>::value;
  653. void* align(size_t alignment, size_t size, void*& ptr, size_t& space);
  654. } // std
  655. */
  656. #include <__config>
  657. #include <__memory/addressof.h>
  658. #include <__memory/allocation_guard.h>
  659. #include <__memory/allocator.h>
  660. #include <__memory/allocator_arg_t.h>
  661. #include <__memory/allocator_traits.h>
  662. #include <__memory/compressed_pair.h>
  663. #include <__memory/concepts.h>
  664. #include <__memory/construct_at.h>
  665. #include <__memory/pointer_traits.h>
  666. #include <__memory/ranges_construct_at.h>
  667. #include <__memory/ranges_uninitialized_algorithms.h>
  668. #include <__memory/raw_storage_iterator.h>
  669. #include <__memory/shared_ptr.h>
  670. #include <__memory/temporary_buffer.h>
  671. #include <__memory/uninitialized_algorithms.h>
  672. #include <__memory/unique_ptr.h>
  673. #include <__memory/uses_allocator.h>
  674. #include <compare>
  675. #include <cstddef>
  676. #include <cstdint>
  677. #include <cstring>
  678. #include <iosfwd>
  679. #include <iterator>
  680. #include <new>
  681. #include <stdexcept>
  682. #include <stlfwd>
  683. #include <tuple>
  684. #include <type_traits>
  685. #include <typeinfo>
  686. #include <utility>
  687. #include <version>
  688. // TODO: remove these headers
  689. #include <__functional/binary_function.h>
  690. #include <__functional/invoke.h>
  691. #include <__functional/operations.h>
  692. #include <__functional/reference_wrapper.h>
  693. #include <__functional/unary_function.h>
  694. #include <__functional/weak_result_type.h>
  695. #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
  696. # include <__memory/auto_ptr.h>
  697. #endif
  698. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  699. # pragma GCC system_header
  700. #endif
  701. _LIBCPP_BEGIN_NAMESPACE_STD
  702. template <class _Alloc, class _Ptr>
  703. _LIBCPP_INLINE_VISIBILITY
  704. void __construct_forward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2) {
  705. static_assert(__is_cpp17_move_insertable<_Alloc>::value,
  706. "The specified type does not meet the requirements of Cpp17MoveInsertable");
  707. typedef allocator_traits<_Alloc> _Traits;
  708. for (; __begin1 != __end1; ++__begin1, (void)++__begin2) {
  709. _Traits::construct(__a, _VSTD::__to_address(__begin2),
  710. #ifdef _LIBCPP_NO_EXCEPTIONS
  711. _VSTD::move(*__begin1)
  712. #else
  713. _VSTD::move_if_noexcept(*__begin1)
  714. #endif
  715. );
  716. }
  717. }
  718. template <class _Alloc, class _Tp, typename enable_if<
  719. (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) &&
  720. is_trivially_move_constructible<_Tp>::value
  721. >::type>
  722. _LIBCPP_INLINE_VISIBILITY
  723. void __construct_forward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2) {
  724. ptrdiff_t _Np = __end1 - __begin1;
  725. if (_Np > 0) {
  726. _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
  727. __begin2 += _Np;
  728. }
  729. }
  730. template <class _Alloc, class _Iter, class _Ptr>
  731. _LIBCPP_INLINE_VISIBILITY
  732. void __construct_range_forward(_Alloc& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2) {
  733. typedef allocator_traits<_Alloc> _Traits;
  734. for (; __begin1 != __end1; ++__begin1, (void) ++__begin2) {
  735. _Traits::construct(__a, _VSTD::__to_address(__begin2), *__begin1);
  736. }
  737. }
  738. template <class _Alloc, class _Source, class _Dest,
  739. class _RawSource = typename remove_const<_Source>::type,
  740. class _RawDest = typename remove_const<_Dest>::type,
  741. class =
  742. typename enable_if<
  743. is_trivially_copy_constructible<_Dest>::value &&
  744. is_same<_RawSource, _RawDest>::value &&
  745. (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Dest*, _Source&>::value)
  746. >::type>
  747. _LIBCPP_INLINE_VISIBILITY
  748. void __construct_range_forward(_Alloc&, _Source* __begin1, _Source* __end1, _Dest*& __begin2) {
  749. ptrdiff_t _Np = __end1 - __begin1;
  750. if (_Np > 0) {
  751. _VSTD::memcpy(const_cast<_RawDest*>(__begin2), __begin1, _Np * sizeof(_Dest));
  752. __begin2 += _Np;
  753. }
  754. }
  755. template <class _Alloc, class _Ptr>
  756. _LIBCPP_INLINE_VISIBILITY
  757. void __construct_backward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2) {
  758. static_assert(__is_cpp17_move_insertable<_Alloc>::value,
  759. "The specified type does not meet the requirements of Cpp17MoveInsertable");
  760. typedef allocator_traits<_Alloc> _Traits;
  761. while (__end1 != __begin1) {
  762. _Traits::construct(__a, _VSTD::__to_address(__end2 - 1),
  763. #ifdef _LIBCPP_NO_EXCEPTIONS
  764. _VSTD::move(*--__end1)
  765. #else
  766. _VSTD::move_if_noexcept(*--__end1)
  767. #endif
  768. );
  769. --__end2;
  770. }
  771. }
  772. template <class _Alloc, class _Tp, class = typename enable_if<
  773. (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) &&
  774. is_trivially_move_constructible<_Tp>::value
  775. >::type>
  776. _LIBCPP_INLINE_VISIBILITY
  777. void __construct_backward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2) {
  778. ptrdiff_t _Np = __end1 - __begin1;
  779. __end2 -= _Np;
  780. if (_Np > 0)
  781. _VSTD::memcpy(static_cast<void*>(__end2), static_cast<void const*>(__begin1), _Np * sizeof(_Tp));
  782. }
  783. struct __destruct_n
  784. {
  785. private:
  786. size_t __size_;
  787. template <class _Tp>
  788. _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
  789. {for (size_t __i = 0; __i < __size_; ++__i, ++__p) __p->~_Tp();}
  790. template <class _Tp>
  791. _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
  792. {}
  793. _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
  794. {++__size_;}
  795. _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
  796. {}
  797. _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
  798. {__size_ = __s;}
  799. _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
  800. {}
  801. public:
  802. _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
  803. : __size_(__s) {}
  804. template <class _Tp>
  805. _LIBCPP_INLINE_VISIBILITY void __incr() _NOEXCEPT
  806. {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
  807. template <class _Tp>
  808. _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
  809. {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
  810. template <class _Tp>
  811. _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
  812. {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
  813. };
  814. _LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
  815. // --- Helper for container swap --
  816. template <typename _Alloc>
  817. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
  818. void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
  819. #if _LIBCPP_STD_VER > 11
  820. _NOEXCEPT
  821. #else
  822. _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
  823. #endif
  824. {
  825. using _VSTD::swap;
  826. swap(__a1, __a2);
  827. }
  828. template <typename _Alloc>
  829. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
  830. void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
  831. template <typename _Alloc>
  832. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
  833. void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
  834. #if _LIBCPP_STD_VER > 11
  835. _NOEXCEPT
  836. #else
  837. _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
  838. #endif
  839. {
  840. _VSTD::__swap_allocator(__a1, __a2,
  841. integral_constant<bool, allocator_traits<_Alloc>::propagate_on_container_swap::value>());
  842. }
  843. template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
  844. struct __noexcept_move_assign_container : public integral_constant<bool,
  845. _Traits::propagate_on_container_move_assignment::value
  846. #if _LIBCPP_STD_VER > 14
  847. || _Traits::is_always_equal::value
  848. #else
  849. && is_nothrow_move_assignable<_Alloc>::value
  850. #endif
  851. > {};
  852. template <class _Tp, class _Alloc>
  853. struct __temp_value {
  854. typedef allocator_traits<_Alloc> _Traits;
  855. typename aligned_storage<sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)>::type __v;
  856. _Alloc &__a;
  857. _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
  858. _Tp & get() { return *__addr(); }
  859. template<class... _Args>
  860. _LIBCPP_NO_CFI
  861. __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc) {
  862. _Traits::construct(__a, reinterpret_cast<_Tp*>(addressof(__v)),
  863. _VSTD::forward<_Args>(__args)...);
  864. }
  865. ~__temp_value() { _Traits::destroy(__a, __addr()); }
  866. };
  867. template<typename _Alloc, typename = void, typename = void>
  868. struct __is_allocator : false_type {};
  869. template<typename _Alloc>
  870. struct __is_allocator<_Alloc,
  871. typename __void_t<typename _Alloc::value_type>::type,
  872. typename __void_t<decltype(declval<_Alloc&>().allocate(size_t(0)))>::type
  873. >
  874. : true_type {};
  875. // __builtin_new_allocator -- A non-templated helper for allocating and
  876. // deallocating memory using __builtin_operator_new and
  877. // __builtin_operator_delete. It should be used in preference to
  878. // `std::allocator<T>` to avoid additional instantiations.
  879. struct __builtin_new_allocator {
  880. struct __builtin_new_deleter {
  881. typedef void* pointer_type;
  882. _LIBCPP_CONSTEXPR explicit __builtin_new_deleter(size_t __size, size_t __align)
  883. : __size_(__size), __align_(__align) {}
  884. void operator()(void* p) const _NOEXCEPT {
  885. _VSTD::__libcpp_deallocate(p, __size_, __align_);
  886. }
  887. private:
  888. size_t __size_;
  889. size_t __align_;
  890. };
  891. typedef unique_ptr<void, __builtin_new_deleter> __holder_t;
  892. static __holder_t __allocate_bytes(size_t __s, size_t __align) {
  893. return __holder_t(_VSTD::__libcpp_allocate(__s, __align),
  894. __builtin_new_deleter(__s, __align));
  895. }
  896. static void __deallocate_bytes(void* __p, size_t __s,
  897. size_t __align) _NOEXCEPT {
  898. _VSTD::__libcpp_deallocate(__p, __s, __align);
  899. }
  900. template <class _Tp>
  901. _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
  902. static __holder_t __allocate_type(size_t __n) {
  903. return __allocate_bytes(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
  904. }
  905. template <class _Tp>
  906. _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
  907. static void __deallocate_type(void* __p, size_t __n) _NOEXCEPT {
  908. __deallocate_bytes(__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
  909. }
  910. };
  911. _LIBCPP_END_NAMESPACE_STD
  912. #if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
  913. //# include <__pstl_memory>
  914. #endif
  915. #endif // _LIBCPP_MEMORY