blob: 68c6ad53d607ad9f4aa0917b0c4755424f8b834e [file] [log] [blame]
jbauch13041cf2016-02-25 14:16:521/*
2 * Copyright 2016 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
Steve Anton10542f22019-01-11 17:11:0011#ifndef RTC_BASE_COPY_ON_WRITE_BUFFER_H_
12#define RTC_BASE_COPY_ON_WRITE_BUFFER_H_
jbauch13041cf2016-02-25 14:16:5213
Yves Gerey988cc082018-10-23 10:03:0114#include <stdint.h>
Jonas Olssona4d87372019-07-05 17:08:3315
Henrik Kjellanderec78f1c2017-06-29 05:52:5016#include <algorithm>
Yves Gerey988cc082018-10-23 10:03:0117#include <cstring>
18#include <string>
19#include <type_traits>
Henrik Kjellanderec78f1c2017-06-29 05:52:5020#include <utility>
jbauch13041cf2016-02-25 14:16:5221
Mirko Bonadeid9708072019-01-25 19:26:4822#include "api/scoped_refptr.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3123#include "rtc_base/buffer.h"
24#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 17:11:0025#include "rtc_base/ref_counted_object.h"
Mirko Bonadei35214fc2019-09-23 12:54:2826#include "rtc_base/system/rtc_export.h"
Henrik Kjellanderec78f1c2017-06-29 05:52:5027
28namespace rtc {
29
Mirko Bonadei35214fc2019-09-23 12:54:2830class RTC_EXPORT CopyOnWriteBuffer {
Henrik Kjellanderec78f1c2017-06-29 05:52:5031 public:
32 // An empty buffer.
33 CopyOnWriteBuffer();
Amit Hilbuch45a2cd22019-03-12 23:58:0234 // Share the data with an existing buffer.
Henrik Kjellanderec78f1c2017-06-29 05:52:5035 CopyOnWriteBuffer(const CopyOnWriteBuffer& buf);
36 // Move contents from an existing buffer.
37 CopyOnWriteBuffer(CopyOnWriteBuffer&& buf);
38
Jeroen de Borst4f6d2332018-07-18 18:25:1239 // Construct a buffer from a string, convenient for unittests.
40 CopyOnWriteBuffer(const std::string& s);
41
Henrik Kjellanderec78f1c2017-06-29 05:52:5042 // Construct a buffer with the specified number of uninitialized bytes.
43 explicit CopyOnWriteBuffer(size_t size);
44 CopyOnWriteBuffer(size_t size, size_t capacity);
45
46 // Construct a buffer and copy the specified number of bytes into it. The
47 // source array may be (const) uint8_t*, int8_t*, or char*.
48 template <typename T,
49 typename std::enable_if<
50 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
51 CopyOnWriteBuffer(const T* data, size_t size)
52 : CopyOnWriteBuffer(data, size, size) {}
53 template <typename T,
54 typename std::enable_if<
55 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
56 CopyOnWriteBuffer(const T* data, size_t size, size_t capacity)
57 : CopyOnWriteBuffer(size, capacity) {
58 if (buffer_) {
59 std::memcpy(buffer_->data(), data, size);
Ilya Nikolaevskiy741bab02019-09-25 12:37:1060 offset_ = 0;
61 size_ = size;
Henrik Kjellanderec78f1c2017-06-29 05:52:5062 }
63 }
64
65 // Construct a buffer from the contents of an array.
66 template <typename T,
67 size_t N,
68 typename std::enable_if<
69 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
70 CopyOnWriteBuffer(const T (&array)[N]) // NOLINT: runtime/explicit
71 : CopyOnWriteBuffer(array, N) {}
72
73 ~CopyOnWriteBuffer();
74
75 // Get a pointer to the data. Just .data() will give you a (const) uint8_t*,
76 // but you may also use .data<int8_t>() and .data<char>().
77 template <typename T = uint8_t,
78 typename std::enable_if<
79 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
80 const T* data() const {
81 return cdata<T>();
82 }
83
84 // Get writable pointer to the data. This will create a copy of the underlying
85 // data if it is shared with other buffers.
86 template <typename T = uint8_t,
87 typename std::enable_if<
88 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
89 T* data() {
90 RTC_DCHECK(IsConsistent());
91 if (!buffer_) {
92 return nullptr;
93 }
Ilya Nikolaevskiy741bab02019-09-25 12:37:1094 UnshareAndEnsureCapacity(capacity());
95 return buffer_->data<T>() + offset_;
Henrik Kjellanderec78f1c2017-06-29 05:52:5096 }
97
98 // Get const pointer to the data. This will not create a copy of the
99 // underlying data if it is shared with other buffers.
100 template <typename T = uint8_t,
101 typename std::enable_if<
102 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
103 const T* cdata() const {
104 RTC_DCHECK(IsConsistent());
105 if (!buffer_) {
106 return nullptr;
107 }
Ilya Nikolaevskiy741bab02019-09-25 12:37:10108 return buffer_->data<T>() + offset_;
Henrik Kjellanderec78f1c2017-06-29 05:52:50109 }
110
111 size_t size() const {
112 RTC_DCHECK(IsConsistent());
Ilya Nikolaevskiy741bab02019-09-25 12:37:10113 return size_;
Henrik Kjellanderec78f1c2017-06-29 05:52:50114 }
115
116 size_t capacity() const {
117 RTC_DCHECK(IsConsistent());
Ilya Nikolaevskiy741bab02019-09-25 12:37:10118 return buffer_ ? buffer_->capacity() - offset_ : 0;
Henrik Kjellanderec78f1c2017-06-29 05:52:50119 }
120
121 CopyOnWriteBuffer& operator=(const CopyOnWriteBuffer& buf) {
122 RTC_DCHECK(IsConsistent());
123 RTC_DCHECK(buf.IsConsistent());
124 if (&buf != this) {
125 buffer_ = buf.buffer_;
Ilya Nikolaevskiy741bab02019-09-25 12:37:10126 offset_ = buf.offset_;
127 size_ = buf.size_;
Henrik Kjellanderec78f1c2017-06-29 05:52:50128 }
129 return *this;
130 }
131
132 CopyOnWriteBuffer& operator=(CopyOnWriteBuffer&& buf) {
133 RTC_DCHECK(IsConsistent());
134 RTC_DCHECK(buf.IsConsistent());
135 buffer_ = std::move(buf.buffer_);
Ilya Nikolaevskiy741bab02019-09-25 12:37:10136 offset_ = buf.offset_;
137 size_ = buf.size_;
138 buf.offset_ = 0;
139 buf.size_ = 0;
Henrik Kjellanderec78f1c2017-06-29 05:52:50140 return *this;
141 }
142
143 bool operator==(const CopyOnWriteBuffer& buf) const;
144
145 bool operator!=(const CopyOnWriteBuffer& buf) const {
146 return !(*this == buf);
147 }
148
149 uint8_t& operator[](size_t index) {
150 RTC_DCHECK_LT(index, size());
151 return data()[index];
152 }
153
154 uint8_t operator[](size_t index) const {
155 RTC_DCHECK_LT(index, size());
156 return cdata()[index];
157 }
158
159 // Replace the contents of the buffer. Accepts the same types as the
160 // constructors.
161 template <typename T,
162 typename std::enable_if<
163 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
164 void SetData(const T* data, size_t size) {
165 RTC_DCHECK(IsConsistent());
166 if (!buffer_) {
167 buffer_ = size > 0 ? new RefCountedObject<Buffer>(data, size) : nullptr;
168 } else if (!buffer_->HasOneRef()) {
Ilya Nikolaevskiy741bab02019-09-25 12:37:10169 buffer_ = new RefCountedObject<Buffer>(data, size, capacity());
Henrik Kjellanderec78f1c2017-06-29 05:52:50170 } else {
171 buffer_->SetData(data, size);
172 }
Ilya Nikolaevskiy741bab02019-09-25 12:37:10173 offset_ = 0;
174 size_ = size;
175
Henrik Kjellanderec78f1c2017-06-29 05:52:50176 RTC_DCHECK(IsConsistent());
177 }
178
179 template <typename T,
180 size_t N,
181 typename std::enable_if<
182 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
183 void SetData(const T (&array)[N]) {
184 SetData(array, N);
185 }
186
187 void SetData(const CopyOnWriteBuffer& buf) {
188 RTC_DCHECK(IsConsistent());
189 RTC_DCHECK(buf.IsConsistent());
190 if (&buf != this) {
191 buffer_ = buf.buffer_;
Ilya Nikolaevskiy741bab02019-09-25 12:37:10192 offset_ = buf.offset_;
193 size_ = buf.size_;
Henrik Kjellanderec78f1c2017-06-29 05:52:50194 }
195 }
196
197 // Append data to the buffer. Accepts the same types as the constructors.
198 template <typename T,
199 typename std::enable_if<
200 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
201 void AppendData(const T* data, size_t size) {
202 RTC_DCHECK(IsConsistent());
203 if (!buffer_) {
204 buffer_ = new RefCountedObject<Buffer>(data, size);
Ilya Nikolaevskiy741bab02019-09-25 12:37:10205 offset_ = 0;
206 size_ = size;
Henrik Kjellanderec78f1c2017-06-29 05:52:50207 RTC_DCHECK(IsConsistent());
208 return;
209 }
210
Ilya Nikolaevskiy741bab02019-09-25 12:37:10211 UnshareAndEnsureCapacity(std::max(capacity(), size_ + size));
212
213 buffer_->SetSize(offset_ +
214 size_); // Remove data to the right of the slice.
Henrik Kjellanderec78f1c2017-06-29 05:52:50215 buffer_->AppendData(data, size);
Ilya Nikolaevskiy741bab02019-09-25 12:37:10216 size_ += size;
217
Henrik Kjellanderec78f1c2017-06-29 05:52:50218 RTC_DCHECK(IsConsistent());
219 }
220
221 template <typename T,
222 size_t N,
223 typename std::enable_if<
224 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
225 void AppendData(const T (&array)[N]) {
226 AppendData(array, N);
227 }
228
229 void AppendData(const CopyOnWriteBuffer& buf) {
230 AppendData(buf.data(), buf.size());
231 }
232
233 // Sets the size of the buffer. If the new size is smaller than the old, the
234 // buffer contents will be kept but truncated; if the new size is greater,
235 // the existing contents will be kept and the new space will be
236 // uninitialized.
237 void SetSize(size_t size);
238
239 // Ensure that the buffer size can be increased to at least capacity without
240 // further reallocation. (Of course, this operation might need to reallocate
241 // the buffer.)
242 void EnsureCapacity(size_t capacity);
243
244 // Resets the buffer to zero size without altering capacity. Works even if the
245 // buffer has been moved from.
246 void Clear();
247
248 // Swaps two buffers.
249 friend void swap(CopyOnWriteBuffer& a, CopyOnWriteBuffer& b) {
250 std::swap(a.buffer_, b.buffer_);
Ilya Nikolaevskiy741bab02019-09-25 12:37:10251 std::swap(a.offset_, b.offset_);
252 std::swap(a.size_, b.size_);
253 }
254
255 CopyOnWriteBuffer Slice(size_t offset, size_t length) const {
256 CopyOnWriteBuffer slice(*this);
257 RTC_DCHECK_LE(offset, size_);
258 RTC_DCHECK_LE(length + offset, size_);
259 slice.offset_ += offset;
260 slice.size_ = length;
261 return slice;
Henrik Kjellanderec78f1c2017-06-29 05:52:50262 }
263
264 private:
265 // Create a copy of the underlying data if it is referenced from other Buffer
Ilya Nikolaevskiy741bab02019-09-25 12:37:10266 // objects or there is not enough capacity.
267 void UnshareAndEnsureCapacity(size_t new_capacity);
Henrik Kjellanderec78f1c2017-06-29 05:52:50268
269 // Pre- and postcondition of all methods.
Ilya Nikolaevskiy741bab02019-09-25 12:37:10270 bool IsConsistent() const {
271 if (buffer_) {
272 return buffer_->capacity() > 0 && offset_ <= buffer_->size() &&
273 offset_ + size_ <= buffer_->size();
274 } else {
275 return size_ == 0 && offset_ == 0;
276 }
277 }
Henrik Kjellanderec78f1c2017-06-29 05:52:50278
279 // buffer_ is either null, or points to an rtc::Buffer with capacity > 0.
280 scoped_refptr<RefCountedObject<Buffer>> buffer_;
Ilya Nikolaevskiy741bab02019-09-25 12:37:10281 // This buffer may represent a slice of a original data.
282 size_t offset_; // Offset of a current slice in the original data in buffer_.
283 // Should be 0 if the buffer_ is empty.
284 size_t size_; // Size of a current slice in the original data in buffer_.
285 // Should be 0 if the buffer_ is empty.
Henrik Kjellanderec78f1c2017-06-29 05:52:50286};
287
288} // namespace rtc
jbauch13041cf2016-02-25 14:16:52289
Steve Anton10542f22019-01-11 17:11:00290#endif // RTC_BASE_COPY_ON_WRITE_BUFFER_H_