memory 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139
  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 <tuple>
  712. #include <type_traits>
  713. #include <typeinfo>
  714. #include <version>
  715. #ifndef _LIBCPP_REMOVE_TRANSITIVE_INCLUDES
  716. # include <iterator>
  717. # include <utility>
  718. #endif
  719. // standard-mandated includes
  720. #include <compare>
  721. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  722. # pragma GCC system_header
  723. #endif
  724. _LIBCPP_BEGIN_NAMESPACE_STD
  725. template <class _Alloc, class _Ptr>
  726. _LIBCPP_INLINE_VISIBILITY
  727. void __construct_forward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2) {
  728. static_assert(__is_cpp17_move_insertable<_Alloc>::value,
  729. "The specified type does not meet the requirements of Cpp17MoveInsertable");
  730. typedef allocator_traits<_Alloc> _Traits;
  731. for (; __begin1 != __end1; ++__begin1, (void)++__begin2) {
  732. _Traits::construct(__a, _VSTD::__to_address(__begin2),
  733. #ifdef _LIBCPP_NO_EXCEPTIONS
  734. _VSTD::move(*__begin1)
  735. #else
  736. _VSTD::move_if_noexcept(*__begin1)
  737. #endif
  738. );
  739. }
  740. }
  741. template <class _Alloc, class _Tp, typename enable_if<
  742. (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) &&
  743. is_trivially_move_constructible<_Tp>::value
  744. >::type>
  745. _LIBCPP_INLINE_VISIBILITY
  746. void __construct_forward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2) {
  747. ptrdiff_t _Np = __end1 - __begin1;
  748. if (_Np > 0) {
  749. _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
  750. __begin2 += _Np;
  751. }
  752. }
  753. template <class _Alloc, class _Iter, class _Ptr>
  754. _LIBCPP_INLINE_VISIBILITY
  755. void __construct_range_forward(_Alloc& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2) {
  756. typedef allocator_traits<_Alloc> _Traits;
  757. for (; __begin1 != __end1; ++__begin1, (void) ++__begin2) {
  758. _Traits::construct(__a, _VSTD::__to_address(__begin2), *__begin1);
  759. }
  760. }
  761. template <class _Alloc, class _Source, class _Dest,
  762. class _RawSource = typename remove_const<_Source>::type,
  763. class _RawDest = typename remove_const<_Dest>::type,
  764. class =
  765. typename enable_if<
  766. is_trivially_copy_constructible<_Dest>::value &&
  767. is_same<_RawSource, _RawDest>::value &&
  768. (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Dest*, _Source&>::value)
  769. >::type>
  770. _LIBCPP_INLINE_VISIBILITY
  771. void __construct_range_forward(_Alloc&, _Source* __begin1, _Source* __end1, _Dest*& __begin2) {
  772. ptrdiff_t _Np = __end1 - __begin1;
  773. if (_Np > 0) {
  774. _VSTD::memcpy(const_cast<_RawDest*>(__begin2), __begin1, _Np * sizeof(_Dest));
  775. __begin2 += _Np;
  776. }
  777. }
  778. template <class _Alloc, class _Ptr>
  779. _LIBCPP_INLINE_VISIBILITY
  780. void __construct_backward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2) {
  781. static_assert(__is_cpp17_move_insertable<_Alloc>::value,
  782. "The specified type does not meet the requirements of Cpp17MoveInsertable");
  783. typedef allocator_traits<_Alloc> _Traits;
  784. while (__end1 != __begin1) {
  785. _Traits::construct(__a, _VSTD::__to_address(__end2 - 1),
  786. #ifdef _LIBCPP_NO_EXCEPTIONS
  787. _VSTD::move(*--__end1)
  788. #else
  789. _VSTD::move_if_noexcept(*--__end1)
  790. #endif
  791. );
  792. --__end2;
  793. }
  794. }
  795. template <class _Alloc, class _Tp, class = typename enable_if<
  796. (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) &&
  797. is_trivially_move_constructible<_Tp>::value
  798. >::type>
  799. _LIBCPP_INLINE_VISIBILITY
  800. void __construct_backward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2) {
  801. ptrdiff_t _Np = __end1 - __begin1;
  802. __end2 -= _Np;
  803. if (_Np > 0)
  804. _VSTD::memcpy(static_cast<void*>(__end2), static_cast<void const*>(__begin1), _Np * sizeof(_Tp));
  805. }
  806. struct __destruct_n
  807. {
  808. private:
  809. size_t __size_;
  810. template <class _Tp>
  811. _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
  812. {for (size_t __i = 0; __i < __size_; ++__i, ++__p) __p->~_Tp();}
  813. template <class _Tp>
  814. _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
  815. {}
  816. _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
  817. {++__size_;}
  818. _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
  819. {}
  820. _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
  821. {__size_ = __s;}
  822. _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
  823. {}
  824. public:
  825. _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
  826. : __size_(__s) {}
  827. template <class _Tp>
  828. _LIBCPP_INLINE_VISIBILITY void __incr() _NOEXCEPT
  829. {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
  830. template <class _Tp>
  831. _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
  832. {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
  833. template <class _Tp>
  834. _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
  835. {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
  836. };
  837. _LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
  838. // --- Helper for container swap --
  839. template <typename _Alloc>
  840. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
  841. void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
  842. #if _LIBCPP_STD_VER > 11
  843. _NOEXCEPT
  844. #else
  845. _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
  846. #endif
  847. {
  848. using _VSTD::swap;
  849. swap(__a1, __a2);
  850. }
  851. template <typename _Alloc>
  852. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
  853. void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
  854. template <typename _Alloc>
  855. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
  856. void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
  857. #if _LIBCPP_STD_VER > 11
  858. _NOEXCEPT
  859. #else
  860. _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
  861. #endif
  862. {
  863. _VSTD::__swap_allocator(__a1, __a2,
  864. integral_constant<bool, allocator_traits<_Alloc>::propagate_on_container_swap::value>());
  865. }
  866. template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
  867. struct __noexcept_move_assign_container : public integral_constant<bool,
  868. _Traits::propagate_on_container_move_assignment::value
  869. #if _LIBCPP_STD_VER > 14
  870. || _Traits::is_always_equal::value
  871. #else
  872. && is_nothrow_move_assignable<_Alloc>::value
  873. #endif
  874. > {};
  875. template <class _Tp, class _Alloc>
  876. struct __temp_value {
  877. typedef allocator_traits<_Alloc> _Traits;
  878. typename aligned_storage<sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)>::type __v;
  879. _Alloc &__a;
  880. _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
  881. _Tp & get() { return *__addr(); }
  882. template<class... _Args>
  883. _LIBCPP_NO_CFI
  884. __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc) {
  885. _Traits::construct(__a, reinterpret_cast<_Tp*>(addressof(__v)),
  886. _VSTD::forward<_Args>(__args)...);
  887. }
  888. ~__temp_value() { _Traits::destroy(__a, __addr()); }
  889. };
  890. template<typename _Alloc, typename = void, typename = void>
  891. struct __is_allocator : false_type {};
  892. template<typename _Alloc>
  893. struct __is_allocator<_Alloc,
  894. typename __void_t<typename _Alloc::value_type>::type,
  895. typename __void_t<decltype(declval<_Alloc&>().allocate(size_t(0)))>::type
  896. >
  897. : true_type {};
  898. // __builtin_new_allocator -- A non-templated helper for allocating and
  899. // deallocating memory using __builtin_operator_new and
  900. // __builtin_operator_delete. It should be used in preference to
  901. // `std::allocator<T>` to avoid additional instantiations.
  902. struct __builtin_new_allocator {
  903. struct __builtin_new_deleter {
  904. typedef void* pointer_type;
  905. _LIBCPP_CONSTEXPR explicit __builtin_new_deleter(size_t __size, size_t __align)
  906. : __size_(__size), __align_(__align) {}
  907. void operator()(void* __p) const _NOEXCEPT {
  908. _VSTD::__libcpp_deallocate(__p, __size_, __align_);
  909. }
  910. private:
  911. size_t __size_;
  912. size_t __align_;
  913. };
  914. typedef unique_ptr<void, __builtin_new_deleter> __holder_t;
  915. static __holder_t __allocate_bytes(size_t __s, size_t __align) {
  916. return __holder_t(_VSTD::__libcpp_allocate(__s, __align),
  917. __builtin_new_deleter(__s, __align));
  918. }
  919. static void __deallocate_bytes(void* __p, size_t __s,
  920. size_t __align) _NOEXCEPT {
  921. _VSTD::__libcpp_deallocate(__p, __s, __align);
  922. }
  923. template <class _Tp>
  924. _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
  925. static __holder_t __allocate_type(size_t __n) {
  926. return __allocate_bytes(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
  927. }
  928. template <class _Tp>
  929. _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
  930. static void __deallocate_type(void* __p, size_t __n) _NOEXCEPT {
  931. __deallocate_bytes(__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
  932. }
  933. };
  934. _LIBCPP_END_NAMESPACE_STD
  935. #if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
  936. #error # include <__pstl_memory>
  937. #endif
  938. #endif // _LIBCPP_MEMORY