henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 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_BUFFER_H_ |
| 12 | #define RTC_BASE_BUFFER_H_ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 13 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 14 | #include <algorithm> |
| 15 | #include <cstring> |
| 16 | #include <memory> |
| 17 | #include <type_traits> |
| 18 | #include <utility> |
| 19 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "api/array_view.h" |
| 21 | #include "rtc_base/checks.h" |
| 22 | #include "rtc_base/type_traits.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 23 | |
| 24 | namespace rtc { |
| 25 | |
| 26 | namespace internal { |
| 27 | |
| 28 | // (Internal; please don't use outside this file.) Determines if elements of |
| 29 | // type U are compatible with a BufferT<T>. For most types, we just ignore |
| 30 | // top-level const and forbid top-level volatile and require T and U to be |
| 31 | // otherwise equal, but all byte-sized integers (notably char, int8_t, and |
| 32 | // uint8_t) are compatible with each other. (Note: We aim to get rid of this |
| 33 | // behavior, and treat all types the same.) |
| 34 | template <typename T, typename U> |
| 35 | struct BufferCompat { |
| 36 | static constexpr bool value = |
| 37 | !std::is_volatile<U>::value && |
| 38 | ((std::is_integral<T>::value && sizeof(T) == 1) |
| 39 | ? (std::is_integral<U>::value && sizeof(U) == 1) |
| 40 | : (std::is_same<T, typename std::remove_const<U>::type>::value)); |
| 41 | }; |
| 42 | |
| 43 | } // namespace internal |
| 44 | |
| 45 | // Basic buffer class, can be grown and shrunk dynamically. |
| 46 | // Unlike std::string/vector, does not initialize data when increasing size. |
| 47 | template <typename T> |
| 48 | class BufferT { |
| 49 | // We want T's destructor and default constructor to be trivial, i.e. perform |
| 50 | // no action, so that we don't have to touch the memory we allocate and |
| 51 | // deallocate. And we want T to be trivially copyable, so that we can copy T |
| 52 | // instances with std::memcpy. This is precisely the definition of a trivial |
| 53 | // type. |
| 54 | static_assert(std::is_trivial<T>::value, "T must be a trivial type."); |
| 55 | |
| 56 | // This class relies heavily on being able to mutate its data. |
| 57 | static_assert(!std::is_const<T>::value, "T may not be const"); |
| 58 | |
| 59 | public: |
| 60 | using value_type = T; |
| 61 | |
| 62 | // An empty BufferT. |
| 63 | BufferT() : size_(0), capacity_(0), data_(nullptr) { |
| 64 | RTC_DCHECK(IsConsistent()); |
| 65 | } |
| 66 | |
| 67 | // Disable copy construction and copy assignment, since copying a buffer is |
| 68 | // expensive enough that we want to force the user to be explicit about it. |
| 69 | BufferT(const BufferT&) = delete; |
| 70 | BufferT& operator=(const BufferT&) = delete; |
| 71 | |
| 72 | BufferT(BufferT&& buf) |
| 73 | : size_(buf.size()), |
| 74 | capacity_(buf.capacity()), |
| 75 | data_(std::move(buf.data_)) { |
| 76 | RTC_DCHECK(IsConsistent()); |
| 77 | buf.OnMovedFrom(); |
| 78 | } |
| 79 | |
| 80 | // Construct a buffer with the specified number of uninitialized elements. |
| 81 | explicit BufferT(size_t size) : BufferT(size, size) {} |
| 82 | |
| 83 | BufferT(size_t size, size_t capacity) |
| 84 | : size_(size), |
| 85 | capacity_(std::max(size, capacity)), |
| 86 | data_(new T[capacity_]) { |
| 87 | RTC_DCHECK(IsConsistent()); |
| 88 | } |
| 89 | |
| 90 | // Construct a buffer and copy the specified number of elements into it. |
| 91 | template <typename U, |
| 92 | typename std::enable_if< |
| 93 | internal::BufferCompat<T, U>::value>::type* = nullptr> |
| 94 | BufferT(const U* data, size_t size) : BufferT(data, size, size) {} |
| 95 | |
| 96 | template <typename U, |
| 97 | typename std::enable_if< |
| 98 | internal::BufferCompat<T, U>::value>::type* = nullptr> |
| 99 | BufferT(U* data, size_t size, size_t capacity) : BufferT(size, capacity) { |
| 100 | static_assert(sizeof(T) == sizeof(U), ""); |
| 101 | std::memcpy(data_.get(), data, size * sizeof(U)); |
| 102 | } |
| 103 | |
| 104 | // Construct a buffer from the contents of an array. |
| 105 | template <typename U, |
| 106 | size_t N, |
| 107 | typename std::enable_if< |
| 108 | internal::BufferCompat<T, U>::value>::type* = nullptr> |
| 109 | BufferT(U (&array)[N]) : BufferT(array, N) {} |
| 110 | |
| 111 | // Get a pointer to the data. Just .data() will give you a (const) T*, but if |
| 112 | // T is a byte-sized integer, you may also use .data<U>() for any other |
| 113 | // byte-sized integer U. |
| 114 | template <typename U = T, |
| 115 | typename std::enable_if< |
| 116 | internal::BufferCompat<T, U>::value>::type* = nullptr> |
| 117 | const U* data() const { |
| 118 | RTC_DCHECK(IsConsistent()); |
| 119 | return reinterpret_cast<U*>(data_.get()); |
| 120 | } |
| 121 | |
| 122 | template <typename U = T, |
| 123 | typename std::enable_if< |
| 124 | internal::BufferCompat<T, U>::value>::type* = nullptr> |
| 125 | U* data() { |
| 126 | RTC_DCHECK(IsConsistent()); |
| 127 | return reinterpret_cast<U*>(data_.get()); |
| 128 | } |
| 129 | |
| 130 | bool empty() const { |
| 131 | RTC_DCHECK(IsConsistent()); |
| 132 | return size_ == 0; |
| 133 | } |
| 134 | |
| 135 | size_t size() const { |
| 136 | RTC_DCHECK(IsConsistent()); |
| 137 | return size_; |
| 138 | } |
| 139 | |
| 140 | size_t capacity() const { |
| 141 | RTC_DCHECK(IsConsistent()); |
| 142 | return capacity_; |
| 143 | } |
| 144 | |
| 145 | BufferT& operator=(BufferT&& buf) { |
| 146 | RTC_DCHECK(IsConsistent()); |
| 147 | RTC_DCHECK(buf.IsConsistent()); |
| 148 | size_ = buf.size_; |
| 149 | capacity_ = buf.capacity_; |
| 150 | data_ = std::move(buf.data_); |
| 151 | buf.OnMovedFrom(); |
| 152 | return *this; |
| 153 | } |
| 154 | |
| 155 | bool operator==(const BufferT& buf) const { |
| 156 | RTC_DCHECK(IsConsistent()); |
| 157 | if (size_ != buf.size_) { |
| 158 | return false; |
| 159 | } |
| 160 | if (std::is_integral<T>::value) { |
| 161 | // Optimization. |
| 162 | return std::memcmp(data_.get(), buf.data_.get(), size_ * sizeof(T)) == 0; |
| 163 | } |
| 164 | for (size_t i = 0; i < size_; ++i) { |
| 165 | if (data_[i] != buf.data_[i]) { |
| 166 | return false; |
| 167 | } |
| 168 | } |
| 169 | return true; |
| 170 | } |
| 171 | |
| 172 | bool operator!=(const BufferT& buf) const { return !(*this == buf); } |
| 173 | |
| 174 | T& operator[](size_t index) { |
| 175 | RTC_DCHECK_LT(index, size_); |
| 176 | return data()[index]; |
| 177 | } |
| 178 | |
| 179 | T operator[](size_t index) const { |
| 180 | RTC_DCHECK_LT(index, size_); |
| 181 | return data()[index]; |
| 182 | } |
| 183 | |
| 184 | T* begin() { return data(); } |
| 185 | T* end() { return data() + size(); } |
| 186 | const T* begin() const { return data(); } |
| 187 | const T* end() const { return data() + size(); } |
| 188 | const T* cbegin() const { return data(); } |
| 189 | const T* cend() const { return data() + size(); } |
| 190 | |
| 191 | // The SetData functions replace the contents of the buffer. They accept the |
| 192 | // same input types as the constructors. |
| 193 | template <typename U, |
| 194 | typename std::enable_if< |
| 195 | internal::BufferCompat<T, U>::value>::type* = nullptr> |
| 196 | void SetData(const U* data, size_t size) { |
| 197 | RTC_DCHECK(IsConsistent()); |
| 198 | size_ = 0; |
| 199 | AppendData(data, size); |
| 200 | } |
| 201 | |
| 202 | template <typename U, |
| 203 | size_t N, |
| 204 | typename std::enable_if< |
| 205 | internal::BufferCompat<T, U>::value>::type* = nullptr> |
| 206 | void SetData(const U (&array)[N]) { |
| 207 | SetData(array, N); |
| 208 | } |
| 209 | |
| 210 | template <typename W, |
| 211 | typename std::enable_if< |
| 212 | HasDataAndSize<const W, const T>::value>::type* = nullptr> |
| 213 | void SetData(const W& w) { |
| 214 | SetData(w.data(), w.size()); |
| 215 | } |
| 216 | |
Karl Wiberg | 09819ec | 2017-11-24 13:26:32 +0100 | [diff] [blame] | 217 | // Replaces the data in the buffer with at most |max_elements| of data, using |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 218 | // the function |setter|, which should have the following signature: |
Karl Wiberg | 09819ec | 2017-11-24 13:26:32 +0100 | [diff] [blame] | 219 | // |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 220 | // size_t setter(ArrayView<U> view) |
Karl Wiberg | 09819ec | 2017-11-24 13:26:32 +0100 | [diff] [blame] | 221 | // |
| 222 | // |setter| is given an appropriately typed ArrayView of length exactly |
| 223 | // |max_elements| that describes the area where it should write the data; it |
| 224 | // should return the number of elements actually written. (If it doesn't fill |
| 225 | // the whole ArrayView, it should leave the unused space at the end.) |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 226 | template <typename U = T, |
| 227 | typename F, |
| 228 | typename std::enable_if< |
| 229 | internal::BufferCompat<T, U>::value>::type* = nullptr> |
| 230 | size_t SetData(size_t max_elements, F&& setter) { |
| 231 | RTC_DCHECK(IsConsistent()); |
| 232 | size_ = 0; |
| 233 | return AppendData<U>(max_elements, std::forward<F>(setter)); |
| 234 | } |
| 235 | |
| 236 | // The AppendData functions add data to the end of the buffer. They accept |
| 237 | // the same input types as the constructors. |
| 238 | template <typename U, |
| 239 | typename std::enable_if< |
| 240 | internal::BufferCompat<T, U>::value>::type* = nullptr> |
| 241 | void AppendData(const U* data, size_t size) { |
| 242 | RTC_DCHECK(IsConsistent()); |
| 243 | const size_t new_size = size_ + size; |
| 244 | EnsureCapacityWithHeadroom(new_size, true); |
| 245 | static_assert(sizeof(T) == sizeof(U), ""); |
| 246 | std::memcpy(data_.get() + size_, data, size * sizeof(U)); |
| 247 | size_ = new_size; |
| 248 | RTC_DCHECK(IsConsistent()); |
| 249 | } |
| 250 | |
| 251 | template <typename U, |
| 252 | size_t N, |
| 253 | typename std::enable_if< |
| 254 | internal::BufferCompat<T, U>::value>::type* = nullptr> |
| 255 | void AppendData(const U (&array)[N]) { |
| 256 | AppendData(array, N); |
| 257 | } |
| 258 | |
| 259 | template <typename W, |
| 260 | typename std::enable_if< |
| 261 | HasDataAndSize<const W, const T>::value>::type* = nullptr> |
| 262 | void AppendData(const W& w) { |
| 263 | AppendData(w.data(), w.size()); |
| 264 | } |
| 265 | |
| 266 | template <typename U, |
| 267 | typename std::enable_if< |
| 268 | internal::BufferCompat<T, U>::value>::type* = nullptr> |
| 269 | void AppendData(const U& item) { |
| 270 | AppendData(&item, 1); |
| 271 | } |
| 272 | |
Karl Wiberg | 09819ec | 2017-11-24 13:26:32 +0100 | [diff] [blame] | 273 | // Appends at most |max_elements| to the end of the buffer, using the function |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 274 | // |setter|, which should have the following signature: |
Karl Wiberg | 09819ec | 2017-11-24 13:26:32 +0100 | [diff] [blame] | 275 | // |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 276 | // size_t setter(ArrayView<U> view) |
Karl Wiberg | 09819ec | 2017-11-24 13:26:32 +0100 | [diff] [blame] | 277 | // |
| 278 | // |setter| is given an appropriately typed ArrayView of length exactly |
| 279 | // |max_elements| that describes the area where it should write the data; it |
| 280 | // should return the number of elements actually written. (If it doesn't fill |
| 281 | // the whole ArrayView, it should leave the unused space at the end.) |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 282 | template <typename U = T, |
| 283 | typename F, |
| 284 | typename std::enable_if< |
| 285 | internal::BufferCompat<T, U>::value>::type* = nullptr> |
| 286 | size_t AppendData(size_t max_elements, F&& setter) { |
| 287 | RTC_DCHECK(IsConsistent()); |
| 288 | const size_t old_size = size_; |
| 289 | SetSize(old_size + max_elements); |
| 290 | U* base_ptr = data<U>() + old_size; |
| 291 | size_t written_elements = setter(rtc::ArrayView<U>(base_ptr, max_elements)); |
| 292 | |
| 293 | RTC_CHECK_LE(written_elements, max_elements); |
| 294 | size_ = old_size + written_elements; |
| 295 | RTC_DCHECK(IsConsistent()); |
| 296 | return written_elements; |
| 297 | } |
| 298 | |
| 299 | // Sets the size of the buffer. If the new size is smaller than the old, the |
| 300 | // buffer contents will be kept but truncated; if the new size is greater, |
| 301 | // the existing contents will be kept and the new space will be |
| 302 | // uninitialized. |
| 303 | void SetSize(size_t size) { |
| 304 | EnsureCapacityWithHeadroom(size, true); |
| 305 | size_ = size; |
| 306 | } |
| 307 | |
| 308 | // Ensure that the buffer size can be increased to at least capacity without |
| 309 | // further reallocation. (Of course, this operation might need to reallocate |
| 310 | // the buffer.) |
| 311 | void EnsureCapacity(size_t capacity) { |
| 312 | // Don't allocate extra headroom, since the user is asking for a specific |
| 313 | // capacity. |
| 314 | EnsureCapacityWithHeadroom(capacity, false); |
| 315 | } |
| 316 | |
| 317 | // Resets the buffer to zero size without altering capacity. Works even if the |
| 318 | // buffer has been moved from. |
| 319 | void Clear() { |
| 320 | size_ = 0; |
| 321 | RTC_DCHECK(IsConsistent()); |
| 322 | } |
| 323 | |
| 324 | // Swaps two buffers. Also works for buffers that have been moved from. |
| 325 | friend void swap(BufferT& a, BufferT& b) { |
| 326 | using std::swap; |
| 327 | swap(a.size_, b.size_); |
| 328 | swap(a.capacity_, b.capacity_); |
| 329 | swap(a.data_, b.data_); |
| 330 | } |
| 331 | |
| 332 | private: |
| 333 | void EnsureCapacityWithHeadroom(size_t capacity, bool extra_headroom) { |
| 334 | RTC_DCHECK(IsConsistent()); |
| 335 | if (capacity <= capacity_) |
| 336 | return; |
| 337 | |
| 338 | // If the caller asks for extra headroom, ensure that the new capacity is |
| 339 | // >= 1.5 times the old capacity. Any constant > 1 is sufficient to prevent |
| 340 | // quadratic behavior; as to why we pick 1.5 in particular, see |
| 341 | // https://github.com/facebook/folly/blob/master/folly/docs/FBVector.md and |
| 342 | // http://www.gahcep.com/cpp-internals-stl-vector-part-1/. |
| 343 | const size_t new_capacity = |
| 344 | extra_headroom ? std::max(capacity, capacity_ + capacity_ / 2) |
| 345 | : capacity; |
| 346 | |
| 347 | std::unique_ptr<T[]> new_data(new T[new_capacity]); |
| 348 | std::memcpy(new_data.get(), data_.get(), size_ * sizeof(T)); |
| 349 | data_ = std::move(new_data); |
| 350 | capacity_ = new_capacity; |
| 351 | RTC_DCHECK(IsConsistent()); |
| 352 | } |
| 353 | |
| 354 | // Precondition for all methods except Clear and the destructor. |
| 355 | // Postcondition for all methods except move construction and move |
| 356 | // assignment, which leave the moved-from object in a possibly inconsistent |
| 357 | // state. |
| 358 | bool IsConsistent() const { |
| 359 | return (data_ || capacity_ == 0) && capacity_ >= size_; |
| 360 | } |
| 361 | |
| 362 | // Called when *this has been moved from. Conceptually it's a no-op, but we |
| 363 | // can mutate the state slightly to help subsequent sanity checks catch bugs. |
| 364 | void OnMovedFrom() { |
| 365 | #if RTC_DCHECK_IS_ON |
| 366 | // Make *this consistent and empty. Shouldn't be necessary, but better safe |
| 367 | // than sorry. |
| 368 | size_ = 0; |
| 369 | capacity_ = 0; |
| 370 | #else |
| 371 | // Ensure that *this is always inconsistent, to provoke bugs. |
| 372 | size_ = 1; |
| 373 | capacity_ = 0; |
| 374 | #endif |
| 375 | } |
| 376 | |
| 377 | size_t size_; |
| 378 | size_t capacity_; |
| 379 | std::unique_ptr<T[]> data_; |
| 380 | }; |
| 381 | |
| 382 | // By far the most common sort of buffer. |
| 383 | using Buffer = BufferT<uint8_t>; |
| 384 | |
| 385 | } // namespace rtc |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 386 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 387 | #endif // RTC_BASE_BUFFER_H_ |