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 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #ifndef RTC_BASE_COPY_ON_WRITE_BUFFER_H_ |
| 12 | #define RTC_BASE_COPY_ON_WRITE_BUFFER_H_ |
jbauch | 13041cf | 2016-02-25 06:16:52 -0800 | [diff] [blame] | 13 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 14 | #include <stdint.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 15 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 16 | #include <algorithm> |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 17 | #include <cstring> |
| 18 | #include <string> |
| 19 | #include <type_traits> |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 20 | #include <utility> |
jbauch | 13041cf | 2016-02-25 06:16:52 -0800 | [diff] [blame] | 21 | |
Mirko Bonadei | d970807 | 2019-01-25 20:26:48 +0100 | [diff] [blame] | 22 | #include "api/scoped_refptr.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 23 | #include "rtc_base/buffer.h" |
| 24 | #include "rtc_base/checks.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 25 | #include "rtc_base/ref_counted_object.h" |
Mirko Bonadei | 35214fc | 2019-09-23 14:54:28 +0200 | [diff] [blame] | 26 | #include "rtc_base/system/rtc_export.h" |
philipel | afc2377 | 2021-09-20 11:22:13 +0200 | [diff] [blame] | 27 | #include "rtc_base/type_traits.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 28 | |
| 29 | namespace rtc { |
| 30 | |
Mirko Bonadei | 35214fc | 2019-09-23 14:54:28 +0200 | [diff] [blame] | 31 | class RTC_EXPORT CopyOnWriteBuffer { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 32 | public: |
| 33 | // An empty buffer. |
| 34 | CopyOnWriteBuffer(); |
Amit Hilbuch | 45a2cd2 | 2019-03-12 16:58:02 -0700 | [diff] [blame] | 35 | // Share the data with an existing buffer. |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 36 | CopyOnWriteBuffer(const CopyOnWriteBuffer& buf); |
| 37 | // Move contents from an existing buffer. |
| 38 | CopyOnWriteBuffer(CopyOnWriteBuffer&& buf); |
| 39 | |
Jeroen de Borst | 4f6d233 | 2018-07-18 11:25:12 -0700 | [diff] [blame] | 40 | // Construct a buffer from a string, convenient for unittests. |
| 41 | CopyOnWriteBuffer(const std::string& s); |
| 42 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 43 | // Construct a buffer with the specified number of uninitialized bytes. |
| 44 | explicit CopyOnWriteBuffer(size_t size); |
| 45 | CopyOnWriteBuffer(size_t size, size_t capacity); |
| 46 | |
| 47 | // Construct a buffer and copy the specified number of bytes into it. The |
| 48 | // source array may be (const) uint8_t*, int8_t*, or char*. |
| 49 | template <typename T, |
| 50 | typename std::enable_if< |
| 51 | internal::BufferCompat<uint8_t, T>::value>::type* = nullptr> |
| 52 | CopyOnWriteBuffer(const T* data, size_t size) |
| 53 | : CopyOnWriteBuffer(data, size, size) {} |
| 54 | template <typename T, |
| 55 | typename std::enable_if< |
| 56 | internal::BufferCompat<uint8_t, T>::value>::type* = nullptr> |
| 57 | CopyOnWriteBuffer(const T* data, size_t size, size_t capacity) |
| 58 | : CopyOnWriteBuffer(size, capacity) { |
| 59 | if (buffer_) { |
| 60 | std::memcpy(buffer_->data(), data, size); |
Ilya Nikolaevskiy | 741bab0 | 2019-09-25 14:37:10 +0200 | [diff] [blame] | 61 | offset_ = 0; |
| 62 | size_ = size; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
| 66 | // Construct a buffer from the contents of an array. |
| 67 | template <typename T, |
| 68 | size_t N, |
| 69 | typename std::enable_if< |
| 70 | internal::BufferCompat<uint8_t, T>::value>::type* = nullptr> |
| 71 | CopyOnWriteBuffer(const T (&array)[N]) // NOLINT: runtime/explicit |
| 72 | : CopyOnWriteBuffer(array, N) {} |
| 73 | |
philipel | afc2377 | 2021-09-20 11:22:13 +0200 | [diff] [blame] | 74 | // Construct a buffer from a vector like type. |
| 75 | template <typename VecT, |
| 76 | typename ElemT = typename std::remove_pointer_t< |
| 77 | decltype(std::declval<VecT>().data())>, |
| 78 | typename std::enable_if_t< |
| 79 | !std::is_same<VecT, CopyOnWriteBuffer>::value && |
| 80 | HasDataAndSize<VecT, ElemT>::value && |
| 81 | internal::BufferCompat<uint8_t, ElemT>::value>* = nullptr> |
| 82 | explicit CopyOnWriteBuffer(const VecT& v) |
| 83 | : CopyOnWriteBuffer(v.data(), v.size()) {} |
| 84 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 85 | ~CopyOnWriteBuffer(); |
| 86 | |
| 87 | // Get a pointer to the data. Just .data() will give you a (const) uint8_t*, |
| 88 | // but you may also use .data<int8_t>() and .data<char>(). |
| 89 | template <typename T = uint8_t, |
| 90 | typename std::enable_if< |
| 91 | internal::BufferCompat<uint8_t, T>::value>::type* = nullptr> |
| 92 | const T* data() const { |
| 93 | return cdata<T>(); |
| 94 | } |
| 95 | |
| 96 | // Get writable pointer to the data. This will create a copy of the underlying |
| 97 | // data if it is shared with other buffers. |
| 98 | template <typename T = uint8_t, |
| 99 | typename std::enable_if< |
| 100 | internal::BufferCompat<uint8_t, T>::value>::type* = nullptr> |
Danil Chapovalov | 82e1875 | 2021-01-07 13:38:48 +0100 | [diff] [blame] | 101 | T* MutableData() { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 102 | RTC_DCHECK(IsConsistent()); |
| 103 | if (!buffer_) { |
| 104 | return nullptr; |
| 105 | } |
Ilya Nikolaevskiy | 741bab0 | 2019-09-25 14:37:10 +0200 | [diff] [blame] | 106 | UnshareAndEnsureCapacity(capacity()); |
| 107 | return buffer_->data<T>() + offset_; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | // Get const pointer to the data. This will not create a copy of the |
| 111 | // underlying data if it is shared with other buffers. |
| 112 | template <typename T = uint8_t, |
| 113 | typename std::enable_if< |
| 114 | internal::BufferCompat<uint8_t, T>::value>::type* = nullptr> |
| 115 | const T* cdata() const { |
| 116 | RTC_DCHECK(IsConsistent()); |
| 117 | if (!buffer_) { |
| 118 | return nullptr; |
| 119 | } |
Ilya Nikolaevskiy | 741bab0 | 2019-09-25 14:37:10 +0200 | [diff] [blame] | 120 | return buffer_->data<T>() + offset_; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | size_t size() const { |
| 124 | RTC_DCHECK(IsConsistent()); |
Ilya Nikolaevskiy | 741bab0 | 2019-09-25 14:37:10 +0200 | [diff] [blame] | 125 | return size_; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | size_t capacity() const { |
| 129 | RTC_DCHECK(IsConsistent()); |
Ilya Nikolaevskiy | 741bab0 | 2019-09-25 14:37:10 +0200 | [diff] [blame] | 130 | return buffer_ ? buffer_->capacity() - offset_ : 0; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | CopyOnWriteBuffer& operator=(const CopyOnWriteBuffer& buf) { |
| 134 | RTC_DCHECK(IsConsistent()); |
| 135 | RTC_DCHECK(buf.IsConsistent()); |
| 136 | if (&buf != this) { |
| 137 | buffer_ = buf.buffer_; |
Ilya Nikolaevskiy | 741bab0 | 2019-09-25 14:37:10 +0200 | [diff] [blame] | 138 | offset_ = buf.offset_; |
| 139 | size_ = buf.size_; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 140 | } |
| 141 | return *this; |
| 142 | } |
| 143 | |
| 144 | CopyOnWriteBuffer& operator=(CopyOnWriteBuffer&& buf) { |
| 145 | RTC_DCHECK(IsConsistent()); |
| 146 | RTC_DCHECK(buf.IsConsistent()); |
| 147 | buffer_ = std::move(buf.buffer_); |
Ilya Nikolaevskiy | 741bab0 | 2019-09-25 14:37:10 +0200 | [diff] [blame] | 148 | offset_ = buf.offset_; |
| 149 | size_ = buf.size_; |
| 150 | buf.offset_ = 0; |
| 151 | buf.size_ = 0; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 152 | return *this; |
| 153 | } |
| 154 | |
| 155 | bool operator==(const CopyOnWriteBuffer& buf) const; |
| 156 | |
| 157 | bool operator!=(const CopyOnWriteBuffer& buf) const { |
| 158 | return !(*this == buf); |
| 159 | } |
| 160 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 161 | uint8_t operator[](size_t index) const { |
| 162 | RTC_DCHECK_LT(index, size()); |
| 163 | return cdata()[index]; |
| 164 | } |
| 165 | |
| 166 | // Replace the contents of the buffer. Accepts the same types as the |
| 167 | // constructors. |
| 168 | template <typename T, |
| 169 | typename std::enable_if< |
| 170 | internal::BufferCompat<uint8_t, T>::value>::type* = nullptr> |
| 171 | void SetData(const T* data, size_t size) { |
| 172 | RTC_DCHECK(IsConsistent()); |
| 173 | if (!buffer_) { |
Danil Chapovalov | 8df643b | 2021-01-22 16:11:10 +0100 | [diff] [blame] | 174 | buffer_ = size > 0 ? new RefCountedBuffer(data, size) : nullptr; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 175 | } else if (!buffer_->HasOneRef()) { |
Danil Chapovalov | 8df643b | 2021-01-22 16:11:10 +0100 | [diff] [blame] | 176 | buffer_ = new RefCountedBuffer(data, size, capacity()); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 177 | } else { |
| 178 | buffer_->SetData(data, size); |
| 179 | } |
Ilya Nikolaevskiy | 741bab0 | 2019-09-25 14:37:10 +0200 | [diff] [blame] | 180 | offset_ = 0; |
| 181 | size_ = size; |
| 182 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 183 | RTC_DCHECK(IsConsistent()); |
| 184 | } |
| 185 | |
| 186 | template <typename T, |
| 187 | size_t N, |
| 188 | typename std::enable_if< |
| 189 | internal::BufferCompat<uint8_t, T>::value>::type* = nullptr> |
| 190 | void SetData(const T (&array)[N]) { |
| 191 | SetData(array, N); |
| 192 | } |
| 193 | |
| 194 | void SetData(const CopyOnWriteBuffer& buf) { |
| 195 | RTC_DCHECK(IsConsistent()); |
| 196 | RTC_DCHECK(buf.IsConsistent()); |
| 197 | if (&buf != this) { |
| 198 | buffer_ = buf.buffer_; |
Ilya Nikolaevskiy | 741bab0 | 2019-09-25 14:37:10 +0200 | [diff] [blame] | 199 | offset_ = buf.offset_; |
| 200 | size_ = buf.size_; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | |
| 204 | // Append data to the buffer. Accepts the same types as the constructors. |
| 205 | template <typename T, |
| 206 | typename std::enable_if< |
| 207 | internal::BufferCompat<uint8_t, T>::value>::type* = nullptr> |
| 208 | void AppendData(const T* data, size_t size) { |
| 209 | RTC_DCHECK(IsConsistent()); |
| 210 | if (!buffer_) { |
Danil Chapovalov | 8df643b | 2021-01-22 16:11:10 +0100 | [diff] [blame] | 211 | buffer_ = new RefCountedBuffer(data, size); |
Ilya Nikolaevskiy | 741bab0 | 2019-09-25 14:37:10 +0200 | [diff] [blame] | 212 | offset_ = 0; |
| 213 | size_ = size; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 214 | RTC_DCHECK(IsConsistent()); |
| 215 | return; |
| 216 | } |
| 217 | |
Ilya Nikolaevskiy | 741bab0 | 2019-09-25 14:37:10 +0200 | [diff] [blame] | 218 | UnshareAndEnsureCapacity(std::max(capacity(), size_ + size)); |
| 219 | |
| 220 | buffer_->SetSize(offset_ + |
| 221 | size_); // Remove data to the right of the slice. |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 222 | buffer_->AppendData(data, size); |
Ilya Nikolaevskiy | 741bab0 | 2019-09-25 14:37:10 +0200 | [diff] [blame] | 223 | size_ += size; |
| 224 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 225 | RTC_DCHECK(IsConsistent()); |
| 226 | } |
| 227 | |
| 228 | template <typename T, |
| 229 | size_t N, |
| 230 | typename std::enable_if< |
| 231 | internal::BufferCompat<uint8_t, T>::value>::type* = nullptr> |
| 232 | void AppendData(const T (&array)[N]) { |
| 233 | AppendData(array, N); |
| 234 | } |
| 235 | |
philipel | afc2377 | 2021-09-20 11:22:13 +0200 | [diff] [blame] | 236 | template <typename VecT, |
| 237 | typename ElemT = typename std::remove_pointer_t< |
| 238 | decltype(std::declval<VecT>().data())>, |
| 239 | typename std::enable_if_t< |
| 240 | HasDataAndSize<VecT, ElemT>::value && |
| 241 | internal::BufferCompat<uint8_t, ElemT>::value>* = nullptr> |
| 242 | void AppendData(const VecT& v) { |
| 243 | AppendData(v.data(), v.size()); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | // Sets the size of the buffer. If the new size is smaller than the old, the |
| 247 | // buffer contents will be kept but truncated; if the new size is greater, |
| 248 | // the existing contents will be kept and the new space will be |
| 249 | // uninitialized. |
| 250 | void SetSize(size_t size); |
| 251 | |
| 252 | // Ensure that the buffer size can be increased to at least capacity without |
| 253 | // further reallocation. (Of course, this operation might need to reallocate |
| 254 | // the buffer.) |
| 255 | void EnsureCapacity(size_t capacity); |
| 256 | |
| 257 | // Resets the buffer to zero size without altering capacity. Works even if the |
| 258 | // buffer has been moved from. |
| 259 | void Clear(); |
| 260 | |
| 261 | // Swaps two buffers. |
| 262 | friend void swap(CopyOnWriteBuffer& a, CopyOnWriteBuffer& b) { |
Danil Chapovalov | 8df643b | 2021-01-22 16:11:10 +0100 | [diff] [blame] | 263 | a.buffer_.swap(b.buffer_); |
Ilya Nikolaevskiy | 741bab0 | 2019-09-25 14:37:10 +0200 | [diff] [blame] | 264 | std::swap(a.offset_, b.offset_); |
| 265 | std::swap(a.size_, b.size_); |
| 266 | } |
| 267 | |
| 268 | CopyOnWriteBuffer Slice(size_t offset, size_t length) const { |
| 269 | CopyOnWriteBuffer slice(*this); |
| 270 | RTC_DCHECK_LE(offset, size_); |
| 271 | RTC_DCHECK_LE(length + offset, size_); |
| 272 | slice.offset_ += offset; |
| 273 | slice.size_ = length; |
| 274 | return slice; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | private: |
Danil Chapovalov | 8df643b | 2021-01-22 16:11:10 +0100 | [diff] [blame] | 278 | using RefCountedBuffer = FinalRefCountedObject<Buffer>; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 279 | // Create a copy of the underlying data if it is referenced from other Buffer |
Ilya Nikolaevskiy | 741bab0 | 2019-09-25 14:37:10 +0200 | [diff] [blame] | 280 | // objects or there is not enough capacity. |
| 281 | void UnshareAndEnsureCapacity(size_t new_capacity); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 282 | |
| 283 | // Pre- and postcondition of all methods. |
Ilya Nikolaevskiy | 741bab0 | 2019-09-25 14:37:10 +0200 | [diff] [blame] | 284 | bool IsConsistent() const { |
| 285 | if (buffer_) { |
| 286 | return buffer_->capacity() > 0 && offset_ <= buffer_->size() && |
| 287 | offset_ + size_ <= buffer_->size(); |
| 288 | } else { |
| 289 | return size_ == 0 && offset_ == 0; |
| 290 | } |
| 291 | } |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 292 | |
| 293 | // buffer_ is either null, or points to an rtc::Buffer with capacity > 0. |
Danil Chapovalov | 8df643b | 2021-01-22 16:11:10 +0100 | [diff] [blame] | 294 | scoped_refptr<RefCountedBuffer> buffer_; |
Ilya Nikolaevskiy | 741bab0 | 2019-09-25 14:37:10 +0200 | [diff] [blame] | 295 | // This buffer may represent a slice of a original data. |
| 296 | size_t offset_; // Offset of a current slice in the original data in buffer_. |
| 297 | // Should be 0 if the buffer_ is empty. |
| 298 | size_t size_; // Size of a current slice in the original data in buffer_. |
| 299 | // Should be 0 if the buffer_ is empty. |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 300 | }; |
| 301 | |
| 302 | } // namespace rtc |
jbauch | 13041cf | 2016-02-25 06:16:52 -0800 | [diff] [blame] | 303 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 304 | #endif // RTC_BASE_COPY_ON_WRITE_BUFFER_H_ |