memory.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //
  2. // detail/memory.hpp
  3. // ~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef BOOST_ASIO_DETAIL_MEMORY_HPP
  11. #define BOOST_ASIO_DETAIL_MEMORY_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #include <cstddef>
  17. #include <cstdlib>
  18. #include <memory>
  19. #include <new>
  20. #include <boost/asio/detail/cstdint.hpp>
  21. #include <boost/asio/detail/throw_exception.hpp>
  22. #if !defined(BOOST_ASIO_HAS_STD_ALIGNED_ALLOC) \
  23. && defined(BOOST_ASIO_HAS_BOOST_ALIGN)
  24. # include <boost/align/aligned_alloc.hpp>
  25. #endif // !defined(BOOST_ASIO_HAS_STD_ALIGNED_ALLOC)
  26. // && defined(BOOST_ASIO_HAS_BOOST_ALIGN)
  27. namespace boost {
  28. namespace asio {
  29. namespace detail {
  30. using std::allocate_shared;
  31. using std::make_shared;
  32. using std::shared_ptr;
  33. using std::weak_ptr;
  34. using std::addressof;
  35. #if defined(BOOST_ASIO_HAS_STD_TO_ADDRESS)
  36. using std::to_address;
  37. #else // defined(BOOST_ASIO_HAS_STD_TO_ADDRESS)
  38. template <typename T>
  39. inline T* to_address(T* p) { return p; }
  40. template <typename T>
  41. inline const T* to_address(const T* p) { return p; }
  42. template <typename T>
  43. inline volatile T* to_address(volatile T* p) { return p; }
  44. template <typename T>
  45. inline const volatile T* to_address(const volatile T* p) { return p; }
  46. #endif // defined(BOOST_ASIO_HAS_STD_TO_ADDRESS)
  47. inline void* align(std::size_t alignment,
  48. std::size_t size, void*& ptr, std::size_t& space)
  49. {
  50. return std::align(alignment, size, ptr, space);
  51. }
  52. } // namespace detail
  53. using std::allocator_arg_t;
  54. # define BOOST_ASIO_USES_ALLOCATOR(t) \
  55. namespace std { \
  56. template <typename Allocator> \
  57. struct uses_allocator<t, Allocator> : true_type {}; \
  58. } \
  59. /**/
  60. # define BOOST_ASIO_REBIND_ALLOC(alloc, t) \
  61. typename std::allocator_traits<alloc>::template rebind_alloc<t>
  62. /**/
  63. inline void* aligned_new(std::size_t align, std::size_t size)
  64. {
  65. #if defined(BOOST_ASIO_HAS_STD_ALIGNED_ALLOC)
  66. align = (align < BOOST_ASIO_DEFAULT_ALIGN) ? BOOST_ASIO_DEFAULT_ALIGN : align;
  67. size = (size % align == 0) ? size : size + (align - size % align);
  68. void* ptr = std::aligned_alloc(align, size);
  69. if (!ptr)
  70. {
  71. std::bad_alloc ex;
  72. boost::asio::detail::throw_exception(ex);
  73. }
  74. return ptr;
  75. #elif defined(BOOST_ASIO_HAS_BOOST_ALIGN)
  76. align = (align < BOOST_ASIO_DEFAULT_ALIGN) ? BOOST_ASIO_DEFAULT_ALIGN : align;
  77. size = (size % align == 0) ? size : size + (align - size % align);
  78. void* ptr = boost::alignment::aligned_alloc(align, size);
  79. if (!ptr)
  80. {
  81. std::bad_alloc ex;
  82. boost::asio::detail::throw_exception(ex);
  83. }
  84. return ptr;
  85. #elif defined(BOOST_ASIO_MSVC)
  86. align = (align < BOOST_ASIO_DEFAULT_ALIGN) ? BOOST_ASIO_DEFAULT_ALIGN : align;
  87. size = (size % align == 0) ? size : size + (align - size % align);
  88. void* ptr = _aligned_malloc(size, align);
  89. if (!ptr)
  90. {
  91. std::bad_alloc ex;
  92. boost::asio::detail::throw_exception(ex);
  93. }
  94. return ptr;
  95. #else // defined(BOOST_ASIO_MSVC)
  96. (void)align;
  97. return ::operator new(size);
  98. #endif // defined(BOOST_ASIO_MSVC)
  99. }
  100. inline void aligned_delete(void* ptr)
  101. {
  102. #if defined(BOOST_ASIO_HAS_STD_ALIGNED_ALLOC)
  103. std::free(ptr);
  104. #elif defined(BOOST_ASIO_HAS_BOOST_ALIGN)
  105. boost::alignment::aligned_free(ptr);
  106. #elif defined(BOOST_ASIO_MSVC)
  107. _aligned_free(ptr);
  108. #else // defined(BOOST_ASIO_MSVC)
  109. ::operator delete(ptr);
  110. #endif // defined(BOOST_ASIO_MSVC)
  111. }
  112. } // namespace asio
  113. } // namespace boost
  114. #endif // BOOST_ASIO_DETAIL_MEMORY_HPP