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 | |
| 127 | size_t size() const { |
kwiberg | e3d9922 | 2016-03-01 01:57:38 -0800 | [diff] [blame] | 128 | RTC_DCHECK(IsConsistent()); |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 129 | return size_; |
| 130 | } |
ossu | b01c781 | 2016-02-24 01:05:56 -0800 | [diff] [blame] | 131 | |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 132 | size_t capacity() 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 capacity_; |
| 135 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 136 | |
kwiberg | a4ac478 | 2016-04-29 08:00:22 -0700 | [diff] [blame] | 137 | BufferT& operator=(BufferT&& buf) { |
kwiberg | e3d9922 | 2016-03-01 01:57:38 -0800 | [diff] [blame] | 138 | RTC_DCHECK(IsConsistent()); |
| 139 | RTC_DCHECK(buf.IsConsistent()); |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 140 | size_ = buf.size_; |
| 141 | capacity_ = buf.capacity_; |
kwiberg | 0eb15ed | 2015-12-17 03:04:15 -0800 | [diff] [blame] | 142 | data_ = std::move(buf.data_); |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 143 | buf.OnMovedFrom(); |
| 144 | return *this; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 145 | } |
| 146 | |
kwiberg | a4ac478 | 2016-04-29 08:00:22 -0700 | [diff] [blame] | 147 | bool operator==(const BufferT& buf) const { |
kwiberg | e3d9922 | 2016-03-01 01:57:38 -0800 | [diff] [blame] | 148 | RTC_DCHECK(IsConsistent()); |
kwiberg | a4ac478 | 2016-04-29 08:00:22 -0700 | [diff] [blame] | 149 | if (size_ != buf.size_) { |
| 150 | return false; |
| 151 | } |
| 152 | if (std::is_integral<T>::value) { |
| 153 | // Optimization. |
| 154 | return std::memcmp(data_.get(), buf.data_.get(), size_ * sizeof(T)) == 0; |
| 155 | } |
| 156 | for (size_t i = 0; i < size_; ++i) { |
| 157 | if (data_[i] != buf.data_[i]) { |
| 158 | return false; |
| 159 | } |
| 160 | } |
| 161 | return true; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 162 | } |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 163 | |
kwiberg | a4ac478 | 2016-04-29 08:00:22 -0700 | [diff] [blame] | 164 | bool operator!=(const BufferT& buf) const { return !(*this == buf); } |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 165 | |
kwiberg | a4ac478 | 2016-04-29 08:00:22 -0700 | [diff] [blame] | 166 | T& operator[](size_t index) { |
ossu | b9338ac | 2016-02-29 09:36:37 -0800 | [diff] [blame] | 167 | RTC_DCHECK_LT(index, size_); |
| 168 | return data()[index]; |
| 169 | } |
| 170 | |
kwiberg | a4ac478 | 2016-04-29 08:00:22 -0700 | [diff] [blame] | 171 | T operator[](size_t index) const { |
ossu | b9338ac | 2016-02-29 09:36:37 -0800 | [diff] [blame] | 172 | RTC_DCHECK_LT(index, size_); |
| 173 | return data()[index]; |
| 174 | } |
| 175 | |
ossu | b01c781 | 2016-02-24 01:05:56 -0800 | [diff] [blame] | 176 | // The SetData functions replace the contents of the buffer. They accept the |
| 177 | // same input types as the constructors. |
kwiberg | a4ac478 | 2016-04-29 08:00:22 -0700 | [diff] [blame] | 178 | template <typename U, |
| 179 | typename std::enable_if< |
| 180 | internal::BufferCompat<T, U>::value>::type* = nullptr> |
| 181 | void SetData(const U* data, size_t size) { |
kwiberg | e3d9922 | 2016-03-01 01:57:38 -0800 | [diff] [blame] | 182 | RTC_DCHECK(IsConsistent()); |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 183 | size_ = 0; |
| 184 | AppendData(data, size); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 185 | } |
ossu | b01c781 | 2016-02-24 01:05:56 -0800 | [diff] [blame] | 186 | |
kwiberg | a4ac478 | 2016-04-29 08:00:22 -0700 | [diff] [blame] | 187 | template <typename U, |
| 188 | size_t N, |
| 189 | typename std::enable_if< |
| 190 | internal::BufferCompat<T, U>::value>::type* = nullptr> |
| 191 | void SetData(const U (&array)[N]) { |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 192 | SetData(array, N); |
| 193 | } |
ossu | b01c781 | 2016-02-24 01:05:56 -0800 | [diff] [blame] | 194 | |
kwiberg | a4ac478 | 2016-04-29 08:00:22 -0700 | [diff] [blame] | 195 | void SetData(const BufferT& buf) { SetData(buf.data(), buf.size()); } |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 196 | |
kwiberg | a4ac478 | 2016-04-29 08:00:22 -0700 | [diff] [blame] | 197 | // Replace the data in the buffer with at most |max_elements| of data, using |
| 198 | // the function |setter|, which should have the following signature: |
| 199 | // size_t setter(ArrayView<U> view) |
ossu | b01c781 | 2016-02-24 01:05:56 -0800 | [diff] [blame] | 200 | // |setter| is given an appropriately typed ArrayView of the area in which to |
| 201 | // 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] | 202 | // return the number of elements actually written. This number must be <= |
| 203 | // |max_elements|. |
| 204 | template <typename U = T, |
| 205 | typename F, |
| 206 | typename std::enable_if< |
| 207 | internal::BufferCompat<T, U>::value>::type* = nullptr> |
| 208 | size_t SetData(size_t max_elements, F&& setter) { |
ossu | b01c781 | 2016-02-24 01:05:56 -0800 | [diff] [blame] | 209 | RTC_DCHECK(IsConsistent()); |
| 210 | size_ = 0; |
kwiberg | a4ac478 | 2016-04-29 08:00:22 -0700 | [diff] [blame] | 211 | return AppendData<U>(max_elements, std::forward<F>(setter)); |
ossu | b01c781 | 2016-02-24 01:05:56 -0800 | [diff] [blame] | 212 | } |
| 213 | |
kwiberg | a4ac478 | 2016-04-29 08:00:22 -0700 | [diff] [blame] | 214 | // The AppendData functions add data to the end of the buffer. They accept |
ossu | b01c781 | 2016-02-24 01:05:56 -0800 | [diff] [blame] | 215 | // the same input types as the constructors. |
kwiberg | a4ac478 | 2016-04-29 08:00:22 -0700 | [diff] [blame] | 216 | template <typename U, |
| 217 | typename std::enable_if< |
| 218 | internal::BufferCompat<T, U>::value>::type* = nullptr> |
| 219 | void AppendData(const U* data, size_t size) { |
kwiberg | e3d9922 | 2016-03-01 01:57:38 -0800 | [diff] [blame] | 220 | RTC_DCHECK(IsConsistent()); |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 221 | const size_t new_size = size_ + size; |
| 222 | EnsureCapacity(new_size); |
kwiberg | a4ac478 | 2016-04-29 08:00:22 -0700 | [diff] [blame] | 223 | static_assert(sizeof(T) == sizeof(U), ""); |
| 224 | std::memcpy(data_.get() + size_, data, size * sizeof(U)); |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 225 | size_ = new_size; |
kwiberg | e3d9922 | 2016-03-01 01:57:38 -0800 | [diff] [blame] | 226 | RTC_DCHECK(IsConsistent()); |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 227 | } |
ossu | b01c781 | 2016-02-24 01:05:56 -0800 | [diff] [blame] | 228 | |
kwiberg | a4ac478 | 2016-04-29 08:00:22 -0700 | [diff] [blame] | 229 | template <typename U, |
| 230 | size_t N, |
| 231 | typename std::enable_if< |
| 232 | internal::BufferCompat<T, U>::value>::type* = nullptr> |
| 233 | void AppendData(const U (&array)[N]) { |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 234 | AppendData(array, N); |
| 235 | } |
ossu | b01c781 | 2016-02-24 01:05:56 -0800 | [diff] [blame] | 236 | |
kwiberg | a4ac478 | 2016-04-29 08:00:22 -0700 | [diff] [blame] | 237 | void AppendData(const BufferT& buf) { AppendData(buf.data(), buf.size()); } |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 238 | |
sprang | 52033d6 | 2016-06-02 02:43:32 -0700 | [diff] [blame] | 239 | template <typename U, |
| 240 | typename std::enable_if< |
| 241 | internal::BufferCompat<T, U>::value>::type* = nullptr> |
| 242 | void AppendData(const U& item) { |
| 243 | AppendData(&item, 1); |
| 244 | } |
| 245 | |
kwiberg | a4ac478 | 2016-04-29 08:00:22 -0700 | [diff] [blame] | 246 | // Append at most |max_elements| to the end of the buffer, using the function |
| 247 | // |setter|, which should have the following signature: |
| 248 | // size_t setter(ArrayView<U> view) |
ossu | b01c781 | 2016-02-24 01:05:56 -0800 | [diff] [blame] | 249 | // |setter| is given an appropriately typed ArrayView of the area in which to |
| 250 | // 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] | 251 | // return the number of elements actually written. This number must be <= |
| 252 | // |max_elements|. |
| 253 | template <typename U = T, |
| 254 | typename F, |
| 255 | typename std::enable_if< |
| 256 | internal::BufferCompat<T, U>::value>::type* = nullptr> |
| 257 | size_t AppendData(size_t max_elements, F&& setter) { |
ossu | b01c781 | 2016-02-24 01:05:56 -0800 | [diff] [blame] | 258 | RTC_DCHECK(IsConsistent()); |
| 259 | const size_t old_size = size_; |
kwiberg | a4ac478 | 2016-04-29 08:00:22 -0700 | [diff] [blame] | 260 | SetSize(old_size + max_elements); |
| 261 | U* base_ptr = data<U>() + old_size; |
| 262 | size_t written_elements = setter(rtc::ArrayView<U>(base_ptr, max_elements)); |
ossu | b01c781 | 2016-02-24 01:05:56 -0800 | [diff] [blame] | 263 | |
kwiberg | a4ac478 | 2016-04-29 08:00:22 -0700 | [diff] [blame] | 264 | RTC_CHECK_LE(written_elements, max_elements); |
| 265 | size_ = old_size + written_elements; |
ossu | b01c781 | 2016-02-24 01:05:56 -0800 | [diff] [blame] | 266 | RTC_DCHECK(IsConsistent()); |
kwiberg | a4ac478 | 2016-04-29 08:00:22 -0700 | [diff] [blame] | 267 | return written_elements; |
ossu | b01c781 | 2016-02-24 01:05:56 -0800 | [diff] [blame] | 268 | } |
| 269 | |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 270 | // Sets the size of the buffer. If the new size is smaller than the old, the |
| 271 | // buffer contents will be kept but truncated; if the new size is greater, |
| 272 | // the existing contents will be kept and the new space will be |
| 273 | // uninitialized. |
kwiberg@webrtc.org | eebcab5 | 2015-03-24 09:19:06 +0000 | [diff] [blame] | 274 | void SetSize(size_t size) { |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 275 | EnsureCapacity(size); |
kwiberg@webrtc.org | eebcab5 | 2015-03-24 09:19:06 +0000 | [diff] [blame] | 276 | size_ = size; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 277 | } |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 278 | |
| 279 | // Ensure that the buffer size can be increased to at least capacity without |
| 280 | // further reallocation. (Of course, this operation might need to reallocate |
| 281 | // the buffer.) |
| 282 | void EnsureCapacity(size_t capacity) { |
kwiberg | e3d9922 | 2016-03-01 01:57:38 -0800 | [diff] [blame] | 283 | RTC_DCHECK(IsConsistent()); |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 284 | if (capacity <= capacity_) |
| 285 | return; |
kwiberg | a4ac478 | 2016-04-29 08:00:22 -0700 | [diff] [blame] | 286 | std::unique_ptr<T[]> new_data(new T[capacity]); |
| 287 | std::memcpy(new_data.get(), data_.get(), size_ * sizeof(T)); |
kwiberg | 0eb15ed | 2015-12-17 03:04:15 -0800 | [diff] [blame] | 288 | data_ = std::move(new_data); |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 289 | capacity_ = capacity; |
kwiberg | e3d9922 | 2016-03-01 01:57:38 -0800 | [diff] [blame] | 290 | RTC_DCHECK(IsConsistent()); |
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: |
| 309 | // Precondition for all methods except Clear and the destructor. |
| 310 | // Postcondition for all methods except move construction and move |
| 311 | // assignment, which leave the moved-from object in a possibly inconsistent |
| 312 | // state. |
| 313 | bool IsConsistent() const { |
| 314 | return (data_ || capacity_ == 0) && capacity_ >= size_; |
| 315 | } |
| 316 | |
| 317 | // Called when *this has been moved from. Conceptually it's a no-op, but we |
| 318 | // can mutate the state slightly to help subsequent sanity checks catch bugs. |
| 319 | void OnMovedFrom() { |
| 320 | #ifdef NDEBUG |
| 321 | // Make *this consistent and empty. Shouldn't be necessary, but better safe |
| 322 | // than sorry. |
| 323 | size_ = 0; |
| 324 | capacity_ = 0; |
| 325 | #else |
| 326 | // Ensure that *this is always inconsistent, to provoke bugs. |
| 327 | size_ = 1; |
| 328 | capacity_ = 0; |
| 329 | #endif |
| 330 | } |
| 331 | |
kwiberg@webrtc.org | eebcab5 | 2015-03-24 09:19:06 +0000 | [diff] [blame] | 332 | size_t size_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 333 | size_t capacity_; |
kwiberg | a4ac478 | 2016-04-29 08:00:22 -0700 | [diff] [blame] | 334 | std::unique_ptr<T[]> data_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 335 | }; |
| 336 | |
kwiberg | a4ac478 | 2016-04-29 08:00:22 -0700 | [diff] [blame] | 337 | // By far the most common sort of buffer. |
| 338 | using Buffer = BufferT<uint8_t>; |
| 339 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 340 | } // namespace rtc |
| 341 | |
| 342 | #endif // WEBRTC_BASE_BUFFER_H_ |