Changeset 032565d in mainline


Ignore:
Timestamp:
2018-07-05T21:41:24Z (6 years ago)
Author:
Dzejrou <dzejrou@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
fc15120
Parents:
f31ea60
git-author:
Dzejrou <dzejrou@…> (2018-05-16 17:48:08)
git-committer:
Dzejrou <dzejrou@…> (2018-07-05 21:41:24)
Message:

cpp: remove impl/memory.hpp and moved its contents to internal/memory/

Location:
uspace/lib/cpp/include
Files:
3 added
2 edited
1 moved

Legend:

Unmodified
Added
Removed
  • uspace/lib/cpp/include/internal/memory/shared_ptr.hpp

    rf31ea60 r032565d  
    3535#include <internal/memory/allocator_arg.hpp>
    3636#include <internal/memory/shared_payload.hpp>
     37#include <internal/memory/unique_ptr.hpp>
    3738#include <internal/trycatch.hpp>
    3839#include <type_traits>
  • uspace/lib/cpp/include/internal/memory/unique_ptr.hpp

    rf31ea60 r032565d  
    2727 */
    2828
    29 #ifndef LIBCPP_MEMORY
    30 #define LIBCPP_MEMORY
     29#ifndef LIBCPP_INTERNAL_MEMORY_UNIQUE_PTR
     30#define LIBCPP_INTERNAL_MEMORY_UNIQUE_PTR
    3131
    3232#include <internal/aux.hpp>
    3333#include <internal/functional/hash.hpp>
    34 #include <internal/memory/addressof.hpp>
    35 #include <internal/memory/allocator_arg.hpp>
    36 #include <internal/memory/type_getters.hpp>
    37 #include <iterator>
    38 #include <limits>
    39 #include <new>
    4034#include <type_traits>
    4135#include <utility>
     
    4337namespace std
    4438{
    45     /**
    46      * 20.7.3, pointer traits:
    47      * Note: The type getters that are used in pointer_traits
    48      *       and allocator_traits are implemented in
    49      *       <internal/memory/type_getters.hpp>.
    50      */
    51 
    52     template<class Ptr>
    53     struct pointer_traits
    54     {
    55         using pointer         = Ptr;
    56         using element_type    = typename aux::ptr_get_element_type<Ptr>::type;
    57         using difference_type = typename aux::ptr_get_difference_type<Ptr>::type;
    58 
    59         template<class U>
    60         using rebind = typename aux::ptr_get_rebind<Ptr, U>::type;
    61 
    62         static pointer pointer_to( // If is_void_t<element_type>, this type is unspecified.
    63             conditional_t<is_void_v<element_type>, char, element_type&> x
    64         )
    65         {
    66             return Ptr::pointer_to(x);
    67         }
    68     };
    69 
    70     template<class T>
    71     struct pointer_traits<T*>
    72     {
    73         using pointer         = T*;
    74         using element_type    = T;
    75         using difference_type = ptrdiff_t;
    76 
    77         template<class U>
    78         using rebind = U*;
    79 
    80         static pointer pointer_to(
    81             conditional_t<is_void_v<element_type>, char, element_type&> x
    82         )
    83         {
    84             return std::addressof(x);
    85         }
    86     };
    87 
    88     /**
    89      * 20.7.4, pointer safety:
    90      */
    91 
    92     // TODO: implement
    93 
    94     /**
    95      * 20.7.5, align:
    96      */
    97 
    98     // TODO: implement
    99 
    100     /**
    101      * 20.7.7, uses_allocator:
    102      */
    103 
    104     namespace aux
    105     {
    106         template<class T, class = void>
    107         struct has_allocator_type: false_type
    108         { /* DUMMY BODY */ };
    109 
    110         template<class T>
    111         struct has_allocator_type<T, void_t<typename T::allocator_type>>
    112             : true_type
    113         { /* DUMMY BODY */ };
    114     }
    115 
    116     template<class T, class Alloc, class = void>
    117     struct uses_allocator
    118         : aux::value_is<
    119         bool, aux::has_allocator_type<T>::value && is_convertible_v<
    120             Alloc, typename T::allocator_type
    121         >
    122     >
    123     { /* DUMMY BODY */ };
    124 
    125     /**
    126      * 20.7.8, allocator traits:
    127      */
    128 
    129     template<class Alloc>
    130     struct allocator_traits
    131     {
    132         using allocator_type = Alloc;
    133 
    134         using value_type         = typename Alloc::value_type;
    135         using pointer            = typename aux::alloc_get_pointer<Alloc>::type;
    136         using const_pointer      = typename aux::alloc_get_const_pointer<Alloc, pointer>::type;
    137         using void_pointer       = typename aux::alloc_get_void_pointer<Alloc, pointer>::type;
    138         using const_void_pointer = typename aux::alloc_get_const_void_pointer<Alloc, pointer>::type;
    139         using difference_type    = typename aux::alloc_get_difference_type<Alloc, pointer>::type;
    140         using size_type          = typename aux::alloc_get_size_type<Alloc, difference_type>::type;
    141 
    142         using propagate_on_container_copy_assignment = typename aux::alloc_get_copy_propagate<Alloc>::type;
    143         using propagate_on_container_move_assignment = typename aux::alloc_get_move_propagate<Alloc>::type;
    144         using propagate_on_container_swap            = typename aux::alloc_get_swap_propagate<Alloc>::type;
    145         using is_always_equal                        = typename aux::alloc_get_always_equal<Alloc>::type;
    146 
    147         template<class T>
    148         using rebind_alloc = typename aux::alloc_get_rebind_alloc<Alloc, T>;
    149 
    150         template<class T>
    151         using rebind_traits = allocator_traits<rebind_alloc<T>>;
    152 
    153         static pointer allocate(Alloc& alloc, size_type n)
    154         {
    155             return alloc.allocate(n);
    156         }
    157 
    158         static pointer allocate(Alloc& alloc, size_type n, const_void_pointer hint)
    159         {
    160             if constexpr (aux::alloc_has_hint_allocate<Alloc, size_type, const_void_pointer>::value)
    161                 return alloc.allocate(n, hint);
    162             else
    163                 return alloc.allocate(n);
    164         }
    165 
    166         static void deallocate(Alloc& alloc, pointer ptr, size_type n)
    167         {
    168             alloc.deallocate(ptr, n);
    169         }
    170 
    171         template<class T, class... Args>
    172         static void construct(Alloc& alloc, T* ptr, Args&&... args)
    173         {
    174             if constexpr (aux::alloc_has_construct<Alloc, T, Args...>::value)
    175                 alloc.construct(ptr, forward<Args>(args)...);
    176             else
    177                 ::new(static_cast<void*>(ptr)) T(forward<Args>(args)...);
    178         }
    179 
    180         template<class T>
    181         static void destroy(Alloc& alloc, T* ptr)
    182         {
    183             if constexpr (aux::alloc_has_destroy<Alloc, T>::value)
    184                 alloc.destroy(ptr);
    185             else
    186                 ptr->~T();
    187         }
    188 
    189         static size_type max_size(const Alloc& alloc) noexcept
    190         {
    191             if constexpr (aux::alloc_has_max_size<Alloc>::value)
    192                 return alloc.max_size();
    193             else
    194                 return numeric_limits<size_type>::max();
    195         }
    196 
    197         static Alloc select_on_container_copy_construction(const Alloc& alloc)
    198         {
    199             if constexpr (aux::alloc_has_select<Alloc>::value)
    200                 return alloc.select_on_container_copy_construction();
    201             else
    202                 return alloc;
    203         }
    204     };
    205 
    206     /**
    207      * 20.7.9, the default allocator
    208      */
    209 
    210     template<class T>
    211     class allocator;
    212 
    213     template<>
    214     class allocator<void>
    215     {
    216         public:
    217             using pointer       = void*;
    218             using const_pointer = const void*;
    219             using value_type    = void;
    220 
    221             template<class U>
    222             struct rebind
    223             {
    224                 using other = allocator<U>;
    225             };
    226     };
    227 
    228     template<class T>
    229     T* addressof(T& x) noexcept;
    230 
    231     template<class T>
    232     class allocator
    233     {
    234         public:
    235             using size_type       = size_t;
    236             using difference_type = ptrdiff_t;
    237             using pointer         = T*;
    238             using const_pointer   = const T*;
    239             using reference       = T&;
    240             using const_reference = const T&;
    241             using value_type      = T;
    242 
    243             template<class U>
    244             struct rebind
    245             {
    246                 using other = allocator<U>;
    247             };
    248 
    249             using propagate_on_container_move_assignment = true_type;
    250             using is_always_equal                        = true_type;
    251 
    252             allocator() noexcept = default;
    253 
    254             allocator(const allocator&) noexcept = default;
    255 
    256             template<class U>
    257             allocator(const allocator<U>&) noexcept
    258             { /* DUMMY BODY */ }
    259 
    260             ~allocator() = default;
    261 
    262             pointer address(reference x) const noexcept
    263             {
    264                 return addressof(x);
    265             }
    266 
    267             const_pointer address(const_reference x) const noexcept
    268             {
    269                 return addressof(x);
    270             }
    271 
    272             pointer allocate(size_type n, allocator<void>::const_pointer = 0)
    273             {
    274                 return static_cast<pointer>(::operator new(n * sizeof(value_type)));
    275             }
    276 
    277             void deallocate(pointer ptr, size_type n)
    278             {
    279                 ::operator delete(ptr, n);
    280             }
    281 
    282             size_type max_size() const noexcept
    283             {
    284                 return numeric_limits<size_type>::max();
    285             }
    286 
    287             template<class U, class... Args>
    288             void construct(U* ptr, Args&&... args)
    289             {
    290                 ::new((void*)ptr) U(forward<Args>(args)...);
    291             }
    292 
    293             template<class U>
    294             void destroy(U* ptr)
    295             {
    296                 ptr->~U();
    297             }
    298     };
    299 
    300     template<class T1, class T2>
    301     bool operator==(const allocator<T1>&, const allocator<T2>&) noexcept
    302     {
    303         return true;
    304     }
    305 
    306     template<class T1, class T2>
    307     bool operator!=(const allocator<T1>&, const allocator<T2>&) noexcept
    308     {
    309         return false;
    310     }
    311 
    312     /**
    313      * 20.7.10, raw storage iterator:
    314      */
    315 
    316     template<class OutputIterator, class T>
    317     class raw_storage_iterator: public iterator<output_iterator_tag, void, void, void, void>
    318     {
    319         public:
    320             explicit raw_storage_iterator(OutputIterator it)
    321                 : it_{it}
    322             { /* DUMMY BODY */ }
    323 
    324             raw_storage_iterator& operator*()
    325             {
    326                 return *this;
    327             }
    328 
    329             raw_storage_iterator& operator=(const T& element)
    330             {
    331                 new(it_) T{element};
    332 
    333                 return *this;
    334             }
    335 
    336             raw_storage_iterator& operator++()
    337             {
    338                 ++it_;
    339 
    340                 return *this;
    341             }
    342 
    343             raw_storage_iterator operator++(int)
    344             {
    345                 return raw_storage_iterator{it_++};
    346             }
    347 
    348         private:
    349             OutputIterator it_;
    350     };
    351 
    352     /**
    353      * 20.7.11, temporary buffers:
    354      */
    355 
    356     template<class T>
    357     pair<T*, ptrdiff_t> get_temporary_buffer(ptrdiff_t n) noexcept
    358     {
    359         T* res{};
    360 
    361         while (n > 0)
    362         {
    363             res = (T*)malloc(n * sizeof(T));
    364 
    365             if (res)
    366                 return make_pair(res, n);
    367 
    368             --n;
    369         }
    370 
    371         return make_pair(nullptr, ptrdiff_t{});
    372     }
    373 
    374     template<class T>
    375     void return_temporary_buffer(T* ptr)
    376     {
    377         free(ptr);
    378     }
    379 
    380     /**
    381      * 20.7.12, specialized algorithms:
    382      */
    383 
    384     template<class Iterator>
    385     struct iterator_traits;
    386 
    387     template<class InputIterator, class ForwardIterator>
    388     ForwardIterator unitialized_copy(
    389         InputIterator first, InputIterator last,
    390         ForwardIterator result
    391     )
    392     {
    393         for (; first != last; ++first, ++result)
    394             ::new (static_cast<void*>(&*result)) typename iterator_traits<ForwardIterator>::value_type(*first);
    395 
    396         return result;
    397     }
    398 
    399     template<class InputIterator, class Size, class ForwardIterator>
    400     ForwardIterator unitialized_copy_n(
    401         InputIterator first, Size n, ForwardIterator result
    402     )
    403     {
    404         for (; n > 0; ++first, --n, ++result)
    405             ::new (static_cast<void*>(&*result)) typename iterator_traits<ForwardIterator>::value_type(*first);
    406 
    407         return result;
    408     }
    409 
    410     template<class ForwardIterator, class T>
    411     void unitialized_fill(
    412         ForwardIterator first, ForwardIterator last, const T& x
    413     )
    414     {
    415         for (; first != last; ++first)
    416             ::new (static_cast<void*>(&*first)) typename iterator_traits<ForwardIterator>::value_type(x);
    417     }
    418 
    419     template<class ForwardIterator, class Size, class T>
    420     ForwardIterator unitialized_fill_n(
    421         ForwardIterator first, Size n, const T& x
    422     )
    423     {
    424         for (; n > 0; ++first, --n)
    425             ::new (static_cast<void*>(&*first)) typename iterator_traits<ForwardIterator>::value_type(x);
    426 
    427         return first;
    428     }
    429 
    43039    /**
    43140     * 20.8, smart pointers:
  • uspace/lib/cpp/include/memory

    rf31ea60 r032565d  
    2727 */
    2828
    29 #include <impl/memory.hpp>
    3029#include <internal/memory/allocator_arg.hpp>
     30#include <internal/memory/allocator_traits.hpp>
    3131#include <internal/memory/addressof.hpp>
     32#include <internal/memory/misc.hpp>
    3233#include <internal/memory/owner_less.hpp>
     34#include <internal/memory/pointer_traits.hpp>
    3335#include <internal/memory/shared_ptr.hpp>
     36#include <internal/memory/unique_ptr.hpp>
    3437#include <internal/memory/weak_ptr.hpp>
Note: See TracChangeset for help on using the changeset viewer.