/* 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 BHO_CORE_DEFAULT_ALLOCATOR_HPP #define BHO_CORE_DEFAULT_ALLOCATOR_HPP #include #include namespace bho { #if defined(BHO_NO_EXCEPTIONS) BHO_NORETURN void throw_exception(const std::exception&); #endif namespace default_ { template struct bool_constant { typedef bool value_type; typedef bool_constant type; static const bool value = V; operator bool() const BHO_NOEXCEPT { return V; } bool operator()() const BHO_NOEXCEPT { return V; } }; template const bool bool_constant::value; 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 bool_constant propagate_on_container_move_assignment; typedef bool_constant is_always_equal; template struct rebind { typedef default_allocator other; }; #if !defined(BHO_NO_CXX11_DEFAULTED_FUNCTIONS) default_allocator() = default; #else BHO_CONSTEXPR default_allocator() BHO_NOEXCEPT { } #endif template BHO_CONSTEXPR default_allocator(const default_allocator&) BHO_NOEXCEPT { } BHO_CONSTEXPR std::size_t max_size() const BHO_NOEXCEPT { return static_cast(-1) / (2 < sizeof(T) ? sizeof(T) : 2); } #if !defined(BHO_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()) { bho::throw_exception(std::bad_alloc()); } void* p = ::operator new(sizeof(T) * n, std::nothrow); if (!p) { bho::throw_exception(std::bad_alloc()); } return static_cast(p); } void deallocate(T* p, std::size_t) { ::operator delete(p, std::nothrow); } #endif #if defined(BHO_NO_CXX11_ALLOCATOR) T* allocate(std::size_t n, const void*) { return allocate(n); } #endif #if (defined(BHO_LIBSTDCXX_VERSION) && BHO_LIBSTDCXX_VERSION < 60000) || \ defined(BHO_NO_CXX11_ALLOCATOR) template void construct(U* p, const V& v) { ::new(p) U(v); } template void destroy(U* p) { p->~U(); (void)p; } #endif }; template BHO_CONSTEXPR inline bool operator==(const default_allocator&, const default_allocator&) BHO_NOEXCEPT { return true; } template BHO_CONSTEXPR inline bool operator!=(const default_allocator&, const default_allocator&) BHO_NOEXCEPT { return false; } } /* default_ */ using default_::default_allocator; } /* boost */ #endif