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

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

Issue 2877023002: Move webrtc/{base => rtc_base} (Closed)
Patch Set: update presubmit.py and DEPS include rules Created 3 years, 5 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 | « webrtc/base/copyonwritebuffer.h ('k') | webrtc/base/copyonwritebuffer_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright 2016 The WebRTC Project Authors. All rights reserved.
3 *
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
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include "webrtc/base/copyonwritebuffer.h"
12
13 namespace rtc {
14
15 CopyOnWriteBuffer::CopyOnWriteBuffer() {
16 RTC_DCHECK(IsConsistent());
17 }
18
19 CopyOnWriteBuffer::CopyOnWriteBuffer(const CopyOnWriteBuffer& buf)
20 : buffer_(buf.buffer_) {
21 }
22
23 CopyOnWriteBuffer::CopyOnWriteBuffer(CopyOnWriteBuffer&& buf)
24 : buffer_(std::move(buf.buffer_)) {
25 }
26
27 CopyOnWriteBuffer::CopyOnWriteBuffer(size_t size)
28 : buffer_(size > 0 ? new RefCountedObject<Buffer>(size) : nullptr) {
29 RTC_DCHECK(IsConsistent());
30 }
31
32 CopyOnWriteBuffer::CopyOnWriteBuffer(size_t size, size_t capacity)
33 : buffer_(size > 0 || capacity > 0
34 ? new RefCountedObject<Buffer>(size, capacity)
35 : nullptr) {
36 RTC_DCHECK(IsConsistent());
37 }
38
39 CopyOnWriteBuffer::~CopyOnWriteBuffer() = default;
40
41 bool CopyOnWriteBuffer::operator==(const CopyOnWriteBuffer& buf) const {
42 // Must either use the same buffer internally or have the same contents.
43 RTC_DCHECK(IsConsistent());
44 RTC_DCHECK(buf.IsConsistent());
45 return buffer_.get() == buf.buffer_.get() ||
46 (buffer_.get() && buf.buffer_.get() &&
47 *buffer_.get() == *buf.buffer_.get());
48 }
49
50 void CopyOnWriteBuffer::SetSize(size_t size) {
51 RTC_DCHECK(IsConsistent());
52 if (!buffer_) {
53 if (size > 0) {
54 buffer_ = new RefCountedObject<Buffer>(size);
55 }
56 RTC_DCHECK(IsConsistent());
57 return;
58 }
59
60 // Clone data if referenced.
61 if (!buffer_->HasOneRef()) {
62 buffer_ = new RefCountedObject<Buffer>(
63 buffer_->data(),
64 std::min(buffer_->size(), size),
65 std::max(buffer_->capacity(), size));
66 }
67 buffer_->SetSize(size);
68 RTC_DCHECK(IsConsistent());
69 }
70
71 void CopyOnWriteBuffer::EnsureCapacity(size_t capacity) {
72 RTC_DCHECK(IsConsistent());
73 if (!buffer_) {
74 if (capacity > 0) {
75 buffer_ = new RefCountedObject<Buffer>(0, capacity);
76 }
77 RTC_DCHECK(IsConsistent());
78 return;
79 } else if (capacity <= buffer_->capacity()) {
80 return;
81 }
82
83 CloneDataIfReferenced(std::max(buffer_->capacity(), capacity));
84 buffer_->EnsureCapacity(capacity);
85 RTC_DCHECK(IsConsistent());
86 }
87
88 void CopyOnWriteBuffer::Clear() {
89 if (!buffer_)
90 return;
91
92 if (buffer_->HasOneRef()) {
93 buffer_->Clear();
94 } else {
95 buffer_ = new RefCountedObject<Buffer>(0, buffer_->capacity());
96 }
97 RTC_DCHECK(IsConsistent());
98 }
99
100 void CopyOnWriteBuffer::CloneDataIfReferenced(size_t new_capacity) {
101 if (buffer_->HasOneRef()) {
102 return;
103 }
104
105 buffer_ = new RefCountedObject<Buffer>(buffer_->data(), buffer_->size(),
106 new_capacity);
107 RTC_DCHECK(IsConsistent());
108 }
109
110
111
112 } // namespace rtc
OLDNEW
« no previous file with comments | « webrtc/base/copyonwritebuffer.h ('k') | webrtc/base/copyonwritebuffer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698