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 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 return cdata()[index]; | 146 return cdata()[index]; |
147 } | 147 } |
148 | 148 |
149 // Replace the contents of the buffer. Accepts the same types as the | 149 // Replace the contents of the buffer. Accepts the same types as the |
150 // constructors. | 150 // constructors. |
151 template <typename T, | 151 template <typename T, |
152 typename std::enable_if< | 152 typename std::enable_if< |
153 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr> | 153 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr> |
154 void SetData(const T* data, size_t size) { | 154 void SetData(const T* data, size_t size) { |
155 RTC_DCHECK(IsConsistent()); | 155 RTC_DCHECK(IsConsistent()); |
156 if (!buffer_ || !buffer_->HasOneRef()) { | 156 if (!buffer_) { |
157 buffer_ = size > 0 ? new RefCountedObject<Buffer>(data, size) | 157 buffer_ = size > 0 ? new RefCountedObject<Buffer>(data, size) : nullptr; |
158 : nullptr; | 158 } else if (!buffer_->HasOneRef()) { |
| 159 buffer_ = new RefCountedObject<Buffer>(data, size, buffer_->capacity()); |
159 } else { | 160 } else { |
160 buffer_->SetData(data, size); | 161 buffer_->SetData(data, size); |
161 } | 162 } |
162 RTC_DCHECK(IsConsistent()); | 163 RTC_DCHECK(IsConsistent()); |
163 } | 164 } |
164 | 165 |
165 template <typename T, | 166 template <typename T, |
166 size_t N, | 167 size_t N, |
167 typename std::enable_if< | 168 typename std::enable_if< |
168 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr> | 169 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr> |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
246 return; | 247 return; |
247 } else if (capacity <= buffer_->capacity()) { | 248 } else if (capacity <= buffer_->capacity()) { |
248 return; | 249 return; |
249 } | 250 } |
250 | 251 |
251 CloneDataIfReferenced(std::max(buffer_->capacity(), capacity)); | 252 CloneDataIfReferenced(std::max(buffer_->capacity(), capacity)); |
252 buffer_->EnsureCapacity(capacity); | 253 buffer_->EnsureCapacity(capacity); |
253 RTC_DCHECK(IsConsistent()); | 254 RTC_DCHECK(IsConsistent()); |
254 } | 255 } |
255 | 256 |
256 // Resets the buffer to zero size and capacity. | 257 // Resets the buffer to zero size without altering capacity. Works even if the |
| 258 // buffer has been moved from. |
257 void Clear() { | 259 void Clear() { |
258 RTC_DCHECK(IsConsistent()); | 260 if (!buffer_) |
259 if (!buffer_ || !buffer_->HasOneRef()) { | 261 return; |
260 buffer_ = nullptr; | 262 |
| 263 if (buffer_->HasOneRef()) { |
| 264 buffer_->Clear(); |
261 } else { | 265 } else { |
262 buffer_->Clear(); | 266 buffer_ = new RefCountedObject<Buffer>(0, buffer_->capacity()); |
263 } | 267 } |
264 RTC_DCHECK(IsConsistent()); | 268 RTC_DCHECK(IsConsistent()); |
265 } | 269 } |
266 | 270 |
267 // Swaps two buffers. | 271 // Swaps two buffers. |
268 friend void swap(CopyOnWriteBuffer& a, CopyOnWriteBuffer& b) { | 272 friend void swap(CopyOnWriteBuffer& a, CopyOnWriteBuffer& b) { |
269 std::swap(a.buffer_, b.buffer_); | 273 std::swap(a.buffer_, b.buffer_); |
270 } | 274 } |
271 | 275 |
272 private: | 276 private: |
(...skipping 14 matching lines...) Expand all Loading... |
287 return (!buffer_ || buffer_->capacity() > 0); | 291 return (!buffer_ || buffer_->capacity() > 0); |
288 } | 292 } |
289 | 293 |
290 // buffer_ is either null, or points to an rtc::Buffer with capacity > 0. | 294 // buffer_ is either null, or points to an rtc::Buffer with capacity > 0. |
291 scoped_refptr<RefCountedObject<Buffer>> buffer_; | 295 scoped_refptr<RefCountedObject<Buffer>> buffer_; |
292 }; | 296 }; |
293 | 297 |
294 } // namespace rtc | 298 } // namespace rtc |
295 | 299 |
296 #endif // WEBRTC_BASE_COPYONWRITEBUFFER_H_ | 300 #endif // WEBRTC_BASE_COPYONWRITEBUFFER_H_ |
OLD | NEW |