Chromium Code Reviews

Unified Diff: webrtc/base/copyonwritebuffer.h

Issue 2328553002: Make CopyOnWriteBuffer keep capacity (Closed)
Patch Set: Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
« no previous file with comments | « no previous file | webrtc/base/copyonwritebuffer_unittest.cc » ('j') | webrtc/base/copyonwritebuffer_unittest.cc » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: webrtc/base/copyonwritebuffer.h
diff --git a/webrtc/base/copyonwritebuffer.h b/webrtc/base/copyonwritebuffer.h
index 72fb4eea53495a2575ee435efade9d182afd4a69..41d4090a869b20f35b3ca01eaffb8f795ff87869 100644
--- a/webrtc/base/copyonwritebuffer.h
+++ b/webrtc/base/copyonwritebuffer.h
@@ -162,6 +162,23 @@ class CopyOnWriteBuffer {
RTC_DCHECK(IsConsistent());
}
+ // Replace the contents of the buffer. Ensure capacity is at least |capacity|.
+ template <typename T,
+ typename std::enable_if<
+ internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
+ void SetData(const T* data, size_t size, size_t capacity) {
kwiberg-webrtc 2016/09/09 08:55:13 Isn't this equivalent to calling x.EnsureCapaci
danilchap 2016/09/09 11:21:17 1. SetData signatures mirror constructor signature
kwiberg-webrtc 2016/09/09 12:59:13 I'm not sure. You can always replace Buffer buf
danilchap 2016/09/09 16:00:56 Yes, I'm wrong. it is not same. Buffer may benefit
+ RTC_DCHECK(IsConsistent());
+ capacity = std::max(size, capacity);
+ if (!buffer_ || !buffer_->HasOneRef() || buffer_->capacity() < capacity) {
+ buffer_ = capacity > 0
+ ? new RefCountedObject<Buffer>(data, size, capacity)
+ : nullptr;
+ } else {
+ buffer_->SetData(data, size);
+ }
+ RTC_DCHECK(IsConsistent());
+ }
+
template <typename T,
size_t N,
typename std::enable_if<
« no previous file with comments | « no previous file | webrtc/base/copyonwritebuffer_unittest.cc » ('j') | webrtc/base/copyonwritebuffer_unittest.cc » ('J')

Powered by Google App Engine