memory.hpp 3.3 KB

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