read_size replaces read_size_helper:

* detail::read_size_helper is removed

* public function read_size helps determine the optimum
  size for calls to a DynamicBuffer's prepare function.

* special case for Asio types which don't conform to
  the concept but have their own helper function.
This commit is contained in:
Vinnie Falco
2017-07-20 08:12:20 -07:00
parent fe4dfed27b
commit a49c096042
18 changed files with 212 additions and 147 deletions
+3 -3
View File
@@ -9,7 +9,7 @@
#define BEAST_DETAIL_OSTREAM_HPP
#include <boost/asio/buffer.hpp>
#include <beast/core/detail/read_size_helper.hpp>
#include <beast/core/read_size.hpp>
#include <memory>
#include <iosfwd>
#include <streambuf>
@@ -129,7 +129,7 @@ private:
using boost::asio::buffer_cast;
using boost::asio::buffer_size;
auto mbs = buf_.prepare(
maybe_read_size_helper(buf_, max_size));
read_size_or_throw(buf_, max_size));
auto const mb = *mbs.begin();
auto const p = buffer_cast<CharT*>(mb);
this->setp(p,
@@ -209,7 +209,7 @@ private:
using boost::asio::buffer_cast;
using boost::asio::buffer_size;
auto mbs = buf_.prepare(
maybe_read_size_helper(buf_, max_size));
read_size_or_throw(buf_, max_size));
auto const mb = *mbs.begin();
auto const p = buffer_cast<CharT*>(mb);
this->setp(p,
@@ -1,72 +0,0 @@
//
// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BEAST_DETAIL_READ_SIZE_HELPER_HPP
#define BEAST_DETAIL_READ_SIZE_HELPER_HPP
#include <beast/core/type_traits.hpp>
#include <boost/assert.hpp>
#include <boost/throw_exception.hpp>
#include <algorithm>
#include <cstddef>
namespace beast {
namespace detail {
/** Returns a natural read size.
This function inspects the capacity, size, and maximum
size of the dynamic buffer. Then it computes a natural
read size given the passed-in upper limit. It favors
a read size that does not require a reallocation, subject
to a reasonable minimum to avoid tiny reads.
Calls to @ref read_size_helper should be made without
namespace qualification, i.e. allowing argument dependent
lookup to take effect, so that overloads of this function
for specific buffer types may be found.
@param buffer The dynamic buffer to inspect.
@param max_size An upper limit on the returned value.
*/
template<class DynamicBuffer>
std::size_t
read_size_helper(
DynamicBuffer const& buffer, std::size_t max_size)
{
static_assert(beast::is_dynamic_buffer<DynamicBuffer>::value,
"DynamicBuffer requirements not met");
BOOST_ASSERT(max_size >= 1);
auto const size = buffer.size();
auto const limit = buffer.max_size() - size;
BOOST_ASSERT(size <= buffer.max_size());
return std::min<std::size_t>(
std::max<std::size_t>(512, buffer.capacity() - size),
std::min<std::size_t>(max_size, limit));
}
/** Return a non-zero natural read size.
*/
template<class DynamicBuffer>
std::size_t
maybe_read_size_helper(
DynamicBuffer const& buffer, std::size_t max_size)
{
static_assert(beast::is_dynamic_buffer<DynamicBuffer>::value,
"DynamicBuffer requirements not met");
auto const n = read_size_helper(buffer, max_size);
if(n == 0)
BOOST_THROW_EXCEPTION(std::length_error{
"buffer overflow"});
return n;
}
} // detail
} // beast
#endif
+75
View File
@@ -0,0 +1,75 @@
//
// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BEAST_IMPL_READ_SIZE_IPP
#define BEAST_IMPL_READ_SIZE_IPP
namespace beast {
namespace detail {
template<class T, class = void>
struct has_read_size_helper : std::false_type {};
template<class T>
struct has_read_size_helper<T, decltype(
read_size_helper(std::declval<T&>(), 512),
(void)0)> : std::true_type
{
};
template<class DynamicBuffer>
std::size_t
read_size(DynamicBuffer& buffer,
std::size_t max_size, std::true_type)
{
return read_size_helper(buffer, max_size);
}
template<class DynamicBuffer>
std::size_t
read_size(DynamicBuffer& buffer,
std::size_t max_size, std::false_type)
{
static_assert(is_dynamic_buffer<DynamicBuffer>::value,
"DynamicBuffer requirements not met");
BOOST_ASSERT(max_size >= 1);
auto const size = buffer.size();
auto const limit = buffer.max_size() - size;
BOOST_ASSERT(size <= buffer.max_size());
return std::min<std::size_t>(
std::max<std::size_t>(512, buffer.capacity() - size),
std::min<std::size_t>(max_size, limit));
}
} // detail
template<class DynamicBuffer>
inline
std::size_t
read_size(
DynamicBuffer& buffer, std::size_t max_size)
{
return detail::read_size(buffer, max_size,
detail::has_read_size_helper<DynamicBuffer>{});
}
template<class DynamicBuffer>
std::size_t
read_size_or_throw(
DynamicBuffer& buffer, std::size_t max_size)
{
auto const n = read_size(buffer, max_size);
if(n == 0)
BOOST_THROW_EXCEPTION(std::length_error{
"buffer overflow"});
return n;
}
} // beast
#endif
+59
View File
@@ -0,0 +1,59 @@
//
// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BEAST_READ_SIZE_HELPER_HPP
#define BEAST_READ_SIZE_HELPER_HPP
#include <beast/core/type_traits.hpp>
#include <boost/throw_exception.hpp>
namespace beast {
/** Returns a natural read size.
This function inspects the capacity, size, and maximum
size of the dynamic buffer. Then it computes a natural
read size given the passed-in upper limit. It favors
a read size that does not require a reallocation, subject
to a reasonable minimum to avoid tiny reads.
@param buffer The dynamic buffer to inspect.
@param max_size An upper limit on the returned value.
@note If the buffer is already at its maximum size, zero
is returned.
*/
template<class DynamicBuffer>
std::size_t
read_size(DynamicBuffer& buffer, std::size_t max_size);
/** Returns a natural read size or throw if the buffer is full.
This function inspects the capacity, size, and maximum
size of the dynamic buffer. Then it computes a natural
read size given the passed-in upper limit. It favors
a read size that does not require a reallocation, subject
to a reasonable minimum to avoid tiny reads.
@param buffer The dynamic buffer to inspect.
@param max_size An upper limit on the returned value.
@throws std::length_error if `max_size > 0` and the buffer
is full.
*/
template<class DynamicBuffer>
std::size_t
read_size_or_throw(DynamicBuffer& buffer,
std::size_t max_size);
} // beast
#include <beast/core/impl/read_size.ipp>
#endif
+3 -4
View File
@@ -14,8 +14,8 @@
#include <beast/http/read.hpp>
#include <beast/core/bind_handler.hpp>
#include <beast/core/handler_ptr.hpp>
#include <beast/core/read_size.hpp>
#include <beast/core/type_traits.hpp>
#include <beast/core/detail/read_size_helper.hpp>
#include <boost/asio/handler_alloc_hook.hpp>
#include <boost/asio/handler_continuation_hook.hpp>
#include <boost/asio/handler_invoke_hook.hpp>
@@ -145,7 +145,7 @@ operator()(error_code ec, std::size_t bytes_transferred)
try
{
mb_.emplace(b_.prepare(
maybe_read_size_helper(b_, 65536)));
read_size_or_throw(b_, 65536)));
}
catch(std::length_error const&)
{
@@ -474,8 +474,7 @@ read_some(
try
{
b.emplace(buffer.prepare(
beast::detail::maybe_read_size_helper(
buffer, 65536)));
read_size_or_throw(buffer, 65536)));
}
catch(std::length_error const&)
{
@@ -10,8 +10,8 @@
#include <beast/core/error.hpp>
#include <beast/core/consuming_buffers.hpp>
#include <beast/core/read_size.hpp>
#include <beast/core/detail/ci_char_traits.hpp>
#include <beast/core/detail/read_size_helper.hpp>
#include <beast/zlib/deflate_stream.hpp>
#include <beast/zlib/inflate_stream.hpp>
#include <beast/websocket/option.hpp>
@@ -370,7 +370,7 @@ inflate(
{
// VFALCO we could be smarter about the size
auto const bs = buffer.prepare(
maybe_read_size_helper(buffer, 65536));
read_size_or_throw(buffer, 65536));
auto const out = *bs.begin();
zs.avail_out = buffer_size(out);
zs.next_out = buffer_cast<void*>(out);