jbauch | 13041cf | 2016-02-25 06:16:52 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "rtc_base/copyonwritebuffer.h" |
jbauch | 13041cf | 2016-02-25 06:16:52 -0800 | [diff] [blame] | 12 | |
| 13 | namespace rtc { |
| 14 | |
| 15 | CopyOnWriteBuffer::CopyOnWriteBuffer() { |
| 16 | RTC_DCHECK(IsConsistent()); |
| 17 | } |
| 18 | |
| 19 | CopyOnWriteBuffer::CopyOnWriteBuffer(const CopyOnWriteBuffer& buf) |
| 20 | : buffer_(buf.buffer_) { |
| 21 | } |
| 22 | |
danilchap | d8a9c53 | 2016-07-30 12:39:26 -0700 | [diff] [blame] | 23 | CopyOnWriteBuffer::CopyOnWriteBuffer(CopyOnWriteBuffer&& buf) |
| 24 | : buffer_(std::move(buf.buffer_)) { |
jbauch | 13041cf | 2016-02-25 06:16:52 -0800 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | CopyOnWriteBuffer::CopyOnWriteBuffer(size_t size) |
| 28 | : buffer_(size > 0 ? new RefCountedObject<Buffer>(size) : nullptr) { |
| 29 | RTC_DCHECK(IsConsistent()); |
| 30 | } |
| 31 | |
| 32 | CopyOnWriteBuffer::CopyOnWriteBuffer(size_t size, size_t capacity) |
| 33 | : buffer_(size > 0 || capacity > 0 |
| 34 | ? new RefCountedObject<Buffer>(size, capacity) |
| 35 | : nullptr) { |
| 36 | RTC_DCHECK(IsConsistent()); |
| 37 | } |
| 38 | |
| 39 | CopyOnWriteBuffer::~CopyOnWriteBuffer() = default; |
| 40 | |
Danil Chapovalov | 3626d7e | 2016-09-14 17:14:30 +0200 | [diff] [blame] | 41 | bool CopyOnWriteBuffer::operator==(const CopyOnWriteBuffer& buf) const { |
| 42 | // Must either use the same buffer internally or have the same contents. |
| 43 | RTC_DCHECK(IsConsistent()); |
| 44 | RTC_DCHECK(buf.IsConsistent()); |
| 45 | return buffer_.get() == buf.buffer_.get() || |
| 46 | (buffer_.get() && buf.buffer_.get() && |
| 47 | *buffer_.get() == *buf.buffer_.get()); |
| 48 | } |
| 49 | |
| 50 | void CopyOnWriteBuffer::SetSize(size_t size) { |
| 51 | RTC_DCHECK(IsConsistent()); |
| 52 | if (!buffer_) { |
| 53 | if (size > 0) { |
| 54 | buffer_ = new RefCountedObject<Buffer>(size); |
| 55 | } |
| 56 | RTC_DCHECK(IsConsistent()); |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | // Clone data if referenced. |
| 61 | if (!buffer_->HasOneRef()) { |
| 62 | buffer_ = new RefCountedObject<Buffer>( |
| 63 | buffer_->data(), |
| 64 | std::min(buffer_->size(), size), |
| 65 | std::max(buffer_->capacity(), size)); |
| 66 | } |
| 67 | buffer_->SetSize(size); |
| 68 | RTC_DCHECK(IsConsistent()); |
| 69 | } |
| 70 | |
| 71 | void CopyOnWriteBuffer::EnsureCapacity(size_t capacity) { |
| 72 | RTC_DCHECK(IsConsistent()); |
| 73 | if (!buffer_) { |
| 74 | if (capacity > 0) { |
| 75 | buffer_ = new RefCountedObject<Buffer>(0, capacity); |
| 76 | } |
| 77 | RTC_DCHECK(IsConsistent()); |
| 78 | return; |
| 79 | } else if (capacity <= buffer_->capacity()) { |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | CloneDataIfReferenced(std::max(buffer_->capacity(), capacity)); |
| 84 | buffer_->EnsureCapacity(capacity); |
| 85 | RTC_DCHECK(IsConsistent()); |
| 86 | } |
| 87 | |
| 88 | void CopyOnWriteBuffer::Clear() { |
| 89 | if (!buffer_) |
| 90 | return; |
| 91 | |
| 92 | if (buffer_->HasOneRef()) { |
| 93 | buffer_->Clear(); |
| 94 | } else { |
| 95 | buffer_ = new RefCountedObject<Buffer>(0, buffer_->capacity()); |
| 96 | } |
| 97 | RTC_DCHECK(IsConsistent()); |
| 98 | } |
| 99 | |
| 100 | void CopyOnWriteBuffer::CloneDataIfReferenced(size_t new_capacity) { |
| 101 | if (buffer_->HasOneRef()) { |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | buffer_ = new RefCountedObject<Buffer>(buffer_->data(), buffer_->size(), |
| 106 | new_capacity); |
| 107 | RTC_DCHECK(IsConsistent()); |
| 108 | } |
| 109 | |
| 110 | |
| 111 | |
jbauch | 13041cf | 2016-02-25 06:16:52 -0800 | [diff] [blame] | 112 | } // namespace rtc |