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

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

Issue 1823503002: Reland Use CopyOnWriteBuffer instead of Buffer to avoid unnecessary copies. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: 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 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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 using t = decltype(F(static_cast<T*>(nullptr))); 42 using t = decltype(F(static_cast<T*>(nullptr)));
43 }; 43 };
44 44
45 } // namespace internal 45 } // namespace internal
46 46
47 // Basic buffer class, can be grown and shrunk dynamically. 47 // Basic buffer class, can be grown and shrunk dynamically.
48 // Unlike std::string/vector, does not initialize data when expanding capacity. 48 // Unlike std::string/vector, does not initialize data when expanding capacity.
49 class Buffer { 49 class Buffer {
50 public: 50 public:
51 Buffer(); // An empty buffer. 51 Buffer(); // An empty buffer.
52 Buffer(const Buffer& buf); // Copy size and contents of an existing buffer.
53 Buffer(Buffer&& buf); // Move contents from an existing buffer. 52 Buffer(Buffer&& buf); // Move contents from an existing buffer.
54 53
55 // Construct a buffer with the specified number of uninitialized bytes. 54 // Construct a buffer with the specified number of uninitialized bytes.
56 explicit Buffer(size_t size); 55 explicit Buffer(size_t size);
57 Buffer(size_t size, size_t capacity); 56 Buffer(size_t size, size_t capacity);
58 57
59 // Construct a buffer and copy the specified number of bytes into it. The 58 // Construct a buffer and copy the specified number of bytes into it. The
60 // source array may be (const) uint8_t*, int8_t*, or char*. 59 // source array may be (const) uint8_t*, int8_t*, or char*.
61 template <typename T, typename internal::ByteType<T>::t = 0> 60 template <typename T, typename internal::ByteType<T>::t = 0>
62 Buffer(const T* data, size_t size) 61 Buffer(const T* data, size_t size)
(...skipping 29 matching lines...) Expand all
92 size_t size() const { 91 size_t size() const {
93 RTC_DCHECK(IsConsistent()); 92 RTC_DCHECK(IsConsistent());
94 return size_; 93 return size_;
95 } 94 }
96 95
97 size_t capacity() const { 96 size_t capacity() const {
98 RTC_DCHECK(IsConsistent()); 97 RTC_DCHECK(IsConsistent());
99 return capacity_; 98 return capacity_;
100 } 99 }
101 100
102 Buffer& operator=(const Buffer& buf) {
103 if (&buf != this)
104 SetData(buf.data(), buf.size());
105 return *this;
106 }
107
108 Buffer& operator=(Buffer&& buf) { 101 Buffer& operator=(Buffer&& buf) {
109 RTC_DCHECK(IsConsistent()); 102 RTC_DCHECK(IsConsistent());
110 RTC_DCHECK(buf.IsConsistent()); 103 RTC_DCHECK(buf.IsConsistent());
111 size_ = buf.size_; 104 size_ = buf.size_;
112 capacity_ = buf.capacity_; 105 capacity_ = buf.capacity_;
113 data_ = std::move(buf.data_); 106 data_ = std::move(buf.data_);
114 buf.OnMovedFrom(); 107 buf.OnMovedFrom();
115 return *this; 108 return *this;
116 } 109 }
117 110
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
263 #else 256 #else
264 // Ensure that *this is always inconsistent, to provoke bugs. 257 // Ensure that *this is always inconsistent, to provoke bugs.
265 size_ = 1; 258 size_ = 1;
266 capacity_ = 0; 259 capacity_ = 0;
267 #endif 260 #endif
268 } 261 }
269 262
270 size_t size_; 263 size_t size_;
271 size_t capacity_; 264 size_t capacity_;
272 std::unique_ptr<uint8_t[]> data_; 265 std::unique_ptr<uint8_t[]> data_;
266
267 RTC_DISALLOW_COPY_AND_ASSIGN(Buffer);
273 }; 268 };
274 269
275 } // namespace rtc 270 } // namespace rtc
276 271
277 #endif // WEBRTC_BASE_BUFFER_H_ 272 #endif // WEBRTC_BASE_BUFFER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698