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 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 (!buffer_ || !buffer_->HasOneRef()) { |
132 buffer_ = new RefCountedObject<Buffer>(data, size, size); | 134 buffer_ = size > 0 ? new RefCountedObject<Buffer>(data, size) |
| 135 : nullptr; |
133 } else { | 136 } else { |
134 buffer_->SetData(data, size); | 137 buffer_->SetData(data, size); |
135 } | 138 } |
136 RTC_DCHECK(IsConsistent()); | 139 RTC_DCHECK(IsConsistent()); |
137 } | 140 } |
138 | 141 |
139 template <typename T, size_t N, typename internal::ByteType<T>::t = 0> | 142 template <typename T, size_t N, typename internal::ByteType<T>::t = 0> |
140 void SetData(const T(&array)[N]) { | 143 void SetData(const T(&array)[N]) { |
141 SetData(array, N); | 144 SetData(array, N); |
142 } | 145 } |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
247 return (!buffer_ || buffer_->capacity() > 0); | 250 return (!buffer_ || buffer_->capacity() > 0); |
248 } | 251 } |
249 | 252 |
250 // buffer_ is either null, or points to an rtc::Buffer with capacity > 0. | 253 // buffer_ is either null, or points to an rtc::Buffer with capacity > 0. |
251 scoped_refptr<RefCountedObject<Buffer>> buffer_; | 254 scoped_refptr<RefCountedObject<Buffer>> buffer_; |
252 }; | 255 }; |
253 | 256 |
254 } // namespace rtc | 257 } // namespace rtc |
255 | 258 |
256 #endif // WEBRTC_BASE_COPYONWRITEBUFFER_H_ | 259 #endif // WEBRTC_BASE_COPYONWRITEBUFFER_H_ |
OLD | NEW |