blob: a7e52beea53c3ce442ac3b97a1db013590e08861 [file] [log] [blame]
jbauchd6ba95e2016-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
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*.
kwiberg6fbbe5d2016-04-29 15:00:2239 template <typename T,
40 typename std::enable_if<
41 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
jbauchd6ba95e2016-02-25 14:16:5242 CopyOnWriteBuffer(const T* data, size_t size)
43 : CopyOnWriteBuffer(data, size, size) {}
kwiberg6fbbe5d2016-04-29 15:00:2244 template <typename T,
45 typename std::enable_if<
46 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
jbauchd6ba95e2016-02-25 14:16:5247 CopyOnWriteBuffer(const T* data, size_t size, size_t capacity)
48 : CopyOnWriteBuffer(size, capacity) {
jbauchbb6decf2016-03-20 13:15:4349 if (buffer_) {
50 std::memcpy(buffer_->data(), data, size);
51 }
jbauchd6ba95e2016-02-25 14:16:5252 }
53
54 // Construct a buffer from the contents of an array.
kwiberg6fbbe5d2016-04-29 15:00:2255 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
jbauchd6ba95e2016-02-25 14:16:5260 : 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>().
kwiberg6fbbe5d2016-04-29 15:00:2266 template <typename T = uint8_t,
67 typename std::enable_if<
68 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
jbauchd6ba95e2016-02-25 14:16:5269 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.
kwiberg6fbbe5d2016-04-29 15:00:2275 template <typename T = uint8_t,
76 typename std::enable_if<
77 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
jbauchd6ba95e2016-02-25 14:16:5278 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.
kwiberg6fbbe5d2016-04-29 15:00:2289 template <typename T = uint8_t,
90 typename std::enable_if<
91 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
jbauchd6ba95e2016-02-25 14:16:5292 T* cdata() const {
93 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());
122 // TODO(jbauch): use std::move once scoped_refptr supports it (issue 5556)
123 buffer_.swap(buf.buffer_);
124 buf.buffer_ = nullptr;
125 return *this;
126 }
127
128 bool operator==(const CopyOnWriteBuffer& buf) const {
129 // Must either use the same buffer internally or have the same contents.
130 RTC_DCHECK(IsConsistent());
131 RTC_DCHECK(buf.IsConsistent());
132 return buffer_.get() == buf.buffer_.get() ||
133 (buffer_.get() && buf.buffer_.get() &&
134 *buffer_.get() == *buf.buffer_.get());
135 }
136
137 bool operator!=(const CopyOnWriteBuffer& buf) const {
138 return !(*this == buf);
139 }
140
jbauchbb6decf2016-03-20 13:15:43141 uint8_t& operator[](size_t index) {
142 RTC_DCHECK_LT(index, size());
143 return data()[index];
144 }
145
146 uint8_t operator[](size_t index) const {
147 RTC_DCHECK_LT(index, size());
148 return cdata()[index];
149 }
150
jbauchd6ba95e2016-02-25 14:16:52151 // Replace the contents of the buffer. Accepts the same types as the
152 // constructors.
kwiberg6fbbe5d2016-04-29 15:00:22153 template <typename T,
154 typename std::enable_if<
155 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
jbauchd6ba95e2016-02-25 14:16:52156 void SetData(const T* data, size_t size) {
157 RTC_DCHECK(IsConsistent());
158 if (!buffer_ || !buffer_->HasOneRef()) {
jbauchbb6decf2016-03-20 13:15:43159 buffer_ = size > 0 ? new RefCountedObject<Buffer>(data, size)
160 : nullptr;
jbauchd6ba95e2016-02-25 14:16:52161 } else {
162 buffer_->SetData(data, size);
163 }
164 RTC_DCHECK(IsConsistent());
165 }
166
kwiberg6fbbe5d2016-04-29 15:00:22167 template <typename T,
168 size_t N,
169 typename std::enable_if<
170 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
171 void SetData(const T (&array)[N]) {
jbauchd6ba95e2016-02-25 14:16:52172 SetData(array, N);
173 }
174
175 void SetData(const CopyOnWriteBuffer& buf) {
176 RTC_DCHECK(IsConsistent());
177 RTC_DCHECK(buf.IsConsistent());
178 if (&buf != this) {
179 buffer_ = buf.buffer_;
180 }
181 }
182
183 // Append data to the buffer. Accepts the same types as the constructors.
kwiberg6fbbe5d2016-04-29 15:00:22184 template <typename T,
185 typename std::enable_if<
186 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
jbauchd6ba95e2016-02-25 14:16:52187 void AppendData(const T* data, size_t size) {
188 RTC_DCHECK(IsConsistent());
189 if (!buffer_) {
190 buffer_ = new RefCountedObject<Buffer>(data, size);
191 RTC_DCHECK(IsConsistent());
192 return;
193 }
194
195 CloneDataIfReferenced(std::max(buffer_->capacity(),
196 buffer_->size() + size));
197 buffer_->AppendData(data, size);
198 RTC_DCHECK(IsConsistent());
199 }
200
kwiberg6fbbe5d2016-04-29 15:00:22201 template <typename T,
202 size_t N,
203 typename std::enable_if<
204 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
205 void AppendData(const T (&array)[N]) {
jbauchd6ba95e2016-02-25 14:16:52206 AppendData(array, N);
207 }
208
209 void AppendData(const CopyOnWriteBuffer& buf) {
210 AppendData(buf.data(), buf.size());
211 }
212
213 // Sets the size of the buffer. If the new size is smaller than the old, the
214 // buffer contents will be kept but truncated; if the new size is greater,
215 // the existing contents will be kept and the new space will be
216 // uninitialized.
217 void SetSize(size_t size) {
218 RTC_DCHECK(IsConsistent());
219 if (!buffer_) {
220 if (size > 0) {
221 buffer_ = new RefCountedObject<Buffer>(size);
222 }
223 RTC_DCHECK(IsConsistent());
224 return;
225 }
226
227 CloneDataIfReferenced(std::max(buffer_->capacity(), size));
228 buffer_->SetSize(size);
229 RTC_DCHECK(IsConsistent());
230 }
231
232 // Ensure that the buffer size can be increased to at least capacity without
233 // further reallocation. (Of course, this operation might need to reallocate
234 // the buffer.)
235 void EnsureCapacity(size_t capacity) {
236 RTC_DCHECK(IsConsistent());
237 if (!buffer_) {
238 if (capacity > 0) {
239 buffer_ = new RefCountedObject<Buffer>(0, capacity);
240 }
241 RTC_DCHECK(IsConsistent());
242 return;
243 } else if (capacity <= buffer_->capacity()) {
244 return;
245 }
246
247 CloneDataIfReferenced(std::max(buffer_->capacity(), capacity));
248 buffer_->EnsureCapacity(capacity);
249 RTC_DCHECK(IsConsistent());
250 }
251
252 // Resets the buffer to zero size and capacity.
253 void Clear() {
254 RTC_DCHECK(IsConsistent());
255 if (!buffer_ || !buffer_->HasOneRef()) {
256 buffer_ = nullptr;
257 } else {
258 buffer_->Clear();
259 }
260 RTC_DCHECK(IsConsistent());
261 }
262
263 // Swaps two buffers.
264 friend void swap(CopyOnWriteBuffer& a, CopyOnWriteBuffer& b) {
265 std::swap(a.buffer_, b.buffer_);
266 }
267
268 private:
269 // Create a copy of the underlying data if it is referenced from other Buffer
270 // objects.
271 void CloneDataIfReferenced(size_t new_capacity) {
272 if (buffer_->HasOneRef()) {
273 return;
274 }
275
276 buffer_ = new RefCountedObject<Buffer>(buffer_->data(), buffer_->size(),
277 new_capacity);
278 RTC_DCHECK(IsConsistent());
279 }
280
281 // Pre- and postcondition of all methods.
282 bool IsConsistent() const {
283 return (!buffer_ || buffer_->capacity() > 0);
284 }
285
286 // buffer_ is either null, or points to an rtc::Buffer with capacity > 0.
287 scoped_refptr<RefCountedObject<Buffer>> buffer_;
288};
289
290} // namespace rtc
291
292#endif // WEBRTC_BASE_COPYONWRITEBUFFER_H_