mirror of
https://github.com/boostorg/container.git
synced 2026-01-19 04:02:17 +00:00
Define operator_new_allocate/operator_delete_deallocate helpers, taking care of __cpp_aligned_new and __cpp_sized_deallocation, and use them in new_allocator
This commit is contained in:
67
include/boost/container/detail/operator_new_helpers.hpp
Normal file
67
include/boost/container/detail/operator_new_helpers.hpp
Normal file
@@ -0,0 +1,67 @@
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// (C) Copyright Ion Gaztanaga 2025-2025. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
// See http://www.boost.org/libs/container for documentation.
|
||||
//
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
#ifndef BOOST_CONTAINER_DETAIL_OPERATOR_NEW_HELPERS_HPP
|
||||
#define BOOST_CONTAINER_DETAIL_OPERATOR_NEW_HELPERS_HPP
|
||||
|
||||
#ifndef BOOST_CONFIG_HPP
|
||||
# include <boost/config.hpp>
|
||||
#endif
|
||||
|
||||
#if defined(BOOST_HAS_PRAGMA_ONCE)
|
||||
# pragma once
|
||||
#endif
|
||||
|
||||
#include <boost/container/detail/std_fwd.hpp>
|
||||
|
||||
namespace boost {
|
||||
namespace container {
|
||||
namespace dtl {
|
||||
|
||||
template <class T>
|
||||
T* operator_new_allocate(std::size_t count)
|
||||
{
|
||||
const std::size_t max_count = std::size_t(-1)/(2*sizeof(T));
|
||||
if(BOOST_UNLIKELY(count > max_count))
|
||||
throw_bad_alloc();
|
||||
#if defined(__cpp_aligned_new)
|
||||
BOOST_IF_CONSTEXPR(__STDCPP_DEFAULT_NEW_ALIGNMENT__ < alignof(T)) {
|
||||
return static_cast<T*>(::operator new(count*sizeof(T), std::align_val_t(alignof(T))));
|
||||
}
|
||||
#endif
|
||||
return static_cast<T*>(::operator new(count*sizeof(T)));
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void operator_delete_deallocate(T* ptr, std::size_t n) BOOST_NOEXCEPT_OR_NOTHROW
|
||||
{
|
||||
(void)n;
|
||||
#ifdef __cpp_aligned_new
|
||||
BOOST_IF_CONSTEXPR(__STDCPP_DEFAULT_NEW_ALIGNMENT__ < alignof(T)) {
|
||||
# if defined(__cpp_sized_deallocation)
|
||||
::operator delete((void*)ptr, n * sizeof(T), std::align_val_t(alignof(T)));
|
||||
#else
|
||||
::operator delete((void*)ptr, std::align_val_t(alignof(T)));
|
||||
# endif
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
# if defined(__cpp_sized_deallocation)
|
||||
::operator delete((void*)ptr, n * sizeof(T));
|
||||
#else
|
||||
::operator delete((void*)ptr);
|
||||
# endif
|
||||
}
|
||||
|
||||
} //namespace dtl {
|
||||
} //namespace container {
|
||||
} //namespace boost {
|
||||
|
||||
#endif //#ifndef BOOST_CONTAINER_DETAIL_OPERATOR_NEW_HELPERS_HPP
|
||||
@@ -22,6 +22,7 @@
|
||||
#include <boost/container/detail/config_begin.hpp>
|
||||
#include <boost/container/detail/workaround.hpp>
|
||||
#include <boost/container/throw_exception.hpp>
|
||||
#include <boost/container/detail/operator_new_helpers.hpp>
|
||||
#include <cstddef>
|
||||
|
||||
//!\file
|
||||
@@ -152,22 +153,14 @@ class new_allocator
|
||||
//!Throws bad_alloc if there is no enough memory
|
||||
pointer allocate(size_type count)
|
||||
{
|
||||
const std::size_t max_count = std::size_t(-1)/(2*sizeof(T));
|
||||
if(BOOST_UNLIKELY(count > max_count))
|
||||
throw_bad_alloc();
|
||||
return static_cast<T*>(::operator new(count*sizeof(T)));
|
||||
return dtl::operator_new_allocate<T>(count);
|
||||
}
|
||||
|
||||
//!Deallocates previously allocated memory.
|
||||
//!Never throws
|
||||
void deallocate(pointer ptr, size_type n) BOOST_NOEXCEPT_OR_NOTHROW
|
||||
{
|
||||
(void)n;
|
||||
# if defined(__cpp_sized_deallocation)
|
||||
::operator delete((void*)ptr, n * sizeof(T));
|
||||
#else
|
||||
::operator delete((void*)ptr);
|
||||
# endif
|
||||
return dtl::operator_delete_deallocate<T>(ptr, n);
|
||||
}
|
||||
|
||||
//!Returns the maximum number of elements that could be allocated.
|
||||
|
||||
Reference in New Issue
Block a user