Changed the semantics of Buffer::Clear to not alter the capacity
Also added a test for Clear to ensure this invariant holds. With this change, it is easy to empty a Buffer and reuse its storage. Further down the line, code filling data into a Buffer could be written to just append to it, with the caller determining if the Buffer should first be cleared or not. There is currently only one use of Buffer::Clear (in AudioEncoderCopyRed::Reset()) and it should benefit from the change, by not requiring a reallocation after Reset. Review URL: https://codereview.webrtc.org/1707693002 Cr-Commit-Position: refs/heads/master@{#11680}
This commit is contained in:
parent
ecdeb4cb94
commit
728012e49f
@ -180,12 +180,10 @@ class Buffer {
|
||||
return std::move(*this);
|
||||
}
|
||||
|
||||
// Resets the buffer to zero size and capacity. Works even if the buffer has
|
||||
// been moved from.
|
||||
// Resets the buffer to zero size without altering capacity. Works even if the
|
||||
// buffer has been moved from.
|
||||
void Clear() {
|
||||
data_.reset();
|
||||
size_ = 0;
|
||||
capacity_ = 0;
|
||||
assert(IsConsistent());
|
||||
}
|
||||
|
||||
|
||||
@ -177,4 +177,16 @@ TEST(BufferTest, TestSwap) {
|
||||
EXPECT_EQ(buf2.data(), data1);
|
||||
}
|
||||
|
||||
TEST(BufferTest, TestClear) {
|
||||
Buffer buf;
|
||||
buf.SetData(kTestData, 15);
|
||||
EXPECT_EQ(buf.size(), 15u);
|
||||
EXPECT_EQ(buf.capacity(), 15u);
|
||||
const char *data = buf.data<char>();
|
||||
buf.Clear();
|
||||
EXPECT_EQ(buf.size(), 0u);
|
||||
EXPECT_EQ(buf.capacity(), 15u); // Hasn't shrunk.
|
||||
EXPECT_EQ(buf.data<char>(), data); // No reallocation.
|
||||
}
|
||||
|
||||
} // namespace rtc
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user