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

Unified Diff: webrtc/base/copyonwritebuffer.h

Issue 2328553002: Make CopyOnWriteBuffer keep capacity (Closed)
Patch Set: heap_allocation -> original_allocation Created 4 years, 3 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
« no previous file with comments | « no previous file | webrtc/base/copyonwritebuffer_unittest.cc » ('j') | no next file with comments »
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 108aaa19d7aae6aed91cda23bfe3f00a21183d0f..70833d6c62b7a50d778e64ca6f62eb43a2389905 100644
--- a/webrtc/base/copyonwritebuffer.h
+++ b/webrtc/base/copyonwritebuffer.h
@@ -153,9 +153,10 @@ class CopyOnWriteBuffer {
internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
void SetData(const T* data, size_t size) {
RTC_DCHECK(IsConsistent());
- if (!buffer_ || !buffer_->HasOneRef()) {
- buffer_ = size > 0 ? new RefCountedObject<Buffer>(data, size)
- : nullptr;
+ if (!buffer_) {
+ buffer_ = size > 0 ? new RefCountedObject<Buffer>(data, size) : nullptr;
+ } else if (!buffer_->HasOneRef()) {
+ buffer_ = new RefCountedObject<Buffer>(data, size, buffer_->capacity());
} else {
buffer_->SetData(data, size);
}
@@ -253,13 +254,16 @@ class CopyOnWriteBuffer {
RTC_DCHECK(IsConsistent());
}
- // Resets the buffer to zero size and capacity.
+ // Resets the buffer to zero size without altering capacity. Works even if the
+ // buffer has been moved from.
void Clear() {
- RTC_DCHECK(IsConsistent());
- if (!buffer_ || !buffer_->HasOneRef()) {
- buffer_ = nullptr;
- } else {
+ if (!buffer_)
+ return;
+
+ if (buffer_->HasOneRef()) {
buffer_->Clear();
+ } else {
+ buffer_ = new RefCountedObject<Buffer>(0, buffer_->capacity());
}
RTC_DCHECK(IsConsistent());
}
« no previous file with comments | « no previous file | webrtc/base/copyonwritebuffer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698