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

Side by Side Diff: webrtc/base/buffer.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: Feedback from tommi. 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
« no previous file with comments | « webrtc/api/webrtcsession_unittest.cc ('k') | webrtc/base/buffer.cc » ('j') | 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 2004 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2004 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 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
43 using t = decltype(F(static_cast<T*>(nullptr))); 43 using t = decltype(F(static_cast<T*>(nullptr)));
44 }; 44 };
45 45
46 } // namespace internal 46 } // namespace internal
47 47
48 // Basic buffer class, can be grown and shrunk dynamically. 48 // Basic buffer class, can be grown and shrunk dynamically.
49 // Unlike std::string/vector, does not initialize data when expanding capacity. 49 // Unlike std::string/vector, does not initialize data when expanding capacity.
50 class Buffer { 50 class Buffer {
51 public: 51 public:
52 Buffer(); // An empty buffer. 52 Buffer(); // An empty buffer.
53 Buffer(const Buffer& buf); // Copy size and contents of an existing buffer.
54 Buffer(Buffer&& buf); // Move contents from an existing buffer. 53 Buffer(Buffer&& buf); // Move contents from an existing buffer.
55 54
56 // Construct a buffer with the specified number of uninitialized bytes. 55 // Construct a buffer with the specified number of uninitialized bytes.
57 explicit Buffer(size_t size); 56 explicit Buffer(size_t size);
58 Buffer(size_t size, size_t capacity); 57 Buffer(size_t size, size_t capacity);
59 58
60 // Construct a buffer and copy the specified number of bytes into it. The 59 // Construct a buffer and copy the specified number of bytes into it. The
61 // source array may be (const) uint8_t*, int8_t*, or char*. 60 // source array may be (const) uint8_t*, int8_t*, or char*.
62 template <typename T, typename internal::ByteType<T>::t = 0> 61 template <typename T, typename internal::ByteType<T>::t = 0>
63 Buffer(const T* data, size_t size) 62 Buffer(const T* data, size_t size)
(...skipping 29 matching lines...) Expand all
93 size_t size() const { 92 size_t size() const {
94 RTC_DCHECK(IsConsistent()); 93 RTC_DCHECK(IsConsistent());
95 return size_; 94 return size_;
96 } 95 }
97 96
98 size_t capacity() const { 97 size_t capacity() const {
99 RTC_DCHECK(IsConsistent()); 98 RTC_DCHECK(IsConsistent());
100 return capacity_; 99 return capacity_;
101 } 100 }
102 101
103 Buffer& operator=(const Buffer& buf) {
104 if (&buf != this)
105 SetData(buf.data(), buf.size());
106 return *this;
107 }
108
109 Buffer& operator=(Buffer&& buf) { 102 Buffer& operator=(Buffer&& buf) {
110 RTC_DCHECK(IsConsistent()); 103 RTC_DCHECK(IsConsistent());
111 RTC_DCHECK(buf.IsConsistent()); 104 RTC_DCHECK(buf.IsConsistent());
112 size_ = buf.size_; 105 size_ = buf.size_;
113 capacity_ = buf.capacity_; 106 capacity_ = buf.capacity_;
114 data_ = std::move(buf.data_); 107 data_ = std::move(buf.data_);
115 buf.OnMovedFrom(); 108 buf.OnMovedFrom();
116 return *this; 109 return *this;
117 } 110 }
118 111
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
273 #else 266 #else
274 // Ensure that *this is always inconsistent, to provoke bugs. 267 // Ensure that *this is always inconsistent, to provoke bugs.
275 size_ = 1; 268 size_ = 1;
276 capacity_ = 0; 269 capacity_ = 0;
277 #endif 270 #endif
278 } 271 }
279 272
280 size_t size_; 273 size_t size_;
281 size_t capacity_; 274 size_t capacity_;
282 std::unique_ptr<uint8_t[]> data_; 275 std::unique_ptr<uint8_t[]> data_;
276
277 RTC_DISALLOW_COPY_AND_ASSIGN(Buffer);
283 }; 278 };
284 279
285 } // namespace rtc 280 } // namespace rtc
286 281
287 #endif // WEBRTC_BASE_BUFFER_H_ 282 #endif // WEBRTC_BASE_BUFFER_H_
OLDNEW
« no previous file with comments | « webrtc/api/webrtcsession_unittest.cc ('k') | webrtc/base/buffer.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698