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 | |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 14 | #include <algorithm> // std::swap (pre-C++11) |
| 15 | #include <cassert> |
| 16 | #include <cstring> |
kwiberg | 8fb3557 | 2016-02-11 13:36:43 -0800 | [diff] [blame] | 17 | #include <memory> |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 18 | #include <utility> // std::swap (C++11 and later) |
kwiberg | 36220ae | 2016-01-12 07:24:19 -0800 | [diff] [blame] | 19 | |
kwiberg | 8fb3557 | 2016-02-11 13:36:43 -0800 | [diff] [blame] | 20 | #include "webrtc/base/constructormagic.h" |
kwiberg | 36220ae | 2016-01-12 07:24:19 -0800 | [diff] [blame] | 21 | #include "webrtc/base/deprecation.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 | |
| 27 | // (Internal; please don't use outside this file.) ByteType<T>::t is int if T |
| 28 | // is uint8_t, int8_t, or char; otherwise, it's a compilation error. Use like |
| 29 | // this: |
| 30 | // |
| 31 | // template <typename T, typename ByteType<T>::t = 0> |
| 32 | // void foo(T* x); |
| 33 | // |
| 34 | // to let foo<T> be defined only for byte-sized integers. |
| 35 | template <typename T> |
| 36 | struct ByteType { |
| 37 | private: |
| 38 | static int F(uint8_t*); |
| 39 | static int F(int8_t*); |
| 40 | static int F(char*); |
| 41 | |
| 42 | public: |
| 43 | using t = decltype(F(static_cast<T*>(nullptr))); |
| 44 | }; |
| 45 | |
| 46 | } // namespace internal |
| 47 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 48 | // Basic buffer class, can be grown and shrunk dynamically. |
| 49 | // Unlike std::string/vector, does not initialize data when expanding capacity. |
noahric | 27dfe20 | 2015-10-23 06:01:10 -0700 | [diff] [blame] | 50 | class Buffer { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 51 | public: |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 52 | Buffer(); // An empty buffer. |
| 53 | Buffer(const Buffer& buf); // Copy size and contents of an existing buffer. |
| 54 | Buffer(Buffer&& buf); // Move contents from an existing buffer. |
| 55 | |
| 56 | // Construct a buffer with the specified number of uninitialized bytes. |
kwiberg@webrtc.org | eebcab5 | 2015-03-24 09:19:06 +0000 | [diff] [blame] | 57 | explicit Buffer(size_t size); |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 58 | Buffer(size_t size, size_t capacity); |
| 59 | |
| 60 | // Construct a buffer and copy the specified number of bytes into it. The |
| 61 | // source array may be (const) uint8_t*, int8_t*, or char*. |
Karl Wiberg | c56ac1e | 2015-05-04 14:54:55 +0200 | [diff] [blame] | 62 | template <typename T, typename internal::ByteType<T>::t = 0> |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 63 | Buffer(const T* data, size_t size) |
| 64 | : Buffer(data, size, size) {} |
Karl Wiberg | c56ac1e | 2015-05-04 14:54:55 +0200 | [diff] [blame] | 65 | template <typename T, typename internal::ByteType<T>::t = 0> |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 66 | Buffer(const T* data, size_t size, size_t capacity) |
| 67 | : Buffer(size, capacity) { |
| 68 | std::memcpy(data_.get(), data, size); |
| 69 | } |
| 70 | |
| 71 | // Construct a buffer from the contents of an array. |
| 72 | template <typename T, size_t N, typename internal::ByteType<T>::t = 0> |
| 73 | Buffer(const T(&array)[N]) |
| 74 | : Buffer(array, N) {} |
| 75 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 76 | ~Buffer(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 77 | |
Karl Wiberg | c56ac1e | 2015-05-04 14:54:55 +0200 | [diff] [blame] | 78 | // Get a pointer to the data. Just .data() will give you a (const) uint8_t*, |
| 79 | // but you may also use .data<int8_t>() and .data<char>(). |
| 80 | template <typename T = uint8_t, typename internal::ByteType<T>::t = 0> |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 81 | const T* data() const { |
| 82 | assert(IsConsistent()); |
| 83 | return reinterpret_cast<T*>(data_.get()); |
| 84 | } |
Karl Wiberg | c56ac1e | 2015-05-04 14:54:55 +0200 | [diff] [blame] | 85 | template <typename T = uint8_t, typename internal::ByteType<T>::t = 0> |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 86 | T* data() { |
| 87 | assert(IsConsistent()); |
| 88 | return reinterpret_cast<T*>(data_.get()); |
| 89 | } |
| 90 | |
| 91 | size_t size() const { |
| 92 | assert(IsConsistent()); |
| 93 | return size_; |
| 94 | } |
| 95 | size_t capacity() const { |
| 96 | assert(IsConsistent()); |
| 97 | return capacity_; |
| 98 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 99 | |
| 100 | Buffer& operator=(const Buffer& buf) { |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 101 | if (&buf != this) |
| 102 | SetData(buf.data(), buf.size()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 103 | return *this; |
| 104 | } |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 105 | Buffer& operator=(Buffer&& buf) { |
| 106 | assert(IsConsistent()); |
| 107 | assert(buf.IsConsistent()); |
| 108 | size_ = buf.size_; |
| 109 | capacity_ = buf.capacity_; |
kwiberg | 0eb15ed | 2015-12-17 03:04:15 -0800 | [diff] [blame] | 110 | data_ = std::move(buf.data_); |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 111 | buf.OnMovedFrom(); |
| 112 | return *this; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 115 | bool operator==(const Buffer& buf) const { |
| 116 | assert(IsConsistent()); |
| 117 | return size_ == buf.size() && memcmp(data_.get(), buf.data(), size_) == 0; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 118 | } |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 119 | |
| 120 | bool operator!=(const Buffer& buf) const { return !(*this == buf); } |
| 121 | |
| 122 | // Replace the contents of the buffer. Accepts the same types as the |
| 123 | // constructors. |
Karl Wiberg | c56ac1e | 2015-05-04 14:54:55 +0200 | [diff] [blame] | 124 | template <typename T, typename internal::ByteType<T>::t = 0> |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 125 | void SetData(const T* data, size_t size) { |
| 126 | assert(IsConsistent()); |
| 127 | size_ = 0; |
| 128 | AppendData(data, size); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 129 | } |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 130 | template <typename T, size_t N, typename internal::ByteType<T>::t = 0> |
| 131 | void SetData(const T(&array)[N]) { |
| 132 | SetData(array, N); |
| 133 | } |
| 134 | void SetData(const Buffer& buf) { SetData(buf.data(), buf.size()); } |
| 135 | |
| 136 | // Append data to the buffer. Accepts the same types as the constructors. |
Karl Wiberg | c56ac1e | 2015-05-04 14:54:55 +0200 | [diff] [blame] | 137 | template <typename T, typename internal::ByteType<T>::t = 0> |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 138 | void AppendData(const T* data, size_t size) { |
| 139 | assert(IsConsistent()); |
| 140 | const size_t new_size = size_ + size; |
| 141 | EnsureCapacity(new_size); |
| 142 | std::memcpy(data_.get() + size_, data, size); |
| 143 | size_ = new_size; |
| 144 | assert(IsConsistent()); |
| 145 | } |
| 146 | template <typename T, size_t N, typename internal::ByteType<T>::t = 0> |
| 147 | void AppendData(const T(&array)[N]) { |
| 148 | AppendData(array, N); |
| 149 | } |
| 150 | void AppendData(const Buffer& buf) { AppendData(buf.data(), buf.size()); } |
| 151 | |
| 152 | // Sets the size of the buffer. If the new size is smaller than the old, the |
| 153 | // buffer contents will be kept but truncated; if the new size is greater, |
| 154 | // the existing contents will be kept and the new space will be |
| 155 | // uninitialized. |
kwiberg@webrtc.org | eebcab5 | 2015-03-24 09:19:06 +0000 | [diff] [blame] | 156 | void SetSize(size_t size) { |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 157 | EnsureCapacity(size); |
kwiberg@webrtc.org | eebcab5 | 2015-03-24 09:19:06 +0000 | [diff] [blame] | 158 | size_ = size; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 159 | } |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 160 | |
| 161 | // Ensure that the buffer size can be increased to at least capacity without |
| 162 | // further reallocation. (Of course, this operation might need to reallocate |
| 163 | // the buffer.) |
| 164 | void EnsureCapacity(size_t capacity) { |
| 165 | assert(IsConsistent()); |
| 166 | if (capacity <= capacity_) |
| 167 | return; |
kwiberg | 8fb3557 | 2016-02-11 13:36:43 -0800 | [diff] [blame] | 168 | std::unique_ptr<uint8_t[]> new_data(new uint8_t[capacity]); |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 169 | std::memcpy(new_data.get(), data_.get(), size_); |
kwiberg | 0eb15ed | 2015-12-17 03:04:15 -0800 | [diff] [blame] | 170 | data_ = std::move(new_data); |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 171 | capacity_ = capacity; |
| 172 | assert(IsConsistent()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 173 | } |
| 174 | |
kwiberg | 0eb15ed | 2015-12-17 03:04:15 -0800 | [diff] [blame] | 175 | // b.Pass() does the same thing as std::move(b). |
kwiberg | 36220ae | 2016-01-12 07:24:19 -0800 | [diff] [blame] | 176 | // Deprecated; remove in March 2016 (bug 5373). |
| 177 | RTC_DEPRECATED Buffer&& Pass() { return DEPRECATED_Pass(); } |
| 178 | Buffer&& DEPRECATED_Pass() { |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 179 | assert(IsConsistent()); |
kwiberg | cea7c2f | 2016-01-07 05:52:04 -0800 | [diff] [blame] | 180 | return std::move(*this); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 183 | // Resets the buffer to zero size and capacity. Works even if the buffer has |
| 184 | // been moved from. |
| 185 | void Clear() { |
| 186 | data_.reset(); |
| 187 | size_ = 0; |
| 188 | capacity_ = 0; |
| 189 | assert(IsConsistent()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 192 | // Swaps two buffers. Also works for buffers that have been moved from. |
| 193 | friend void swap(Buffer& a, Buffer& b) { |
| 194 | using std::swap; |
| 195 | swap(a.size_, b.size_); |
| 196 | swap(a.capacity_, b.capacity_); |
| 197 | swap(a.data_, b.data_); |
| 198 | } |
| 199 | |
| 200 | private: |
| 201 | // Precondition for all methods except Clear and the destructor. |
| 202 | // Postcondition for all methods except move construction and move |
| 203 | // assignment, which leave the moved-from object in a possibly inconsistent |
| 204 | // state. |
| 205 | bool IsConsistent() const { |
| 206 | return (data_ || capacity_ == 0) && capacity_ >= size_; |
| 207 | } |
| 208 | |
| 209 | // Called when *this has been moved from. Conceptually it's a no-op, but we |
| 210 | // can mutate the state slightly to help subsequent sanity checks catch bugs. |
| 211 | void OnMovedFrom() { |
| 212 | #ifdef NDEBUG |
| 213 | // Make *this consistent and empty. Shouldn't be necessary, but better safe |
| 214 | // than sorry. |
| 215 | size_ = 0; |
| 216 | capacity_ = 0; |
| 217 | #else |
| 218 | // Ensure that *this is always inconsistent, to provoke bugs. |
| 219 | size_ = 1; |
| 220 | capacity_ = 0; |
| 221 | #endif |
| 222 | } |
| 223 | |
kwiberg@webrtc.org | eebcab5 | 2015-03-24 09:19:06 +0000 | [diff] [blame] | 224 | size_t size_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 225 | size_t capacity_; |
kwiberg | 8fb3557 | 2016-02-11 13:36:43 -0800 | [diff] [blame] | 226 | std::unique_ptr<uint8_t[]> data_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 227 | }; |
| 228 | |
| 229 | } // namespace rtc |
| 230 | |
| 231 | #endif // WEBRTC_BASE_BUFFER_H_ |