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