// -*- C++ -*- //===------------------------ memory_resource -----------------------------===// // // The LLVM Compiler Infrastructure // // This file is distributed under the University of Illinois Open Source // License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// #ifndef _LIBCPP_EXPERIMENTAL_MEMORY_RESOURCE #define _LIBCPP_EXPERIMENTAL_MEMORY_RESOURCE /** experimental/memory_resource synopsis // C++1y namespace std { namespace experimental { inline namespace fundamentals_v1 { namespace pmr { class memory_resource; bool operator==(const memory_resource& a, const memory_resource& b) noexcept; bool operator!=(const memory_resource& a, const memory_resource& b) noexcept; template class polymorphic_allocator; template bool operator==(const polymorphic_allocator& a, const polymorphic_allocator& b) noexcept; template bool operator!=(const polymorphic_allocator& a, const polymorphic_allocator& b) noexcept; // The name resource_adaptor_imp is for exposition only. template class resource_adaptor_imp; template using resource_adaptor = resource_adaptor_imp< allocator_traits::rebind_alloc>; // Global memory resources memory_resource* new_delete_resource() noexcept; memory_resource* null_memory_resource() noexcept; // The default memory resource memory_resource* set_default_resource(memory_resource* r) noexcept; memory_resource* get_default_resource() noexcept; // Standard memory resources struct pool_options; class synchronized_pool_resource; class unsynchronized_pool_resource; class monotonic_buffer_resource; } // namespace pmr } // namespace fundamentals_v1 } // namespace experimental } // namespace std */ #include #include #include #include #include #include #include <__tuple> #include #include #include #include #include <__debug> #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) #pragma GCC system_header #endif _LIBCPP_PUSH_MACROS #include <__undef_macros> _LIBCPP_BEGIN_NAMESPACE_LFTS_PMR // Round __s up to next multiple of __a. inline _LIBCPP_INLINE_VISIBILITY size_t __aligned_allocation_size(size_t __s, size_t __a) _NOEXCEPT { _LIBCPP_ASSERT(__s + __a > __s, "aligned allocation size overflows"); return (__s + __a - 1) & ~(__a - 1); } // 8.5, memory.resource class _LIBCPP_TYPE_VIS memory_resource { static const size_t __max_align = alignof(max_align_t); // 8.5.2, memory.resource.public public: virtual ~memory_resource() = default; _LIBCPP_INLINE_VISIBILITY void* allocate(size_t __bytes, size_t __align = __max_align) { return do_allocate(__bytes, __align); } _LIBCPP_INLINE_VISIBILITY void deallocate(void * __p, size_t __bytes, size_t __align = __max_align) { do_deallocate(__p, __bytes, __align); } _LIBCPP_INLINE_VISIBILITY bool is_equal(memory_resource const & __other) const _NOEXCEPT { return do_is_equal(__other); } // 8.5.3, memory.resource.priv protected: virtual void* do_allocate(size_t, size_t) = 0; virtual void do_deallocate(void*, size_t, size_t) = 0; virtual bool do_is_equal(memory_resource const &) const _NOEXCEPT = 0; }; // 8.5.4, memory.resource.eq inline _LIBCPP_INLINE_VISIBILITY bool operator==(memory_resource const & __lhs, memory_resource const & __rhs) _NOEXCEPT { return &__lhs == &__rhs || __lhs.is_equal(__rhs); } inline _LIBCPP_INLINE_VISIBILITY bool operator!=(memory_resource const & __lhs, memory_resource const & __rhs) _NOEXCEPT { return !(__lhs == __rhs); } _LIBCPP_FUNC_VIS memory_resource * new_delete_resource() _NOEXCEPT; _LIBCPP_FUNC_VIS memory_resource * null_memory_resource() _NOEXCEPT; _LIBCPP_FUNC_VIS memory_resource * get_default_resource() _NOEXCEPT; _LIBCPP_FUNC_VIS memory_resource * set_default_resource(memory_resource * __new_res) _NOEXCEPT; // 8.6, memory.polymorphic.allocator.class // 8.6.1, memory.polymorphic.allocator.overview template class _LIBCPP_TEMPLATE_VIS polymorphic_allocator { public: typedef _ValueType value_type; // 8.6.2, memory.polymorphic.allocator.ctor _LIBCPP_INLINE_VISIBILITY polymorphic_allocator() _NOEXCEPT : __res_(_VSTD_LFTS_PMR::get_default_resource()) {} _LIBCPP_INLINE_VISIBILITY polymorphic_allocator(memory_resource * __r) _NOEXCEPT : __res_(__r) {} polymorphic_allocator(polymorphic_allocator const &) = default; template _LIBCPP_INLINE_VISIBILITY polymorphic_allocator(polymorphic_allocator<_Tp> const & __other) _NOEXCEPT : __res_(__other.resource()) {} polymorphic_allocator & operator=(polymorphic_allocator const &) = delete; // 8.6.3, memory.polymorphic.allocator.mem _LIBCPP_INLINE_VISIBILITY _ValueType* allocate(size_t __n) { if (__n > __max_size()) { __throw_length_error( "std::experimental::pmr::polymorphic_allocator::allocate(size_t n)" " 'n' exceeds maximum supported size"); } return static_cast<_ValueType*>( __res_->allocate(__n * sizeof(_ValueType), alignof(_ValueType)) ); } _LIBCPP_INLINE_VISIBILITY void deallocate(_ValueType * __p, size_t __n) _NOEXCEPT { _LIBCPP_ASSERT(__n <= __max_size(), "deallocate called for size which exceeds max_size()"); __res_->deallocate(__p, __n * sizeof(_ValueType), alignof(_ValueType)); } template _LIBCPP_INLINE_VISIBILITY void construct(_Tp* __p, _Ts &&... __args) { _VSTD_LFTS::__lfts_user_alloc_construct( __p, *this, _VSTD::forward<_Ts>(__args)... ); } template _LIBCPP_INLINE_VISIBILITY void construct(pair<_T1, _T2>* __p, piecewise_construct_t, tuple<_Args1...> __x, tuple<_Args2...> __y) { ::new ((void*)__p) pair<_T1, _T2>(piecewise_construct , __transform_tuple( typename __lfts_uses_alloc_ctor< _T1, polymorphic_allocator&, _Args1... >::type() , _VSTD::move(__x) , typename __make_tuple_indices::type{} ) , __transform_tuple( typename __lfts_uses_alloc_ctor< _T2, polymorphic_allocator&, _Args2... >::type() , _VSTD::move(__y) , typename __make_tuple_indices::type{} ) ); } template _LIBCPP_INLINE_VISIBILITY void construct(pair<_T1, _T2>* __p) { construct(__p, piecewise_construct, tuple<>(), tuple<>()); } template _LIBCPP_INLINE_VISIBILITY void construct(pair<_T1, _T2> * __p, _Up && __u, _Vp && __v) { construct(__p, piecewise_construct , _VSTD::forward_as_tuple(_VSTD::forward<_Up>(__u)) , _VSTD::forward_as_tuple(_VSTD::forward<_Vp>(__v))); } template _LIBCPP_INLINE_VISIBILITY void construct(pair<_T1, _T2> * __p, pair<_U1, _U2> const & __pr) { construct(__p, piecewise_construct , _VSTD::forward_as_tuple(__pr.first) , _VSTD::forward_as_tuple(__pr.second)); } template _LIBCPP_INLINE_VISIBILITY void construct(pair<_T1, _T2> * __p, pair<_U1, _U2> && __pr){ construct(__p, piecewise_construct , _VSTD::forward_as_tuple(_VSTD::forward<_U1>(__pr.first)) , _VSTD::forward_as_tuple(_VSTD::forward<_U2>(__pr.second))); } template _LIBCPP_INLINE_VISIBILITY void destroy(_Tp * __p) _NOEXCEPT { __p->~_Tp(); } _LIBCPP_INLINE_VISIBILITY polymorphic_allocator select_on_container_copy_construction() const _NOEXCEPT { return polymorphic_allocator(); } _LIBCPP_INLINE_VISIBILITY memory_resource * resource() const _NOEXCEPT { return __res_; } private: template _LIBCPP_INLINE_VISIBILITY tuple<_Args&&...> __transform_tuple(integral_constant, tuple<_Args...>&& __t, __tuple_indices<_Idx...>) const { return _VSTD::forward_as_tuple(_VSTD::get<_Idx>(_VSTD::move(__t))...); } template _LIBCPP_INLINE_VISIBILITY tuple __transform_tuple(integral_constant, tuple<_Args...> && __t, __tuple_indices<_Idx...>) { using _Tup = tuple; return _Tup(allocator_arg, *this, _VSTD::get<_Idx>(_VSTD::move(__t))...); } template _LIBCPP_INLINE_VISIBILITY tuple<_Args&&..., polymorphic_allocator&> __transform_tuple(integral_constant, tuple<_Args...> && __t, __tuple_indices<_Idx...>) { using _Tup = tuple<_Args&&..., polymorphic_allocator&>; return _Tup(_VSTD::get<_Idx>(_VSTD::move(__t))..., *this); } _LIBCPP_INLINE_VISIBILITY size_t __max_size() const _NOEXCEPT { return numeric_limits::max() / sizeof(value_type); } memory_resource * __res_; }; // 8.6.4, memory.polymorphic.allocator.eq template inline _LIBCPP_INLINE_VISIBILITY bool operator==(polymorphic_allocator<_Tp> const & __lhs, polymorphic_allocator<_Up> const & __rhs) _NOEXCEPT { return *__lhs.resource() == *__rhs.resource(); } template inline _LIBCPP_INLINE_VISIBILITY bool operator!=(polymorphic_allocator<_Tp> const & __lhs, polymorphic_allocator<_Up> const & __rhs) _NOEXCEPT { return !(__lhs == __rhs); } // 8.7, memory.resource.adaptor // 8.7.1, memory.resource.adaptor.overview template class _LIBCPP_TEMPLATE_VIS __resource_adaptor_imp : public memory_resource { using _CTraits = allocator_traits<_CharAlloc>; static_assert(is_same::value && is_same::value && is_same::value, ""); static const size_t _MaxAlign = alignof(max_align_t); using _Alloc = typename _CTraits::template rebind_alloc< typename aligned_storage<_MaxAlign, _MaxAlign>::type >; using _ValueType = typename _Alloc::value_type; _Alloc __alloc_; public: typedef _CharAlloc allocator_type; __resource_adaptor_imp() = default; __resource_adaptor_imp(__resource_adaptor_imp const &) = default; __resource_adaptor_imp(__resource_adaptor_imp &&) = default; // 8.7.2, memory.resource.adaptor.ctor _LIBCPP_INLINE_VISIBILITY explicit __resource_adaptor_imp(allocator_type const & __a) : __alloc_(__a) {} _LIBCPP_INLINE_VISIBILITY explicit __resource_adaptor_imp(allocator_type && __a) : __alloc_(_VSTD::move(__a)) {} __resource_adaptor_imp & operator=(__resource_adaptor_imp const &) = default; _LIBCPP_INLINE_VISIBILITY allocator_type get_allocator() const { return __alloc_; } // 8.7.3, memory.resource.adaptor.mem protected: virtual void * do_allocate(size_t __bytes, size_t) { if (__bytes > __max_size()) { __throw_length_error( "std::experimental::pmr::resource_adaptor::do_allocate(size_t bytes, size_t align)" " 'bytes' exceeds maximum supported size"); } size_t __s = __aligned_allocation_size(__bytes, _MaxAlign) / _MaxAlign; return __alloc_.allocate(__s); } virtual void do_deallocate(void * __p, size_t __bytes, size_t) { _LIBCPP_ASSERT(__bytes <= __max_size(), "do_deallocate called for size which exceeds the maximum allocation size"); size_t __s = __aligned_allocation_size(__bytes, _MaxAlign) / _MaxAlign; __alloc_.deallocate((_ValueType*)__p, __s); } virtual bool do_is_equal(memory_resource const & __other) const _NOEXCEPT { __resource_adaptor_imp const * __p = dynamic_cast<__resource_adaptor_imp const *>(&__other); return __p ? __alloc_ == __p->__alloc_ : false; } private: _LIBCPP_INLINE_VISIBILITY size_t __max_size() const _NOEXCEPT { return numeric_limits::max() - _MaxAlign; } }; template using resource_adaptor = __resource_adaptor_imp< typename allocator_traits<_Alloc>::template rebind_alloc >; _LIBCPP_END_NAMESPACE_LFTS_PMR _LIBCPP_POP_MACROS #endif /* _LIBCPP_EXPERIMENTAL_MEMORY_RESOURCE */