memory 42 KB

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