blob: 3048b9179f46246a8654f5c5d2547075cb943ce8 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef RTC_BASE_BUFFER_H_
12#define RTC_BASE_BUFFER_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Yves Gerey3e707812018-11-28 16:47:49 +010014#include <stdint.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020015
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020016#include <algorithm>
17#include <cstring>
18#include <memory>
19#include <type_traits>
20#include <utility>
21
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "api/array_view.h"
23#include "rtc_base/checks.h"
24#include "rtc_base/type_traits.h"
Joachim Bauch5b32f232018-03-07 20:02:26 +010025#include "rtc_base/zero_memory.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020026
27namespace rtc {
28
29namespace internal {
30
31// (Internal; please don't use outside this file.) Determines if elements of
32// type U are compatible with a BufferT<T>. For most types, we just ignore
33// top-level const and forbid top-level volatile and require T and U to be
34// otherwise equal, but all byte-sized integers (notably char, int8_t, and
35// uint8_t) are compatible with each other. (Note: We aim to get rid of this
36// behavior, and treat all types the same.)
37template <typename T, typename U>
38struct BufferCompat {
39 static constexpr bool value =
40 !std::is_volatile<U>::value &&
41 ((std::is_integral<T>::value && sizeof(T) == 1)
42 ? (std::is_integral<U>::value && sizeof(U) == 1)
43 : (std::is_same<T, typename std::remove_const<U>::type>::value));
44};
45
46} // namespace internal
47
48// Basic buffer class, can be grown and shrunk dynamically.
49// Unlike std::string/vector, does not initialize data when increasing size.
Joachim Bauch5b32f232018-03-07 20:02:26 +010050// If "ZeroOnFree" is true, any memory is explicitly cleared before releasing.
51// The type alias "ZeroOnFreeBuffer" below should be used instead of setting
52// "ZeroOnFree" in the template manually to "true".
53template <typename T, bool ZeroOnFree = false>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020054class BufferT {
55 // We want T's destructor and default constructor to be trivial, i.e. perform
56 // no action, so that we don't have to touch the memory we allocate and
57 // deallocate. And we want T to be trivially copyable, so that we can copy T
58 // instances with std::memcpy. This is precisely the definition of a trivial
59 // type.
60 static_assert(std::is_trivial<T>::value, "T must be a trivial type.");
61
62 // This class relies heavily on being able to mutate its data.
63 static_assert(!std::is_const<T>::value, "T may not be const");
64
65 public:
66 using value_type = T;
Karl Wiberg215963c2020-02-04 14:31:39 +010067 using const_iterator = const T*;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020068
69 // An empty BufferT.
70 BufferT() : size_(0), capacity_(0), data_(nullptr) {
71 RTC_DCHECK(IsConsistent());
72 }
73
74 // Disable copy construction and copy assignment, since copying a buffer is
75 // expensive enough that we want to force the user to be explicit about it.
76 BufferT(const BufferT&) = delete;
77 BufferT& operator=(const BufferT&) = delete;
78
79 BufferT(BufferT&& buf)
80 : size_(buf.size()),
81 capacity_(buf.capacity()),
82 data_(std::move(buf.data_)) {
83 RTC_DCHECK(IsConsistent());
84 buf.OnMovedFrom();
85 }
86
87 // Construct a buffer with the specified number of uninitialized elements.
88 explicit BufferT(size_t size) : BufferT(size, size) {}
89
90 BufferT(size_t size, size_t capacity)
91 : size_(size),
92 capacity_(std::max(size, capacity)),
Oleh Prypin7d984ee2018-08-03 00:03:17 +020093 data_(capacity_ > 0 ? new T[capacity_] : nullptr) {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020094 RTC_DCHECK(IsConsistent());
95 }
96
97 // Construct a buffer and copy the specified number of elements into it.
98 template <typename U,
99 typename std::enable_if<
100 internal::BufferCompat<T, U>::value>::type* = nullptr>
101 BufferT(const U* data, size_t size) : BufferT(data, size, size) {}
102
103 template <typename U,
104 typename std::enable_if<
105 internal::BufferCompat<T, U>::value>::type* = nullptr>
106 BufferT(U* data, size_t size, size_t capacity) : BufferT(size, capacity) {
107 static_assert(sizeof(T) == sizeof(U), "");
108 std::memcpy(data_.get(), data, size * sizeof(U));
109 }
110
111 // Construct a buffer from the contents of an array.
112 template <typename U,
113 size_t N,
114 typename std::enable_if<
115 internal::BufferCompat<T, U>::value>::type* = nullptr>
116 BufferT(U (&array)[N]) : BufferT(array, N) {}
117
Joachim Bauch5b32f232018-03-07 20:02:26 +0100118 ~BufferT() { MaybeZeroCompleteBuffer(); }
119
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200120 // Get a pointer to the data. Just .data() will give you a (const) T*, but if
121 // T is a byte-sized integer, you may also use .data<U>() for any other
122 // byte-sized integer U.
123 template <typename U = T,
124 typename std::enable_if<
125 internal::BufferCompat<T, U>::value>::type* = nullptr>
126 const U* data() const {
127 RTC_DCHECK(IsConsistent());
128 return reinterpret_cast<U*>(data_.get());
129 }
130
131 template <typename U = T,
132 typename std::enable_if<
133 internal::BufferCompat<T, U>::value>::type* = nullptr>
134 U* data() {
135 RTC_DCHECK(IsConsistent());
136 return reinterpret_cast<U*>(data_.get());
137 }
138
139 bool empty() const {
140 RTC_DCHECK(IsConsistent());
141 return size_ == 0;
142 }
143
144 size_t size() const {
145 RTC_DCHECK(IsConsistent());
146 return size_;
147 }
148
149 size_t capacity() const {
150 RTC_DCHECK(IsConsistent());
151 return capacity_;
152 }
153
154 BufferT& operator=(BufferT&& buf) {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200155 RTC_DCHECK(buf.IsConsistent());
Karl Wiberg9d247952018-10-10 12:52:17 +0200156 MaybeZeroCompleteBuffer();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200157 size_ = buf.size_;
158 capacity_ = buf.capacity_;
Karl Wiberg4f3ce272018-10-17 13:34:33 +0200159 using std::swap;
160 swap(data_, buf.data_);
161 buf.data_.reset();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200162 buf.OnMovedFrom();
163 return *this;
164 }
165
166 bool operator==(const BufferT& buf) const {
167 RTC_DCHECK(IsConsistent());
168 if (size_ != buf.size_) {
169 return false;
170 }
171 if (std::is_integral<T>::value) {
172 // Optimization.
173 return std::memcmp(data_.get(), buf.data_.get(), size_ * sizeof(T)) == 0;
174 }
175 for (size_t i = 0; i < size_; ++i) {
176 if (data_[i] != buf.data_[i]) {
177 return false;
178 }
179 }
180 return true;
181 }
182
183 bool operator!=(const BufferT& buf) const { return !(*this == buf); }
184
185 T& operator[](size_t index) {
186 RTC_DCHECK_LT(index, size_);
187 return data()[index];
188 }
189
190 T operator[](size_t index) const {
191 RTC_DCHECK_LT(index, size_);
192 return data()[index];
193 }
194
195 T* begin() { return data(); }
196 T* end() { return data() + size(); }
197 const T* begin() const { return data(); }
198 const T* end() const { return data() + size(); }
199 const T* cbegin() const { return data(); }
200 const T* cend() const { return data() + size(); }
201
202 // The SetData functions replace the contents of the buffer. They accept the
203 // same input types as the constructors.
204 template <typename U,
205 typename std::enable_if<
206 internal::BufferCompat<T, U>::value>::type* = nullptr>
207 void SetData(const U* data, size_t size) {
208 RTC_DCHECK(IsConsistent());
Joachim Bauch5b32f232018-03-07 20:02:26 +0100209 const size_t old_size = size_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200210 size_ = 0;
211 AppendData(data, size);
Joachim Bauch5b32f232018-03-07 20:02:26 +0100212 if (ZeroOnFree && size_ < old_size) {
213 ZeroTrailingData(old_size - size_);
214 }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200215 }
216
217 template <typename U,
218 size_t N,
219 typename std::enable_if<
220 internal::BufferCompat<T, U>::value>::type* = nullptr>
221 void SetData(const U (&array)[N]) {
222 SetData(array, N);
223 }
224
225 template <typename W,
226 typename std::enable_if<
227 HasDataAndSize<const W, const T>::value>::type* = nullptr>
228 void SetData(const W& w) {
229 SetData(w.data(), w.size());
230 }
231
Karl Wiberg09819ec2017-11-24 13:26:32 +0100232 // Replaces the data in the buffer with at most |max_elements| of data, using
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200233 // the function |setter|, which should have the following signature:
Karl Wiberg09819ec2017-11-24 13:26:32 +0100234 //
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200235 // size_t setter(ArrayView<U> view)
Karl Wiberg09819ec2017-11-24 13:26:32 +0100236 //
237 // |setter| is given an appropriately typed ArrayView of length exactly
238 // |max_elements| that describes the area where it should write the data; it
239 // should return the number of elements actually written. (If it doesn't fill
240 // the whole ArrayView, it should leave the unused space at the end.)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200241 template <typename U = T,
242 typename F,
243 typename std::enable_if<
244 internal::BufferCompat<T, U>::value>::type* = nullptr>
245 size_t SetData(size_t max_elements, F&& setter) {
246 RTC_DCHECK(IsConsistent());
Joachim Bauch5b32f232018-03-07 20:02:26 +0100247 const size_t old_size = size_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200248 size_ = 0;
Joachim Bauch5b32f232018-03-07 20:02:26 +0100249 const size_t written = AppendData<U>(max_elements, std::forward<F>(setter));
250 if (ZeroOnFree && size_ < old_size) {
251 ZeroTrailingData(old_size - size_);
252 }
253 return written;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200254 }
255
256 // The AppendData functions add data to the end of the buffer. They accept
257 // the same input types as the constructors.
258 template <typename U,
259 typename std::enable_if<
260 internal::BufferCompat<T, U>::value>::type* = nullptr>
261 void AppendData(const U* data, size_t size) {
262 RTC_DCHECK(IsConsistent());
263 const size_t new_size = size_ + size;
264 EnsureCapacityWithHeadroom(new_size, true);
265 static_assert(sizeof(T) == sizeof(U), "");
266 std::memcpy(data_.get() + size_, data, size * sizeof(U));
267 size_ = new_size;
268 RTC_DCHECK(IsConsistent());
269 }
270
271 template <typename U,
272 size_t N,
273 typename std::enable_if<
274 internal::BufferCompat<T, U>::value>::type* = nullptr>
275 void AppendData(const U (&array)[N]) {
276 AppendData(array, N);
277 }
278
279 template <typename W,
280 typename std::enable_if<
281 HasDataAndSize<const W, const T>::value>::type* = nullptr>
282 void AppendData(const W& w) {
283 AppendData(w.data(), w.size());
284 }
285
286 template <typename U,
287 typename std::enable_if<
288 internal::BufferCompat<T, U>::value>::type* = nullptr>
289 void AppendData(const U& item) {
290 AppendData(&item, 1);
291 }
292
Karl Wiberg09819ec2017-11-24 13:26:32 +0100293 // Appends at most |max_elements| to the end of the buffer, using the function
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200294 // |setter|, which should have the following signature:
Karl Wiberg09819ec2017-11-24 13:26:32 +0100295 //
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200296 // size_t setter(ArrayView<U> view)
Karl Wiberg09819ec2017-11-24 13:26:32 +0100297 //
298 // |setter| is given an appropriately typed ArrayView of length exactly
299 // |max_elements| that describes the area where it should write the data; it
300 // should return the number of elements actually written. (If it doesn't fill
301 // the whole ArrayView, it should leave the unused space at the end.)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200302 template <typename U = T,
303 typename F,
304 typename std::enable_if<
305 internal::BufferCompat<T, U>::value>::type* = nullptr>
306 size_t AppendData(size_t max_elements, F&& setter) {
307 RTC_DCHECK(IsConsistent());
308 const size_t old_size = size_;
309 SetSize(old_size + max_elements);
310 U* base_ptr = data<U>() + old_size;
311 size_t written_elements = setter(rtc::ArrayView<U>(base_ptr, max_elements));
312
313 RTC_CHECK_LE(written_elements, max_elements);
314 size_ = old_size + written_elements;
315 RTC_DCHECK(IsConsistent());
316 return written_elements;
317 }
318
319 // Sets the size of the buffer. If the new size is smaller than the old, the
320 // buffer contents will be kept but truncated; if the new size is greater,
321 // the existing contents will be kept and the new space will be
322 // uninitialized.
323 void SetSize(size_t size) {
Joachim Bauch5b32f232018-03-07 20:02:26 +0100324 const size_t old_size = size_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200325 EnsureCapacityWithHeadroom(size, true);
326 size_ = size;
Joachim Bauch5b32f232018-03-07 20:02:26 +0100327 if (ZeroOnFree && size_ < old_size) {
328 ZeroTrailingData(old_size - size_);
329 }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200330 }
331
332 // Ensure that the buffer size can be increased to at least capacity without
333 // further reallocation. (Of course, this operation might need to reallocate
334 // the buffer.)
335 void EnsureCapacity(size_t capacity) {
336 // Don't allocate extra headroom, since the user is asking for a specific
337 // capacity.
338 EnsureCapacityWithHeadroom(capacity, false);
339 }
340
341 // Resets the buffer to zero size without altering capacity. Works even if the
342 // buffer has been moved from.
343 void Clear() {
Joachim Bauch5b32f232018-03-07 20:02:26 +0100344 MaybeZeroCompleteBuffer();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200345 size_ = 0;
346 RTC_DCHECK(IsConsistent());
347 }
348
349 // Swaps two buffers. Also works for buffers that have been moved from.
350 friend void swap(BufferT& a, BufferT& b) {
351 using std::swap;
352 swap(a.size_, b.size_);
353 swap(a.capacity_, b.capacity_);
354 swap(a.data_, b.data_);
355 }
356
357 private:
358 void EnsureCapacityWithHeadroom(size_t capacity, bool extra_headroom) {
359 RTC_DCHECK(IsConsistent());
360 if (capacity <= capacity_)
361 return;
362
363 // If the caller asks for extra headroom, ensure that the new capacity is
364 // >= 1.5 times the old capacity. Any constant > 1 is sufficient to prevent
365 // quadratic behavior; as to why we pick 1.5 in particular, see
366 // https://github.com/facebook/folly/blob/master/folly/docs/FBVector.md and
367 // http://www.gahcep.com/cpp-internals-stl-vector-part-1/.
368 const size_t new_capacity =
369 extra_headroom ? std::max(capacity, capacity_ + capacity_ / 2)
370 : capacity;
371
372 std::unique_ptr<T[]> new_data(new T[new_capacity]);
373 std::memcpy(new_data.get(), data_.get(), size_ * sizeof(T));
Joachim Bauch5b32f232018-03-07 20:02:26 +0100374 MaybeZeroCompleteBuffer();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200375 data_ = std::move(new_data);
376 capacity_ = new_capacity;
377 RTC_DCHECK(IsConsistent());
378 }
379
Joachim Bauch5b32f232018-03-07 20:02:26 +0100380 // Zero the complete buffer if template argument "ZeroOnFree" is true.
381 void MaybeZeroCompleteBuffer() {
Karl Wiberg9d247952018-10-10 12:52:17 +0200382 if (ZeroOnFree && capacity_ > 0) {
Joachim Bauch5b32f232018-03-07 20:02:26 +0100383 // It would be sufficient to only zero "size_" elements, as all other
384 // methods already ensure that the unused capacity contains no sensitive
Karl Wiberg9d247952018-10-10 12:52:17 +0200385 // data---but better safe than sorry.
Joachim Bauch5b32f232018-03-07 20:02:26 +0100386 ExplicitZeroMemory(data_.get(), capacity_ * sizeof(T));
387 }
388 }
389
390 // Zero the first "count" elements of unused capacity.
391 void ZeroTrailingData(size_t count) {
392 RTC_DCHECK(IsConsistent());
393 RTC_DCHECK_LE(count, capacity_ - size_);
394 ExplicitZeroMemory(data_.get() + size_, count * sizeof(T));
395 }
396
Karl Wibergb3b01792018-10-10 12:44:12 +0200397 // Precondition for all methods except Clear, operator= and the destructor.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200398 // Postcondition for all methods except move construction and move
399 // assignment, which leave the moved-from object in a possibly inconsistent
400 // state.
401 bool IsConsistent() const {
402 return (data_ || capacity_ == 0) && capacity_ >= size_;
403 }
404
405 // Called when *this has been moved from. Conceptually it's a no-op, but we
406 // can mutate the state slightly to help subsequent sanity checks catch bugs.
407 void OnMovedFrom() {
Karl Wiberg4f3ce272018-10-17 13:34:33 +0200408 RTC_DCHECK(!data_); // Our heap block should have been stolen.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200409#if RTC_DCHECK_IS_ON
Karl Wibergb3b01792018-10-10 12:44:12 +0200410 // Ensure that *this is always inconsistent, to provoke bugs.
411 size_ = 1;
412 capacity_ = 0;
413#else
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200414 // Make *this consistent and empty. Shouldn't be necessary, but better safe
415 // than sorry.
416 size_ = 0;
417 capacity_ = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200418#endif
419 }
420
421 size_t size_;
422 size_t capacity_;
423 std::unique_ptr<T[]> data_;
424};
425
426// By far the most common sort of buffer.
427using Buffer = BufferT<uint8_t>;
428
Joachim Bauch5b32f232018-03-07 20:02:26 +0100429// A buffer that zeros memory before releasing it.
430template <typename T>
431using ZeroOnFreeBuffer = BufferT<T, true>;
432
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200433} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000434
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200435#endif // RTC_BASE_BUFFER_H_