recycling_allocator.hpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //
  2. // detail/recycling_allocator.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 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_RECYCLING_ALLOCATOR_HPP
  11. #define BOOST_ASIO_DETAIL_RECYCLING_ALLOCATOR_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 <boost/asio/detail/memory.hpp>
  17. #include <boost/asio/detail/thread_context.hpp>
  18. #include <boost/asio/detail/thread_info_base.hpp>
  19. #include <boost/asio/detail/push_options.hpp>
  20. namespace boost {
  21. namespace asio {
  22. namespace detail {
  23. template <typename T, typename Purpose = thread_info_base::default_tag>
  24. class recycling_allocator
  25. {
  26. public:
  27. typedef T value_type;
  28. template <typename U>
  29. struct rebind
  30. {
  31. typedef recycling_allocator<U, Purpose> other;
  32. };
  33. recycling_allocator()
  34. {
  35. }
  36. template <typename U>
  37. recycling_allocator(const recycling_allocator<U, Purpose>&)
  38. {
  39. }
  40. T* allocate(std::size_t n)
  41. {
  42. typedef thread_context::thread_call_stack call_stack;
  43. void* p = thread_info_base::allocate(Purpose(),
  44. call_stack::top(), sizeof(T) * n);
  45. return static_cast<T*>(p);
  46. }
  47. void deallocate(T* p, std::size_t n)
  48. {
  49. typedef thread_context::thread_call_stack call_stack;
  50. thread_info_base::deallocate(Purpose(),
  51. call_stack::top(), p, sizeof(T) * n);
  52. }
  53. };
  54. template <typename Purpose>
  55. class recycling_allocator<void, Purpose>
  56. {
  57. public:
  58. typedef void value_type;
  59. template <typename U>
  60. struct rebind
  61. {
  62. typedef recycling_allocator<U, Purpose> other;
  63. };
  64. recycling_allocator()
  65. {
  66. }
  67. template <typename U>
  68. recycling_allocator(const recycling_allocator<U, Purpose>&)
  69. {
  70. }
  71. };
  72. template <typename Allocator, typename Purpose>
  73. struct get_recycling_allocator
  74. {
  75. typedef Allocator type;
  76. static type get(const Allocator& a) { return a; }
  77. };
  78. template <typename T, typename Purpose>
  79. struct get_recycling_allocator<std::allocator<T>, Purpose>
  80. {
  81. typedef recycling_allocator<T, Purpose> type;
  82. static type get(const std::allocator<T>&) { return type(); }
  83. };
  84. } // namespace detail
  85. } // namespace asio
  86. } // namespace boost
  87. #include <boost/asio/detail/pop_options.hpp>
  88. #endif // BOOST_ASIO_DETAIL_RECYCLING_ALLOCATOR_HPP