blob: 70833d6c62b7a50d778e64ca6f62eb43a2389905 [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
11#ifndef WEBRTC_BASE_COPYONWRITEBUFFER_H_
12#define WEBRTC_BASE_COPYONWRITEBUFFER_H_
13
14#include <algorithm>
15#include <utility>
16
17#include "webrtc/base/buffer.h"
18#include "webrtc/base/checks.h"
19#include "webrtc/base/refcount.h"
20#include "webrtc/base/scoped_ref_ptr.h"
21
22namespace rtc {
23
24class CopyOnWriteBuffer {
25 public:
26 // An empty buffer.
27 CopyOnWriteBuffer();
28 // Copy size and contents of an existing buffer.
29 CopyOnWriteBuffer(const CopyOnWriteBuffer& buf);
30 // Move contents from an existing buffer.
31 CopyOnWriteBuffer(CopyOnWriteBuffer&& buf);
32
33 // Construct a buffer with the specified number of uninitialized bytes.
34 explicit CopyOnWriteBuffer(size_t size);
35 CopyOnWriteBuffer(size_t size, size_t capacity);
36
37 // Construct a buffer and copy the specified number of bytes into it. The
38 // source array may be (const) uint8_t*, int8_t*, or char*.
kwiberga4ac4782016-04-29 08:00:22 -070039 template <typename T,
40 typename std::enable_if<
41 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
jbauch13041cf2016-02-25 06:16:52 -080042 CopyOnWriteBuffer(const T* data, size_t size)
43 : CopyOnWriteBuffer(data, size, size) {}
kwiberga4ac4782016-04-29 08:00:22 -070044 template <typename T,
45 typename std::enable_if<
46 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
jbauch13041cf2016-02-25 06:16:52 -080047 CopyOnWriteBuffer(const T* data, size_t size, size_t capacity)
48 : CopyOnWriteBuffer(size, capacity) {
jbaucheec21bd2016-03-20 06:15:43 -070049 if (buffer_) {
50 std::memcpy(buffer_->data(), data, size);
51 }
jbauch13041cf2016-02-25 06:16:52 -080052 }
53
54 // Construct a buffer from the contents of an array.
kwiberga4ac4782016-04-29 08:00:22 -070055 template <typename T,
56 size_t N,
57 typename std::enable_if<
58 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
59 CopyOnWriteBuffer(const T (&array)[N]) // NOLINT: runtime/explicit
jbauch13041cf2016-02-25 06:16:52 -080060 : CopyOnWriteBuffer(array, N) {}
61
62 ~CopyOnWriteBuffer();
63
64 // Get a pointer to the data. Just .data() will give you a (const) uint8_t*,
65 // but you may also use .data<int8_t>() and .data<char>().
kwiberga4ac4782016-04-29 08:00:22 -070066 template <typename T = uint8_t,
67 typename std::enable_if<
68 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
jbauch13041cf2016-02-25 06:16:52 -080069 const T* data() const {
70 return cdata<T>();
71 }
72
73 // Get writable pointer to the data. This will create a copy of the underlying
74 // data if it is shared with other buffers.
kwiberga4ac4782016-04-29 08:00:22 -070075 template <typename T = uint8_t,
76 typename std::enable_if<
77 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
jbauch13041cf2016-02-25 06:16:52 -080078 T* data() {
79 RTC_DCHECK(IsConsistent());
80 if (!buffer_) {
81 return nullptr;
82 }
83 CloneDataIfReferenced(buffer_->capacity());
84 return buffer_->data<T>();
85 }
86
87 // Get const pointer to the data. This will not create a copy of the
88 // underlying data if it is shared with other buffers.
kwiberga4ac4782016-04-29 08:00:22 -070089 template <typename T = uint8_t,
90 typename std::enable_if<
91 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
danilchap8a0b1282016-07-30 13:42:27 -070092 const T* cdata() const {
jbauch13041cf2016-02-25 06:16:52 -080093 RTC_DCHECK(IsConsistent());
94 if (!buffer_) {
95 return nullptr;
96 }
97 return buffer_->data<T>();
98 }
99
100 size_t size() const {
101 RTC_DCHECK(IsConsistent());
102 return buffer_ ? buffer_->size() : 0;
103 }
104
105 size_t capacity() const {
106 RTC_DCHECK(IsConsistent());
107 return buffer_ ? buffer_->capacity() : 0;
108 }
109
110 CopyOnWriteBuffer& operator=(const CopyOnWriteBuffer& buf) {
111 RTC_DCHECK(IsConsistent());
112 RTC_DCHECK(buf.IsConsistent());
113 if (&buf != this) {
114 buffer_ = buf.buffer_;
115 }
116 return *this;
117 }
118
119 CopyOnWriteBuffer& operator=(CopyOnWriteBuffer&& buf) {
120 RTC_DCHECK(IsConsistent());
121 RTC_DCHECK(buf.IsConsistent());
danilchapd8a9c532016-07-30 12:39:26 -0700122 buffer_ = std::move(buf.buffer_);
jbauch13041cf2016-02-25 06:16:52 -0800123 return *this;
124 }
125
126 bool operator==(const CopyOnWriteBuffer& buf) const {
127 // Must either use the same buffer internally or have the same contents.
128 RTC_DCHECK(IsConsistent());
129 RTC_DCHECK(buf.IsConsistent());
130 return buffer_.get() == buf.buffer_.get() ||
131 (buffer_.get() && buf.buffer_.get() &&
132 *buffer_.get() == *buf.buffer_.get());
133 }
134
135 bool operator!=(const CopyOnWriteBuffer& buf) const {
136 return !(*this == buf);
137 }
138
jbaucheec21bd2016-03-20 06:15:43 -0700139 uint8_t& operator[](size_t index) {
140 RTC_DCHECK_LT(index, size());
141 return data()[index];
142 }
143
144 uint8_t operator[](size_t index) const {
145 RTC_DCHECK_LT(index, size());
146 return cdata()[index];
147 }
148
jbauch13041cf2016-02-25 06:16:52 -0800149 // Replace the contents of the buffer. Accepts the same types as the
150 // constructors.
kwiberga4ac4782016-04-29 08:00:22 -0700151 template <typename T,
152 typename std::enable_if<
153 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
jbauch13041cf2016-02-25 06:16:52 -0800154 void SetData(const T* data, size_t size) {
155 RTC_DCHECK(IsConsistent());
Danil Chapovalov2b2779f2016-09-13 14:15:10 +0200156 if (!buffer_) {
157 buffer_ = size > 0 ? new RefCountedObject<Buffer>(data, size) : nullptr;
158 } else if (!buffer_->HasOneRef()) {
159 buffer_ = new RefCountedObject<Buffer>(data, size, buffer_->capacity());
jbauch13041cf2016-02-25 06:16:52 -0800160 } else {
161 buffer_->SetData(data, size);
162 }
163 RTC_DCHECK(IsConsistent());
164 }
165
kwiberga4ac4782016-04-29 08:00:22 -0700166 template <typename T,
167 size_t N,
168 typename std::enable_if<
169 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
170 void SetData(const T (&array)[N]) {
jbauch13041cf2016-02-25 06:16:52 -0800171 SetData(array, N);
172 }
173
174 void SetData(const CopyOnWriteBuffer& buf) {
175 RTC_DCHECK(IsConsistent());
176 RTC_DCHECK(buf.IsConsistent());
177 if (&buf != this) {
178 buffer_ = buf.buffer_;
179 }
180 }
181
182 // Append data to the buffer. Accepts the same types as the constructors.
kwiberga4ac4782016-04-29 08:00:22 -0700183 template <typename T,
184 typename std::enable_if<
185 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
jbauch13041cf2016-02-25 06:16:52 -0800186 void AppendData(const T* data, size_t size) {
187 RTC_DCHECK(IsConsistent());
188 if (!buffer_) {
189 buffer_ = new RefCountedObject<Buffer>(data, size);
190 RTC_DCHECK(IsConsistent());
191 return;
192 }
193
194 CloneDataIfReferenced(std::max(buffer_->capacity(),
195 buffer_->size() + size));
196 buffer_->AppendData(data, size);
197 RTC_DCHECK(IsConsistent());
198 }
199
kwiberga4ac4782016-04-29 08:00:22 -0700200 template <typename T,
201 size_t N,
202 typename std::enable_if<
203 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
204 void AppendData(const T (&array)[N]) {
jbauch13041cf2016-02-25 06:16:52 -0800205 AppendData(array, N);
206 }
207
208 void AppendData(const CopyOnWriteBuffer& buf) {
209 AppendData(buf.data(), buf.size());
210 }
211
212 // Sets the size of the buffer. If the new size is smaller than the old, the
213 // buffer contents will be kept but truncated; if the new size is greater,
214 // the existing contents will be kept and the new space will be
215 // uninitialized.
216 void SetSize(size_t size) {
217 RTC_DCHECK(IsConsistent());
218 if (!buffer_) {
219 if (size > 0) {
220 buffer_ = new RefCountedObject<Buffer>(size);
221 }
222 RTC_DCHECK(IsConsistent());
223 return;
224 }
225
Danil Chapovalov291cd8f2016-09-09 17:32:33 +0200226 // Clone data if referenced.
227 if (!buffer_->HasOneRef()) {
228 buffer_ = new RefCountedObject<Buffer>(
229 buffer_->data(),
230 std::min(buffer_->size(), size),
231 std::max(buffer_->capacity(), size));
232 }
jbauch13041cf2016-02-25 06:16:52 -0800233 buffer_->SetSize(size);
234 RTC_DCHECK(IsConsistent());
235 }
236
237 // Ensure that the buffer size can be increased to at least capacity without
238 // further reallocation. (Of course, this operation might need to reallocate
239 // the buffer.)
240 void EnsureCapacity(size_t capacity) {
241 RTC_DCHECK(IsConsistent());
242 if (!buffer_) {
243 if (capacity > 0) {
244 buffer_ = new RefCountedObject<Buffer>(0, capacity);
245 }
246 RTC_DCHECK(IsConsistent());
247 return;
248 } else if (capacity <= buffer_->capacity()) {
249 return;
250 }
251
252 CloneDataIfReferenced(std::max(buffer_->capacity(), capacity));
253 buffer_->EnsureCapacity(capacity);
254 RTC_DCHECK(IsConsistent());
255 }
256
Danil Chapovalov2b2779f2016-09-13 14:15:10 +0200257 // Resets the buffer to zero size without altering capacity. Works even if the
258 // buffer has been moved from.
jbauch13041cf2016-02-25 06:16:52 -0800259 void Clear() {
Danil Chapovalov2b2779f2016-09-13 14:15:10 +0200260 if (!buffer_)
261 return;
262
263 if (buffer_->HasOneRef()) {
jbauch13041cf2016-02-25 06:16:52 -0800264 buffer_->Clear();
Danil Chapovalov2b2779f2016-09-13 14:15:10 +0200265 } else {
266 buffer_ = new RefCountedObject<Buffer>(0, buffer_->capacity());
jbauch13041cf2016-02-25 06:16:52 -0800267 }
268 RTC_DCHECK(IsConsistent());
269 }
270
271 // Swaps two buffers.
272 friend void swap(CopyOnWriteBuffer& a, CopyOnWriteBuffer& b) {
273 std::swap(a.buffer_, b.buffer_);
274 }
275
276 private:
277 // Create a copy of the underlying data if it is referenced from other Buffer
278 // objects.
279 void CloneDataIfReferenced(size_t new_capacity) {
280 if (buffer_->HasOneRef()) {
281 return;
282 }
283
284 buffer_ = new RefCountedObject<Buffer>(buffer_->data(), buffer_->size(),
285 new_capacity);
286 RTC_DCHECK(IsConsistent());
287 }
288
289 // Pre- and postcondition of all methods.
290 bool IsConsistent() const {
291 return (!buffer_ || buffer_->capacity() > 0);
292 }
293
294 // buffer_ is either null, or points to an rtc::Buffer with capacity > 0.
295 scoped_refptr<RefCountedObject<Buffer>> buffer_;
296};
297
298} // namespace rtc
299
300#endif // WEBRTC_BASE_COPYONWRITEBUFFER_H_