diff --git a/CHANGELOG.md b/CHANGELOG.md index 7a47a705..7baa05fa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ Version 55: * Don't allocate memory to handle obs-fold * Add Beast CMake interface target * Avoid a parser allocation using non-flat buffer +* read_size replaces read_size_helper -------------------------------------------------------------------------------- diff --git a/doc/quickref.xml b/doc/quickref.xml index fc724f32..b0a85a72 100644 --- a/doc/quickref.xml +++ b/doc/quickref.xml @@ -189,6 +189,8 @@ buffer_prefix buffers ostream + read_size + read_size_or_throw system_category to_static_string diff --git a/include/beast/core/detail/ostream.hpp b/include/beast/core/detail/ostream.hpp index d5d4cb38..5f2f92c2 100644 --- a/include/beast/core/detail/ostream.hpp +++ b/include/beast/core/detail/ostream.hpp @@ -9,7 +9,7 @@ #define BEAST_DETAIL_OSTREAM_HPP #include -#include +#include #include #include #include @@ -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(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(mb); this->setp(p, diff --git a/include/beast/core/detail/read_size_helper.hpp b/include/beast/core/detail/read_size_helper.hpp deleted file mode 100644 index 433ba38d..00000000 --- a/include/beast/core/detail/read_size_helper.hpp +++ /dev/null @@ -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 -#include -#include -#include -#include - -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 -std::size_t -read_size_helper( - DynamicBuffer const& buffer, std::size_t max_size) -{ - static_assert(beast::is_dynamic_buffer::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::max(512, buffer.capacity() - size), - std::min(max_size, limit)); -} - -/** Return a non-zero natural read size. -*/ -template -std::size_t -maybe_read_size_helper( - DynamicBuffer const& buffer, std::size_t max_size) -{ - static_assert(beast::is_dynamic_buffer::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 diff --git a/include/beast/core/impl/read_size.ipp b/include/beast/core/impl/read_size.ipp new file mode 100644 index 00000000..56bc81c7 --- /dev/null +++ b/include/beast/core/impl/read_size.ipp @@ -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 +struct has_read_size_helper : std::false_type {}; + +template +struct has_read_size_helper(), 512), + (void)0)> : std::true_type +{ +}; + +template +std::size_t +read_size(DynamicBuffer& buffer, + std::size_t max_size, std::true_type) +{ + return read_size_helper(buffer, max_size); +} + +template +std::size_t +read_size(DynamicBuffer& buffer, + std::size_t max_size, std::false_type) +{ + static_assert(is_dynamic_buffer::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::max(512, buffer.capacity() - size), + std::min(max_size, limit)); +} + +} // detail + +template +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{}); +} + +template +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 diff --git a/include/beast/core/read_size.hpp b/include/beast/core/read_size.hpp new file mode 100644 index 00000000..72ecc9cb --- /dev/null +++ b/include/beast/core/read_size.hpp @@ -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 +#include + +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 +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 +std::size_t +read_size_or_throw(DynamicBuffer& buffer, + std::size_t max_size); + +} // beast + +#include + +#endif diff --git a/include/beast/http/impl/read.ipp b/include/beast/http/impl/read.ipp index 1d804763..7aaa2f69 100644 --- a/include/beast/http/impl/read.ipp +++ b/include/beast/http/impl/read.ipp @@ -14,8 +14,8 @@ #include #include #include +#include #include -#include #include #include #include @@ -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&) { diff --git a/include/beast/websocket/detail/pmd_extension.hpp b/include/beast/websocket/detail/pmd_extension.hpp index dab7adcf..dadf94af 100644 --- a/include/beast/websocket/detail/pmd_extension.hpp +++ b/include/beast/websocket/detail/pmd_extension.hpp @@ -10,8 +10,8 @@ #include #include +#include #include -#include #include #include #include @@ -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(out); diff --git a/test/Jamfile b/test/Jamfile index ccaab675..dd42c5b3 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -34,6 +34,7 @@ unit-test core-tests : core/handler_ptr.cpp core/multi_buffer.cpp core/ostream.cpp + core/read_size.cpp core/static_buffer.cpp core/static_string.cpp core/string_param.cpp diff --git a/test/core/CMakeLists.txt b/test/core/CMakeLists.txt index e1369ef2..4aa8e429 100644 --- a/test/core/CMakeLists.txt +++ b/test/core/CMakeLists.txt @@ -29,6 +29,7 @@ add_executable (core-tests handler_ptr.cpp multi_buffer.cpp ostream.cpp + read_size.cpp static_buffer.cpp static_string.cpp string_param.cpp diff --git a/test/core/buffer_bench.cpp b/test/core/buffer_bench.cpp index aaa31068..d77da056 100644 --- a/test/core/buffer_bench.cpp +++ b/test/core/buffer_bench.cpp @@ -7,8 +7,8 @@ #include #include +#include #include -#include #include #include #include @@ -111,9 +111,8 @@ public: { for(auto remain = size; remain;) { - using beast::detail::read_size_helper; auto const n = fill(b.prepare( - read_size_helper(b, remain))); + read_size(b, remain))); b.commit(n); remain -= n; total += n; diff --git a/test/core/buffer_test.hpp b/test/core/buffer_test.hpp index 2aac00ed..6a31d285 100644 --- a/test/core/buffer_test.hpp +++ b/test/core/buffer_test.hpp @@ -9,8 +9,8 @@ #define BEAST_TEST_BUFFER_TEST_HPP #include +#include #include -#include #include #include #include @@ -112,37 +112,6 @@ size_rev_post(ConstBufferSequence const& buffers) return n; } -namespace has_read_size_helper -{ - using beast::detail::read_size_helper; - - template - struct trait : std::false_type {}; - - template - struct trait() = - read_size_helper(std::declval(), 0), - (void)0)> : std::true_type - { - }; -} - -//------------------------------------------------------------------------------ - -// Make sure read_size_helper works -template -inline -void -check_read_size_helper() -{ - static_assert(is_dynamic_buffer::value, - "DynamicBuffer requirements not met "); - - static_assert(has_read_size_helper::trait::value, - "Missing read_size_helper for dynamic buffer"); -} - } // test } // beast diff --git a/test/core/buffers_adapter.cpp b/test/core/buffers_adapter.cpp index 7e88c8af..eff1f223 100644 --- a/test/core/buffers_adapter.cpp +++ b/test/core/buffers_adapter.cpp @@ -183,15 +183,11 @@ public: type buffer; buffers_adapter< type::mutable_buffers_type> ba{buffer.prepare(512)}; - using beast::detail::read_size_helper; - read_size_helper(ba, 1024); + read_size(ba, 1024); } void run() override { - test::check_read_size_helper>(); - testBuffersAdapter(); testCommit(); testIssue386(); diff --git a/test/core/flat_buffer.cpp b/test/core/flat_buffer.cpp index b4e32e69..293cb5da 100644 --- a/test/core/flat_buffer.cpp +++ b/test/core/flat_buffer.cpp @@ -11,8 +11,8 @@ #include "buffer_test.hpp" #include +#include #include -#include #include #include #include @@ -261,19 +261,18 @@ public: BEAST_EXPECT(to_string(b.data()) == "4567890123"); } - // read_size_helper + // read_size { - using detail::read_size_helper; flat_buffer b{10}; - BEAST_EXPECT(read_size_helper(b, 512) == 10); + BEAST_EXPECT(read_size(b, 512) == 10); b.prepare(4); b.commit(4); - BEAST_EXPECT(read_size_helper(b, 512) == 6); + BEAST_EXPECT(read_size(b, 512) == 6); b.consume(2); - BEAST_EXPECT(read_size_helper(b, 512) == 8); + BEAST_EXPECT(read_size(b, 512) == 8); b.prepare(8); b.commit(8); - BEAST_EXPECT(read_size_helper(b, 512) == 0); + BEAST_EXPECT(read_size(b, 512) == 0); } // swap @@ -344,8 +343,6 @@ public: void run() override { - test::check_read_size_helper(); - testBuffer(); } }; diff --git a/test/core/multi_buffer.cpp b/test/core/multi_buffer.cpp index e97e7dbb..f7c39e23 100644 --- a/test/core/multi_buffer.cpp +++ b/test/core/multi_buffer.cpp @@ -562,27 +562,24 @@ public: } } - // read_size_helper + // read_size { - using detail::read_size_helper; multi_buffer b{10}; - BEAST_EXPECT(read_size_helper(b, 512) == 10); + BEAST_EXPECT(read_size(b, 512) == 10); b.prepare(4); b.commit(4); - BEAST_EXPECT(read_size_helper(b, 512) == 6); + BEAST_EXPECT(read_size(b, 512) == 6); b.consume(2); - BEAST_EXPECT(read_size_helper(b, 512) == 8); + BEAST_EXPECT(read_size(b, 512) == 8); b.prepare(8); b.commit(8); - BEAST_EXPECT(read_size_helper(b, 512) == 0); + BEAST_EXPECT(read_size(b, 512) == 0); } } void run() override { - test::check_read_size_helper(); - testMatrix1(); testMatrix2(); testIterators(); diff --git a/test/core/read_size.cpp b/test/core/read_size.cpp new file mode 100644 index 00000000..40a01c0c --- /dev/null +++ b/test/core/read_size.cpp @@ -0,0 +1,44 @@ +// +// 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) +// + +// Test that header file is self-contained. +#include + +#include +#include +#include +#include + +#include + +namespace beast { + +class read_size_test : public beast::unit_test::suite +{ +public: + template + void + check() + { + DynamicBuffer buffer; + read_size(buffer, 65536); + pass(); + } + + void + run() override + { + check(); + check(); + check(); + check(); + } +}; + +BEAST_DEFINE_TESTSUITE(read_size,core,beast); + +} // beast diff --git a/test/core/static_buffer.cpp b/test/core/static_buffer.cpp index bebbfa10..83a79168 100644 --- a/test/core/static_buffer.cpp +++ b/test/core/static_buffer.cpp @@ -190,19 +190,18 @@ public: } } - // read_size_helper + // read_size { - using detail::read_size_helper; static_buffer_n<10> b; - BEAST_EXPECT(read_size_helper(b, 512) == 10); + BEAST_EXPECT(read_size(b, 512) == 10); b.prepare(4); b.commit(4); - BEAST_EXPECT(read_size_helper(b, 512) == 6); + BEAST_EXPECT(read_size(b, 512) == 6); b.consume(2); - BEAST_EXPECT(read_size_helper(b, 512) == 8); + BEAST_EXPECT(read_size(b, 512) == 8); b.prepare(8); b.commit(8); - BEAST_EXPECT(read_size_helper(b, 512) == 0); + BEAST_EXPECT(read_size(b, 512) == 0); } // base @@ -224,8 +223,6 @@ public: void run() override { - test::check_read_size_helper>(); - testBuffer(); //testStaticBuffer(); } diff --git a/test/http/doc_http_samples.cpp b/test/http/doc_http_samples.cpp index 5153f57b..10c808bd 100644 --- a/test/http/doc_http_samples.cpp +++ b/test/http/doc_http_samples.cpp @@ -8,8 +8,8 @@ #include #include +#include #include -#include #include #include #include