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}
diff --git a/webrtc/base/buffer.h b/webrtc/base/buffer.h
index 5c9380a..2340396 100644
--- a/webrtc/base/buffer.h
+++ b/webrtc/base/buffer.h
@@ -180,12 +180,10 @@
     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 0b93b9b..f917bc6 100644
--- a/webrtc/base/buffer_unittest.cc
+++ b/webrtc/base/buffer_unittest.cc
@@ -177,4 +177,16 @@
   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