Add basic_streambuf::alloc_size

fix #133
This commit is contained in:
Vinnie Falco
2016-10-14 20:46:38 -04:00
parent 92e7afb801
commit 6732af5822
3 changed files with 59 additions and 5 deletions

View File

@ -3,6 +3,7 @@
* Change implicit to default value in example * Change implicit to default value in example
* Tidy up some declarations * Tidy up some declarations
* Fix basic_streambuf::capacity * Fix basic_streambuf::capacity
* Add basic_streambuf::alloc_size
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------

View File

@ -25,7 +25,7 @@ namespace beast {
the sequence to accommodate changes in the size of the character the sequence to accommodate changes in the size of the character
sequence. sequence.
@note Meets the requirements of @b `DynamicBuffer`. @note Meets the requirements of @b DynamicBuffer.
@tparam Allocator The allocator to use for managing memory. @tparam Allocator The allocator to use for managing memory.
*/ */
@ -209,6 +209,36 @@ public:
return this->member(); 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. /// Returns the size of the input sequence.
size_type size_type
size() const size() const

View File

@ -397,11 +397,12 @@ public:
BEAST_EXPECT(to_string(sb.data()) == "x"); BEAST_EXPECT(to_string(sb.data()) == "x");
} }
void testReadSizeHelper() void testCapacity()
{ {
using boost::asio::buffer_size; 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, 0) == 0);
BEAST_EXPECT(read_size_helper(sb, 1) == 1); BEAST_EXPECT(read_size_helper(sb, 1) == 1);
BEAST_EXPECT(read_size_helper(sb, 10) == 10); BEAST_EXPECT(read_size_helper(sb, 10) == 10);
@ -414,6 +415,7 @@ public:
} }
{ {
streambuf sb(1000); streambuf sb(1000);
BEAST_EXPECT(sb.alloc_size() == 1000);
BEAST_EXPECT(read_size_helper(sb, 0) == 0); BEAST_EXPECT(read_size_helper(sb, 0) == 0);
BEAST_EXPECT(read_size_helper(sb, 1) == 1); BEAST_EXPECT(read_size_helper(sb, 1) == 1);
BEAST_EXPECT(read_size_helper(sb, 1000) == 1000); BEAST_EXPECT(read_size_helper(sb, 1000) == 1000);
@ -435,12 +437,33 @@ public:
BEAST_EXPECT(read_size_helper(sb, 2000) == 997); 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(2)) == 1);
BEAST_EXPECT(test::buffer_count(sb.prepare(3)) == 2); BEAST_EXPECT(test::buffer_count(sb.prepare(3)) == 2);
BEAST_EXPECT(buffer_size(sb.prepare(5)) == 5); BEAST_EXPECT(buffer_size(sb.prepare(5)) == 5);
BEAST_EXPECT(read_size_helper(sb, 10) == 6); 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 void run() override
@ -453,7 +476,7 @@ public:
testMatrix(); testMatrix();
testIterators(); testIterators();
testOutputStream(); testOutputStream();
testReadSizeHelper(); testCapacity();
} }
}; };