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

Side by Side Diff: webrtc/base/copyonwritebuffer.h

Issue 2317283004: CopyOnWriteBuffer::SetSize to smaller size memcpy less. (Closed)
Patch Set: ... 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 unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2016 The WebRTC Project Authors. All rights reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after
215 void SetSize(size_t size) { 215 void SetSize(size_t size) {
216 RTC_DCHECK(IsConsistent()); 216 RTC_DCHECK(IsConsistent());
217 if (!buffer_) { 217 if (!buffer_) {
218 if (size > 0) { 218 if (size > 0) {
219 buffer_ = new RefCountedObject<Buffer>(size); 219 buffer_ = new RefCountedObject<Buffer>(size);
220 } 220 }
221 RTC_DCHECK(IsConsistent()); 221 RTC_DCHECK(IsConsistent());
222 return; 222 return;
223 } 223 }
224 224
225 CloneDataIfReferenced(std::max(buffer_->capacity(), size)); 225 // Clone data if referenced.
226 if (!buffer_->HasOneRef()) {
227 buffer_ = new RefCountedObject<Buffer>(buffer_->data(),
228 std::min(buffer_->size(), size),
229 std::max(buffer_->capacity(), size));
kwiberg-webrtc 2016/09/09 08:28:29 You maintain the capacity of the original buffer h
danilchap 2016/09/09 11:25:33 Yes. there is no need to reallocate with full capa
danilchap 2016/09/09 12:58:44 Actually, it is debatable if SetSize allowed to de
230 }
226 buffer_->SetSize(size); 231 buffer_->SetSize(size);
227 RTC_DCHECK(IsConsistent()); 232 RTC_DCHECK(IsConsistent());
228 } 233 }
229 234
230 // Ensure that the buffer size can be increased to at least capacity without 235 // Ensure that the buffer size can be increased to at least capacity without
231 // further reallocation. (Of course, this operation might need to reallocate 236 // further reallocation. (Of course, this operation might need to reallocate
232 // the buffer.) 237 // the buffer.)
233 void EnsureCapacity(size_t capacity) { 238 void EnsureCapacity(size_t capacity) {
234 RTC_DCHECK(IsConsistent()); 239 RTC_DCHECK(IsConsistent());
235 if (!buffer_) { 240 if (!buffer_) {
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
281 return (!buffer_ || buffer_->capacity() > 0); 286 return (!buffer_ || buffer_->capacity() > 0);
282 } 287 }
283 288
284 // buffer_ is either null, or points to an rtc::Buffer with capacity > 0. 289 // buffer_ is either null, or points to an rtc::Buffer with capacity > 0.
285 scoped_refptr<RefCountedObject<Buffer>> buffer_; 290 scoped_refptr<RefCountedObject<Buffer>> buffer_;
286 }; 291 };
287 292
288 } // namespace rtc 293 } // namespace rtc
289 294
290 #endif // WEBRTC_BASE_COPYONWRITEBUFFER_H_ 295 #endif // WEBRTC_BASE_COPYONWRITEBUFFER_H_
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698