Chris@16: // Chris@16: // detail/handler_alloc_helpers.hpp Chris@16: // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Chris@16: // Chris@101: // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com) Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. (See accompanying Chris@16: // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: Chris@16: #ifndef BOOST_ASIO_DETAIL_HANDLER_ALLOC_HELPERS_HPP Chris@16: #define BOOST_ASIO_DETAIL_HANDLER_ALLOC_HELPERS_HPP Chris@16: Chris@16: #if defined(_MSC_VER) && (_MSC_VER >= 1200) Chris@16: # pragma once Chris@16: #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: Chris@16: // Calls to asio_handler_allocate and asio_handler_deallocate must be made from Chris@16: // a namespace that does not contain any overloads of these functions. The Chris@16: // boost_asio_handler_alloc_helpers namespace is defined here for that purpose. Chris@16: namespace boost_asio_handler_alloc_helpers { Chris@16: Chris@16: template Chris@16: inline void* allocate(std::size_t s, Handler& h) Chris@16: { Chris@16: #if !defined(BOOST_ASIO_HAS_HANDLER_HOOKS) Chris@16: return ::operator new(s); Chris@16: #else Chris@16: using boost::asio::asio_handler_allocate; Chris@16: return asio_handler_allocate(s, boost::asio::detail::addressof(h)); Chris@16: #endif Chris@16: } Chris@16: Chris@16: template Chris@16: inline void deallocate(void* p, std::size_t s, Handler& h) Chris@16: { Chris@16: #if !defined(BOOST_ASIO_HAS_HANDLER_HOOKS) Chris@16: ::operator delete(p); Chris@16: #else Chris@16: using boost::asio::asio_handler_deallocate; Chris@16: asio_handler_deallocate(p, s, boost::asio::detail::addressof(h)); Chris@16: #endif Chris@16: } Chris@16: Chris@16: } // namespace boost_asio_handler_alloc_helpers Chris@16: Chris@16: #define BOOST_ASIO_DEFINE_HANDLER_PTR(op) \ Chris@16: struct ptr \ Chris@16: { \ Chris@16: Handler* h; \ Chris@16: void* v; \ Chris@16: op* p; \ Chris@16: ~ptr() \ Chris@16: { \ Chris@16: reset(); \ Chris@16: } \ Chris@16: void reset() \ Chris@16: { \ Chris@16: if (p) \ Chris@16: { \ Chris@16: p->~op(); \ Chris@16: p = 0; \ Chris@16: } \ Chris@16: if (v) \ Chris@16: { \ Chris@16: boost_asio_handler_alloc_helpers::deallocate(v, sizeof(op), *h); \ Chris@16: v = 0; \ Chris@16: } \ Chris@16: } \ Chris@16: } \ Chris@16: /**/ Chris@16: Chris@16: #include Chris@16: Chris@16: #endif // BOOST_ASIO_DETAIL_HANDLER_ALLOC_HELPERS_HPP