handler_alloc_hook.hpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //
  2. // handler_alloc_hook.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2008 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_HANDLER_ALLOC_HOOK_HPP
  11. #define BOOST_ASIO_HANDLER_ALLOC_HOOK_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/push_options.hpp>
  16. #include <boost/asio/detail/push_options.hpp>
  17. #include <cstddef>
  18. #include <boost/config.hpp>
  19. #include <boost/asio/detail/pop_options.hpp>
  20. namespace boost {
  21. namespace asio {
  22. /// Default allocation function for handlers.
  23. /**
  24. * Asynchronous operations may need to allocate temporary objects. Since
  25. * asynchronous operations have a handler function object, these temporary
  26. * objects can be said to be associated with the handler.
  27. *
  28. * Implement asio_handler_allocate and asio_handler_deallocate for your own
  29. * handlers to provide custom allocation for these temporary objects.
  30. *
  31. * This default implementation is simply:
  32. * @code
  33. * return ::operator new(size);
  34. * @endcode
  35. *
  36. * @note All temporary objects associated with a handler will be deallocated
  37. * before the upcall to the handler is performed. This allows the same memory to
  38. * be reused for a subsequent asynchronous operation initiated by the handler.
  39. *
  40. * @par Example
  41. * @code
  42. * class my_handler;
  43. *
  44. * void* asio_handler_allocate(std::size_t size, my_handler* context)
  45. * {
  46. * return ::operator new(size);
  47. * }
  48. *
  49. * void asio_handler_deallocate(void* pointer, std::size_t size,
  50. * my_handler* context)
  51. * {
  52. * ::operator delete(pointer);
  53. * }
  54. * @endcode
  55. */
  56. inline void* asio_handler_allocate(std::size_t size, ...)
  57. {
  58. return ::operator new(size);
  59. }
  60. /// Default deallocation function for handlers.
  61. /**
  62. * Implement asio_handler_allocate and asio_handler_deallocate for your own
  63. * handlers to provide custom allocation for the associated temporary objects.
  64. *
  65. * This default implementation is simply:
  66. * @code
  67. * ::operator delete(pointer);
  68. * @endcode
  69. *
  70. * @sa asio_handler_allocate.
  71. */
  72. inline void asio_handler_deallocate(void* pointer, std::size_t size, ...)
  73. {
  74. (void)(size);
  75. ::operator delete(pointer);
  76. }
  77. } // namespace asio
  78. } // namespace boost
  79. #include <boost/asio/detail/pop_options.hpp>
  80. #endif // BOOST_ASIO_HANDLER_ALLOC_HOOK_HPP