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 | |
| 11 | #ifndef WEBRTC_BASE_COPYONWRITEBUFFER_H_ |
| 12 | #define WEBRTC_BASE_COPYONWRITEBUFFER_H_ |
| 13 | |
| 14 | #include <algorithm> |
| 15 | #include <utility> |
| 16 | |
| 17 | #include "webrtc/base/buffer.h" |
| 18 | #include "webrtc/base/checks.h" |
| 19 | #include "webrtc/base/refcount.h" |
| 20 | #include "webrtc/base/scoped_ref_ptr.h" |
| 21 | |
| 22 | namespace rtc { |
| 23 | |
| 24 | class CopyOnWriteBuffer { |
| 25 | public: |
| 26 | // An empty buffer. |
| 27 | CopyOnWriteBuffer(); |
| 28 | // Copy size and contents of an existing buffer. |
| 29 | CopyOnWriteBuffer(const CopyOnWriteBuffer& buf); |
| 30 | // Move contents from an existing buffer. |
| 31 | CopyOnWriteBuffer(CopyOnWriteBuffer&& buf); |
| 32 | |
| 33 | // Construct a buffer with the specified number of uninitialized bytes. |
| 34 | explicit CopyOnWriteBuffer(size_t size); |
| 35 | CopyOnWriteBuffer(size_t size, size_t capacity); |
| 36 | |
| 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*. |
| 39 | template <typename T, typename internal::ByteType<T>::t = 0> |
| 40 | CopyOnWriteBuffer(const T* data, size_t size) |
| 41 | : CopyOnWriteBuffer(data, size, size) {} |
| 42 | template <typename T, typename internal::ByteType<T>::t = 0> |
| 43 | CopyOnWriteBuffer(const T* data, size_t size, size_t capacity) |
| 44 | : CopyOnWriteBuffer(size, capacity) { |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame^] | 45 | if (buffer_) { |
| 46 | std::memcpy(buffer_->data(), data, size); |
| 47 | } |
jbauch | 13041cf | 2016-02-25 06:16:52 -0800 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | // Construct a buffer from the contents of an array. |
| 51 | template <typename T, size_t N, typename internal::ByteType<T>::t = 0> |
| 52 | CopyOnWriteBuffer(const T(&array)[N]) // NOLINT: runtime/explicit |
| 53 | : CopyOnWriteBuffer(array, N) {} |
| 54 | |
| 55 | ~CopyOnWriteBuffer(); |
| 56 | |
| 57 | // Get a pointer to the data. Just .data() will give you a (const) uint8_t*, |
| 58 | // but you may also use .data<int8_t>() and .data<char>(). |
| 59 | template <typename T = uint8_t, typename internal::ByteType<T>::t = 0> |
| 60 | const T* data() const { |
| 61 | return cdata<T>(); |
| 62 | } |
| 63 | |
| 64 | // Get writable pointer to the data. This will create a copy of the underlying |
| 65 | // data if it is shared with other buffers. |
| 66 | template <typename T = uint8_t, typename internal::ByteType<T>::t = 0> |
| 67 | T* data() { |
| 68 | RTC_DCHECK(IsConsistent()); |
| 69 | if (!buffer_) { |
| 70 | return nullptr; |
| 71 | } |
| 72 | CloneDataIfReferenced(buffer_->capacity()); |
| 73 | return buffer_->data<T>(); |
| 74 | } |
| 75 | |
| 76 | // Get const pointer to the data. This will not create a copy of the |
| 77 | // underlying data if it is shared with other buffers. |
| 78 | template <typename T = uint8_t, typename internal::ByteType<T>::t = 0> |
| 79 | T* cdata() const { |
| 80 | RTC_DCHECK(IsConsistent()); |
| 81 | if (!buffer_) { |
| 82 | return nullptr; |
| 83 | } |
| 84 | return buffer_->data<T>(); |
| 85 | } |
| 86 | |
| 87 | size_t size() const { |
| 88 | RTC_DCHECK(IsConsistent()); |
| 89 | return buffer_ ? buffer_->size() : 0; |
| 90 | } |
| 91 | |
| 92 | size_t capacity() const { |
| 93 | RTC_DCHECK(IsConsistent()); |
| 94 | return buffer_ ? buffer_->capacity() : 0; |
| 95 | } |
| 96 | |
| 97 | CopyOnWriteBuffer& operator=(const CopyOnWriteBuffer& buf) { |
| 98 | RTC_DCHECK(IsConsistent()); |
| 99 | RTC_DCHECK(buf.IsConsistent()); |
| 100 | if (&buf != this) { |
| 101 | buffer_ = buf.buffer_; |
| 102 | } |
| 103 | return *this; |
| 104 | } |
| 105 | |
| 106 | CopyOnWriteBuffer& operator=(CopyOnWriteBuffer&& buf) { |
| 107 | RTC_DCHECK(IsConsistent()); |
| 108 | RTC_DCHECK(buf.IsConsistent()); |
| 109 | // TODO(jbauch): use std::move once scoped_refptr supports it (issue 5556) |
| 110 | buffer_.swap(buf.buffer_); |
| 111 | buf.buffer_ = nullptr; |
| 112 | return *this; |
| 113 | } |
| 114 | |
| 115 | bool operator==(const CopyOnWriteBuffer& buf) const { |
| 116 | // Must either use the same buffer internally or have the same contents. |
| 117 | RTC_DCHECK(IsConsistent()); |
| 118 | RTC_DCHECK(buf.IsConsistent()); |
| 119 | return buffer_.get() == buf.buffer_.get() || |
| 120 | (buffer_.get() && buf.buffer_.get() && |
| 121 | *buffer_.get() == *buf.buffer_.get()); |
| 122 | } |
| 123 | |
| 124 | bool operator!=(const CopyOnWriteBuffer& buf) const { |
| 125 | return !(*this == buf); |
| 126 | } |
| 127 | |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame^] | 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 | |
jbauch | 13041cf | 2016-02-25 06:16:52 -0800 | [diff] [blame] | 138 | // Replace the contents of the buffer. Accepts the same types as the |
| 139 | // constructors. |
| 140 | template <typename T, typename internal::ByteType<T>::t = 0> |
| 141 | void SetData(const T* data, size_t size) { |
| 142 | RTC_DCHECK(IsConsistent()); |
| 143 | if (!buffer_ || !buffer_->HasOneRef()) { |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame^] | 144 | buffer_ = size > 0 ? new RefCountedObject<Buffer>(data, size) |
| 145 | : nullptr; |
jbauch | 13041cf | 2016-02-25 06:16:52 -0800 | [diff] [blame] | 146 | } else { |
| 147 | buffer_->SetData(data, size); |
| 148 | } |
| 149 | RTC_DCHECK(IsConsistent()); |
| 150 | } |
| 151 | |
| 152 | template <typename T, size_t N, typename internal::ByteType<T>::t = 0> |
| 153 | void SetData(const T(&array)[N]) { |
| 154 | SetData(array, N); |
| 155 | } |
| 156 | |
| 157 | void SetData(const CopyOnWriteBuffer& buf) { |
| 158 | RTC_DCHECK(IsConsistent()); |
| 159 | RTC_DCHECK(buf.IsConsistent()); |
| 160 | if (&buf != this) { |
| 161 | buffer_ = buf.buffer_; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | // Append data to the buffer. Accepts the same types as the constructors. |
| 166 | template <typename T, typename internal::ByteType<T>::t = 0> |
| 167 | void AppendData(const T* data, size_t size) { |
| 168 | RTC_DCHECK(IsConsistent()); |
| 169 | if (!buffer_) { |
| 170 | buffer_ = new RefCountedObject<Buffer>(data, size); |
| 171 | RTC_DCHECK(IsConsistent()); |
| 172 | return; |
| 173 | } |
| 174 | |
| 175 | CloneDataIfReferenced(std::max(buffer_->capacity(), |
| 176 | buffer_->size() + size)); |
| 177 | buffer_->AppendData(data, size); |
| 178 | RTC_DCHECK(IsConsistent()); |
| 179 | } |
| 180 | |
| 181 | template <typename T, size_t N, typename internal::ByteType<T>::t = 0> |
| 182 | void AppendData(const T(&array)[N]) { |
| 183 | AppendData(array, N); |
| 184 | } |
| 185 | |
| 186 | void AppendData(const CopyOnWriteBuffer& buf) { |
| 187 | AppendData(buf.data(), buf.size()); |
| 188 | } |
| 189 | |
| 190 | // Sets the size of the buffer. If the new size is smaller than the old, the |
| 191 | // buffer contents will be kept but truncated; if the new size is greater, |
| 192 | // the existing contents will be kept and the new space will be |
| 193 | // uninitialized. |
| 194 | void SetSize(size_t size) { |
| 195 | RTC_DCHECK(IsConsistent()); |
| 196 | if (!buffer_) { |
| 197 | if (size > 0) { |
| 198 | buffer_ = new RefCountedObject<Buffer>(size); |
| 199 | } |
| 200 | RTC_DCHECK(IsConsistent()); |
| 201 | return; |
| 202 | } |
| 203 | |
| 204 | CloneDataIfReferenced(std::max(buffer_->capacity(), size)); |
| 205 | buffer_->SetSize(size); |
| 206 | RTC_DCHECK(IsConsistent()); |
| 207 | } |
| 208 | |
| 209 | // Ensure that the buffer size can be increased to at least capacity without |
| 210 | // further reallocation. (Of course, this operation might need to reallocate |
| 211 | // the buffer.) |
| 212 | void EnsureCapacity(size_t capacity) { |
| 213 | RTC_DCHECK(IsConsistent()); |
| 214 | if (!buffer_) { |
| 215 | if (capacity > 0) { |
| 216 | buffer_ = new RefCountedObject<Buffer>(0, capacity); |
| 217 | } |
| 218 | RTC_DCHECK(IsConsistent()); |
| 219 | return; |
| 220 | } else if (capacity <= buffer_->capacity()) { |
| 221 | return; |
| 222 | } |
| 223 | |
| 224 | CloneDataIfReferenced(std::max(buffer_->capacity(), capacity)); |
| 225 | buffer_->EnsureCapacity(capacity); |
| 226 | RTC_DCHECK(IsConsistent()); |
| 227 | } |
| 228 | |
| 229 | // Resets the buffer to zero size and capacity. |
| 230 | void Clear() { |
| 231 | RTC_DCHECK(IsConsistent()); |
| 232 | if (!buffer_ || !buffer_->HasOneRef()) { |
| 233 | buffer_ = nullptr; |
| 234 | } else { |
| 235 | buffer_->Clear(); |
| 236 | } |
| 237 | RTC_DCHECK(IsConsistent()); |
| 238 | } |
| 239 | |
| 240 | // Swaps two buffers. |
| 241 | friend void swap(CopyOnWriteBuffer& a, CopyOnWriteBuffer& b) { |
| 242 | std::swap(a.buffer_, b.buffer_); |
| 243 | } |
| 244 | |
| 245 | private: |
| 246 | // Create a copy of the underlying data if it is referenced from other Buffer |
| 247 | // objects. |
| 248 | void CloneDataIfReferenced(size_t new_capacity) { |
| 249 | if (buffer_->HasOneRef()) { |
| 250 | return; |
| 251 | } |
| 252 | |
| 253 | buffer_ = new RefCountedObject<Buffer>(buffer_->data(), buffer_->size(), |
| 254 | new_capacity); |
| 255 | RTC_DCHECK(IsConsistent()); |
| 256 | } |
| 257 | |
| 258 | // Pre- and postcondition of all methods. |
| 259 | bool IsConsistent() const { |
| 260 | return (!buffer_ || buffer_->capacity() > 0); |
| 261 | } |
| 262 | |
| 263 | // buffer_ is either null, or points to an rtc::Buffer with capacity > 0. |
| 264 | scoped_refptr<RefCountedObject<Buffer>> buffer_; |
| 265 | }; |
| 266 | |
| 267 | } // namespace rtc |
| 268 | |
| 269 | #endif // WEBRTC_BASE_COPYONWRITEBUFFER_H_ |