recycling_allocator.hpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //
  2. // detail/recycling_allocator.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2018 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>
  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> other;
  32. };
  33. recycling_allocator()
  34. {
  35. }
  36. template <typename U>
  37. recycling_allocator(const recycling_allocator<U>&)
  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(call_stack::top(), sizeof(T) * n);
  44. return static_cast<T*>(p);
  45. }
  46. void deallocate(T* p, std::size_t n)
  47. {
  48. typedef thread_context::thread_call_stack call_stack;
  49. thread_info_base::deallocate(call_stack::top(), p, sizeof(T) * n);
  50. }
  51. };
  52. template <>
  53. class recycling_allocator<void>
  54. {
  55. public:
  56. typedef void value_type;
  57. template <typename U>
  58. struct rebind
  59. {
  60. typedef recycling_allocator<U> other;
  61. };
  62. recycling_allocator()
  63. {
  64. }
  65. template <typename U>
  66. recycling_allocator(const recycling_allocator<U>&)
  67. {
  68. }
  69. };
  70. template <typename Allocator>
  71. struct get_recycling_allocator
  72. {
  73. typedef Allocator type;
  74. static type get(const Allocator& a) { return a; }
  75. };
  76. template <typename T>
  77. struct get_recycling_allocator<std::allocator<T> >
  78. {
  79. typedef recycling_allocator<T> type;
  80. static type get(const std::allocator<T>&) { return type(); }
  81. };
  82. } // namespace detail
  83. } // namespace asio
  84. } // namespace boost
  85. #include <boost/asio/detail/pop_options.hpp>
  86. #endif // BOOST_ASIO_DETAIL_RECYCLING_ALLOCATOR_HPP