Revise max_size implementation in default_allocator

This commit is contained in:
Glen Fernandes 2020-04-27 14:22:39 -04:00
parent bb0ef6d41e
commit 12ff209bf7
2 changed files with 3 additions and 15 deletions

View File

@ -9,7 +9,6 @@ Distributed under the Boost Software License, Version 1.0.
#define BOOST_CORE_DEFAULT_ALLOCATOR_HPP #define BOOST_CORE_DEFAULT_ALLOCATOR_HPP
#include <boost/config.hpp> #include <boost/config.hpp>
#include <limits>
#include <new> #include <new>
#if defined(BOOST_LIBSTDCXX_VERSION) && BOOST_LIBSTDCXX_VERSION < 60000 #if defined(BOOST_LIBSTDCXX_VERSION) && BOOST_LIBSTDCXX_VERSION < 60000
@ -82,10 +81,7 @@ struct default_allocator {
BOOST_NOEXCEPT { } BOOST_NOEXCEPT { }
BOOST_CONSTEXPR std::size_t max_size() const BOOST_NOEXCEPT { BOOST_CONSTEXPR std::size_t max_size() const BOOST_NOEXCEPT {
return std::numeric_limits<std::ptrdiff_t>::max() return static_cast<std::size_t>(-1) / (2 < sizeof(T) ? sizeof(T) : 2);
< std::numeric_limits<std::size_t>::max() / sizeof(T)
? std::numeric_limits<std::ptrdiff_t>::max()
: std::numeric_limits<std::size_t>::max() / sizeof(T);
} }
#if !defined(BOOST_NO_EXCEPTIONS) #if !defined(BOOST_NO_EXCEPTIONS)
@ -125,6 +121,7 @@ struct default_allocator {
template<class U> template<class U>
void destroy(U* p) { void destroy(U* p) {
p->~U(); p->~U();
(void)p;
} }
#endif #endif
}; };

View File

@ -173,20 +173,11 @@ void test_construct_other()
(void)a5; (void)a5;
} }
#if defined(PTRDIFF_MAX) && defined(SIZE_MAX)
template<class T> template<class T>
std::size_t max_size() std::size_t max_size()
{ {
return PTRDIFF_MAX < SIZE_MAX / sizeof(T) return static_cast<std::size_t>(-1) / (2 < sizeof(T) ? sizeof(T) : 2);
? PTRDIFF_MAX : SIZE_MAX / sizeof(T);
} }
#else
template<class T>
std::size_t max_size()
{
return ~static_cast<std::size_t>(0) / sizeof(T);
}
#endif
void test_max_size() void test_max_size()
{ {