File indexing completed on 2025-01-18 09:30:29
0001
0002
0003
0004
0005
0006
0007
0008 #ifndef BOOST_CORE_DEFAULT_ALLOCATOR_HPP
0009 #define BOOST_CORE_DEFAULT_ALLOCATOR_HPP
0010
0011 #include <boost/config.hpp>
0012 #include <new>
0013
0014 namespace boost {
0015
0016 #if defined(BOOST_NO_EXCEPTIONS)
0017 BOOST_NORETURN void throw_exception(const std::exception&);
0018 #endif
0019
0020 namespace default_ {
0021
0022 template<bool V>
0023 struct bool_constant {
0024 typedef bool value_type;
0025 typedef bool_constant type;
0026
0027 static const bool value = V;
0028
0029 operator bool() const BOOST_NOEXCEPT {
0030 return V;
0031 }
0032
0033 bool operator()() const BOOST_NOEXCEPT {
0034 return V;
0035 }
0036 };
0037
0038 template<bool V>
0039 const bool bool_constant<V>::value;
0040
0041 template<class T>
0042 struct add_reference {
0043 typedef T& type;
0044 };
0045
0046 template<>
0047 struct add_reference<void> {
0048 typedef void type;
0049 };
0050
0051 template<>
0052 struct add_reference<const void> {
0053 typedef const void type;
0054 };
0055
0056 template<class T>
0057 struct default_allocator {
0058 typedef T value_type;
0059 typedef T* pointer;
0060 typedef const T* const_pointer;
0061 typedef typename add_reference<T>::type reference;
0062 typedef typename add_reference<const T>::type const_reference;
0063 typedef std::size_t size_type;
0064 typedef std::ptrdiff_t difference_type;
0065 typedef bool_constant<true> propagate_on_container_move_assignment;
0066 typedef bool_constant<true> is_always_equal;
0067
0068 template<class U>
0069 struct rebind {
0070 typedef default_allocator<U> other;
0071 };
0072
0073 #if !defined(BOOST_NO_CXX11_DEFAULTED_FUNCTIONS)
0074 default_allocator() = default;
0075 #else
0076 BOOST_CONSTEXPR default_allocator() BOOST_NOEXCEPT { }
0077 #endif
0078
0079 template<class U>
0080 BOOST_CONSTEXPR default_allocator(const default_allocator<U>&)
0081 BOOST_NOEXCEPT { }
0082
0083 BOOST_CONSTEXPR std::size_t max_size() const BOOST_NOEXCEPT {
0084 return static_cast<std::size_t>(-1) / (2 < sizeof(T) ? sizeof(T) : 2);
0085 }
0086
0087 #if !defined(BOOST_NO_EXCEPTIONS)
0088 T* allocate(std::size_t n) {
0089 if (n > max_size()) {
0090 throw std::bad_alloc();
0091 }
0092 return static_cast<T*>(::operator new(sizeof(T) * n));
0093 }
0094
0095 void deallocate(T* p, std::size_t) {
0096 ::operator delete(p);
0097 }
0098 #else
0099 T* allocate(std::size_t n) {
0100 if (n > max_size()) {
0101 boost::throw_exception(std::bad_alloc());
0102 }
0103 void* p = ::operator new(sizeof(T) * n, std::nothrow);
0104 if (!p) {
0105 boost::throw_exception(std::bad_alloc());
0106 }
0107 return static_cast<T*>(p);
0108 }
0109
0110 void deallocate(T* p, std::size_t) {
0111 ::operator delete(p, std::nothrow);
0112 }
0113 #endif
0114
0115 #if defined(BOOST_NO_CXX11_ALLOCATOR)
0116 T* allocate(std::size_t n, const void*) {
0117 return allocate(n);
0118 }
0119 #endif
0120
0121 #if (defined(BOOST_LIBSTDCXX_VERSION) && BOOST_LIBSTDCXX_VERSION < 60000) || \
0122 defined(BOOST_NO_CXX11_ALLOCATOR)
0123 template<class U, class V>
0124 void construct(U* p, const V& v) {
0125 ::new(p) U(v);
0126 }
0127
0128 template<class U>
0129 void destroy(U* p) {
0130 p->~U();
0131 (void)p;
0132 }
0133 #endif
0134 };
0135
0136 template<class T, class U>
0137 BOOST_CONSTEXPR inline bool
0138 operator==(const default_allocator<T>&,
0139 const default_allocator<U>&) BOOST_NOEXCEPT
0140 {
0141 return true;
0142 }
0143
0144 template<class T, class U>
0145 BOOST_CONSTEXPR inline bool
0146 operator!=(const default_allocator<T>&,
0147 const default_allocator<U>&) BOOST_NOEXCEPT
0148 {
0149 return false;
0150 }
0151
0152 }
0153
0154 using default_::default_allocator;
0155
0156 }
0157
0158 #endif