Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(19)

Unified Diff: webrtc/base/copyonwritebuffer.h

Issue 1823503002: Reland Use CopyOnWriteBuffer instead of Buffer to avoid unnecessary copies. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: webrtc/base/copyonwritebuffer.h
diff --git a/webrtc/base/copyonwritebuffer.h b/webrtc/base/copyonwritebuffer.h
index 17f2710f3ecbd404646268d4af9a003d7c2e5465..87f24bf51d6db78aecc44442c810d0d0887d8dec 100644
--- a/webrtc/base/copyonwritebuffer.h
+++ b/webrtc/base/copyonwritebuffer.h
@@ -42,7 +42,9 @@ class CopyOnWriteBuffer {
template <typename T, typename internal::ByteType<T>::t = 0>
CopyOnWriteBuffer(const T* data, size_t size, size_t capacity)
: CopyOnWriteBuffer(size, capacity) {
- std::memcpy(buffer_->data(), data, size);
+ if (buffer_) {
+ std::memcpy(buffer_->data(), data, size);
+ }
}
// Construct a buffer from the contents of an array.
@@ -123,13 +125,24 @@ class CopyOnWriteBuffer {
return !(*this == buf);
}
+ uint8_t& operator[](size_t index) {
+ RTC_DCHECK_LT(index, size());
+ return data()[index];
+ }
+
+ uint8_t operator[](size_t index) const {
+ RTC_DCHECK_LT(index, size());
+ return cdata()[index];
+ }
+
// Replace the contents of the buffer. Accepts the same types as the
// constructors.
template <typename T, typename internal::ByteType<T>::t = 0>
void SetData(const T* data, size_t size) {
RTC_DCHECK(IsConsistent());
if (!buffer_ || !buffer_->HasOneRef()) {
- buffer_ = new RefCountedObject<Buffer>(data, size, size);
+ buffer_ = size > 0 ? new RefCountedObject<Buffer>(data, size)
+ : nullptr;
} else {
buffer_->SetData(data, size);
}

Powered by Google App Engine
This is Rietveld 408576698