blob: ecc4b2321d42c30e1a0fa836824620c84ad62966 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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ång16426202016-06-16 15:54:44 +020014#include <algorithm>
Karl Wiberg94784372015-04-20 14:03:07 +020015#include <cstring>
kwiberg8fb35572016-02-11 13:36:43 -080016#include <memory>
kwiberga4ac4782016-04-29 08:00:22 -070017#include <type_traits>
ossub01c7812016-02-24 01:05:56 -080018#include <utility>
kwiberg36220ae2016-01-12 07:24:19 -080019
ossub01c7812016-02-24 01:05:56 -080020#include "webrtc/base/array_view.h"
21#include "webrtc/base/checks.h"
kwibergd3134032016-09-05 07:46:20 -070022#include "webrtc/base/type_traits.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000023
24namespace rtc {
25
Karl Wiberg94784372015-04-20 14:03:07 +020026namespace internal {
27
kwiberga4ac4782016-04-29 08:00:22 -070028// (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.)
34template <typename T, typename U>
35struct 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));
Karl Wiberg94784372015-04-20 14:03:07 +020041};
42
43} // namespace internal
44
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000045// Basic buffer class, can be grown and shrunk dynamically.
kwiberga4ac4782016-04-29 08:00:22 -070046// Unlike std::string/vector, does not initialize data when increasing size.
47template <typename T>
48class 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
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000059 public:
Danil Chapovalov13301662017-06-28 15:15:30 +020060 using value_type = T;
61
kwiberga4ac4782016-04-29 08:00:22 -070062 // An empty BufferT.
63 BufferT() : size_(0), capacity_(0), data_(nullptr) {
64 RTC_DCHECK(IsConsistent());
65 }
Karl Wiberg94784372015-04-20 14:03:07 +020066
kwiberga4ac4782016-04-29 08:00:22 -070067 // 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;
Karl Wiberg94784372015-04-20 14:03:07 +020071
kwiberga4ac4782016-04-29 08:00:22 -070072 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 }
ossub01c7812016-02-24 01:05:56 -080079
kwiberga4ac4782016-04-29 08:00:22 -070080 // 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));
Karl Wiberg94784372015-04-20 14:03:07 +0200102 }
103
104 // Construct a buffer from the contents of an array.
kwiberga4ac4782016-04-29 08:00:22 -0700105 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) {}
Karl Wiberg94784372015-04-20 14:03:07 +0200110
kwiberga4ac4782016-04-29 08:00:22 -0700111 // 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 {
kwiberge3d99222016-03-01 01:57:38 -0800118 RTC_DCHECK(IsConsistent());
kwiberga4ac4782016-04-29 08:00:22 -0700119 return reinterpret_cast<U*>(data_.get());
Karl Wiberg94784372015-04-20 14:03:07 +0200120 }
ossub01c7812016-02-24 01:05:56 -0800121
kwiberga4ac4782016-04-29 08:00:22 -0700122 template <typename U = T,
123 typename std::enable_if<
124 internal::BufferCompat<T, U>::value>::type* = nullptr>
125 U* data() {
kwiberge3d99222016-03-01 01:57:38 -0800126 RTC_DCHECK(IsConsistent());
kwiberga4ac4782016-04-29 08:00:22 -0700127 return reinterpret_cast<U*>(data_.get());
Karl Wiberg94784372015-04-20 14:03:07 +0200128 }
129
ossu5955e242016-08-31 08:40:04 -0700130 bool empty() const {
131 RTC_DCHECK(IsConsistent());
132 return size_ == 0;
133 }
134
Karl Wiberg94784372015-04-20 14:03:07 +0200135 size_t size() const {
kwiberge3d99222016-03-01 01:57:38 -0800136 RTC_DCHECK(IsConsistent());
Karl Wiberg94784372015-04-20 14:03:07 +0200137 return size_;
138 }
ossub01c7812016-02-24 01:05:56 -0800139
Karl Wiberg94784372015-04-20 14:03:07 +0200140 size_t capacity() const {
kwiberge3d99222016-03-01 01:57:38 -0800141 RTC_DCHECK(IsConsistent());
Karl Wiberg94784372015-04-20 14:03:07 +0200142 return capacity_;
143 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000144
kwiberga4ac4782016-04-29 08:00:22 -0700145 BufferT& operator=(BufferT&& buf) {
kwiberge3d99222016-03-01 01:57:38 -0800146 RTC_DCHECK(IsConsistent());
147 RTC_DCHECK(buf.IsConsistent());
Karl Wiberg94784372015-04-20 14:03:07 +0200148 size_ = buf.size_;
149 capacity_ = buf.capacity_;
kwiberg0eb15ed2015-12-17 03:04:15 -0800150 data_ = std::move(buf.data_);
Karl Wiberg94784372015-04-20 14:03:07 +0200151 buf.OnMovedFrom();
152 return *this;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000153 }
154
kwiberga4ac4782016-04-29 08:00:22 -0700155 bool operator==(const BufferT& buf) const {
kwiberge3d99222016-03-01 01:57:38 -0800156 RTC_DCHECK(IsConsistent());
kwiberga4ac4782016-04-29 08:00:22 -0700157 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;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000170 }
Karl Wiberg94784372015-04-20 14:03:07 +0200171
kwiberga4ac4782016-04-29 08:00:22 -0700172 bool operator!=(const BufferT& buf) const { return !(*this == buf); }
Karl Wiberg94784372015-04-20 14:03:07 +0200173
kwiberga4ac4782016-04-29 08:00:22 -0700174 T& operator[](size_t index) {
ossub9338ac2016-02-29 09:36:37 -0800175 RTC_DCHECK_LT(index, size_);
176 return data()[index];
177 }
178
kwiberga4ac4782016-04-29 08:00:22 -0700179 T operator[](size_t index) const {
ossub9338ac2016-02-29 09:36:37 -0800180 RTC_DCHECK_LT(index, size_);
181 return data()[index];
182 }
183
kwiberg1ba21eb2017-04-05 07:38:06 -0700184 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
ossub01c7812016-02-24 01:05:56 -0800191 // The SetData functions replace the contents of the buffer. They accept the
192 // same input types as the constructors.
kwiberga4ac4782016-04-29 08:00:22 -0700193 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) {
kwiberge3d99222016-03-01 01:57:38 -0800197 RTC_DCHECK(IsConsistent());
Karl Wiberg94784372015-04-20 14:03:07 +0200198 size_ = 0;
199 AppendData(data, size);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000200 }
ossub01c7812016-02-24 01:05:56 -0800201
kwiberga4ac4782016-04-29 08:00:22 -0700202 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]) {
Karl Wiberg94784372015-04-20 14:03:07 +0200207 SetData(array, N);
208 }
ossub01c7812016-02-24 01:05:56 -0800209
kwibergd3134032016-09-05 07:46:20 -0700210 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 }
Karl Wiberg94784372015-04-20 14:03:07 +0200216
kwiberga4ac4782016-04-29 08:00:22 -0700217 // Replace the data in the buffer with at most |max_elements| of data, using
218 // the function |setter|, which should have the following signature:
219 // size_t setter(ArrayView<U> view)
ossub01c7812016-02-24 01:05:56 -0800220 // |setter| is given an appropriately typed ArrayView of the area in which to
221 // write the data (i.e. starting at the beginning of the buffer) and should
kwiberga4ac4782016-04-29 08:00:22 -0700222 // return the number of elements actually written. This number must be <=
223 // |max_elements|.
224 template <typename U = T,
225 typename F,
226 typename std::enable_if<
227 internal::BufferCompat<T, U>::value>::type* = nullptr>
228 size_t SetData(size_t max_elements, F&& setter) {
ossub01c7812016-02-24 01:05:56 -0800229 RTC_DCHECK(IsConsistent());
230 size_ = 0;
kwiberga4ac4782016-04-29 08:00:22 -0700231 return AppendData<U>(max_elements, std::forward<F>(setter));
ossub01c7812016-02-24 01:05:56 -0800232 }
233
kwiberga4ac4782016-04-29 08:00:22 -0700234 // The AppendData functions add data to the end of the buffer. They accept
ossub01c7812016-02-24 01:05:56 -0800235 // the same input types as the constructors.
kwiberga4ac4782016-04-29 08:00:22 -0700236 template <typename U,
237 typename std::enable_if<
238 internal::BufferCompat<T, U>::value>::type* = nullptr>
239 void AppendData(const U* data, size_t size) {
kwiberge3d99222016-03-01 01:57:38 -0800240 RTC_DCHECK(IsConsistent());
Karl Wiberg94784372015-04-20 14:03:07 +0200241 const size_t new_size = size_ + size;
kwibergc8535972016-06-20 04:47:39 -0700242 EnsureCapacityWithHeadroom(new_size, true);
kwiberga4ac4782016-04-29 08:00:22 -0700243 static_assert(sizeof(T) == sizeof(U), "");
244 std::memcpy(data_.get() + size_, data, size * sizeof(U));
Karl Wiberg94784372015-04-20 14:03:07 +0200245 size_ = new_size;
kwiberge3d99222016-03-01 01:57:38 -0800246 RTC_DCHECK(IsConsistent());
Karl Wiberg94784372015-04-20 14:03:07 +0200247 }
ossub01c7812016-02-24 01:05:56 -0800248
kwiberga4ac4782016-04-29 08:00:22 -0700249 template <typename U,
250 size_t N,
251 typename std::enable_if<
252 internal::BufferCompat<T, U>::value>::type* = nullptr>
253 void AppendData(const U (&array)[N]) {
Karl Wiberg94784372015-04-20 14:03:07 +0200254 AppendData(array, N);
255 }
ossub01c7812016-02-24 01:05:56 -0800256
kwibergd3134032016-09-05 07:46:20 -0700257 template <typename W,
258 typename std::enable_if<
259 HasDataAndSize<const W, const T>::value>::type* = nullptr>
260 void AppendData(const W& w) {
261 AppendData(w.data(), w.size());
262 }
Karl Wiberg94784372015-04-20 14:03:07 +0200263
sprang52033d62016-06-02 02:43:32 -0700264 template <typename U,
265 typename std::enable_if<
266 internal::BufferCompat<T, U>::value>::type* = nullptr>
267 void AppendData(const U& item) {
268 AppendData(&item, 1);
269 }
270
kwiberga4ac4782016-04-29 08:00:22 -0700271 // Append at most |max_elements| to the end of the buffer, using the function
272 // |setter|, which should have the following signature:
273 // size_t setter(ArrayView<U> view)
ossub01c7812016-02-24 01:05:56 -0800274 // |setter| is given an appropriately typed ArrayView of the area in which to
275 // write the data (i.e. starting at the former end of the buffer) and should
kwiberga4ac4782016-04-29 08:00:22 -0700276 // return the number of elements actually written. This number must be <=
277 // |max_elements|.
278 template <typename U = T,
279 typename F,
280 typename std::enable_if<
281 internal::BufferCompat<T, U>::value>::type* = nullptr>
282 size_t AppendData(size_t max_elements, F&& setter) {
ossub01c7812016-02-24 01:05:56 -0800283 RTC_DCHECK(IsConsistent());
284 const size_t old_size = size_;
kwiberga4ac4782016-04-29 08:00:22 -0700285 SetSize(old_size + max_elements);
286 U* base_ptr = data<U>() + old_size;
287 size_t written_elements = setter(rtc::ArrayView<U>(base_ptr, max_elements));
ossub01c7812016-02-24 01:05:56 -0800288
kwiberga4ac4782016-04-29 08:00:22 -0700289 RTC_CHECK_LE(written_elements, max_elements);
290 size_ = old_size + written_elements;
ossub01c7812016-02-24 01:05:56 -0800291 RTC_DCHECK(IsConsistent());
kwiberga4ac4782016-04-29 08:00:22 -0700292 return written_elements;
ossub01c7812016-02-24 01:05:56 -0800293 }
294
Karl Wiberg94784372015-04-20 14:03:07 +0200295 // Sets the size of the buffer. If the new size is smaller than the old, the
296 // buffer contents will be kept but truncated; if the new size is greater,
297 // the existing contents will be kept and the new space will be
298 // uninitialized.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000299 void SetSize(size_t size) {
kwibergc8535972016-06-20 04:47:39 -0700300 EnsureCapacityWithHeadroom(size, true);
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000301 size_ = size;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000302 }
Karl Wiberg94784372015-04-20 14:03:07 +0200303
304 // Ensure that the buffer size can be increased to at least capacity without
305 // further reallocation. (Of course, this operation might need to reallocate
306 // the buffer.)
307 void EnsureCapacity(size_t capacity) {
kwibergc8535972016-06-20 04:47:39 -0700308 // Don't allocate extra headroom, since the user is asking for a specific
309 // capacity.
310 EnsureCapacityWithHeadroom(capacity, false);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000311 }
312
ossu728012e2016-02-19 02:38:32 -0800313 // Resets the buffer to zero size without altering capacity. Works even if the
314 // buffer has been moved from.
Karl Wiberg94784372015-04-20 14:03:07 +0200315 void Clear() {
Karl Wiberg94784372015-04-20 14:03:07 +0200316 size_ = 0;
kwiberge3d99222016-03-01 01:57:38 -0800317 RTC_DCHECK(IsConsistent());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000318 }
319
Karl Wiberg94784372015-04-20 14:03:07 +0200320 // Swaps two buffers. Also works for buffers that have been moved from.
kwiberga4ac4782016-04-29 08:00:22 -0700321 friend void swap(BufferT& a, BufferT& b) {
Karl Wiberg94784372015-04-20 14:03:07 +0200322 using std::swap;
323 swap(a.size_, b.size_);
324 swap(a.capacity_, b.capacity_);
325 swap(a.data_, b.data_);
326 }
327
328 private:
kwibergc8535972016-06-20 04:47:39 -0700329 void EnsureCapacityWithHeadroom(size_t capacity, bool extra_headroom) {
330 RTC_DCHECK(IsConsistent());
331 if (capacity <= capacity_)
332 return;
333
334 // If the caller asks for extra headroom, ensure that the new capacity is
335 // >= 1.5 times the old capacity. Any constant > 1 is sufficient to prevent
336 // quadratic behavior; as to why we pick 1.5 in particular, see
337 // https://github.com/facebook/folly/blob/master/folly/docs/FBVector.md and
338 // http://www.gahcep.com/cpp-internals-stl-vector-part-1/.
339 const size_t new_capacity =
340 extra_headroom ? std::max(capacity, capacity_ + capacity_ / 2)
341 : capacity;
342
343 std::unique_ptr<T[]> new_data(new T[new_capacity]);
344 std::memcpy(new_data.get(), data_.get(), size_ * sizeof(T));
345 data_ = std::move(new_data);
346 capacity_ = new_capacity;
347 RTC_DCHECK(IsConsistent());
348 }
349
Karl Wiberg94784372015-04-20 14:03:07 +0200350 // Precondition for all methods except Clear and the destructor.
351 // Postcondition for all methods except move construction and move
352 // assignment, which leave the moved-from object in a possibly inconsistent
353 // state.
354 bool IsConsistent() const {
355 return (data_ || capacity_ == 0) && capacity_ >= size_;
356 }
357
358 // Called when *this has been moved from. Conceptually it's a no-op, but we
359 // can mutate the state slightly to help subsequent sanity checks catch bugs.
360 void OnMovedFrom() {
kwiberg5377bc72016-10-04 13:46:56 -0700361#if RTC_DCHECK_IS_ON
Karl Wiberg94784372015-04-20 14:03:07 +0200362 // Make *this consistent and empty. Shouldn't be necessary, but better safe
363 // than sorry.
364 size_ = 0;
365 capacity_ = 0;
366#else
367 // Ensure that *this is always inconsistent, to provoke bugs.
368 size_ = 1;
369 capacity_ = 0;
370#endif
371 }
372
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000373 size_t size_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000374 size_t capacity_;
kwiberga4ac4782016-04-29 08:00:22 -0700375 std::unique_ptr<T[]> data_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000376};
377
kwiberga4ac4782016-04-29 08:00:22 -0700378// By far the most common sort of buffer.
379using Buffer = BufferT<uint8_t>;
380
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000381} // namespace rtc
382
383#endif // WEBRTC_BASE_BUFFER_H_