From 728012e49fc9f0c4ecda92781abbeda7cfc8a03f Mon Sep 17 00:00:00 2001 From: ossu Date: Fri, 19 Feb 2016 02:38:32 -0800 Subject: [PATCH] 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} --- webrtc/base/buffer.h | 6 ++---- webrtc/base/buffer_unittest.cc | 12 ++++++++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/webrtc/base/buffer.h b/webrtc/base/buffer.h index 5c9380afee..234039607a 100644 --- a/webrtc/base/buffer.h +++ b/webrtc/base/buffer.h @@ -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()); } diff --git a/webrtc/base/buffer_unittest.cc b/webrtc/base/buffer_unittest.cc index 0b93b9b56e..f917bc6181 100644 --- a/webrtc/base/buffer_unittest.cc +++ b/webrtc/base/buffer_unittest.cc @@ -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(); + buf.Clear(); + EXPECT_EQ(buf.size(), 0u); + EXPECT_EQ(buf.capacity(), 15u); // Hasn't shrunk. + EXPECT_EQ(buf.data(), data); // No reallocation. +} + } // namespace rtc