blob: ea4868fc1464d2bccdafe63abfe83f1399e2c374 [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"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020026
27namespace rtc {
28
29class CopyOnWriteBuffer {
30 public:
31 // An empty buffer.
32 CopyOnWriteBuffer();
Amit Hilbuch45a2cd22019-03-12 16:58:02 -070033 // Share the data with an existing buffer.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020034 CopyOnWriteBuffer(const CopyOnWriteBuffer& buf);
35 // Move contents from an existing buffer.
36 CopyOnWriteBuffer(CopyOnWriteBuffer&& buf);
37
Jeroen de Borst4f6d2332018-07-18 11:25:12 -070038 // Construct a buffer from a string, convenient for unittests.
39 CopyOnWriteBuffer(const std::string& s);
40
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020041 // Construct a buffer with the specified number of uninitialized bytes.
42 explicit CopyOnWriteBuffer(size_t size);
43 CopyOnWriteBuffer(size_t size, size_t capacity);
44
45 // Construct a buffer and copy the specified number of bytes into it. The
46 // source array may be (const) uint8_t*, int8_t*, or char*.
47 template <typename T,
48 typename std::enable_if<
49 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
50 CopyOnWriteBuffer(const T* data, size_t size)
51 : CopyOnWriteBuffer(data, size, size) {}
52 template <typename T,
53 typename std::enable_if<
54 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
55 CopyOnWriteBuffer(const T* data, size_t size, size_t capacity)
56 : CopyOnWriteBuffer(size, capacity) {
57 if (buffer_) {
58 std::memcpy(buffer_->data(), data, size);
Ilya Nikolaevskiy741bab02019-09-25 14:37:10 +020059 offset_ = 0;
60 size_ = size;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020061 }
62 }
63
64 // Construct a buffer from the contents of an array.
65 template <typename T,
66 size_t N,
67 typename std::enable_if<
68 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
69 CopyOnWriteBuffer(const T (&array)[N]) // NOLINT: runtime/explicit
70 : CopyOnWriteBuffer(array, N) {}
71
72 ~CopyOnWriteBuffer();
73
74 // Get a pointer to the data. Just .data() will give you a (const) uint8_t*,
75 // but you may also use .data<int8_t>() and .data<char>().
76 template <typename T = uint8_t,
77 typename std::enable_if<
78 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
79 const T* data() const {
80 return cdata<T>();
81 }
82
83 // Get writable pointer to the data. This will create a copy of the underlying
84 // data if it is shared with other buffers.
85 template <typename T = uint8_t,
86 typename std::enable_if<
87 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
88 T* data() {
89 RTC_DCHECK(IsConsistent());
90 if (!buffer_) {
91 return nullptr;
92 }
Ilya Nikolaevskiy741bab02019-09-25 14:37:10 +020093 UnshareAndEnsureCapacity(capacity());
94 return buffer_->data<T>() + offset_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020095 }
96
97 // Get const pointer to the data. This will not create a copy of the
98 // underlying data if it is shared with other buffers.
99 template <typename T = uint8_t,
100 typename std::enable_if<
101 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
102 const T* cdata() const {
103 RTC_DCHECK(IsConsistent());
104 if (!buffer_) {
105 return nullptr;
106 }
Ilya Nikolaevskiy741bab02019-09-25 14:37:10 +0200107 return buffer_->data<T>() + offset_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200108 }
109
110 size_t size() const {
111 RTC_DCHECK(IsConsistent());
Ilya Nikolaevskiy741bab02019-09-25 14:37:10 +0200112 return size_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200113 }
114
115 size_t capacity() const {
116 RTC_DCHECK(IsConsistent());
Ilya Nikolaevskiy741bab02019-09-25 14:37:10 +0200117 return buffer_ ? buffer_->capacity() - offset_ : 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200118 }
119
120 CopyOnWriteBuffer& operator=(const CopyOnWriteBuffer& buf) {
121 RTC_DCHECK(IsConsistent());
122 RTC_DCHECK(buf.IsConsistent());
123 if (&buf != this) {
124 buffer_ = buf.buffer_;
Ilya Nikolaevskiy741bab02019-09-25 14:37:10 +0200125 offset_ = buf.offset_;
126 size_ = buf.size_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200127 }
128 return *this;
129 }
130
131 CopyOnWriteBuffer& operator=(CopyOnWriteBuffer&& buf) {
132 RTC_DCHECK(IsConsistent());
133 RTC_DCHECK(buf.IsConsistent());
134 buffer_ = std::move(buf.buffer_);
Ilya Nikolaevskiy741bab02019-09-25 14:37:10 +0200135 offset_ = buf.offset_;
136 size_ = buf.size_;
137 buf.offset_ = 0;
138 buf.size_ = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200139 return *this;
140 }
141
142 bool operator==(const CopyOnWriteBuffer& buf) const;
143
144 bool operator!=(const CopyOnWriteBuffer& buf) const {
145 return !(*this == buf);
146 }
147
148 uint8_t& operator[](size_t index) {
149 RTC_DCHECK_LT(index, size());
150 return data()[index];
151 }
152
153 uint8_t operator[](size_t index) const {
154 RTC_DCHECK_LT(index, size());
155 return cdata()[index];
156 }
157
158 // Replace the contents of the buffer. Accepts the same types as the
159 // constructors.
160 template <typename T,
161 typename std::enable_if<
162 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
163 void SetData(const T* data, size_t size) {
164 RTC_DCHECK(IsConsistent());
165 if (!buffer_) {
166 buffer_ = size > 0 ? new RefCountedObject<Buffer>(data, size) : nullptr;
167 } else if (!buffer_->HasOneRef()) {
Ilya Nikolaevskiy741bab02019-09-25 14:37:10 +0200168 buffer_ = new RefCountedObject<Buffer>(data, size, capacity());
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200169 } else {
170 buffer_->SetData(data, size);
171 }
Ilya Nikolaevskiy741bab02019-09-25 14:37:10 +0200172 offset_ = 0;
173 size_ = size;
174
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200175 RTC_DCHECK(IsConsistent());
176 }
177
178 template <typename T,
179 size_t N,
180 typename std::enable_if<
181 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
182 void SetData(const T (&array)[N]) {
183 SetData(array, N);
184 }
185
186 void SetData(const CopyOnWriteBuffer& buf) {
187 RTC_DCHECK(IsConsistent());
188 RTC_DCHECK(buf.IsConsistent());
189 if (&buf != this) {
190 buffer_ = buf.buffer_;
Ilya Nikolaevskiy741bab02019-09-25 14:37:10 +0200191 offset_ = buf.offset_;
192 size_ = buf.size_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200193 }
194 }
195
196 // Append data to the buffer. Accepts the same types as the constructors.
197 template <typename T,
198 typename std::enable_if<
199 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
200 void AppendData(const T* data, size_t size) {
201 RTC_DCHECK(IsConsistent());
202 if (!buffer_) {
203 buffer_ = new RefCountedObject<Buffer>(data, size);
Ilya Nikolaevskiy741bab02019-09-25 14:37:10 +0200204 offset_ = 0;
205 size_ = size;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200206 RTC_DCHECK(IsConsistent());
207 return;
208 }
209
Ilya Nikolaevskiy741bab02019-09-25 14:37:10 +0200210 UnshareAndEnsureCapacity(std::max(capacity(), size_ + size));
211
212 buffer_->SetSize(offset_ +
213 size_); // Remove data to the right of the slice.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200214 buffer_->AppendData(data, size);
Ilya Nikolaevskiy741bab02019-09-25 14:37:10 +0200215 size_ += size;
216
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200217 RTC_DCHECK(IsConsistent());
218 }
219
220 template <typename T,
221 size_t N,
222 typename std::enable_if<
223 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
224 void AppendData(const T (&array)[N]) {
225 AppendData(array, N);
226 }
227
228 void AppendData(const CopyOnWriteBuffer& buf) {
229 AppendData(buf.data(), buf.size());
230 }
231
232 // Sets the size of the buffer. If the new size is smaller than the old, the
233 // buffer contents will be kept but truncated; if the new size is greater,
234 // the existing contents will be kept and the new space will be
235 // uninitialized.
236 void SetSize(size_t size);
237
238 // Ensure that the buffer size can be increased to at least capacity without
239 // further reallocation. (Of course, this operation might need to reallocate
240 // the buffer.)
241 void EnsureCapacity(size_t capacity);
242
243 // Resets the buffer to zero size without altering capacity. Works even if the
244 // buffer has been moved from.
245 void Clear();
246
247 // Swaps two buffers.
248 friend void swap(CopyOnWriteBuffer& a, CopyOnWriteBuffer& b) {
249 std::swap(a.buffer_, b.buffer_);
Ilya Nikolaevskiy741bab02019-09-25 14:37:10 +0200250 std::swap(a.offset_, b.offset_);
251 std::swap(a.size_, b.size_);
252 }
253
254 CopyOnWriteBuffer Slice(size_t offset, size_t length) const {
255 CopyOnWriteBuffer slice(*this);
256 RTC_DCHECK_LE(offset, size_);
257 RTC_DCHECK_LE(length + offset, size_);
258 slice.offset_ += offset;
259 slice.size_ = length;
260 return slice;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200261 }
262
263 private:
264 // Create a copy of the underlying data if it is referenced from other Buffer
Ilya Nikolaevskiy741bab02019-09-25 14:37:10 +0200265 // objects or there is not enough capacity.
266 void UnshareAndEnsureCapacity(size_t new_capacity);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200267
268 // Pre- and postcondition of all methods.
Ilya Nikolaevskiy741bab02019-09-25 14:37:10 +0200269 bool IsConsistent() const {
270 if (buffer_) {
271 return buffer_->capacity() > 0 && offset_ <= buffer_->size() &&
272 offset_ + size_ <= buffer_->size();
273 } else {
274 return size_ == 0 && offset_ == 0;
275 }
276 }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200277
278 // buffer_ is either null, or points to an rtc::Buffer with capacity > 0.
279 scoped_refptr<RefCountedObject<Buffer>> buffer_;
Ilya Nikolaevskiy741bab02019-09-25 14:37:10 +0200280 // This buffer may represent a slice of a original data.
281 size_t offset_; // Offset of a current slice in the original data in buffer_.
282 // Should be 0 if the buffer_ is empty.
283 size_t size_; // Size of a current slice in the original data in buffer_.
284 // Should be 0 if the buffer_ is empty.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200285};
286
287} // namespace rtc
jbauch13041cf2016-02-25 06:16:52 -0800288
Steve Anton10542f22019-01-11 09:11:00 -0800289#endif // RTC_BASE_COPY_ON_WRITE_BUFFER_H_