| OLD | NEW |
| 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 Loading... |
| 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 if (buffer_) { | 45 std::memcpy(buffer_->data(), data, size); |
| 46 std::memcpy(buffer_->data(), data, size); | |
| 47 } | |
| 48 } | 46 } |
| 49 | 47 |
| 50 // Construct a buffer from the contents of an array. | 48 // Construct a buffer from the contents of an array. |
| 51 template <typename T, size_t N, typename internal::ByteType<T>::t = 0> | 49 template <typename T, size_t N, typename internal::ByteType<T>::t = 0> |
| 52 CopyOnWriteBuffer(const T(&array)[N]) // NOLINT: runtime/explicit | 50 CopyOnWriteBuffer(const T(&array)[N]) // NOLINT: runtime/explicit |
| 53 : CopyOnWriteBuffer(array, N) {} | 51 : CopyOnWriteBuffer(array, N) {} |
| 54 | 52 |
| 55 ~CopyOnWriteBuffer(); | 53 ~CopyOnWriteBuffer(); |
| 56 | 54 |
| 57 // Get a pointer to the data. Just .data() will give you a (const) uint8_t*, | 55 // Get a pointer to the data. Just .data() will give you a (const) uint8_t*, |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 RTC_DCHECK(buf.IsConsistent()); | 116 RTC_DCHECK(buf.IsConsistent()); |
| 119 return buffer_.get() == buf.buffer_.get() || | 117 return buffer_.get() == buf.buffer_.get() || |
| 120 (buffer_.get() && buf.buffer_.get() && | 118 (buffer_.get() && buf.buffer_.get() && |
| 121 *buffer_.get() == *buf.buffer_.get()); | 119 *buffer_.get() == *buf.buffer_.get()); |
| 122 } | 120 } |
| 123 | 121 |
| 124 bool operator!=(const CopyOnWriteBuffer& buf) const { | 122 bool operator!=(const CopyOnWriteBuffer& buf) const { |
| 125 return !(*this == buf); | 123 return !(*this == buf); |
| 126 } | 124 } |
| 127 | 125 |
| 128 uint8_t& operator[](size_t index) { | |
| 129 RTC_DCHECK_LT(index, size()); | |
| 130 return data()[index]; | |
| 131 } | |
| 132 | |
| 133 uint8_t operator[](size_t index) const { | |
| 134 RTC_DCHECK_LT(index, size()); | |
| 135 return cdata()[index]; | |
| 136 } | |
| 137 | |
| 138 // Replace the contents of the buffer. Accepts the same types as the | 126 // Replace the contents of the buffer. Accepts the same types as the |
| 139 // constructors. | 127 // constructors. |
| 140 template <typename T, typename internal::ByteType<T>::t = 0> | 128 template <typename T, typename internal::ByteType<T>::t = 0> |
| 141 void SetData(const T* data, size_t size) { | 129 void SetData(const T* data, size_t size) { |
| 142 RTC_DCHECK(IsConsistent()); | 130 RTC_DCHECK(IsConsistent()); |
| 143 if (!buffer_ || !buffer_->HasOneRef()) { | 131 if (!buffer_ || !buffer_->HasOneRef()) { |
| 144 buffer_ = size > 0 ? new RefCountedObject<Buffer>(data, size) | 132 buffer_ = new RefCountedObject<Buffer>(data, size, size); |
| 145 : nullptr; | |
| 146 } else { | 133 } else { |
| 147 buffer_->SetData(data, size); | 134 buffer_->SetData(data, size); |
| 148 } | 135 } |
| 149 RTC_DCHECK(IsConsistent()); | 136 RTC_DCHECK(IsConsistent()); |
| 150 } | 137 } |
| 151 | 138 |
| 152 template <typename T, size_t N, typename internal::ByteType<T>::t = 0> | 139 template <typename T, size_t N, typename internal::ByteType<T>::t = 0> |
| 153 void SetData(const T(&array)[N]) { | 140 void SetData(const T(&array)[N]) { |
| 154 SetData(array, N); | 141 SetData(array, N); |
| 155 } | 142 } |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 260 return (!buffer_ || buffer_->capacity() > 0); | 247 return (!buffer_ || buffer_->capacity() > 0); |
| 261 } | 248 } |
| 262 | 249 |
| 263 // buffer_ is either null, or points to an rtc::Buffer with capacity > 0. | 250 // buffer_ is either null, or points to an rtc::Buffer with capacity > 0. |
| 264 scoped_refptr<RefCountedObject<Buffer>> buffer_; | 251 scoped_refptr<RefCountedObject<Buffer>> buffer_; |
| 265 }; | 252 }; |
| 266 | 253 |
| 267 } // namespace rtc | 254 } // namespace rtc |
| 268 | 255 |
| 269 #endif // WEBRTC_BASE_COPYONWRITEBUFFER_H_ | 256 #endif // WEBRTC_BASE_COPYONWRITEBUFFER_H_ |
| OLD | NEW |