From ffe2e8e02b88646f738cb8a8a5220eddd2ae5eeb Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Thu, 5 Jan 2017 09:09:59 -0500 Subject: [PATCH] Avoid copies in handler_alloc --- CHANGELOG.md | 1 + include/beast/core/handler_alloc.hpp | 32 +++++++--------------------- 2 files changed, 9 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e7238741..8db2521d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ * Fix broken Intellisense * Implement the Asio deallocation-before-invocation guarantee * Add handler helpers +* Avoid copies in handler_alloc -------------------------------------------------------------------------------- diff --git a/include/beast/core/handler_alloc.hpp b/include/beast/core/handler_alloc.hpp index f6692c0b..814fee31 100644 --- a/include/beast/core/handler_alloc.hpp +++ b/include/beast/core/handler_alloc.hpp @@ -28,11 +28,10 @@ namespace beast { @tparam T The type of objects allocated by the allocator. - @tparam CompletionHandler The type of handler. Copies will be made - of the handler as needed. + @tparam CompletionHandler The type of handler. - @note Allocated memory is only valid until the handler is called. The - caller is still responsible for freeing memory. + @note Memory allocated by this allocator must be freed before + the handler is invoked or undefined behavior results. */ #if GENERATING_DOCS template @@ -50,7 +49,7 @@ private: template friend class handler_alloc; - CompletionHandler h_; + CompletionHandler& h_; public: using value_type = T; @@ -70,31 +69,16 @@ public: /** Construct the allocator. - The handler is moved or copied into the allocator. + A reference of the handler is stored. The handler must + remain valid for at least the lifetime of the allocator. */ explicit - handler_alloc(CompletionHandler&& h) - : h_(std::move(h)) - { - } - - /** Construct the allocator. - - A copy of the handler is made. - */ - explicit - handler_alloc(CompletionHandler const& h) + handler_alloc(CompletionHandler& h) : h_(h) { } - template - handler_alloc( - handler_alloc&& other) - : h_(std::move(other.h_)) - { - } - + /// Copy constructor template handler_alloc( handler_alloc const& other)