From 6732af5822b18b5c97121ca55f44ca62bfd3d138 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Fri, 14 Oct 2016 20:46:38 -0400 Subject: [PATCH] Add basic_streambuf::alloc_size fix #133 --- CHANGELOG.md | 1 + include/beast/core/basic_streambuf.hpp | 32 +++++++++++++++++++++++++- test/core/basic_streambuf.cpp | 31 +++++++++++++++++++++---- 3 files changed, 59 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 25d35a88..5449cca3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ * Change implicit to default value in example * Tidy up some declarations * Fix basic_streambuf::capacity +* Add basic_streambuf::alloc_size -------------------------------------------------------------------------------- diff --git a/include/beast/core/basic_streambuf.hpp b/include/beast/core/basic_streambuf.hpp index 6aee9603..8c57678d 100644 --- a/include/beast/core/basic_streambuf.hpp +++ b/include/beast/core/basic_streambuf.hpp @@ -25,7 +25,7 @@ namespace beast { the sequence to accommodate changes in the size of the character sequence. - @note Meets the requirements of @b `DynamicBuffer`. + @note Meets the requirements of @b DynamicBuffer. @tparam Allocator The allocator to use for managing memory. */ @@ -209,6 +209,36 @@ public: return this->member(); } + /** Returns the default allocation size. + + This is the smallest size that the stream buffer will allocate. + The size of the allocation can influence capacity, which will + affect algorithms that use capacity to efficiently read from + streams. + */ + std::size_t + alloc_size() const + { + return alloc_size_; + } + + /** Set the default allocation size. + + This is the smallest size that the stream buffer will allocate. + The size of the allocation can influence capacity, which will + affect algorithms that use capacity to efficiently read from + streams. + + @note This will not affect any already-existing allocations. + + @param n The number of bytes. + */ + void + alloc_size(std::size_t n) + { + alloc_size_ = n; + } + /// Returns the size of the input sequence. size_type size() const diff --git a/test/core/basic_streambuf.cpp b/test/core/basic_streambuf.cpp index 9bb79b6a..bf1c4d39 100644 --- a/test/core/basic_streambuf.cpp +++ b/test/core/basic_streambuf.cpp @@ -397,11 +397,12 @@ public: BEAST_EXPECT(to_string(sb.data()) == "x"); } - void testReadSizeHelper() + void testCapacity() { using boost::asio::buffer_size; { - streambuf sb(10); + streambuf sb{10}; + BEAST_EXPECT(sb.alloc_size() == 10); BEAST_EXPECT(read_size_helper(sb, 0) == 0); BEAST_EXPECT(read_size_helper(sb, 1) == 1); BEAST_EXPECT(read_size_helper(sb, 10) == 10); @@ -414,6 +415,7 @@ public: } { streambuf sb(1000); + BEAST_EXPECT(sb.alloc_size() == 1000); BEAST_EXPECT(read_size_helper(sb, 0) == 0); BEAST_EXPECT(read_size_helper(sb, 1) == 1); BEAST_EXPECT(read_size_helper(sb, 1000) == 1000); @@ -435,12 +437,33 @@ public: BEAST_EXPECT(read_size_helper(sb, 2000) == 997); } { - streambuf sb(2); + streambuf sb{2}; + BEAST_EXPECT(sb.alloc_size() == 2); BEAST_EXPECT(test::buffer_count(sb.prepare(2)) == 1); BEAST_EXPECT(test::buffer_count(sb.prepare(3)) == 2); BEAST_EXPECT(buffer_size(sb.prepare(5)) == 5); BEAST_EXPECT(read_size_helper(sb, 10) == 6); } + { + auto avail = + [](streambuf const& sb) + { + return sb.capacity() - sb.size(); + }; + streambuf sb{100}; + BEAST_EXPECT(sb.alloc_size() == 100); + BEAST_EXPECT(avail(sb) == 0); + sb.prepare(100); + BEAST_EXPECT(avail(sb) == 100); + sb.commit(100); + BEAST_EXPECT(avail(sb) == 0); + sb.consume(100); + BEAST_EXPECT(avail(sb) == 0); + sb.alloc_size(200); + BEAST_EXPECT(sb.alloc_size() == 200); + sb.prepare(1); + BEAST_EXPECT(avail(sb) == 200); + } } void run() override @@ -453,7 +476,7 @@ public: testMatrix(); testIterators(); testOutputStream(); - testReadSizeHelper(); + testCapacity(); } };