Tidy up read_size_helper and dynamic buffers

fix #376
This commit is contained in:
Vinnie Falco committed 2017-07-20 08:12:17 -07:00
1 parent 3543b4b913
commit 929174d4d8
15 files changed
+105 -46

No files matched your search

+3
View File
@@ -9,6 +9,7 @@
#define BEAST_DETAIL_OSTREAM_HPP
#include <boost/asio/buffer.hpp>
#include <beast/core/detail/read_size_helper.hpp>
#include <memory>
#include <ostream>
#include <streambuf>
@@ -127,6 +128,7 @@ private:
{
using boost::asio::buffer_cast;
using boost::asio::buffer_size;
using beast::detail::read_size_helper;
auto mbs = buf_.prepare(
read_size_helper(buf_, max_size));
auto const mb = *mbs.begin();
@@ -207,6 +209,7 @@ private:
{
using boost::asio::buffer_cast;
using boost::asio::buffer_size;
using beast::detail::read_size_helper;
auto mbs = buf_.prepare(
read_size_helper(buf_, max_size));
auto const mb = *mbs.begin();
@@ -0,0 +1,35 @@
//
// 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 <algorithm>
#include <cstddef>
namespace beast {
namespace detail {
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();
return std::min<std::size_t>(
std::max<std::size_t>(512, buffer.capacity() - size),
std::min<std::size_t>(max_size, buffer.max_size() - size));
}
} // detail
} // beast
#endif
-7
View File
@@ -283,13 +283,6 @@ public:
void
shrink_to_fit();
// Helper for boost::asio::read_until
template<class OtherAlloc>
friend
std::size_t
read_size_helper(basic_flat_buffer<
OtherAlloc> const&, std::size_t);
private:
void
move_from(basic_flat_buffer& other);
-17
View File
@@ -311,23 +311,6 @@ shrink_to_fit()
end_ = out_;
}
template<class Allocator>
std::size_t
read_size_helper(basic_flat_buffer<
Allocator> const& fb, std::size_t max_size)
{
BOOST_ASSERT(max_size >= 1);
auto const len = fb.size();
auto const avail = fb.capacity() - len;
if (avail > 0)
return (std::min)(avail, max_size);
auto size = (std::min)(
fb.capacity() * 2, fb.max_size()) - len;
if(size == 0)
size = 1;
return (std::min)(size, max_size);
}
} // beast
#endif
+11 -13
View File
@@ -854,21 +854,19 @@ basic_multi_buffer<Allocator>::debug_check() const
template<class Allocator>
std::size_t
read_size_helper(basic_multi_buffer<
Allocator> const& multi_buffer, std::size_t max_size)
read_size_helper(
basic_multi_buffer<Allocator> const& buffer,
std::size_t max_size)
{
BOOST_ASSERT(max_size >= 1);
// If we already have an allocated
// buffer, try to fill that up first
auto const avail = multi_buffer.capacity() - multi_buffer.size();
if (avail > 0)
return (std::min)(avail, max_size);
// Try to have just one new block allocated
constexpr std::size_t low = 512;
if (multi_buffer.alloc_size_ > low)
return (std::min)(max_size, multi_buffer.alloc_size_);
// ...but enforce a 512 byte minimum.
return (std::min)(max_size, low);
auto const size = buffer.size();
auto const avail = std::min<std::size_t>(
buffer.capacity() - size, max_size);
if(avail > 0)
return avail; // avoid allocation
return std::min<std::size_t>(
std::min<std::size_t>(max_size, buffer.max_size() - size),
avail + buffer.alloc_size());
}
} // beast
+7 -7
View File
@@ -285,13 +285,6 @@ public:
void
consume(size_type n);
// Helper for boost::asio::read_until
template<class OtherAllocator>
friend
std::size_t
read_size_helper(basic_multi_buffer<
OtherAllocator> const& multi_buffer, std::size_t max_size);
private:
void
clear();
@@ -315,6 +308,13 @@ private:
debug_check() const;
};
/// Helper for boost::asio::read_until
template<class Allocator>
std::size_t
read_size_helper(
basic_multi_buffer<Allocator> const& buffer,
std::size_t max_size);
/// A typical multi buffer
using multi_buffer = basic_multi_buffer<std::allocator<char>>;
+2
View File
@@ -15,6 +15,7 @@
#include <beast/core/bind_handler.hpp>
#include <beast/core/handler_ptr.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>
@@ -160,6 +161,7 @@ operator()(error_code ec,
case 2:
case 3:
{
using beast::detail::read_size_helper;
auto const size =
read_size_helper(d.db, 65536);
BOOST_ASSERT(size > 0);
+2
View File
@@ -15,6 +15,7 @@
#include <beast/core/bind_handler.hpp>
#include <beast/core/handler_ptr.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>
@@ -53,6 +54,7 @@ read_some_buffer(
do_read:
boost::optional<typename
DynamicBuffer::mutable_buffers_type> mb;
using beast::detail::read_size_helper;
auto const size =
read_size_helper(buffer, 65536);
BOOST_ASSERT(size > 0);
@@ -11,6 +11,7 @@
#include <beast/core/error.hpp>
#include <beast/core/consuming_buffers.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>
@@ -368,6 +369,7 @@ inflate(
for(;;)
{
// VFALCO we could be smarter about the size
using beast::detail::read_size_helper;
auto const bs = buffer.prepare(
read_size_helper(buffer, 65536));
auto const out = *bs.begin();