handler_alloc_hook.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // handler_alloc_hook.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2022 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. #if defined(ASIO_NO_DEPRECATED)
  20. // Places in asio that would have previously called the allocate or deallocate
  21. // hooks to manage memory, now call them only to check whether the result types
  22. // are these types. If the result is not the correct type, it indicates that
  23. // the user code still has the old hooks in place, and if so we want to trigger
  24. // a compile error.
  25. enum asio_handler_allocate_is_no_longer_used {};
  26. enum asio_handler_deallocate_is_no_longer_used {};
  27. typedef asio_handler_allocate_is_no_longer_used
  28. asio_handler_allocate_is_deprecated;
  29. typedef asio_handler_deallocate_is_no_longer_used
  30. asio_handler_deallocate_is_deprecated;
  31. #else // defined(ASIO_NO_DEPRECATED)
  32. typedef void* asio_handler_allocate_is_deprecated;
  33. typedef void asio_handler_deallocate_is_deprecated;
  34. #endif // defined(ASIO_NO_DEPRECATED)
  35. /// (Deprecated: Use the associated_allocator trait.) Default allocation
  36. /// function for handlers.
  37. /**
  38. * Asynchronous operations may need to allocate temporary objects. Since
  39. * asynchronous operations have a handler function object, these temporary
  40. * objects can be said to be associated with the handler.
  41. *
  42. * Implement asio_handler_allocate and asio_handler_deallocate for your own
  43. * handlers to provide custom allocation for these temporary objects.
  44. *
  45. * The default implementation of these allocation hooks uses <tt>::operator
  46. * new</tt> and <tt>::operator delete</tt>.
  47. *
  48. * @note All temporary objects associated with a handler will be deallocated
  49. * before the upcall to the handler is performed. This allows the same memory to
  50. * be reused for a subsequent asynchronous operation initiated by the handler.
  51. *
  52. * @par Example
  53. * @code
  54. * class my_handler;
  55. *
  56. * void* asio_handler_allocate(std::size_t size, my_handler* context)
  57. * {
  58. * return ::operator new(size);
  59. * }
  60. *
  61. * void asio_handler_deallocate(void* pointer, std::size_t size,
  62. * my_handler* context)
  63. * {
  64. * ::operator delete(pointer);
  65. * }
  66. * @endcode
  67. */
  68. ASIO_DECL asio_handler_allocate_is_deprecated
  69. asio_handler_allocate(std::size_t size, ...);
  70. /// Default deallocation function for handlers.
  71. /**
  72. * Implement asio_handler_allocate and asio_handler_deallocate for your own
  73. * handlers to provide custom allocation for the associated temporary objects.
  74. *
  75. * The default implementation of these allocation hooks uses <tt>::operator
  76. * new</tt> and <tt>::operator delete</tt>.
  77. *
  78. * @sa asio_handler_allocate.
  79. */
  80. ASIO_DECL asio_handler_deallocate_is_deprecated
  81. asio_handler_deallocate(void* pointer, std::size_t size, ...);
  82. } // namespace asio
  83. #include "asio/detail/pop_options.hpp"
  84. #if defined(ASIO_HEADER_ONLY)
  85. # include "asio/impl/handler_alloc_hook.ipp"
  86. #endif // defined(ASIO_HEADER_ONLY)
  87. #endif // ASIO_HANDLER_ALLOC_HOOK_HPP