|
| 1 | +////////////////////////////////////////////////////////////////////////////// |
| 2 | +// |
| 3 | +// (C) Copyright Ion Gaztanaga 2025-2025. Distributed under the Boost |
| 4 | +// Software License, Version 1.0. (See accompanying file |
| 5 | +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | +// |
| 7 | +// See http://www.boost.org/libs/container for documentation. |
| 8 | +// |
| 9 | +////////////////////////////////////////////////////////////////////////////// |
| 10 | +#ifndef BOOST_CONTAINER_DETAIL_OPERATOR_NEW_HELPERS_HPP |
| 11 | +#define BOOST_CONTAINER_DETAIL_OPERATOR_NEW_HELPERS_HPP |
| 12 | + |
| 13 | +#ifndef BOOST_CONFIG_HPP |
| 14 | +# include <boost/config.hpp> |
| 15 | +#endif |
| 16 | + |
| 17 | +#if defined(BOOST_HAS_PRAGMA_ONCE) |
| 18 | +# pragma once |
| 19 | +#endif |
| 20 | + |
| 21 | +#include <boost/container/detail/std_fwd.hpp> |
| 22 | + |
| 23 | +namespace boost { |
| 24 | +namespace container { |
| 25 | +namespace dtl { |
| 26 | + |
| 27 | +template <class T> |
| 28 | +T* operator_new_allocate(std::size_t count) |
| 29 | +{ |
| 30 | + const std::size_t max_count = std::size_t(-1)/(2*sizeof(T)); |
| 31 | + if(BOOST_UNLIKELY(count > max_count)) |
| 32 | + throw_bad_alloc(); |
| 33 | + #if defined(__cpp_aligned_new) |
| 34 | + BOOST_IF_CONSTEXPR(__STDCPP_DEFAULT_NEW_ALIGNMENT__ < alignof(T)) { |
| 35 | + return static_cast<T*>(::operator new(count*sizeof(T), std::align_val_t(alignof(T)))); |
| 36 | + } |
| 37 | + #endif |
| 38 | + return static_cast<T*>(::operator new(count*sizeof(T))); |
| 39 | +} |
| 40 | + |
| 41 | +template <class T> |
| 42 | +void operator_delete_deallocate(T* ptr, std::size_t n) BOOST_NOEXCEPT_OR_NOTHROW |
| 43 | +{ |
| 44 | + (void)n; |
| 45 | + #ifdef __cpp_aligned_new |
| 46 | + BOOST_IF_CONSTEXPR(__STDCPP_DEFAULT_NEW_ALIGNMENT__ < alignof(T)) { |
| 47 | + # if defined(__cpp_sized_deallocation) |
| 48 | + ::operator delete((void*)ptr, n * sizeof(T), std::align_val_t(alignof(T))); |
| 49 | + #else |
| 50 | + ::operator delete((void*)ptr, std::align_val_t(alignof(T))); |
| 51 | + # endif |
| 52 | + return; |
| 53 | + } |
| 54 | + #endif |
| 55 | + |
| 56 | + # if defined(__cpp_sized_deallocation) |
| 57 | + ::operator delete((void*)ptr, n * sizeof(T)); |
| 58 | + #else |
| 59 | + ::operator delete((void*)ptr); |
| 60 | + # endif |
| 61 | +} |
| 62 | + |
| 63 | +} //namespace dtl { |
| 64 | +} //namespace container { |
| 65 | +} //namespace boost { |
| 66 | + |
| 67 | +#endif //#ifndef BOOST_CONTAINER_DETAIL_OPERATOR_NEW_HELPERS_HPP |
0 commit comments