memory.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (C) 2011-2013 Vicente J. Botet Escriba
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // See http://www.boost.org/libs/thread for documentation.
  9. //
  10. //////////////////////////////////////////////////////////////////////////////
  11. #ifndef BOOST_THREAD_DETAIL_MEMORY_HPP
  12. #define BOOST_THREAD_DETAIL_MEMORY_HPP
  13. #include <boost/config.hpp>
  14. #include <boost/container/allocator_traits.hpp>
  15. #include <boost/container/scoped_allocator.hpp>
  16. #include <boost/type_traits/remove_cv.hpp>
  17. #include <boost/type_traits/is_convertible.hpp>
  18. #include <boost/type_traits/is_scalar.hpp>
  19. #include <boost/type_traits/is_pointer.hpp>
  20. #include <boost/type_traits/is_same.hpp>
  21. #include <boost/static_assert.hpp>
  22. namespace boost
  23. {
  24. namespace thread_detail
  25. {
  26. template <class _Alloc>
  27. class allocator_destructor
  28. {
  29. typedef container::allocator_traits<_Alloc> alloc_traits;
  30. public:
  31. typedef typename alloc_traits::pointer pointer;
  32. typedef typename alloc_traits::size_type size_type;
  33. private:
  34. _Alloc alloc_;
  35. size_type s_;
  36. public:
  37. allocator_destructor(_Alloc& a, size_type s)BOOST_NOEXCEPT
  38. : alloc_(a), s_(s)
  39. {}
  40. void operator()(pointer p)BOOST_NOEXCEPT
  41. {
  42. alloc_traits::destroy(alloc_, p);
  43. alloc_traits::deallocate(alloc_, p, s_);
  44. }
  45. };
  46. } //namespace thread_detail
  47. typedef container::allocator_arg_t allocator_arg_t;
  48. BOOST_CONSTEXPR_OR_CONST allocator_arg_t allocator_arg = {};
  49. template <class T, class Alloc>
  50. struct uses_allocator: public container::uses_allocator<T, Alloc>
  51. {
  52. };
  53. template <class Ptr>
  54. struct pointer_traits
  55. {
  56. typedef Ptr pointer;
  57. // typedef <details> element_type;
  58. // typedef <details> difference_type;
  59. // template <class U> using rebind = <details>;
  60. //
  61. // static pointer pointer_to(<details>);
  62. };
  63. template <class T>
  64. struct pointer_traits<T*>
  65. {
  66. typedef T* pointer;
  67. typedef T element_type;
  68. typedef ptrdiff_t difference_type;
  69. // template <class U> using rebind = U*;
  70. //
  71. // static pointer pointer_to(<details>) noexcept;
  72. };
  73. namespace thread_detail {
  74. template <class _Ptr1, class _Ptr2,
  75. bool = is_same<typename remove_cv<typename pointer_traits<_Ptr1>::element_type>::type,
  76. typename remove_cv<typename pointer_traits<_Ptr2>::element_type>::type
  77. >::value
  78. >
  79. struct same_or_less_cv_qualified_imp
  80. : is_convertible<_Ptr1, _Ptr2> {};
  81. template <class _Ptr1, class _Ptr2>
  82. struct same_or_less_cv_qualified_imp<_Ptr1, _Ptr2, false>
  83. : false_type {};
  84. template <class _Ptr1, class _Ptr2, bool = is_scalar<_Ptr1>::value &&
  85. !is_pointer<_Ptr1>::value>
  86. struct same_or_less_cv_qualified
  87. : same_or_less_cv_qualified_imp<_Ptr1, _Ptr2> {};
  88. template <class _Ptr1, class _Ptr2>
  89. struct same_or_less_cv_qualified<_Ptr1, _Ptr2, true>
  90. : false_type {};
  91. }
  92. template <class T>
  93. struct BOOST_SYMBOL_VISIBLE default_delete
  94. {
  95. #ifndef BOOST_NO_CXX11_DEFAULTED_FUNCTIONS
  96. BOOST_SYMBOL_VISIBLE
  97. BOOST_CONSTEXPR default_delete() = default;
  98. #else
  99. BOOST_SYMBOL_VISIBLE
  100. BOOST_CONSTEXPR default_delete() BOOST_NOEXCEPT {}
  101. #endif
  102. template <class U>
  103. BOOST_SYMBOL_VISIBLE
  104. default_delete(const default_delete<U>&,
  105. typename enable_if<is_convertible<U*, T*> >::type* = 0) BOOST_NOEXCEPT {}
  106. BOOST_SYMBOL_VISIBLE
  107. void operator() (T* ptr) const BOOST_NOEXCEPT
  108. {
  109. BOOST_STATIC_ASSERT_MSG(sizeof(T) > 0, "default_delete can not delete incomplete type");
  110. delete ptr;
  111. }
  112. };
  113. template <class T>
  114. struct BOOST_SYMBOL_VISIBLE default_delete<T[]>
  115. {
  116. public:
  117. #ifndef BOOST_NO_CXX11_DEFAULTED_FUNCTIONS
  118. BOOST_SYMBOL_VISIBLE
  119. BOOST_CONSTEXPR default_delete() = default;
  120. #else
  121. BOOST_SYMBOL_VISIBLE
  122. BOOST_CONSTEXPR default_delete() BOOST_NOEXCEPT {}
  123. #endif
  124. template <class U>
  125. BOOST_SYMBOL_VISIBLE
  126. default_delete(const default_delete<U[]>&,
  127. typename enable_if<thread_detail::same_or_less_cv_qualified<U*, T*> >::type* = 0) BOOST_NOEXCEPT {}
  128. template <class U>
  129. BOOST_SYMBOL_VISIBLE
  130. void operator() (U* ptr,
  131. typename enable_if<thread_detail::same_or_less_cv_qualified<U*, T*> >::type* = 0) const BOOST_NOEXCEPT
  132. {
  133. BOOST_STATIC_ASSERT_MSG(sizeof(T) > 0, "default_delete can not delete incomplete type");
  134. delete [] ptr;
  135. }
  136. };
  137. } // namespace boost
  138. #endif // BOOST_THREAD_DETAIL_MEMORY_HPP