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 | #ifndef RTC_BASE_COPYONWRITEBUFFER_H_ |
| 12 | #define RTC_BASE_COPYONWRITEBUFFER_H_ |
jbauch | 13041cf | 2016-02-25 06:16:52 -0800 | [diff] [blame] | 13 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 14 | #include <algorithm> |
| 15 | #include <utility> |
jbauch | 13041cf | 2016-02-25 06:16:52 -0800 | [diff] [blame] | 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "rtc_base/buffer.h" |
| 18 | #include "rtc_base/checks.h" |
Niels Möller | 84255bb | 2017-10-06 13:43:23 +0200 | [diff] [blame] | 19 | #include "rtc_base/refcountedobject.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "rtc_base/scoped_ref_ptr.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 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 | |
Jeroen de Borst | 4f6d233 | 2018-07-18 11:25:12 -0700 | [diff] [blame] | 33 | // Construct a buffer from a string, convenient for unittests. |
| 34 | CopyOnWriteBuffer(const std::string& s); |
| 35 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 36 | // Construct a buffer with the specified number of uninitialized bytes. |
| 37 | explicit CopyOnWriteBuffer(size_t size); |
| 38 | CopyOnWriteBuffer(size_t size, size_t capacity); |
| 39 | |
| 40 | // Construct a buffer and copy the specified number of bytes into it. The |
| 41 | // source array may be (const) uint8_t*, int8_t*, or char*. |
| 42 | template <typename T, |
| 43 | typename std::enable_if< |
| 44 | internal::BufferCompat<uint8_t, T>::value>::type* = nullptr> |
| 45 | CopyOnWriteBuffer(const T* data, size_t size) |
| 46 | : CopyOnWriteBuffer(data, size, size) {} |
| 47 | template <typename T, |
| 48 | typename std::enable_if< |
| 49 | internal::BufferCompat<uint8_t, T>::value>::type* = nullptr> |
| 50 | CopyOnWriteBuffer(const T* data, size_t size, size_t capacity) |
| 51 | : CopyOnWriteBuffer(size, capacity) { |
| 52 | if (buffer_) { |
| 53 | std::memcpy(buffer_->data(), data, size); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | // Construct a buffer from the contents of an array. |
| 58 | template <typename T, |
| 59 | size_t N, |
| 60 | typename std::enable_if< |
| 61 | internal::BufferCompat<uint8_t, T>::value>::type* = nullptr> |
| 62 | CopyOnWriteBuffer(const T (&array)[N]) // NOLINT: runtime/explicit |
| 63 | : CopyOnWriteBuffer(array, N) {} |
| 64 | |
| 65 | ~CopyOnWriteBuffer(); |
| 66 | |
| 67 | // Get a pointer to the data. Just .data() will give you a (const) uint8_t*, |
| 68 | // but you may also use .data<int8_t>() and .data<char>(). |
| 69 | template <typename T = uint8_t, |
| 70 | typename std::enable_if< |
| 71 | internal::BufferCompat<uint8_t, T>::value>::type* = nullptr> |
| 72 | const T* data() const { |
| 73 | return cdata<T>(); |
| 74 | } |
| 75 | |
| 76 | // Get writable pointer to the data. This will create a copy of the underlying |
| 77 | // data if it is shared with other buffers. |
| 78 | template <typename T = uint8_t, |
| 79 | typename std::enable_if< |
| 80 | internal::BufferCompat<uint8_t, T>::value>::type* = nullptr> |
| 81 | T* data() { |
| 82 | RTC_DCHECK(IsConsistent()); |
| 83 | if (!buffer_) { |
| 84 | return nullptr; |
| 85 | } |
| 86 | CloneDataIfReferenced(buffer_->capacity()); |
| 87 | return buffer_->data<T>(); |
| 88 | } |
| 89 | |
| 90 | // Get const pointer to the data. This will not create a copy of the |
| 91 | // underlying data if it is shared with other buffers. |
| 92 | template <typename T = uint8_t, |
| 93 | typename std::enable_if< |
| 94 | internal::BufferCompat<uint8_t, T>::value>::type* = nullptr> |
| 95 | const T* cdata() const { |
| 96 | RTC_DCHECK(IsConsistent()); |
| 97 | if (!buffer_) { |
| 98 | return nullptr; |
| 99 | } |
| 100 | return buffer_->data<T>(); |
| 101 | } |
| 102 | |
| 103 | size_t size() const { |
| 104 | RTC_DCHECK(IsConsistent()); |
| 105 | return buffer_ ? buffer_->size() : 0; |
| 106 | } |
| 107 | |
| 108 | size_t capacity() const { |
| 109 | RTC_DCHECK(IsConsistent()); |
| 110 | return buffer_ ? buffer_->capacity() : 0; |
| 111 | } |
| 112 | |
| 113 | CopyOnWriteBuffer& operator=(const CopyOnWriteBuffer& buf) { |
| 114 | RTC_DCHECK(IsConsistent()); |
| 115 | RTC_DCHECK(buf.IsConsistent()); |
| 116 | if (&buf != this) { |
| 117 | buffer_ = buf.buffer_; |
| 118 | } |
| 119 | return *this; |
| 120 | } |
| 121 | |
| 122 | CopyOnWriteBuffer& operator=(CopyOnWriteBuffer&& buf) { |
| 123 | RTC_DCHECK(IsConsistent()); |
| 124 | RTC_DCHECK(buf.IsConsistent()); |
| 125 | buffer_ = std::move(buf.buffer_); |
| 126 | return *this; |
| 127 | } |
| 128 | |
| 129 | bool operator==(const CopyOnWriteBuffer& buf) const; |
| 130 | |
| 131 | bool operator!=(const CopyOnWriteBuffer& buf) const { |
| 132 | return !(*this == buf); |
| 133 | } |
| 134 | |
| 135 | uint8_t& operator[](size_t index) { |
| 136 | RTC_DCHECK_LT(index, size()); |
| 137 | return data()[index]; |
| 138 | } |
| 139 | |
| 140 | uint8_t operator[](size_t index) const { |
| 141 | RTC_DCHECK_LT(index, size()); |
| 142 | return cdata()[index]; |
| 143 | } |
| 144 | |
| 145 | // Replace the contents of the buffer. Accepts the same types as the |
| 146 | // constructors. |
| 147 | template <typename T, |
| 148 | typename std::enable_if< |
| 149 | internal::BufferCompat<uint8_t, T>::value>::type* = nullptr> |
| 150 | void SetData(const T* data, size_t size) { |
| 151 | RTC_DCHECK(IsConsistent()); |
| 152 | if (!buffer_) { |
| 153 | buffer_ = size > 0 ? new RefCountedObject<Buffer>(data, size) : nullptr; |
| 154 | } else if (!buffer_->HasOneRef()) { |
| 155 | buffer_ = new RefCountedObject<Buffer>(data, size, buffer_->capacity()); |
| 156 | } else { |
| 157 | buffer_->SetData(data, size); |
| 158 | } |
| 159 | RTC_DCHECK(IsConsistent()); |
| 160 | } |
| 161 | |
| 162 | template <typename T, |
| 163 | size_t N, |
| 164 | typename std::enable_if< |
| 165 | internal::BufferCompat<uint8_t, T>::value>::type* = nullptr> |
| 166 | void SetData(const T (&array)[N]) { |
| 167 | SetData(array, N); |
| 168 | } |
| 169 | |
| 170 | void SetData(const CopyOnWriteBuffer& buf) { |
| 171 | RTC_DCHECK(IsConsistent()); |
| 172 | RTC_DCHECK(buf.IsConsistent()); |
| 173 | if (&buf != this) { |
| 174 | buffer_ = buf.buffer_; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | // Append data to the buffer. Accepts the same types as the constructors. |
| 179 | template <typename T, |
| 180 | typename std::enable_if< |
| 181 | internal::BufferCompat<uint8_t, T>::value>::type* = nullptr> |
| 182 | void AppendData(const T* data, size_t size) { |
| 183 | RTC_DCHECK(IsConsistent()); |
| 184 | if (!buffer_) { |
| 185 | buffer_ = new RefCountedObject<Buffer>(data, size); |
| 186 | RTC_DCHECK(IsConsistent()); |
| 187 | return; |
| 188 | } |
| 189 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 190 | CloneDataIfReferenced( |
| 191 | std::max(buffer_->capacity(), buffer_->size() + size)); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 192 | buffer_->AppendData(data, size); |
| 193 | RTC_DCHECK(IsConsistent()); |
| 194 | } |
| 195 | |
| 196 | template <typename T, |
| 197 | size_t N, |
| 198 | typename std::enable_if< |
| 199 | internal::BufferCompat<uint8_t, T>::value>::type* = nullptr> |
| 200 | void AppendData(const T (&array)[N]) { |
| 201 | AppendData(array, N); |
| 202 | } |
| 203 | |
| 204 | void AppendData(const CopyOnWriteBuffer& buf) { |
| 205 | AppendData(buf.data(), buf.size()); |
| 206 | } |
| 207 | |
| 208 | // Sets the size of the buffer. If the new size is smaller than the old, the |
| 209 | // buffer contents will be kept but truncated; if the new size is greater, |
| 210 | // the existing contents will be kept and the new space will be |
| 211 | // uninitialized. |
| 212 | void SetSize(size_t size); |
| 213 | |
| 214 | // Ensure that the buffer size can be increased to at least capacity without |
| 215 | // further reallocation. (Of course, this operation might need to reallocate |
| 216 | // the buffer.) |
| 217 | void EnsureCapacity(size_t capacity); |
| 218 | |
| 219 | // Resets the buffer to zero size without altering capacity. Works even if the |
| 220 | // buffer has been moved from. |
| 221 | void Clear(); |
| 222 | |
| 223 | // Swaps two buffers. |
| 224 | friend void swap(CopyOnWriteBuffer& a, CopyOnWriteBuffer& b) { |
| 225 | std::swap(a.buffer_, b.buffer_); |
| 226 | } |
| 227 | |
| 228 | private: |
| 229 | // Create a copy of the underlying data if it is referenced from other Buffer |
| 230 | // objects. |
| 231 | void CloneDataIfReferenced(size_t new_capacity); |
| 232 | |
| 233 | // Pre- and postcondition of all methods. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 234 | bool IsConsistent() const { return (!buffer_ || buffer_->capacity() > 0); } |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 235 | |
| 236 | // buffer_ is either null, or points to an rtc::Buffer with capacity > 0. |
| 237 | scoped_refptr<RefCountedObject<Buffer>> buffer_; |
| 238 | }; |
| 239 | |
| 240 | } // namespace rtc |
jbauch | 13041cf | 2016-02-25 06:16:52 -0800 | [diff] [blame] | 241 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 242 | #endif // RTC_BASE_COPYONWRITEBUFFER_H_ |