memory.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //
  2. // detail/memory.hpp
  3. // ~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2022 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 ASIO_DETAIL_MEMORY_HPP
  11. #define 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 "asio/detail/config.hpp"
  16. #include <cstddef>
  17. #include <cstdlib>
  18. #include <memory>
  19. #include <new>
  20. #include "asio/detail/throw_exception.hpp"
  21. #if !defined(ASIO_HAS_STD_SHARED_PTR)
  22. # include <boost/make_shared.hpp>
  23. # include <boost/shared_ptr.hpp>
  24. # include <boost/weak_ptr.hpp>
  25. #endif // !defined(ASIO_HAS_STD_SHARED_PTR)
  26. #if !defined(ASIO_HAS_STD_ADDRESSOF)
  27. # include <boost/utility/addressof.hpp>
  28. #endif // !defined(ASIO_HAS_STD_ADDRESSOF)
  29. #if !defined(ASIO_HAS_STD_ALIGNED_ALLOC) \
  30. && defined(ASIO_HAS_BOOST_ALIGN) \
  31. && defined(ASIO_HAS_ALIGNOF)
  32. # include <boost/align/aligned_alloc.hpp>
  33. #endif // !defined(ASIO_HAS_STD_ALIGNED_ALLOC)
  34. // && defined(ASIO_HAS_BOOST_ALIGN)
  35. // && defined(ASIO_HAS_ALIGNOF)
  36. namespace asio {
  37. namespace detail {
  38. #if defined(ASIO_HAS_STD_SHARED_PTR)
  39. using std::make_shared;
  40. using std::shared_ptr;
  41. using std::weak_ptr;
  42. #else // defined(ASIO_HAS_STD_SHARED_PTR)
  43. using boost::make_shared;
  44. using boost::shared_ptr;
  45. using boost::weak_ptr;
  46. #endif // defined(ASIO_HAS_STD_SHARED_PTR)
  47. #if defined(ASIO_HAS_STD_ADDRESSOF)
  48. using std::addressof;
  49. #else // defined(ASIO_HAS_STD_ADDRESSOF)
  50. using boost::addressof;
  51. #endif // defined(ASIO_HAS_STD_ADDRESSOF)
  52. } // namespace detail
  53. #if defined(ASIO_HAS_CXX11_ALLOCATORS)
  54. using std::allocator_arg_t;
  55. # define ASIO_USES_ALLOCATOR(t) \
  56. namespace std { \
  57. template <typename Allocator> \
  58. struct uses_allocator<t, Allocator> : true_type {}; \
  59. } \
  60. /**/
  61. # define ASIO_REBIND_ALLOC(alloc, t) \
  62. typename std::allocator_traits<alloc>::template rebind_alloc<t>
  63. /**/
  64. #else // defined(ASIO_HAS_CXX11_ALLOCATORS)
  65. struct allocator_arg_t {};
  66. # define ASIO_USES_ALLOCATOR(t)
  67. # define ASIO_REBIND_ALLOC(alloc, t) \
  68. typename alloc::template rebind<t>::other
  69. /**/
  70. #endif // defined(ASIO_HAS_CXX11_ALLOCATORS)
  71. inline void* aligned_new(std::size_t align, std::size_t size)
  72. {
  73. #if defined(ASIO_HAS_STD_ALIGNED_ALLOC) && defined(ASIO_HAS_ALIGNOF)
  74. align = (align < ASIO_DEFAULT_ALIGN) ? ASIO_DEFAULT_ALIGN : align;
  75. size = (size % align == 0) ? size : size + (align - size % align);
  76. void* ptr = std::aligned_alloc(align, size);
  77. if (!ptr)
  78. {
  79. std::bad_alloc ex;
  80. asio::detail::throw_exception(ex);
  81. }
  82. return ptr;
  83. #elif defined(ASIO_HAS_BOOST_ALIGN) && defined(ASIO_HAS_ALIGNOF)
  84. align = (align < ASIO_DEFAULT_ALIGN) ? ASIO_DEFAULT_ALIGN : align;
  85. size = (size % align == 0) ? size : size + (align - size % align);
  86. void* ptr = boost::alignment::aligned_alloc(align, size);
  87. if (!ptr)
  88. {
  89. std::bad_alloc ex;
  90. asio::detail::throw_exception(ex);
  91. }
  92. return ptr;
  93. #elif defined(ASIO_MSVC) && defined(ASIO_HAS_ALIGNOF)
  94. align = (align < ASIO_DEFAULT_ALIGN) ? ASIO_DEFAULT_ALIGN : align;
  95. size = (size % align == 0) ? size : size + (align - size % align);
  96. void* ptr = _aligned_malloc(size, align);
  97. if (!ptr)
  98. {
  99. std::bad_alloc ex;
  100. asio::detail::throw_exception(ex);
  101. }
  102. return ptr;
  103. #else // defined(ASIO_MSVC) && defined(ASIO_HAS_ALIGNOF)
  104. (void)align;
  105. return ::operator new(size);
  106. #endif // defined(ASIO_MSVC) && defined(ASIO_HAS_ALIGNOF)
  107. }
  108. inline void aligned_delete(void* ptr)
  109. {
  110. #if defined(ASIO_HAS_STD_ALIGNED_ALLOC) && defined(ASIO_HAS_ALIGNOF)
  111. std::free(ptr);
  112. #elif defined(ASIO_HAS_BOOST_ALIGN) && defined(ASIO_HAS_ALIGNOF)
  113. boost::alignment::aligned_free(ptr);
  114. #elif defined(ASIO_MSVC) && defined(ASIO_HAS_ALIGNOF)
  115. _aligned_free(ptr);
  116. #else // defined(ASIO_MSVC) && defined(ASIO_HAS_ALIGNOF)
  117. ::operator delete(ptr);
  118. #endif // defined(ASIO_MSVC) && defined(ASIO_HAS_ALIGNOF)
  119. }
  120. } // namespace asio
  121. #endif // ASIO_DETAIL_MEMORY_HPP