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