blob: 68c6ad53d607ad9f4aa0917b0c4755424f8b834e [file] [log] [blame]
jbauch13041cf2016-02-25 06:16:52 -08001/*
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 09:11:00 -080011#ifndef RTC_BASE_COPY_ON_WRITE_BUFFER_H_
12#define RTC_BASE_COPY_ON_WRITE_BUFFER_H_
jbauch13041cf2016-02-25 06:16:52 -080013
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <stdint.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020015
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020016#include <algorithm>
Yves Gerey988cc082018-10-23 12:03:01 +020017#include <cstring>
18#include <string>
19#include <type_traits>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020020#include <utility>
jbauch13041cf2016-02-25 06:16:52 -080021
Mirko Bonadeid9708072019-01-25 20:26:48 +010022#include "api/scoped_refptr.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "rtc_base/buffer.h"
24#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080025#include "rtc_base/ref_counted_object.h"
Mirko Bonadei35214fc2019-09-23 14:54:28 +020026#include "rtc_base/system/rtc_export.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020027
28namespace rtc {
29
Mirko Bonadei35214fc2019-09-23 14:54:28 +020030class RTC_EXPORT CopyOnWriteBuffer {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020031 public:
32 // An empty buffer.
33 CopyOnWriteBuffer();
Amit Hilbuch45a2cd22019-03-12 16:58:02 -070034 // Share the data with an existing buffer.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020035 CopyOnWriteBuffer(const CopyOnWriteBuffer& buf);
36 // Move contents from an existing buffer.
37 CopyOnWriteBuffer(CopyOnWriteBuffer&& buf);
38
Jeroen de Borst4f6d2332018-07-18 11:25:12 -070039 // Construct a buffer from a string, convenient for unittests.
40 CopyOnWriteBuffer(const std::string& s);
41
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020042 // 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 14:37:10 +020060 offset_ = 0;
61 size_ = size;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020062 }
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 14:37:10 +020094 UnshareAndEnsureCapacity(capacity());
95 return buffer_->data<T>() + offset_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020096 }
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 14:37:10 +0200108 return buffer_->data<T>() + offset_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200109 }
110
111 size_t size() const {
112 RTC_DCHECK(IsConsistent());
Ilya Nikolaevskiy741bab02019-09-25 14:37:10 +0200113 return size_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200114 }
115
116 size_t capacity() const {
117 RTC_DCHECK(IsConsistent());
Ilya Nikolaevskiy741bab02019-09-25 14:37:10 +0200118 return buffer_ ? buffer_->capacity() - offset_ : 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200119 }
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 14:37:10 +0200126 offset_ = buf.offset_;
127 size_ = buf.size_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200128 }
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 14:37:10 +0200136 offset_ = buf.offset_;
137 size_ = buf.size_;
138 buf.offset_ = 0;
139 buf.size_ = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200140 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 14:37:10 +0200169 buffer_ = new RefCountedObject<Buffer>(data, size, capacity());
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200170 } else {
171 buffer_->SetData(data, size);
172 }
Ilya Nikolaevskiy741bab02019-09-25 14:37:10 +0200173 offset_ = 0;
174 size_ = size;
175
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200176 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 14:37:10 +0200192 offset_ = buf.offset_;
193 size_ = buf.size_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200194 }
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 14:37:10 +0200205 offset_ = 0;
206 size_ = size;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200207 RTC_DCHECK(IsConsistent());
208 return;
209 }
210
Ilya Nikolaevskiy741bab02019-09-25 14:37:10 +0200211 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 07:52:50 +0200215 buffer_->AppendData(data, size);
Ilya Nikolaevskiy741bab02019-09-25 14:37:10 +0200216 size_ += size;
217
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200218 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 14:37:10 +0200251 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 07:52:50 +0200262 }
263
264 private:
265 // Create a copy of the underlying data if it is referenced from other Buffer
Ilya Nikolaevskiy741bab02019-09-25 14:37:10 +0200266 // objects or there is not enough capacity.
267 void UnshareAndEnsureCapacity(size_t new_capacity);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200268
269 // Pre- and postcondition of all methods.
Ilya Nikolaevskiy741bab02019-09-25 14:37:10 +0200270 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 07:52:50 +0200278
279 // buffer_ is either null, or points to an rtc::Buffer with capacity > 0.
280 scoped_refptr<RefCountedObject<Buffer>> buffer_;
Ilya Nikolaevskiy741bab02019-09-25 14:37:10 +0200281 // 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 07:52:50 +0200286};
287
288} // namespace rtc
jbauch13041cf2016-02-25 06:16:52 -0800289
Steve Anton10542f22019-01-11 09:11:00 -0800290#endif // RTC_BASE_COPY_ON_WRITE_BUFFER_H_