diff --git a/websocketpp/transport/asio/base.hpp b/websocketpp/transport/asio/base.hpp index de438972d5..28ac933c6b 100644 --- a/websocketpp/transport/asio/base.hpp +++ b/websocketpp/transport/asio/base.hpp @@ -34,6 +34,10 @@ #include +#include +#include +#include + #include namespace websocketpp { @@ -45,6 +49,110 @@ namespace transport { */ namespace asio { +// + +// Class to manage the memory to be used for handler-based custom allocation. +// It contains a single block of memory which may be returned for allocation +// requests. If the memory is in use when an allocation request is made, the +// allocator delegates allocation to the global heap. +class handler_allocator + : private boost::noncopyable +{ +public: + handler_allocator() + : in_use_(false) + { + } + + void* allocate(std::size_t size) + { + if (!in_use_ && size < storage_.size) + { + in_use_ = true; + return storage_.address(); + } + else + { + return ::operator new(size); + } + } + + void deallocate(void* pointer) + { + if (pointer == storage_.address()) + { + in_use_ = false; + } + else + { + ::operator delete(pointer); + } + } + +private: + // Storage space used for handler-based custom memory allocation. + boost::aligned_storage<1024> storage_; + + // Whether the handler-based custom allocation storage has been used. + bool in_use_; +}; + +// Wrapper class template for handler objects to allow handler memory +// allocation to be customised. Calls to operator() are forwarded to the +// encapsulated handler. +template +class custom_alloc_handler +{ +public: + custom_alloc_handler(handler_allocator& a, Handler h) + : allocator_(a), + handler_(h) + { + } + + template + void operator()(Arg1 arg1) + { + handler_(arg1); + } + + template + void operator()(Arg1 arg1, Arg2 arg2) + { + handler_(arg1, arg2); + } + + friend void* asio_handler_allocate(std::size_t size, + custom_alloc_handler* this_handler) + { + return this_handler->allocator_.allocate(size); + } + + friend void asio_handler_deallocate(void* pointer, std::size_t /*size*/, + custom_alloc_handler* this_handler) + { + this_handler->allocator_.deallocate(pointer); + } + +private: + handler_allocator& allocator_; + Handler handler_; +}; + +// Helper function to wrap a handler object to add custom allocation. +template +inline custom_alloc_handler make_custom_alloc_handler( + handler_allocator& a, Handler h) +{ + return custom_alloc_handler(a, h); +} + + + + + + + // Forward declaration of class endpoint so that it can be friended/referenced // before being included. template diff --git a/websocketpp/transport/asio/connection.hpp b/websocketpp/transport/asio/connection.hpp index 6a97d06d66..9ab0600467 100644 --- a/websocketpp/transport/asio/connection.hpp +++ b/websocketpp/transport/asio/connection.hpp @@ -761,7 +761,7 @@ protected: lib::placeholders::_1, lib::placeholders::_2 ))*/ - m_async_read_handler + make_custom_alloc_handler(m_handler_allocator,m_async_read_handler) ); } @@ -795,12 +795,13 @@ protected: boost::asio::async_write( socket_con_type::get_socket(), m_bufs, - m_strand->wrap(lib::bind( + /*m_strand->wrap(lib::bind( &type::handle_async_write, get_shared(), handler, lib::placeholders::_1 - )) + ))*/ + make_custom_alloc_handler(m_handler_allocator,m_async_write_handler) ); } @@ -816,12 +817,13 @@ protected: boost::asio::async_write( socket_con_type::get_socket(), m_bufs, - m_strand->wrap(lib::bind( + /*m_strand->wrap(lib::bind( &type::handle_async_write, get_shared(), handler, lib::placeholders::_1 - )) + ))*/ + make_custom_alloc_handler(m_handler_allocator,m_async_write_handler) ); } @@ -998,6 +1000,8 @@ private: // Handlers tcp_init_handler m_tcp_init_handler; + handler_allocator m_handler_allocator; + read_handler m_read_handler; write_handler m_write_handler; init_handler m_init_handler;