10b57cec5SDimitry Andric// -*- C++ -*- 2*349cc55cSDimitry Andric//===----------------------------------------------------------------------===// 30b57cec5SDimitry Andric// 40b57cec5SDimitry Andric// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 50b57cec5SDimitry Andric// See https://llvm.org/LICENSE.txt for license information. 60b57cec5SDimitry Andric// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 70b57cec5SDimitry Andric// 80b57cec5SDimitry Andric//===----------------------------------------------------------------------===// 90b57cec5SDimitry Andric 100b57cec5SDimitry Andric#ifndef _LIBCPP_MEMORY 110b57cec5SDimitry Andric#define _LIBCPP_MEMORY 120b57cec5SDimitry Andric 130b57cec5SDimitry Andric/* 140b57cec5SDimitry Andric memory synopsis 150b57cec5SDimitry Andric 160b57cec5SDimitry Andricnamespace std 170b57cec5SDimitry Andric{ 180b57cec5SDimitry Andric 190b57cec5SDimitry Andricstruct allocator_arg_t { }; 200b57cec5SDimitry Andricinline constexpr allocator_arg_t allocator_arg = allocator_arg_t(); 210b57cec5SDimitry Andric 220b57cec5SDimitry Andrictemplate <class T, class Alloc> struct uses_allocator; 230b57cec5SDimitry Andric 240b57cec5SDimitry Andrictemplate <class Ptr> 250b57cec5SDimitry Andricstruct pointer_traits 260b57cec5SDimitry Andric{ 270b57cec5SDimitry Andric typedef Ptr pointer; 280b57cec5SDimitry Andric typedef <details> element_type; 290b57cec5SDimitry Andric typedef <details> difference_type; 300b57cec5SDimitry Andric 310b57cec5SDimitry Andric template <class U> using rebind = <details>; 320b57cec5SDimitry Andric 330b57cec5SDimitry Andric static pointer pointer_to(<details>); 340b57cec5SDimitry Andric}; 350b57cec5SDimitry Andric 360b57cec5SDimitry Andrictemplate <class T> 370b57cec5SDimitry Andricstruct pointer_traits<T*> 380b57cec5SDimitry Andric{ 390b57cec5SDimitry Andric typedef T* pointer; 400b57cec5SDimitry Andric typedef T element_type; 410b57cec5SDimitry Andric typedef ptrdiff_t difference_type; 420b57cec5SDimitry Andric 430b57cec5SDimitry Andric template <class U> using rebind = U*; 440b57cec5SDimitry Andric 450b57cec5SDimitry Andric static pointer pointer_to(<details>) noexcept; // constexpr in C++20 460b57cec5SDimitry Andric}; 470b57cec5SDimitry Andric 480b57cec5SDimitry Andrictemplate <class T> constexpr T* to_address(T* p) noexcept; // C++20 49e8d8bef9SDimitry Andrictemplate <class Ptr> constexpr auto to_address(const Ptr& p) noexcept; // C++20 500b57cec5SDimitry Andric 510b57cec5SDimitry Andrictemplate <class Alloc> 520b57cec5SDimitry Andricstruct allocator_traits 530b57cec5SDimitry Andric{ 540b57cec5SDimitry Andric typedef Alloc allocator_type; 550b57cec5SDimitry Andric typedef typename allocator_type::value_type 560b57cec5SDimitry Andric value_type; 570b57cec5SDimitry Andric 580b57cec5SDimitry Andric typedef Alloc::pointer | value_type* pointer; 590b57cec5SDimitry Andric typedef Alloc::const_pointer 600b57cec5SDimitry Andric | pointer_traits<pointer>::rebind<const value_type> 610b57cec5SDimitry Andric const_pointer; 620b57cec5SDimitry Andric typedef Alloc::void_pointer 630b57cec5SDimitry Andric | pointer_traits<pointer>::rebind<void> 640b57cec5SDimitry Andric void_pointer; 650b57cec5SDimitry Andric typedef Alloc::const_void_pointer 660b57cec5SDimitry Andric | pointer_traits<pointer>::rebind<const void> 670b57cec5SDimitry Andric const_void_pointer; 680b57cec5SDimitry Andric typedef Alloc::difference_type 690b57cec5SDimitry Andric | pointer_traits<pointer>::difference_type 700b57cec5SDimitry Andric difference_type; 710b57cec5SDimitry Andric typedef Alloc::size_type 720b57cec5SDimitry Andric | make_unsigned<difference_type>::type 730b57cec5SDimitry Andric size_type; 740b57cec5SDimitry Andric typedef Alloc::propagate_on_container_copy_assignment 750b57cec5SDimitry Andric | false_type propagate_on_container_copy_assignment; 760b57cec5SDimitry Andric typedef Alloc::propagate_on_container_move_assignment 770b57cec5SDimitry Andric | false_type propagate_on_container_move_assignment; 780b57cec5SDimitry Andric typedef Alloc::propagate_on_container_swap 790b57cec5SDimitry Andric | false_type propagate_on_container_swap; 800b57cec5SDimitry Andric typedef Alloc::is_always_equal 810b57cec5SDimitry Andric | is_empty is_always_equal; 820b57cec5SDimitry Andric 835ffd83dbSDimitry Andric template <class T> using rebind_alloc = Alloc::rebind<T>::other | Alloc<T, Args...>; 840b57cec5SDimitry Andric template <class T> using rebind_traits = allocator_traits<rebind_alloc<T>>; 850b57cec5SDimitry Andric 86e8d8bef9SDimitry Andric static pointer allocate(allocator_type& a, size_type n); // constexpr and [[nodiscard]] in C++20 87e8d8bef9SDimitry Andric static pointer allocate(allocator_type& a, size_type n, const_void_pointer hint); // constexpr and [[nodiscard]] in C++20 880b57cec5SDimitry Andric 89e8d8bef9SDimitry Andric static void deallocate(allocator_type& a, pointer p, size_type n) noexcept; // constexpr in C++20 900b57cec5SDimitry Andric 910b57cec5SDimitry Andric template <class T, class... Args> 92e8d8bef9SDimitry Andric static void construct(allocator_type& a, T* p, Args&&... args); // constexpr in C++20 930b57cec5SDimitry Andric 940b57cec5SDimitry Andric template <class T> 95e8d8bef9SDimitry Andric static void destroy(allocator_type& a, T* p); // constexpr in C++20 960b57cec5SDimitry Andric 97e8d8bef9SDimitry Andric static size_type max_size(const allocator_type& a); // noexcept in C++14, constexpr in C++20 98e8d8bef9SDimitry Andric static allocator_type select_on_container_copy_construction(const allocator_type& a); // constexpr in C++20 990b57cec5SDimitry Andric}; 1000b57cec5SDimitry Andric 1010b57cec5SDimitry Andrictemplate <> 10223408297SDimitry Andricclass allocator<void> // removed in C++20 1030b57cec5SDimitry Andric{ 1040b57cec5SDimitry Andricpublic: 105fe6060f1SDimitry Andric typedef void* pointer; 106fe6060f1SDimitry Andric typedef const void* const_pointer; 107fe6060f1SDimitry Andric typedef void value_type; 1080b57cec5SDimitry Andric 109fe6060f1SDimitry Andric template <class _Up> struct rebind {typedef allocator<_Up> other;}; 1100b57cec5SDimitry Andric}; 1110b57cec5SDimitry Andric 1120b57cec5SDimitry Andrictemplate <class T> 1130b57cec5SDimitry Andricclass allocator 1140b57cec5SDimitry Andric{ 1150b57cec5SDimitry Andricpublic: 116e8d8bef9SDimitry Andric typedef size_t size_type; 117e8d8bef9SDimitry Andric typedef ptrdiff_t difference_type; 1185ffd83dbSDimitry Andric typedef T* pointer; // deprecated in C++17, removed in C++20 1195ffd83dbSDimitry Andric typedef const T* const_pointer; // deprecated in C++17, removed in C++20 1205ffd83dbSDimitry Andric typedef typename add_lvalue_reference<T>::type 1215ffd83dbSDimitry Andric reference; // deprecated in C++17, removed in C++20 1225ffd83dbSDimitry Andric typedef typename add_lvalue_reference<const T>::type 1235ffd83dbSDimitry Andric const_reference; // deprecated in C++17, removed in C++20 1245ffd83dbSDimitry Andric 1250b57cec5SDimitry Andric typedef T value_type; 1260b57cec5SDimitry Andric 1275ffd83dbSDimitry Andric template <class U> struct rebind {typedef allocator<U> other;}; // deprecated in C++17, removed in C++20 1285ffd83dbSDimitry Andric 1295ffd83dbSDimitry Andric typedef true_type propagate_on_container_move_assignment; 1305ffd83dbSDimitry Andric typedef true_type is_always_equal; 1310b57cec5SDimitry Andric 1320b57cec5SDimitry Andric constexpr allocator() noexcept; // constexpr in C++20 1330b57cec5SDimitry Andric constexpr allocator(const allocator&) noexcept; // constexpr in C++20 1340b57cec5SDimitry Andric template <class U> 1350b57cec5SDimitry Andric constexpr allocator(const allocator<U>&) noexcept; // constexpr in C++20 136e8d8bef9SDimitry Andric ~allocator(); // constexpr in C++20 1375ffd83dbSDimitry Andric pointer address(reference x) const noexcept; // deprecated in C++17, removed in C++20 1385ffd83dbSDimitry Andric const_pointer address(const_reference x) const noexcept; // deprecated in C++17, removed in C++20 1395ffd83dbSDimitry Andric T* allocate(size_t n, const void* hint); // deprecated in C++17, removed in C++20 140e8d8bef9SDimitry Andric T* allocate(size_t n); // constexpr in C++20 141e8d8bef9SDimitry Andric void deallocate(T* p, size_t n) noexcept; // constexpr in C++20 1425ffd83dbSDimitry Andric size_type max_size() const noexcept; // deprecated in C++17, removed in C++20 1430b57cec5SDimitry Andric template<class U, class... Args> 1445ffd83dbSDimitry Andric void construct(U* p, Args&&... args); // deprecated in C++17, removed in C++20 1450b57cec5SDimitry Andric template <class U> 1465ffd83dbSDimitry Andric void destroy(U* p); // deprecated in C++17, removed in C++20 1470b57cec5SDimitry Andric}; 1480b57cec5SDimitry Andric 1490b57cec5SDimitry Andrictemplate <class T, class U> 150e8d8bef9SDimitry Andricbool operator==(const allocator<T>&, const allocator<U>&) noexcept; // constexpr in C++20 1510b57cec5SDimitry Andric 1520b57cec5SDimitry Andrictemplate <class T, class U> 153e8d8bef9SDimitry Andricbool operator!=(const allocator<T>&, const allocator<U>&) noexcept; // constexpr in C++20 1540b57cec5SDimitry Andric 1550b57cec5SDimitry Andrictemplate <class OutputIterator, class T> 156fe6060f1SDimitry Andricclass raw_storage_iterator // deprecated in C++17, removed in C++20 157fe6060f1SDimitry Andric : public iterator<output_iterator_tag, void, void, void, void> // until C++17 1580b57cec5SDimitry Andric{ 1590b57cec5SDimitry Andricpublic: 160fe6060f1SDimitry Andric typedef output_iterator_tag iterator_category; 161fe6060f1SDimitry Andric typedef void value_type; 162fe6060f1SDimitry Andric typedef void difference_type; // until C++20 163fe6060f1SDimitry Andric typedef ptrdiff_t difference_type; // since C++20 164fe6060f1SDimitry Andric typedef void pointer; 165fe6060f1SDimitry Andric typedef void reference; 166fe6060f1SDimitry Andric 1670b57cec5SDimitry Andric explicit raw_storage_iterator(OutputIterator x); 1680b57cec5SDimitry Andric raw_storage_iterator& operator*(); 1690b57cec5SDimitry Andric raw_storage_iterator& operator=(const T& element); 1700b57cec5SDimitry Andric raw_storage_iterator& operator++(); 1710b57cec5SDimitry Andric raw_storage_iterator operator++(int); 1720b57cec5SDimitry Andric}; 1730b57cec5SDimitry Andric 1740b57cec5SDimitry Andrictemplate <class T> pair<T*,ptrdiff_t> get_temporary_buffer(ptrdiff_t n) noexcept; 1750b57cec5SDimitry Andrictemplate <class T> void return_temporary_buffer(T* p) noexcept; 1760b57cec5SDimitry Andric 1770b57cec5SDimitry Andrictemplate <class T> T* addressof(T& r) noexcept; 1780b57cec5SDimitry Andrictemplate <class T> T* addressof(const T&& r) noexcept = delete; 1790b57cec5SDimitry Andric 1800b57cec5SDimitry Andrictemplate <class InputIterator, class ForwardIterator> 1810b57cec5SDimitry AndricForwardIterator 1820b57cec5SDimitry Andricuninitialized_copy(InputIterator first, InputIterator last, ForwardIterator result); 1830b57cec5SDimitry Andric 1840b57cec5SDimitry Andrictemplate <class InputIterator, class Size, class ForwardIterator> 1850b57cec5SDimitry AndricForwardIterator 1860b57cec5SDimitry Andricuninitialized_copy_n(InputIterator first, Size n, ForwardIterator result); 1870b57cec5SDimitry Andric 1880b57cec5SDimitry Andrictemplate <class ForwardIterator, class T> 1890b57cec5SDimitry Andricvoid uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& x); 1900b57cec5SDimitry Andric 1910b57cec5SDimitry Andrictemplate <class ForwardIterator, class Size, class T> 1920b57cec5SDimitry AndricForwardIterator 1930b57cec5SDimitry Andricuninitialized_fill_n(ForwardIterator first, Size n, const T& x); 1940b57cec5SDimitry Andric 195e8d8bef9SDimitry Andrictemplate <class T, class ...Args> 196e8d8bef9SDimitry Andricconstexpr T* construct_at(T* location, Args&& ...args); // since C++20 197e8d8bef9SDimitry Andric 1980b57cec5SDimitry Andrictemplate <class T> 199e8d8bef9SDimitry Andricvoid destroy_at(T* location); // constexpr in C++20 2000b57cec5SDimitry Andric 2010b57cec5SDimitry Andrictemplate <class ForwardIterator> 202e8d8bef9SDimitry Andricvoid destroy(ForwardIterator first, ForwardIterator last); // constexpr in C++20 2030b57cec5SDimitry Andric 2040b57cec5SDimitry Andrictemplate <class ForwardIterator, class Size> 205e8d8bef9SDimitry AndricForwardIterator destroy_n(ForwardIterator first, Size n); // constexpr in C++20 2060b57cec5SDimitry Andric 2070b57cec5SDimitry Andrictemplate <class InputIterator, class ForwardIterator> 2080b57cec5SDimitry Andric ForwardIterator uninitialized_move(InputIterator first, InputIterator last, ForwardIterator result); 2090b57cec5SDimitry Andric 2100b57cec5SDimitry Andrictemplate <class InputIterator, class Size, class ForwardIterator> 2110b57cec5SDimitry Andric pair<InputIterator,ForwardIterator> uninitialized_move_n(InputIterator first, Size n, ForwardIterator result); 2120b57cec5SDimitry Andric 2130b57cec5SDimitry Andrictemplate <class ForwardIterator> 2140b57cec5SDimitry Andric void uninitialized_value_construct(ForwardIterator first, ForwardIterator last); 2150b57cec5SDimitry Andric 2160b57cec5SDimitry Andrictemplate <class ForwardIterator, class Size> 2170b57cec5SDimitry Andric ForwardIterator uninitialized_value_construct_n(ForwardIterator first, Size n); 2180b57cec5SDimitry Andric 2190b57cec5SDimitry Andrictemplate <class ForwardIterator> 2200b57cec5SDimitry Andric void uninitialized_default_construct(ForwardIterator first, ForwardIterator last); 2210b57cec5SDimitry Andric 2220b57cec5SDimitry Andrictemplate <class ForwardIterator, class Size> 2230b57cec5SDimitry Andric ForwardIterator uninitialized_default_construct_n(ForwardIterator first, Size n); 2240b57cec5SDimitry Andric 2250b57cec5SDimitry Andrictemplate <class Y> struct auto_ptr_ref {}; // deprecated in C++11, removed in C++17 2260b57cec5SDimitry Andric 2270b57cec5SDimitry Andrictemplate<class X> 2280b57cec5SDimitry Andricclass auto_ptr // deprecated in C++11, removed in C++17 2290b57cec5SDimitry Andric{ 2300b57cec5SDimitry Andricpublic: 2310b57cec5SDimitry Andric typedef X element_type; 2320b57cec5SDimitry Andric 2330b57cec5SDimitry Andric explicit auto_ptr(X* p =0) throw(); 2340b57cec5SDimitry Andric auto_ptr(auto_ptr&) throw(); 2350b57cec5SDimitry Andric template<class Y> auto_ptr(auto_ptr<Y>&) throw(); 2360b57cec5SDimitry Andric auto_ptr& operator=(auto_ptr&) throw(); 2370b57cec5SDimitry Andric template<class Y> auto_ptr& operator=(auto_ptr<Y>&) throw(); 2380b57cec5SDimitry Andric auto_ptr& operator=(auto_ptr_ref<X> r) throw(); 2390b57cec5SDimitry Andric ~auto_ptr() throw(); 2400b57cec5SDimitry Andric 2410b57cec5SDimitry Andric typename add_lvalue_reference<X>::type operator*() const throw(); 2420b57cec5SDimitry Andric X* operator->() const throw(); 2430b57cec5SDimitry Andric X* get() const throw(); 2440b57cec5SDimitry Andric X* release() throw(); 2450b57cec5SDimitry Andric void reset(X* p =0) throw(); 2460b57cec5SDimitry Andric 2470b57cec5SDimitry Andric auto_ptr(auto_ptr_ref<X>) throw(); 2480b57cec5SDimitry Andric template<class Y> operator auto_ptr_ref<Y>() throw(); 2490b57cec5SDimitry Andric template<class Y> operator auto_ptr<Y>() throw(); 2500b57cec5SDimitry Andric}; 2510b57cec5SDimitry Andric 2520b57cec5SDimitry Andrictemplate <class T> 2530b57cec5SDimitry Andricstruct default_delete 2540b57cec5SDimitry Andric{ 2550b57cec5SDimitry Andric constexpr default_delete() noexcept = default; 2560b57cec5SDimitry Andric template <class U> default_delete(const default_delete<U>&) noexcept; 2570b57cec5SDimitry Andric 2580b57cec5SDimitry Andric void operator()(T*) const noexcept; 2590b57cec5SDimitry Andric}; 2600b57cec5SDimitry Andric 2610b57cec5SDimitry Andrictemplate <class T> 2620b57cec5SDimitry Andricstruct default_delete<T[]> 2630b57cec5SDimitry Andric{ 2640b57cec5SDimitry Andric constexpr default_delete() noexcept = default; 2650b57cec5SDimitry Andric void operator()(T*) const noexcept; 2660b57cec5SDimitry Andric template <class U> void operator()(U*) const = delete; 2670b57cec5SDimitry Andric}; 2680b57cec5SDimitry Andric 2690b57cec5SDimitry Andrictemplate <class T, class D = default_delete<T>> 2700b57cec5SDimitry Andricclass unique_ptr 2710b57cec5SDimitry Andric{ 2720b57cec5SDimitry Andricpublic: 2730b57cec5SDimitry Andric typedef see below pointer; 2740b57cec5SDimitry Andric typedef T element_type; 2750b57cec5SDimitry Andric typedef D deleter_type; 2760b57cec5SDimitry Andric 2770b57cec5SDimitry Andric // constructors 2780b57cec5SDimitry Andric constexpr unique_ptr() noexcept; 2790b57cec5SDimitry Andric explicit unique_ptr(pointer p) noexcept; 2800b57cec5SDimitry Andric unique_ptr(pointer p, see below d1) noexcept; 2810b57cec5SDimitry Andric unique_ptr(pointer p, see below d2) noexcept; 2820b57cec5SDimitry Andric unique_ptr(unique_ptr&& u) noexcept; 2830b57cec5SDimitry Andric unique_ptr(nullptr_t) noexcept : unique_ptr() { } 2840b57cec5SDimitry Andric template <class U, class E> 2850b57cec5SDimitry Andric unique_ptr(unique_ptr<U, E>&& u) noexcept; 2860b57cec5SDimitry Andric template <class U> 2870b57cec5SDimitry Andric unique_ptr(auto_ptr<U>&& u) noexcept; // removed in C++17 2880b57cec5SDimitry Andric 2890b57cec5SDimitry Andric // destructor 2900b57cec5SDimitry Andric ~unique_ptr(); 2910b57cec5SDimitry Andric 2920b57cec5SDimitry Andric // assignment 2930b57cec5SDimitry Andric unique_ptr& operator=(unique_ptr&& u) noexcept; 2940b57cec5SDimitry Andric template <class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept; 2950b57cec5SDimitry Andric unique_ptr& operator=(nullptr_t) noexcept; 2960b57cec5SDimitry Andric 2970b57cec5SDimitry Andric // observers 2980b57cec5SDimitry Andric typename add_lvalue_reference<T>::type operator*() const; 2990b57cec5SDimitry Andric pointer operator->() const noexcept; 3000b57cec5SDimitry Andric pointer get() const noexcept; 3010b57cec5SDimitry Andric deleter_type& get_deleter() noexcept; 3020b57cec5SDimitry Andric const deleter_type& get_deleter() const noexcept; 3030b57cec5SDimitry Andric explicit operator bool() const noexcept; 3040b57cec5SDimitry Andric 3050b57cec5SDimitry Andric // modifiers 3060b57cec5SDimitry Andric pointer release() noexcept; 3070b57cec5SDimitry Andric void reset(pointer p = pointer()) noexcept; 3080b57cec5SDimitry Andric void swap(unique_ptr& u) noexcept; 3090b57cec5SDimitry Andric}; 3100b57cec5SDimitry Andric 3110b57cec5SDimitry Andrictemplate <class T, class D> 3120b57cec5SDimitry Andricclass unique_ptr<T[], D> 3130b57cec5SDimitry Andric{ 3140b57cec5SDimitry Andricpublic: 3150b57cec5SDimitry Andric typedef implementation-defined pointer; 3160b57cec5SDimitry Andric typedef T element_type; 3170b57cec5SDimitry Andric typedef D deleter_type; 3180b57cec5SDimitry Andric 3190b57cec5SDimitry Andric // constructors 3200b57cec5SDimitry Andric constexpr unique_ptr() noexcept; 3210b57cec5SDimitry Andric explicit unique_ptr(pointer p) noexcept; 3220b57cec5SDimitry Andric unique_ptr(pointer p, see below d) noexcept; 3230b57cec5SDimitry Andric unique_ptr(pointer p, see below d) noexcept; 3240b57cec5SDimitry Andric unique_ptr(unique_ptr&& u) noexcept; 3250b57cec5SDimitry Andric unique_ptr(nullptr_t) noexcept : unique_ptr() { } 3260b57cec5SDimitry Andric 3270b57cec5SDimitry Andric // destructor 3280b57cec5SDimitry Andric ~unique_ptr(); 3290b57cec5SDimitry Andric 3300b57cec5SDimitry Andric // assignment 3310b57cec5SDimitry Andric unique_ptr& operator=(unique_ptr&& u) noexcept; 3320b57cec5SDimitry Andric unique_ptr& operator=(nullptr_t) noexcept; 3330b57cec5SDimitry Andric 3340b57cec5SDimitry Andric // observers 3350b57cec5SDimitry Andric T& operator[](size_t i) const; 3360b57cec5SDimitry Andric pointer get() const noexcept; 3370b57cec5SDimitry Andric deleter_type& get_deleter() noexcept; 3380b57cec5SDimitry Andric const deleter_type& get_deleter() const noexcept; 3390b57cec5SDimitry Andric explicit operator bool() const noexcept; 3400b57cec5SDimitry Andric 3410b57cec5SDimitry Andric // modifiers 3420b57cec5SDimitry Andric pointer release() noexcept; 3430b57cec5SDimitry Andric void reset(pointer p = pointer()) noexcept; 3440b57cec5SDimitry Andric void reset(nullptr_t) noexcept; 3450b57cec5SDimitry Andric template <class U> void reset(U) = delete; 3460b57cec5SDimitry Andric void swap(unique_ptr& u) noexcept; 3470b57cec5SDimitry Andric}; 3480b57cec5SDimitry Andric 3490b57cec5SDimitry Andrictemplate <class T, class D> 3500b57cec5SDimitry Andric void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y) noexcept; 3510b57cec5SDimitry Andric 3520b57cec5SDimitry Andrictemplate <class T1, class D1, class T2, class D2> 3530b57cec5SDimitry Andric bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y); 3540b57cec5SDimitry Andrictemplate <class T1, class D1, class T2, class D2> 3550b57cec5SDimitry Andric bool operator!=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y); 3560b57cec5SDimitry Andrictemplate <class T1, class D1, class T2, class D2> 3570b57cec5SDimitry Andric bool operator<(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y); 3580b57cec5SDimitry Andrictemplate <class T1, class D1, class T2, class D2> 3590b57cec5SDimitry Andric bool operator<=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y); 3600b57cec5SDimitry Andrictemplate <class T1, class D1, class T2, class D2> 3610b57cec5SDimitry Andric bool operator>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y); 3620b57cec5SDimitry Andrictemplate <class T1, class D1, class T2, class D2> 3630b57cec5SDimitry Andric bool operator>=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y); 3640b57cec5SDimitry Andric 3650b57cec5SDimitry Andrictemplate <class T, class D> 3660b57cec5SDimitry Andric bool operator==(const unique_ptr<T, D>& x, nullptr_t) noexcept; 3670b57cec5SDimitry Andrictemplate <class T, class D> 3680b57cec5SDimitry Andric bool operator==(nullptr_t, const unique_ptr<T, D>& y) noexcept; 3690b57cec5SDimitry Andrictemplate <class T, class D> 3700b57cec5SDimitry Andric bool operator!=(const unique_ptr<T, D>& x, nullptr_t) noexcept; 3710b57cec5SDimitry Andrictemplate <class T, class D> 3720b57cec5SDimitry Andric bool operator!=(nullptr_t, const unique_ptr<T, D>& y) noexcept; 3730b57cec5SDimitry Andric 3740b57cec5SDimitry Andrictemplate <class T, class D> 3750b57cec5SDimitry Andric bool operator<(const unique_ptr<T, D>& x, nullptr_t); 3760b57cec5SDimitry Andrictemplate <class T, class D> 3770b57cec5SDimitry Andric bool operator<(nullptr_t, const unique_ptr<T, D>& y); 3780b57cec5SDimitry Andrictemplate <class T, class D> 3790b57cec5SDimitry Andric bool operator<=(const unique_ptr<T, D>& x, nullptr_t); 3800b57cec5SDimitry Andrictemplate <class T, class D> 3810b57cec5SDimitry Andric bool operator<=(nullptr_t, const unique_ptr<T, D>& y); 3820b57cec5SDimitry Andrictemplate <class T, class D> 3830b57cec5SDimitry Andric bool operator>(const unique_ptr<T, D>& x, nullptr_t); 3840b57cec5SDimitry Andrictemplate <class T, class D> 3850b57cec5SDimitry Andric bool operator>(nullptr_t, const unique_ptr<T, D>& y); 3860b57cec5SDimitry Andrictemplate <class T, class D> 3870b57cec5SDimitry Andric bool operator>=(const unique_ptr<T, D>& x, nullptr_t); 3880b57cec5SDimitry Andrictemplate <class T, class D> 3890b57cec5SDimitry Andric bool operator>=(nullptr_t, const unique_ptr<T, D>& y); 3900b57cec5SDimitry Andric 3910b57cec5SDimitry Andricclass bad_weak_ptr 3920b57cec5SDimitry Andric : public std::exception 3930b57cec5SDimitry Andric{ 3940b57cec5SDimitry Andric bad_weak_ptr() noexcept; 3950b57cec5SDimitry Andric}; 3960b57cec5SDimitry Andric 3970b57cec5SDimitry Andrictemplate<class T, class... Args> unique_ptr<T> make_unique(Args&&... args); // C++14 3980b57cec5SDimitry Andrictemplate<class T> unique_ptr<T> make_unique(size_t n); // C++14 3990b57cec5SDimitry Andrictemplate<class T, class... Args> unspecified make_unique(Args&&...) = delete; // C++14, T == U[N] 4000b57cec5SDimitry Andric 4010b57cec5SDimitry Andrictemplate<class E, class T, class Y, class D> 4020b57cec5SDimitry Andric basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, unique_ptr<Y, D> const& p); 4030b57cec5SDimitry Andric 4040b57cec5SDimitry Andrictemplate<class T> 4050b57cec5SDimitry Andricclass shared_ptr 4060b57cec5SDimitry Andric{ 4070b57cec5SDimitry Andricpublic: 408*349cc55cSDimitry Andric typedef T element_type; // until C++17 409*349cc55cSDimitry Andric typedef remove_extent_t<T> element_type; // since C++17 4100b57cec5SDimitry Andric typedef weak_ptr<T> weak_type; // C++17 4110b57cec5SDimitry Andric 4120b57cec5SDimitry Andric // constructors: 4130b57cec5SDimitry Andric constexpr shared_ptr() noexcept; 4140b57cec5SDimitry Andric template<class Y> explicit shared_ptr(Y* p); 4150b57cec5SDimitry Andric template<class Y, class D> shared_ptr(Y* p, D d); 4160b57cec5SDimitry Andric template<class Y, class D, class A> shared_ptr(Y* p, D d, A a); 4170b57cec5SDimitry Andric template <class D> shared_ptr(nullptr_t p, D d); 4180b57cec5SDimitry Andric template <class D, class A> shared_ptr(nullptr_t p, D d, A a); 4190b57cec5SDimitry Andric template<class Y> shared_ptr(const shared_ptr<Y>& r, T *p) noexcept; 4200b57cec5SDimitry Andric shared_ptr(const shared_ptr& r) noexcept; 4210b57cec5SDimitry Andric template<class Y> shared_ptr(const shared_ptr<Y>& r) noexcept; 4220b57cec5SDimitry Andric shared_ptr(shared_ptr&& r) noexcept; 4230b57cec5SDimitry Andric template<class Y> shared_ptr(shared_ptr<Y>&& r) noexcept; 4240b57cec5SDimitry Andric template<class Y> explicit shared_ptr(const weak_ptr<Y>& r); 4250b57cec5SDimitry Andric template<class Y> shared_ptr(auto_ptr<Y>&& r); // removed in C++17 4260b57cec5SDimitry Andric template <class Y, class D> shared_ptr(unique_ptr<Y, D>&& r); 4270b57cec5SDimitry Andric shared_ptr(nullptr_t) : shared_ptr() { } 4280b57cec5SDimitry Andric 4290b57cec5SDimitry Andric // destructor: 4300b57cec5SDimitry Andric ~shared_ptr(); 4310b57cec5SDimitry Andric 4320b57cec5SDimitry Andric // assignment: 4330b57cec5SDimitry Andric shared_ptr& operator=(const shared_ptr& r) noexcept; 4340b57cec5SDimitry Andric template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r) noexcept; 4350b57cec5SDimitry Andric shared_ptr& operator=(shared_ptr&& r) noexcept; 4360b57cec5SDimitry Andric template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r); 4370b57cec5SDimitry Andric template<class Y> shared_ptr& operator=(auto_ptr<Y>&& r); // removed in C++17 4380b57cec5SDimitry Andric template <class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r); 4390b57cec5SDimitry Andric 4400b57cec5SDimitry Andric // modifiers: 4410b57cec5SDimitry Andric void swap(shared_ptr& r) noexcept; 4420b57cec5SDimitry Andric void reset() noexcept; 4430b57cec5SDimitry Andric template<class Y> void reset(Y* p); 4440b57cec5SDimitry Andric template<class Y, class D> void reset(Y* p, D d); 4450b57cec5SDimitry Andric template<class Y, class D, class A> void reset(Y* p, D d, A a); 4460b57cec5SDimitry Andric 4470b57cec5SDimitry Andric // observers: 4480b57cec5SDimitry Andric T* get() const noexcept; 4490b57cec5SDimitry Andric T& operator*() const noexcept; 4500b57cec5SDimitry Andric T* operator->() const noexcept; 4510b57cec5SDimitry Andric long use_count() const noexcept; 4520b57cec5SDimitry Andric bool unique() const noexcept; 4530b57cec5SDimitry Andric explicit operator bool() const noexcept; 4540b57cec5SDimitry Andric template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept; 4550b57cec5SDimitry Andric template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept; 4560b57cec5SDimitry Andric}; 4570b57cec5SDimitry Andric 4585ffd83dbSDimitry Andrictemplate<class T> 4595ffd83dbSDimitry Andricshared_ptr(weak_ptr<T>) -> shared_ptr<T>; 4605ffd83dbSDimitry Andrictemplate<class T, class D> 4615ffd83dbSDimitry Andricshared_ptr(unique_ptr<T, D>) -> shared_ptr<T>; 4625ffd83dbSDimitry Andric 4630b57cec5SDimitry Andric// shared_ptr comparisons: 4640b57cec5SDimitry Andrictemplate<class T, class U> 4650b57cec5SDimitry Andric bool operator==(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept; 4660b57cec5SDimitry Andrictemplate<class T, class U> 4670b57cec5SDimitry Andric bool operator!=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept; 4680b57cec5SDimitry Andrictemplate<class T, class U> 4690b57cec5SDimitry Andric bool operator<(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept; 4700b57cec5SDimitry Andrictemplate<class T, class U> 4710b57cec5SDimitry Andric bool operator>(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept; 4720b57cec5SDimitry Andrictemplate<class T, class U> 4730b57cec5SDimitry Andric bool operator<=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept; 4740b57cec5SDimitry Andrictemplate<class T, class U> 4750b57cec5SDimitry Andric bool operator>=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept; 4760b57cec5SDimitry Andric 4770b57cec5SDimitry Andrictemplate <class T> 4780b57cec5SDimitry Andric bool operator==(const shared_ptr<T>& x, nullptr_t) noexcept; 4790b57cec5SDimitry Andrictemplate <class T> 4800b57cec5SDimitry Andric bool operator==(nullptr_t, const shared_ptr<T>& y) noexcept; 4810b57cec5SDimitry Andrictemplate <class T> 4820b57cec5SDimitry Andric bool operator!=(const shared_ptr<T>& x, nullptr_t) noexcept; 4830b57cec5SDimitry Andrictemplate <class T> 4840b57cec5SDimitry Andric bool operator!=(nullptr_t, const shared_ptr<T>& y) noexcept; 4850b57cec5SDimitry Andrictemplate <class T> 4860b57cec5SDimitry Andric bool operator<(const shared_ptr<T>& x, nullptr_t) noexcept; 4870b57cec5SDimitry Andrictemplate <class T> 4880b57cec5SDimitry Andricbool operator<(nullptr_t, const shared_ptr<T>& y) noexcept; 4890b57cec5SDimitry Andrictemplate <class T> 4900b57cec5SDimitry Andric bool operator<=(const shared_ptr<T>& x, nullptr_t) noexcept; 4910b57cec5SDimitry Andrictemplate <class T> 4920b57cec5SDimitry Andric bool operator<=(nullptr_t, const shared_ptr<T>& y) noexcept; 4930b57cec5SDimitry Andrictemplate <class T> 4940b57cec5SDimitry Andric bool operator>(const shared_ptr<T>& x, nullptr_t) noexcept; 4950b57cec5SDimitry Andrictemplate <class T> 4960b57cec5SDimitry Andric bool operator>(nullptr_t, const shared_ptr<T>& y) noexcept; 4970b57cec5SDimitry Andrictemplate <class T> 4980b57cec5SDimitry Andric bool operator>=(const shared_ptr<T>& x, nullptr_t) noexcept; 4990b57cec5SDimitry Andrictemplate <class T> 5000b57cec5SDimitry Andric bool operator>=(nullptr_t, const shared_ptr<T>& y) noexcept; 5010b57cec5SDimitry Andric 5020b57cec5SDimitry Andric// shared_ptr specialized algorithms: 5030b57cec5SDimitry Andrictemplate<class T> void swap(shared_ptr<T>& a, shared_ptr<T>& b) noexcept; 5040b57cec5SDimitry Andric 5050b57cec5SDimitry Andric// shared_ptr casts: 5060b57cec5SDimitry Andrictemplate<class T, class U> 5070b57cec5SDimitry Andric shared_ptr<T> static_pointer_cast(shared_ptr<U> const& r) noexcept; 5080b57cec5SDimitry Andrictemplate<class T, class U> 5090b57cec5SDimitry Andric shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const& r) noexcept; 5100b57cec5SDimitry Andrictemplate<class T, class U> 5110b57cec5SDimitry Andric shared_ptr<T> const_pointer_cast(shared_ptr<U> const& r) noexcept; 5120b57cec5SDimitry Andric 5130b57cec5SDimitry Andric// shared_ptr I/O: 5140b57cec5SDimitry Andrictemplate<class E, class T, class Y> 5150b57cec5SDimitry Andric basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, shared_ptr<Y> const& p); 5160b57cec5SDimitry Andric 5170b57cec5SDimitry Andric// shared_ptr get_deleter: 5180b57cec5SDimitry Andrictemplate<class D, class T> D* get_deleter(shared_ptr<T> const& p) noexcept; 5190b57cec5SDimitry Andric 5200b57cec5SDimitry Andrictemplate<class T, class... Args> 5210b57cec5SDimitry Andric shared_ptr<T> make_shared(Args&&... args); 5220b57cec5SDimitry Andrictemplate<class T, class A, class... Args> 5230b57cec5SDimitry Andric shared_ptr<T> allocate_shared(const A& a, Args&&... args); 5240b57cec5SDimitry Andric 5250b57cec5SDimitry Andrictemplate<class T> 5260b57cec5SDimitry Andricclass weak_ptr 5270b57cec5SDimitry Andric{ 5280b57cec5SDimitry Andricpublic: 529*349cc55cSDimitry Andric typedef T element_type; // until C++17 530*349cc55cSDimitry Andric typedef remove_extent_t<T> element_type; // since C++17 5310b57cec5SDimitry Andric 5320b57cec5SDimitry Andric // constructors 5330b57cec5SDimitry Andric constexpr weak_ptr() noexcept; 5340b57cec5SDimitry Andric template<class Y> weak_ptr(shared_ptr<Y> const& r) noexcept; 5350b57cec5SDimitry Andric weak_ptr(weak_ptr const& r) noexcept; 5360b57cec5SDimitry Andric template<class Y> weak_ptr(weak_ptr<Y> const& r) noexcept; 5370b57cec5SDimitry Andric weak_ptr(weak_ptr&& r) noexcept; // C++14 5380b57cec5SDimitry Andric template<class Y> weak_ptr(weak_ptr<Y>&& r) noexcept; // C++14 5390b57cec5SDimitry Andric 5400b57cec5SDimitry Andric // destructor 5410b57cec5SDimitry Andric ~weak_ptr(); 5420b57cec5SDimitry Andric 5430b57cec5SDimitry Andric // assignment 5440b57cec5SDimitry Andric weak_ptr& operator=(weak_ptr const& r) noexcept; 5450b57cec5SDimitry Andric template<class Y> weak_ptr& operator=(weak_ptr<Y> const& r) noexcept; 5460b57cec5SDimitry Andric template<class Y> weak_ptr& operator=(shared_ptr<Y> const& r) noexcept; 5470b57cec5SDimitry Andric weak_ptr& operator=(weak_ptr&& r) noexcept; // C++14 5480b57cec5SDimitry Andric template<class Y> weak_ptr& operator=(weak_ptr<Y>&& r) noexcept; // C++14 5490b57cec5SDimitry Andric 5500b57cec5SDimitry Andric // modifiers 5510b57cec5SDimitry Andric void swap(weak_ptr& r) noexcept; 5520b57cec5SDimitry Andric void reset() noexcept; 5530b57cec5SDimitry Andric 5540b57cec5SDimitry Andric // observers 5550b57cec5SDimitry Andric long use_count() const noexcept; 5560b57cec5SDimitry Andric bool expired() const noexcept; 5570b57cec5SDimitry Andric shared_ptr<T> lock() const noexcept; 5580b57cec5SDimitry Andric template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept; 5590b57cec5SDimitry Andric template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept; 5600b57cec5SDimitry Andric}; 5610b57cec5SDimitry Andric 5625ffd83dbSDimitry Andrictemplate<class T> 5635ffd83dbSDimitry Andricweak_ptr(shared_ptr<T>) -> weak_ptr<T>; 5645ffd83dbSDimitry Andric 5650b57cec5SDimitry Andric// weak_ptr specialized algorithms: 5660b57cec5SDimitry Andrictemplate<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept; 5670b57cec5SDimitry Andric 5680b57cec5SDimitry Andric// class owner_less: 5690b57cec5SDimitry Andrictemplate<class T> struct owner_less; 5700b57cec5SDimitry Andric 5710b57cec5SDimitry Andrictemplate<class T> 5720b57cec5SDimitry Andricstruct owner_less<shared_ptr<T> > 5730b57cec5SDimitry Andric : binary_function<shared_ptr<T>, shared_ptr<T>, bool> 5740b57cec5SDimitry Andric{ 5750b57cec5SDimitry Andric typedef bool result_type; 5760b57cec5SDimitry Andric bool operator()(shared_ptr<T> const&, shared_ptr<T> const&) const noexcept; 5770b57cec5SDimitry Andric bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept; 5780b57cec5SDimitry Andric bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept; 5790b57cec5SDimitry Andric}; 5800b57cec5SDimitry Andric 5810b57cec5SDimitry Andrictemplate<class T> 5820b57cec5SDimitry Andricstruct owner_less<weak_ptr<T> > 5830b57cec5SDimitry Andric : binary_function<weak_ptr<T>, weak_ptr<T>, bool> 5840b57cec5SDimitry Andric{ 5850b57cec5SDimitry Andric typedef bool result_type; 5860b57cec5SDimitry Andric bool operator()(weak_ptr<T> const&, weak_ptr<T> const&) const noexcept; 5870b57cec5SDimitry Andric bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept; 5880b57cec5SDimitry Andric bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept; 5890b57cec5SDimitry Andric}; 5900b57cec5SDimitry Andric 5910b57cec5SDimitry Andrictemplate <> // Added in C++14 5920b57cec5SDimitry Andricstruct owner_less<void> 5930b57cec5SDimitry Andric{ 5940b57cec5SDimitry Andric template <class _Tp, class _Up> 5950b57cec5SDimitry Andric bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept; 5960b57cec5SDimitry Andric template <class _Tp, class _Up> 5970b57cec5SDimitry Andric bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept; 5980b57cec5SDimitry Andric template <class _Tp, class _Up> 5990b57cec5SDimitry Andric bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept; 6000b57cec5SDimitry Andric template <class _Tp, class _Up> 6010b57cec5SDimitry Andric bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept; 6020b57cec5SDimitry Andric 6030b57cec5SDimitry Andric typedef void is_transparent; 6040b57cec5SDimitry Andric}; 6050b57cec5SDimitry Andric 6060b57cec5SDimitry Andrictemplate<class T> 6070b57cec5SDimitry Andricclass enable_shared_from_this 6080b57cec5SDimitry Andric{ 6090b57cec5SDimitry Andricprotected: 6100b57cec5SDimitry Andric constexpr enable_shared_from_this() noexcept; 6110b57cec5SDimitry Andric enable_shared_from_this(enable_shared_from_this const&) noexcept; 6120b57cec5SDimitry Andric enable_shared_from_this& operator=(enable_shared_from_this const&) noexcept; 6130b57cec5SDimitry Andric ~enable_shared_from_this(); 6140b57cec5SDimitry Andricpublic: 6150b57cec5SDimitry Andric shared_ptr<T> shared_from_this(); 6160b57cec5SDimitry Andric shared_ptr<T const> shared_from_this() const; 6170b57cec5SDimitry Andric}; 6180b57cec5SDimitry Andric 6190b57cec5SDimitry Andrictemplate<class T> 6200b57cec5SDimitry Andric bool atomic_is_lock_free(const shared_ptr<T>* p); 6210b57cec5SDimitry Andrictemplate<class T> 6220b57cec5SDimitry Andric shared_ptr<T> atomic_load(const shared_ptr<T>* p); 6230b57cec5SDimitry Andrictemplate<class T> 6240b57cec5SDimitry Andric shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order mo); 6250b57cec5SDimitry Andrictemplate<class T> 6260b57cec5SDimitry Andric void atomic_store(shared_ptr<T>* p, shared_ptr<T> r); 6270b57cec5SDimitry Andrictemplate<class T> 6280b57cec5SDimitry Andric void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo); 6290b57cec5SDimitry Andrictemplate<class T> 6300b57cec5SDimitry Andric shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r); 6310b57cec5SDimitry Andrictemplate<class T> 6320b57cec5SDimitry Andric shared_ptr<T> 6330b57cec5SDimitry Andric atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo); 6340b57cec5SDimitry Andrictemplate<class T> 6350b57cec5SDimitry Andric bool 6360b57cec5SDimitry Andric atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w); 6370b57cec5SDimitry Andrictemplate<class T> 6380b57cec5SDimitry Andric bool 6390b57cec5SDimitry Andric atomic_compare_exchange_strong( shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w); 6400b57cec5SDimitry Andrictemplate<class T> 6410b57cec5SDimitry Andric bool 6420b57cec5SDimitry Andric atomic_compare_exchange_weak_explicit(shared_ptr<T>* p, shared_ptr<T>* v, 6430b57cec5SDimitry Andric shared_ptr<T> w, memory_order success, 6440b57cec5SDimitry Andric memory_order failure); 6450b57cec5SDimitry Andrictemplate<class T> 6460b57cec5SDimitry Andric bool 6470b57cec5SDimitry Andric atomic_compare_exchange_strong_explicit(shared_ptr<T>* p, shared_ptr<T>* v, 6480b57cec5SDimitry Andric shared_ptr<T> w, memory_order success, 6490b57cec5SDimitry Andric memory_order failure); 6500b57cec5SDimitry Andric// Hash support 6510b57cec5SDimitry Andrictemplate <class T> struct hash; 6520b57cec5SDimitry Andrictemplate <class T, class D> struct hash<unique_ptr<T, D> >; 6530b57cec5SDimitry Andrictemplate <class T> struct hash<shared_ptr<T> >; 6540b57cec5SDimitry Andric 6550b57cec5SDimitry Andrictemplate <class T, class Alloc> 6560b57cec5SDimitry Andric inline constexpr bool uses_allocator_v = uses_allocator<T, Alloc>::value; 6570b57cec5SDimitry Andric 6580b57cec5SDimitry Andricvoid* align(size_t alignment, size_t size, void*& ptr, size_t& space); 6590b57cec5SDimitry Andric 6600b57cec5SDimitry Andric} // std 6610b57cec5SDimitry Andric 6620b57cec5SDimitry Andric*/ 6630b57cec5SDimitry Andric 6640b57cec5SDimitry Andric#include <__config> 665fe6060f1SDimitry Andric#include <__functional_base> 666fe6060f1SDimitry Andric#include <__memory/addressof.h> 667fe6060f1SDimitry Andric#include <__memory/allocation_guard.h> 668fe6060f1SDimitry Andric#include <__memory/allocator.h> 669fe6060f1SDimitry Andric#include <__memory/allocator_arg_t.h> 670fe6060f1SDimitry Andric#include <__memory/allocator_traits.h> 671fe6060f1SDimitry Andric#include <__memory/compressed_pair.h> 672fe6060f1SDimitry Andric#include <__memory/construct_at.h> 673fe6060f1SDimitry Andric#include <__memory/pointer_traits.h> 674fe6060f1SDimitry Andric#include <__memory/raw_storage_iterator.h> 675fe6060f1SDimitry Andric#include <__memory/shared_ptr.h> 676fe6060f1SDimitry Andric#include <__memory/temporary_buffer.h> 677fe6060f1SDimitry Andric#include <__memory/uninitialized_algorithms.h> 678fe6060f1SDimitry Andric#include <__memory/unique_ptr.h> 679fe6060f1SDimitry Andric#include <__memory/uses_allocator.h> 680fe6060f1SDimitry Andric#include <compare> 6810b57cec5SDimitry Andric#include <cstddef> 6820b57cec5SDimitry Andric#include <cstdint> 6830b57cec5SDimitry Andric#include <cstring> 684fe6060f1SDimitry Andric#include <iosfwd> 685fe6060f1SDimitry Andric#include <iterator> 686fe6060f1SDimitry Andric#include <new> 687fe6060f1SDimitry Andric#include <stdexcept> 688fe6060f1SDimitry Andric#include <tuple> 689fe6060f1SDimitry Andric#include <type_traits> 690fe6060f1SDimitry Andric#include <typeinfo> 691fe6060f1SDimitry Andric#include <utility> 6920b57cec5SDimitry Andric#include <version> 6930b57cec5SDimitry Andric 694fe6060f1SDimitry Andric#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR) 695fe6060f1SDimitry Andric# include <__memory/auto_ptr.h> 696fe6060f1SDimitry Andric#endif 697fe6060f1SDimitry Andric 6980b57cec5SDimitry Andric#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 6990b57cec5SDimitry Andric#pragma GCC system_header 7000b57cec5SDimitry Andric#endif 7010b57cec5SDimitry Andric 7020b57cec5SDimitry Andric_LIBCPP_BEGIN_NAMESPACE_STD 7030b57cec5SDimitry Andric 7040b57cec5SDimitry Andrictemplate <class _Alloc, class _Ptr> 7050b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY 706e8d8bef9SDimitry Andricvoid __construct_forward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2) { 707e8d8bef9SDimitry Andric static_assert(__is_cpp17_move_insertable<_Alloc>::value, 708e8d8bef9SDimitry Andric "The specified type does not meet the requirements of Cpp17MoveInsertable"); 709e8d8bef9SDimitry Andric typedef allocator_traits<_Alloc> _Traits; 710e8d8bef9SDimitry Andric for (; __begin1 != __end1; ++__begin1, (void)++__begin2) { 711e8d8bef9SDimitry Andric _Traits::construct(__a, _VSTD::__to_address(__begin2), 712e40139ffSDimitry Andric#ifdef _LIBCPP_NO_EXCEPTIONS 713e40139ffSDimitry Andric _VSTD::move(*__begin1) 714e40139ffSDimitry Andric#else 715e40139ffSDimitry Andric _VSTD::move_if_noexcept(*__begin1) 716e40139ffSDimitry Andric#endif 717e40139ffSDimitry Andric ); 7180b57cec5SDimitry Andric } 719e8d8bef9SDimitry Andric} 7200b57cec5SDimitry Andric 721e8d8bef9SDimitry Andrictemplate <class _Alloc, class _Tp, typename enable_if< 722e8d8bef9SDimitry Andric (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) && 723e8d8bef9SDimitry Andric is_trivially_move_constructible<_Tp>::value 724e8d8bef9SDimitry Andric>::type> 7250b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY 726e8d8bef9SDimitry Andricvoid __construct_forward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2) { 7270b57cec5SDimitry Andric ptrdiff_t _Np = __end1 - __begin1; 728e8d8bef9SDimitry Andric if (_Np > 0) { 7290b57cec5SDimitry Andric _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp)); 7300b57cec5SDimitry Andric __begin2 += _Np; 7310b57cec5SDimitry Andric } 7320b57cec5SDimitry Andric} 7330b57cec5SDimitry Andric 734e8d8bef9SDimitry Andrictemplate <class _Alloc, class _Iter, class _Ptr> 7350b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY 736e8d8bef9SDimitry Andricvoid __construct_range_forward(_Alloc& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2) { 737e8d8bef9SDimitry Andric typedef allocator_traits<_Alloc> _Traits; 738e8d8bef9SDimitry Andric for (; __begin1 != __end1; ++__begin1, (void) ++__begin2) { 739e8d8bef9SDimitry Andric _Traits::construct(__a, _VSTD::__to_address(__begin2), *__begin1); 740e8d8bef9SDimitry Andric } 7410b57cec5SDimitry Andric} 7420b57cec5SDimitry Andric 743e8d8bef9SDimitry Andrictemplate <class _Alloc, class _Source, class _Dest, 744e8d8bef9SDimitry Andric class _RawSource = typename remove_const<_Source>::type, 745e8d8bef9SDimitry Andric class _RawDest = typename remove_const<_Dest>::type, 746e8d8bef9SDimitry Andric class = 747e8d8bef9SDimitry Andric typename enable_if< 748e8d8bef9SDimitry Andric is_trivially_copy_constructible<_Dest>::value && 749e8d8bef9SDimitry Andric is_same<_RawSource, _RawDest>::value && 750e8d8bef9SDimitry Andric (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Dest*, _Source&>::value) 751e8d8bef9SDimitry Andric >::type> 7520b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY 753e8d8bef9SDimitry Andricvoid __construct_range_forward(_Alloc&, _Source* __begin1, _Source* __end1, _Dest*& __begin2) { 7540b57cec5SDimitry Andric ptrdiff_t _Np = __end1 - __begin1; 755e8d8bef9SDimitry Andric if (_Np > 0) { 756e8d8bef9SDimitry Andric _VSTD::memcpy(const_cast<_RawDest*>(__begin2), __begin1, _Np * sizeof(_Dest)); 7570b57cec5SDimitry Andric __begin2 += _Np; 7580b57cec5SDimitry Andric } 7590b57cec5SDimitry Andric} 7600b57cec5SDimitry Andric 761e8d8bef9SDimitry Andrictemplate <class _Alloc, class _Ptr> 7620b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY 763e8d8bef9SDimitry Andricvoid __construct_backward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2) { 764e8d8bef9SDimitry Andric static_assert(__is_cpp17_move_insertable<_Alloc>::value, 765e40139ffSDimitry Andric "The specified type does not meet the requirements of Cpp17MoveInsertable"); 766e8d8bef9SDimitry Andric typedef allocator_traits<_Alloc> _Traits; 767e8d8bef9SDimitry Andric while (__end1 != __begin1) { 768e8d8bef9SDimitry Andric _Traits::construct(__a, _VSTD::__to_address(__end2 - 1), 769e40139ffSDimitry Andric#ifdef _LIBCPP_NO_EXCEPTIONS 770e40139ffSDimitry Andric _VSTD::move(*--__end1) 771e40139ffSDimitry Andric#else 772e40139ffSDimitry Andric _VSTD::move_if_noexcept(*--__end1) 773e40139ffSDimitry Andric#endif 774e40139ffSDimitry Andric ); 7750b57cec5SDimitry Andric --__end2; 7760b57cec5SDimitry Andric } 7770b57cec5SDimitry Andric} 7780b57cec5SDimitry Andric 779e8d8bef9SDimitry Andrictemplate <class _Alloc, class _Tp, class = typename enable_if< 780e8d8bef9SDimitry Andric (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) && 781e8d8bef9SDimitry Andric is_trivially_move_constructible<_Tp>::value 782e8d8bef9SDimitry Andric>::type> 7830b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY 784e8d8bef9SDimitry Andricvoid __construct_backward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2) { 7850b57cec5SDimitry Andric ptrdiff_t _Np = __end1 - __begin1; 7860b57cec5SDimitry Andric __end2 -= _Np; 7870b57cec5SDimitry Andric if (_Np > 0) 788fe6060f1SDimitry Andric _VSTD::memcpy(static_cast<void*>(__end2), static_cast<void const*>(__begin1), _Np * sizeof(_Tp)); 7890b57cec5SDimitry Andric} 7900b57cec5SDimitry Andric 7910b57cec5SDimitry Andricstruct __destruct_n 7920b57cec5SDimitry Andric{ 7930b57cec5SDimitry Andricprivate: 7940b57cec5SDimitry Andric size_t __size_; 7950b57cec5SDimitry Andric 7960b57cec5SDimitry Andric template <class _Tp> 7970b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT 7980b57cec5SDimitry Andric {for (size_t __i = 0; __i < __size_; ++__i, ++__p) __p->~_Tp();} 7990b57cec5SDimitry Andric 8000b57cec5SDimitry Andric template <class _Tp> 8010b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT 8020b57cec5SDimitry Andric {} 8030b57cec5SDimitry Andric 8040b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT 8050b57cec5SDimitry Andric {++__size_;} 8060b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT 8070b57cec5SDimitry Andric {} 8080b57cec5SDimitry Andric 8090b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT 8100b57cec5SDimitry Andric {__size_ = __s;} 8110b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT 8120b57cec5SDimitry Andric {} 8130b57cec5SDimitry Andricpublic: 8140b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT 8150b57cec5SDimitry Andric : __size_(__s) {} 8160b57cec5SDimitry Andric 8170b57cec5SDimitry Andric template <class _Tp> 818e8d8bef9SDimitry Andric _LIBCPP_INLINE_VISIBILITY void __incr() _NOEXCEPT 8190b57cec5SDimitry Andric {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());} 8200b57cec5SDimitry Andric 8210b57cec5SDimitry Andric template <class _Tp> 8220b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT 8230b57cec5SDimitry Andric {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());} 8240b57cec5SDimitry Andric 8250b57cec5SDimitry Andric template <class _Tp> 8260b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT 8270b57cec5SDimitry Andric {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());} 8280b57cec5SDimitry Andric}; 8290b57cec5SDimitry Andric 8300b57cec5SDimitry Andric_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space); 8310b57cec5SDimitry Andric 8320b57cec5SDimitry Andric// --- Helper for container swap -- 8330b57cec5SDimitry Andrictemplate <typename _Alloc> 8340b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY 8350b57cec5SDimitry Andricvoid __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type) 8360b57cec5SDimitry Andric#if _LIBCPP_STD_VER >= 14 8370b57cec5SDimitry Andric _NOEXCEPT 8380b57cec5SDimitry Andric#else 8390b57cec5SDimitry Andric _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value) 8400b57cec5SDimitry Andric#endif 8410b57cec5SDimitry Andric{ 8420b57cec5SDimitry Andric using _VSTD::swap; 8430b57cec5SDimitry Andric swap(__a1, __a2); 8440b57cec5SDimitry Andric} 8450b57cec5SDimitry Andric 8460b57cec5SDimitry Andrictemplate <typename _Alloc> 8470b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY 8480b57cec5SDimitry Andricvoid __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {} 8490b57cec5SDimitry Andric 850e8d8bef9SDimitry Andrictemplate <typename _Alloc> 851e8d8bef9SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY 852e8d8bef9SDimitry Andricvoid __swap_allocator(_Alloc & __a1, _Alloc & __a2) 853e8d8bef9SDimitry Andric#if _LIBCPP_STD_VER >= 14 854e8d8bef9SDimitry Andric _NOEXCEPT 855e8d8bef9SDimitry Andric#else 856e8d8bef9SDimitry Andric _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value) 857e8d8bef9SDimitry Andric#endif 858e8d8bef9SDimitry Andric{ 859e8d8bef9SDimitry Andric _VSTD::__swap_allocator(__a1, __a2, 860fe6060f1SDimitry Andric integral_constant<bool, allocator_traits<_Alloc>::propagate_on_container_swap::value>()); 861e8d8bef9SDimitry Andric} 862e8d8bef9SDimitry Andric 8630b57cec5SDimitry Andrictemplate <typename _Alloc, typename _Traits=allocator_traits<_Alloc> > 8640b57cec5SDimitry Andricstruct __noexcept_move_assign_container : public integral_constant<bool, 8650b57cec5SDimitry Andric _Traits::propagate_on_container_move_assignment::value 8660b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 14 8670b57cec5SDimitry Andric || _Traits::is_always_equal::value 8680b57cec5SDimitry Andric#else 8690b57cec5SDimitry Andric && is_nothrow_move_assignable<_Alloc>::value 8700b57cec5SDimitry Andric#endif 8710b57cec5SDimitry Andric > {}; 8720b57cec5SDimitry Andric 8730b57cec5SDimitry Andric 8740b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc> 8750b57cec5SDimitry Andricstruct __temp_value { 8760b57cec5SDimitry Andric typedef allocator_traits<_Alloc> _Traits; 8770b57cec5SDimitry Andric 8780b57cec5SDimitry Andric typename aligned_storage<sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)>::type __v; 8790b57cec5SDimitry Andric _Alloc &__a; 8800b57cec5SDimitry Andric 8810b57cec5SDimitry Andric _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); } 8820b57cec5SDimitry Andric _Tp & get() { return *__addr(); } 8830b57cec5SDimitry Andric 8840b57cec5SDimitry Andric template<class... _Args> 8850b57cec5SDimitry Andric _LIBCPP_NO_CFI 8860b57cec5SDimitry Andric __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc) { 8870b57cec5SDimitry Andric _Traits::construct(__a, reinterpret_cast<_Tp*>(addressof(__v)), 8880b57cec5SDimitry Andric _VSTD::forward<_Args>(__args)...); 8890b57cec5SDimitry Andric } 8900b57cec5SDimitry Andric 8910b57cec5SDimitry Andric ~__temp_value() { _Traits::destroy(__a, __addr()); } 8920b57cec5SDimitry Andric }; 8930b57cec5SDimitry Andric 8940b57cec5SDimitry Andrictemplate<typename _Alloc, typename = void, typename = void> 8950b57cec5SDimitry Andricstruct __is_allocator : false_type {}; 8960b57cec5SDimitry Andric 8970b57cec5SDimitry Andrictemplate<typename _Alloc> 8980b57cec5SDimitry Andricstruct __is_allocator<_Alloc, 8990b57cec5SDimitry Andric typename __void_t<typename _Alloc::value_type>::type, 900fe6060f1SDimitry Andric typename __void_t<decltype(declval<_Alloc&>().allocate(size_t(0)))>::type 9010b57cec5SDimitry Andric > 9020b57cec5SDimitry Andric : true_type {}; 9030b57cec5SDimitry Andric 9040b57cec5SDimitry Andric// __builtin_new_allocator -- A non-templated helper for allocating and 9050b57cec5SDimitry Andric// deallocating memory using __builtin_operator_new and 9060b57cec5SDimitry Andric// __builtin_operator_delete. It should be used in preference to 9070b57cec5SDimitry Andric// `std::allocator<T>` to avoid additional instantiations. 9080b57cec5SDimitry Andricstruct __builtin_new_allocator { 9090b57cec5SDimitry Andric struct __builtin_new_deleter { 9100b57cec5SDimitry Andric typedef void* pointer_type; 9110b57cec5SDimitry Andric 9120b57cec5SDimitry Andric _LIBCPP_CONSTEXPR explicit __builtin_new_deleter(size_t __size, size_t __align) 9130b57cec5SDimitry Andric : __size_(__size), __align_(__align) {} 9140b57cec5SDimitry Andric 9150b57cec5SDimitry Andric void operator()(void* p) const _NOEXCEPT { 916e8d8bef9SDimitry Andric _VSTD::__libcpp_deallocate(p, __size_, __align_); 9170b57cec5SDimitry Andric } 9180b57cec5SDimitry Andric 9190b57cec5SDimitry Andric private: 9200b57cec5SDimitry Andric size_t __size_; 9210b57cec5SDimitry Andric size_t __align_; 9220b57cec5SDimitry Andric }; 9230b57cec5SDimitry Andric 9240b57cec5SDimitry Andric typedef unique_ptr<void, __builtin_new_deleter> __holder_t; 9250b57cec5SDimitry Andric 9260b57cec5SDimitry Andric static __holder_t __allocate_bytes(size_t __s, size_t __align) { 927e8d8bef9SDimitry Andric return __holder_t(_VSTD::__libcpp_allocate(__s, __align), 9280b57cec5SDimitry Andric __builtin_new_deleter(__s, __align)); 9290b57cec5SDimitry Andric } 9300b57cec5SDimitry Andric 9310b57cec5SDimitry Andric static void __deallocate_bytes(void* __p, size_t __s, 9320b57cec5SDimitry Andric size_t __align) _NOEXCEPT { 933e8d8bef9SDimitry Andric _VSTD::__libcpp_deallocate(__p, __s, __align); 9340b57cec5SDimitry Andric } 9350b57cec5SDimitry Andric 9360b57cec5SDimitry Andric template <class _Tp> 9370b57cec5SDimitry Andric _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE 9380b57cec5SDimitry Andric static __holder_t __allocate_type(size_t __n) { 9390b57cec5SDimitry Andric return __allocate_bytes(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)); 9400b57cec5SDimitry Andric } 9410b57cec5SDimitry Andric 9420b57cec5SDimitry Andric template <class _Tp> 9430b57cec5SDimitry Andric _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE 9440b57cec5SDimitry Andric static void __deallocate_type(void* __p, size_t __n) _NOEXCEPT { 9450b57cec5SDimitry Andric __deallocate_bytes(__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)); 9460b57cec5SDimitry Andric } 9470b57cec5SDimitry Andric}; 9480b57cec5SDimitry Andric 9490b57cec5SDimitry Andric 9500b57cec5SDimitry Andric_LIBCPP_END_NAMESPACE_STD 9510b57cec5SDimitry Andric 952e40139ffSDimitry Andric#if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17 953e40139ffSDimitry Andric# include <__pstl_memory> 954e40139ffSDimitry Andric#endif 955e40139ffSDimitry Andric 9560b57cec5SDimitry Andric#endif // _LIBCPP_MEMORY 957