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