operator_new_helpers.hpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. #ifndef BOOST_CONFIG_HPP
  13. # include <boost/config.hpp>
  14. #endif
  15. #if defined(BOOST_HAS_PRAGMA_ONCE)
  16. # pragma once
  17. #endif
  18. #include <boost/container/detail/std_fwd.hpp>
  19. #include <boost/container/throw_exception.hpp>
  20. namespace boost {
  21. namespace container {
  22. namespace dtl {
  23. template <class T>
  24. T* operator_new_allocate(std::size_t count)
  25. {
  26. const std::size_t max_count = std::size_t(-1)/(2*sizeof(T));
  27. if(BOOST_UNLIKELY(count > max_count))
  28. throw_bad_alloc();
  29. #if defined(__cpp_aligned_new)
  30. BOOST_IF_CONSTEXPR(__STDCPP_DEFAULT_NEW_ALIGNMENT__ < alignof(T)) {
  31. return static_cast<T*>(::operator new(count*sizeof(T), std::align_val_t(alignof(T))));
  32. }
  33. #endif
  34. return static_cast<T*>(::operator new(count*sizeof(T)));
  35. }
  36. template <class T>
  37. void operator_delete_deallocate(T* ptr, std::size_t n) BOOST_NOEXCEPT_OR_NOTHROW
  38. {
  39. (void)n;
  40. #ifdef __cpp_aligned_new
  41. BOOST_IF_CONSTEXPR(__STDCPP_DEFAULT_NEW_ALIGNMENT__ < alignof(T)) {
  42. # if defined(__cpp_sized_deallocation)
  43. ::operator delete((void*)ptr, n * sizeof(T), std::align_val_t(alignof(T)));
  44. #else
  45. ::operator delete((void*)ptr, std::align_val_t(alignof(T)));
  46. # endif
  47. return;
  48. }
  49. #endif
  50. # if defined(__cpp_sized_deallocation)
  51. ::operator delete((void*)ptr, n * sizeof(T));
  52. #else
  53. ::operator delete((void*)ptr);
  54. # endif
  55. }
  56. } //namespace dtl {
  57. } //namespace container {
  58. } //namespace boost {
  59. #endif //#ifndef BOOST_CONTAINER_DETAIL_OPERATOR_NEW_HELPERS_HPP