/* Copyright 2019 Glen Joseph Fernandes (glenjofe@gmail.com) Distributed under the Boost Software License, Version 1.0. (http://www.boost.org/LICENSE_1_0.txt) */ #ifndef BOOST_CORE_DEFAULT_ALLOCATOR_HPP #define BOOST_CORE_DEFAULT_ALLOCATOR_HPP #include #include #include #if defined(BOOST_LIBSTDCXX_VERSION) && BOOST_LIBSTDCXX_VERSION < 60000 #define BOOST_CORE_NO_CXX11_ALLOCATOR #endif namespace boost { #if defined(BOOST_NO_EXCEPTIONS) void throw_exception(const std::exception&); #endif namespace default_ { struct true_type { typedef bool value_type; typedef true_type type; BOOST_STATIC_CONSTANT(bool, value = true); BOOST_CONSTEXPR operator bool() const BOOST_NOEXCEPT { return true; } BOOST_CONSTEXPR bool operator()() const BOOST_NOEXCEPT { return true; } }; template struct add_reference { typedef T& type; }; template<> struct add_reference { typedef void type; }; template<> struct add_reference { typedef const void type; }; template struct default_allocator { typedef T value_type; typedef T* pointer; typedef const T* const_pointer; typedef typename add_reference::type reference; typedef typename add_reference::type const_reference; typedef std::size_t size_type; typedef std::ptrdiff_t difference_type; typedef true_type propagate_on_container_move_assignment; typedef true_type is_always_equal; template struct rebind { typedef default_allocator other; }; #if !defined(BOOST_NO_CXX11_DEFAULTED_FUNCTIONS) default_allocator() = default; #else BOOST_CONSTEXPR default_allocator() BOOST_NOEXCEPT { } #endif template BOOST_CONSTEXPR default_allocator(const default_allocator&) BOOST_NOEXCEPT { } #if defined(PTRDIFF_MAX) && defined(SIZE_MAX) BOOST_CONSTEXPR std::size_t max_size() const BOOST_NOEXCEPT { return PTRDIFF_MAX < SIZE_MAX / sizeof(T) ? PTRDIFF_MAX : SIZE_MAX / sizeof(T); } #else BOOST_CONSTEXPR std::size_t max_size() const BOOST_NOEXCEPT { return ~static_cast(0) / sizeof(T); } #endif #if !defined(BOOST_NO_EXCEPTIONS) T* allocate(std::size_t n) { if (n > max_size()) { throw std::bad_alloc(); } return static_cast(::operator new(sizeof(T) * n)); } void deallocate(T* p, std::size_t) { ::operator delete(p); } #else T* allocate(std::size_t n) { if (n > max_size()) { boost::throw_exception(std::bad_alloc()); } void* p = ::operator new(sizeof(T) * n, std::nothrow); if (!p) { boost::throw_exception(std::bad_alloc()); } return static_cast(p); } void deallocate(T* p, std::size_t) { ::operator delete(p, std::nothrow); } #endif #if defined(BOOST_NO_CXX11_ALLOCATOR) || defined(BOOST_CORE_NO_CXX11_ALLOCATOR) template void construct(U* p, const V& v) { ::new(p) U(v); } template void destroy(U* p) { p->~U(); } #endif }; template BOOST_CONSTEXPR inline bool operator==(const default_allocator&, const default_allocator&) BOOST_NOEXCEPT { return true; } template BOOST_CONSTEXPR inline bool operator!=(const default_allocator&, const default_allocator&) BOOST_NOEXCEPT { return false; } } /* default_ */ using default_::default_allocator; } /* boost */ #endif