2 Copyright 2019 Glen Joseph Fernandes
5 Distributed under the Boost Software License, Version 1.0.
6 (http://www.boost.org/LICENSE_1_0.txt)
8 #ifndef BOOST_CORE_DEFAULT_ALLOCATOR_HPP
9 #define BOOST_CORE_DEFAULT_ALLOCATOR_HPP
11 #include <boost/config.hpp>
15 #if defined(BOOST_LIBSTDCXX_VERSION) && BOOST_LIBSTDCXX_VERSION < 60000
16 #define BOOST_CORE_NO_CXX11_ALLOCATOR
21 #if defined(BOOST_NO_EXCEPTIONS)
22 BOOST_NORETURN void throw_exception(const std::exception&);
28 typedef bool value_type;
29 typedef true_type type;
31 BOOST_STATIC_CONSTANT(bool, value = true);
33 BOOST_CONSTEXPR operator bool() const BOOST_NOEXCEPT {
37 BOOST_CONSTEXPR bool operator()() const BOOST_NOEXCEPT {
43 struct add_reference {
48 struct add_reference<void> {
53 struct add_reference<const void> {
54 typedef const void type;
58 struct default_allocator {
61 typedef const T* const_pointer;
62 typedef typename add_reference<T>::type reference;
63 typedef typename add_reference<const T>::type const_reference;
64 typedef std::size_t size_type;
65 typedef std::ptrdiff_t difference_type;
66 typedef true_type propagate_on_container_move_assignment;
67 typedef true_type is_always_equal;
71 typedef default_allocator<U> other;
74 #if !defined(BOOST_NO_CXX11_DEFAULTED_FUNCTIONS)
75 default_allocator() = default;
77 BOOST_CONSTEXPR default_allocator() BOOST_NOEXCEPT { }
81 BOOST_CONSTEXPR default_allocator(const default_allocator<U>&)
84 #if defined(PTRDIFF_MAX) && defined(SIZE_MAX)
85 BOOST_CONSTEXPR std::size_t max_size() const BOOST_NOEXCEPT {
86 return PTRDIFF_MAX < SIZE_MAX / sizeof(T)
87 ? PTRDIFF_MAX : SIZE_MAX / sizeof(T);
90 BOOST_CONSTEXPR std::size_t max_size() const BOOST_NOEXCEPT {
91 return ~static_cast<std::size_t>(0) / sizeof(T);
95 #if !defined(BOOST_NO_EXCEPTIONS)
96 T* allocate(std::size_t n) {
98 throw std::bad_alloc();
100 return static_cast<T*>(::operator new(sizeof(T) * n));
103 void deallocate(T* p, std::size_t) {
104 ::operator delete(p);
107 T* allocate(std::size_t n) {
108 if (n > max_size()) {
109 boost::throw_exception(std::bad_alloc());
111 void* p = ::operator new(sizeof(T) * n, std::nothrow);
113 boost::throw_exception(std::bad_alloc());
115 return static_cast<T*>(p);
118 void deallocate(T* p, std::size_t) {
119 ::operator delete(p, std::nothrow);
123 #if defined(BOOST_NO_CXX11_ALLOCATOR) || defined(BOOST_CORE_NO_CXX11_ALLOCATOR)
124 template<class U, class V>
125 void construct(U* p, const V& v) {
136 template<class T, class U>
137 BOOST_CONSTEXPR inline bool
138 operator==(const default_allocator<T>&,
139 const default_allocator<U>&) BOOST_NOEXCEPT
144 template<class T, class U>
145 BOOST_CONSTEXPR inline bool
146 operator!=(const default_allocator<T>&,
147 const default_allocator<U>&) BOOST_NOEXCEPT
154 using default_::default_allocator;