Chromium Code Reviews| 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>( | |
| 160 data, size, std::max(size, buffer_->capacity())); | |
|
kwiberg-webrtc
2016/09/13 09:24:30
I don't think you need the std::max---the Buffer c
danilchap
2016/09/13 09:56:36
Done.
It is nicer without extra max.
| |
| 159 } else { | 161 } else { |
| 160 buffer_->SetData(data, size); | 162 buffer_->SetData(data, size); |
| 161 } | 163 } |
| 162 RTC_DCHECK(IsConsistent()); | 164 RTC_DCHECK(IsConsistent()); |
| 163 } | 165 } |
| 164 | 166 |
| 165 template <typename T, | 167 template <typename T, |
| 166 size_t N, | 168 size_t N, |
| 167 typename std::enable_if< | 169 typename std::enable_if< |
| 168 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr> | 170 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr> |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 246 return; | 248 return; |
| 247 } else if (capacity <= buffer_->capacity()) { | 249 } else if (capacity <= buffer_->capacity()) { |
| 248 return; | 250 return; |
| 249 } | 251 } |
| 250 | 252 |
| 251 CloneDataIfReferenced(std::max(buffer_->capacity(), capacity)); | 253 CloneDataIfReferenced(std::max(buffer_->capacity(), capacity)); |
| 252 buffer_->EnsureCapacity(capacity); | 254 buffer_->EnsureCapacity(capacity); |
| 253 RTC_DCHECK(IsConsistent()); | 255 RTC_DCHECK(IsConsistent()); |
| 254 } | 256 } |
| 255 | 257 |
| 256 // Resets the buffer to zero size and capacity. | 258 // Resets the buffer to zero size without altering capacity. Works even if the |
| 259 // buffer has been moved from. | |
| 257 void Clear() { | 260 void Clear() { |
| 258 RTC_DCHECK(IsConsistent()); | 261 if (!buffer_) |
| 259 if (!buffer_ || !buffer_->HasOneRef()) { | 262 return; |
| 260 buffer_ = nullptr; | 263 |
| 264 if (!buffer_->HasOneRef()) { | |
|
kwiberg-webrtc
2016/09/13 09:24:30
You can eliminate the negation if you swap the if
danilchap
2016/09/13 09:56:36
Done.
| |
| 265 buffer_ = new RefCountedObject<Buffer>(0, buffer_->capacity()); | |
| 261 } else { | 266 } else { |
| 262 buffer_->Clear(); | 267 buffer_->Clear(); |
| 263 } | 268 } |
| 264 RTC_DCHECK(IsConsistent()); | 269 RTC_DCHECK(IsConsistent()); |
| 265 } | 270 } |
| 266 | 271 |
| 267 // Swaps two buffers. | 272 // Swaps two buffers. |
| 268 friend void swap(CopyOnWriteBuffer& a, CopyOnWriteBuffer& b) { | 273 friend void swap(CopyOnWriteBuffer& a, CopyOnWriteBuffer& b) { |
| 269 std::swap(a.buffer_, b.buffer_); | 274 std::swap(a.buffer_, b.buffer_); |
| 270 } | 275 } |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 287 return (!buffer_ || buffer_->capacity() > 0); | 292 return (!buffer_ || buffer_->capacity() > 0); |
| 288 } | 293 } |
| 289 | 294 |
| 290 // buffer_ is either null, or points to an rtc::Buffer with capacity > 0. | 295 // buffer_ is either null, or points to an rtc::Buffer with capacity > 0. |
| 291 scoped_refptr<RefCountedObject<Buffer>> buffer_; | 296 scoped_refptr<RefCountedObject<Buffer>> buffer_; |
| 292 }; | 297 }; |
| 293 | 298 |
| 294 } // namespace rtc | 299 } // namespace rtc |
| 295 | 300 |
| 296 #endif // WEBRTC_BASE_COPYONWRITEBUFFER_H_ | 301 #endif // WEBRTC_BASE_COPYONWRITEBUFFER_H_ |
| OLD | NEW |