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

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

Issue 1785713005: Use CopyOnWriteBuffer instead of Buffer to avoid unnecessary copies. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Fix errors on bots (java, objc). 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 unified diff | Download patch
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 24 matching lines...) Expand all
35 CopyOnWriteBuffer(size_t size, size_t capacity); 35 CopyOnWriteBuffer(size_t size, size_t capacity);
36 36
37 // Construct a buffer and copy the specified number of bytes into it. The 37 // Construct a buffer and copy the specified number of bytes into it. The
38 // source array may be (const) uint8_t*, int8_t*, or char*. 38 // source array may be (const) uint8_t*, int8_t*, or char*.
39 template <typename T, typename internal::ByteType<T>::t = 0> 39 template <typename T, typename internal::ByteType<T>::t = 0>
40 CopyOnWriteBuffer(const T* data, size_t size) 40 CopyOnWriteBuffer(const T* data, size_t size)
41 : CopyOnWriteBuffer(data, size, size) {} 41 : CopyOnWriteBuffer(data, size, size) {}
42 template <typename T, typename internal::ByteType<T>::t = 0> 42 template <typename T, typename internal::ByteType<T>::t = 0>
43 CopyOnWriteBuffer(const T* data, size_t size, size_t capacity) 43 CopyOnWriteBuffer(const T* data, size_t size, size_t capacity)
44 : CopyOnWriteBuffer(size, capacity) { 44 : CopyOnWriteBuffer(size, capacity) {
45 std::memcpy(buffer_->data(), data, size); 45 if (size > 0) {
46 std::memcpy(buffer_->data(), data, size);
47 }
kwiberg-webrtc 2016/03/14 11:53:49 The reason for having the conditional is clearer i
joachim 2016/03/17 20:27:21 Done.
46 } 48 }
47 49
48 // Construct a buffer from the contents of an array. 50 // Construct a buffer from the contents of an array.
49 template <typename T, size_t N, typename internal::ByteType<T>::t = 0> 51 template <typename T, size_t N, typename internal::ByteType<T>::t = 0>
50 CopyOnWriteBuffer(const T(&array)[N]) // NOLINT: runtime/explicit 52 CopyOnWriteBuffer(const T(&array)[N]) // NOLINT: runtime/explicit
51 : CopyOnWriteBuffer(array, N) {} 53 : CopyOnWriteBuffer(array, N) {}
52 54
53 ~CopyOnWriteBuffer(); 55 ~CopyOnWriteBuffer();
54 56
55 // Get a pointer to the data. Just .data() will give you a (const) uint8_t*, 57 // Get a pointer to the data. Just .data() will give you a (const) uint8_t*,
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
121 123
122 bool operator!=(const CopyOnWriteBuffer& buf) const { 124 bool operator!=(const CopyOnWriteBuffer& buf) const {
123 return !(*this == buf); 125 return !(*this == buf);
124 } 126 }
125 127
126 // Replace the contents of the buffer. Accepts the same types as the 128 // Replace the contents of the buffer. Accepts the same types as the
127 // constructors. 129 // constructors.
128 template <typename T, typename internal::ByteType<T>::t = 0> 130 template <typename T, typename internal::ByteType<T>::t = 0>
129 void SetData(const T* data, size_t size) { 131 void SetData(const T* data, size_t size) {
130 RTC_DCHECK(IsConsistent()); 132 RTC_DCHECK(IsConsistent());
131 if (!buffer_ || !buffer_->HasOneRef()) { 133 if (size && (!buffer_ || !buffer_->HasOneRef())) {
132 buffer_ = new RefCountedObject<Buffer>(data, size, size); 134 buffer_ = new RefCountedObject<Buffer>(data, size);
133 } else { 135 } else if (buffer_) {
134 buffer_->SetData(data, size); 136 buffer_->SetData(data, size);
135 } 137 }
136 RTC_DCHECK(IsConsistent()); 138 RTC_DCHECK(IsConsistent());
kwiberg-webrtc 2016/03/14 11:53:49 Hmm. This is still wrong: if size == 0 and !buffer
joachim 2016/03/17 20:27:21 Thanks. Changed and added a test.
137 } 139 }
138 140
139 template <typename T, size_t N, typename internal::ByteType<T>::t = 0> 141 template <typename T, size_t N, typename internal::ByteType<T>::t = 0>
140 void SetData(const T(&array)[N]) { 142 void SetData(const T(&array)[N]) {
141 SetData(array, N); 143 SetData(array, N);
142 } 144 }
143 145
144 void SetData(const CopyOnWriteBuffer& buf) { 146 void SetData(const CopyOnWriteBuffer& buf) {
145 RTC_DCHECK(IsConsistent()); 147 RTC_DCHECK(IsConsistent());
146 RTC_DCHECK(buf.IsConsistent()); 148 RTC_DCHECK(buf.IsConsistent());
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
247 return (!buffer_ || buffer_->capacity() > 0); 249 return (!buffer_ || buffer_->capacity() > 0);
248 } 250 }
249 251
250 // buffer_ is either null, or points to an rtc::Buffer with capacity > 0. 252 // buffer_ is either null, or points to an rtc::Buffer with capacity > 0.
251 scoped_refptr<RefCountedObject<Buffer>> buffer_; 253 scoped_refptr<RefCountedObject<Buffer>> buffer_;
252 }; 254 };
253 255
254 } // namespace rtc 256 } // namespace rtc
255 257
256 #endif // WEBRTC_BASE_COPYONWRITEBUFFER_H_ 258 #endif // WEBRTC_BASE_COPYONWRITEBUFFER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698