handler_alloc_hook.hpp 2.3 KB

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