blob: c4bba874ad7426811b65b510ba607ccf8af3c07b [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef RTC_BASE_COPYONWRITEBUFFER_H_
12#define RTC_BASE_COPYONWRITEBUFFER_H_
jbauch13041cf2016-02-25 06:16:52 -080013
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020014#include <algorithm>
15#include <utility>
jbauch13041cf2016-02-25 06:16:52 -080016
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "rtc_base/buffer.h"
18#include "rtc_base/checks.h"
19#include "rtc_base/refcount.h"
Niels Möller84255bb2017-10-06 13:43:23 +020020#include "rtc_base/refcountedobject.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/scoped_ref_ptr.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020022
23namespace rtc {
24
25class CopyOnWriteBuffer {
26 public:
27 // An empty buffer.
28 CopyOnWriteBuffer();
29 // Copy size and contents of an existing buffer.
30 CopyOnWriteBuffer(const CopyOnWriteBuffer& buf);
31 // Move contents from an existing buffer.
32 CopyOnWriteBuffer(CopyOnWriteBuffer&& buf);
33
34 // Construct a buffer with the specified number of uninitialized bytes.
35 explicit CopyOnWriteBuffer(size_t size);
36 CopyOnWriteBuffer(size_t size, size_t capacity);
37
38 // Construct a buffer and copy the specified number of bytes into it. The
39 // source array may be (const) uint8_t*, int8_t*, or char*.
40 template <typename T,
41 typename std::enable_if<
42 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
43 CopyOnWriteBuffer(const T* data, size_t size)
44 : CopyOnWriteBuffer(data, size, size) {}
45 template <typename T,
46 typename std::enable_if<
47 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
48 CopyOnWriteBuffer(const T* data, size_t size, size_t capacity)
49 : CopyOnWriteBuffer(size, capacity) {
50 if (buffer_) {
51 std::memcpy(buffer_->data(), data, size);
52 }
53 }
54
55 // Construct a buffer from the contents of an array.
56 template <typename T,
57 size_t N,
58 typename std::enable_if<
59 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
60 CopyOnWriteBuffer(const T (&array)[N]) // NOLINT: runtime/explicit
61 : CopyOnWriteBuffer(array, N) {}
62
63 ~CopyOnWriteBuffer();
64
65 // Get a pointer to the data. Just .data() will give you a (const) uint8_t*,
66 // but you may also use .data<int8_t>() and .data<char>().
67 template <typename T = uint8_t,
68 typename std::enable_if<
69 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
70 const T* data() const {
71 return cdata<T>();
72 }
73
74 // Get writable pointer to the data. This will create a copy of the underlying
75 // data if it is shared with other buffers.
76 template <typename T = uint8_t,
77 typename std::enable_if<
78 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
79 T* data() {
80 RTC_DCHECK(IsConsistent());
81 if (!buffer_) {
82 return nullptr;
83 }
84 CloneDataIfReferenced(buffer_->capacity());
85 return buffer_->data<T>();
86 }
87
88 // Get const pointer to the data. This will not create a copy of the
89 // underlying data if it is shared with other buffers.
90 template <typename T = uint8_t,
91 typename std::enable_if<
92 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
93 const T* cdata() const {
94 RTC_DCHECK(IsConsistent());
95 if (!buffer_) {
96 return nullptr;
97 }
98 return buffer_->data<T>();
99 }
100
101 size_t size() const {
102 RTC_DCHECK(IsConsistent());
103 return buffer_ ? buffer_->size() : 0;
104 }
105
106 size_t capacity() const {
107 RTC_DCHECK(IsConsistent());
108 return buffer_ ? buffer_->capacity() : 0;
109 }
110
111 CopyOnWriteBuffer& operator=(const CopyOnWriteBuffer& buf) {
112 RTC_DCHECK(IsConsistent());
113 RTC_DCHECK(buf.IsConsistent());
114 if (&buf != this) {
115 buffer_ = buf.buffer_;
116 }
117 return *this;
118 }
119
120 CopyOnWriteBuffer& operator=(CopyOnWriteBuffer&& buf) {
121 RTC_DCHECK(IsConsistent());
122 RTC_DCHECK(buf.IsConsistent());
123 buffer_ = std::move(buf.buffer_);
124 return *this;
125 }
126
127 bool operator==(const CopyOnWriteBuffer& buf) const;
128
129 bool operator!=(const CopyOnWriteBuffer& buf) const {
130 return !(*this == buf);
131 }
132
133 uint8_t& operator[](size_t index) {
134 RTC_DCHECK_LT(index, size());
135 return data()[index];
136 }
137
138 uint8_t operator[](size_t index) const {
139 RTC_DCHECK_LT(index, size());
140 return cdata()[index];
141 }
142
143 // Replace the contents of the buffer. Accepts the same types as the
144 // constructors.
145 template <typename T,
146 typename std::enable_if<
147 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
148 void SetData(const T* data, size_t size) {
149 RTC_DCHECK(IsConsistent());
150 if (!buffer_) {
151 buffer_ = size > 0 ? new RefCountedObject<Buffer>(data, size) : nullptr;
152 } else if (!buffer_->HasOneRef()) {
153 buffer_ = new RefCountedObject<Buffer>(data, size, buffer_->capacity());
154 } else {
155 buffer_->SetData(data, size);
156 }
157 RTC_DCHECK(IsConsistent());
158 }
159
160 template <typename T,
161 size_t N,
162 typename std::enable_if<
163 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
164 void SetData(const T (&array)[N]) {
165 SetData(array, N);
166 }
167
168 void SetData(const CopyOnWriteBuffer& buf) {
169 RTC_DCHECK(IsConsistent());
170 RTC_DCHECK(buf.IsConsistent());
171 if (&buf != this) {
172 buffer_ = buf.buffer_;
173 }
174 }
175
176 // Append data to the buffer. Accepts the same types as the constructors.
177 template <typename T,
178 typename std::enable_if<
179 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
180 void AppendData(const T* data, size_t size) {
181 RTC_DCHECK(IsConsistent());
182 if (!buffer_) {
183 buffer_ = new RefCountedObject<Buffer>(data, size);
184 RTC_DCHECK(IsConsistent());
185 return;
186 }
187
188 CloneDataIfReferenced(std::max(buffer_->capacity(),
189 buffer_->size() + size));
190 buffer_->AppendData(data, size);
191 RTC_DCHECK(IsConsistent());
192 }
193
194 template <typename T,
195 size_t N,
196 typename std::enable_if<
197 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
198 void AppendData(const T (&array)[N]) {
199 AppendData(array, N);
200 }
201
202 void AppendData(const CopyOnWriteBuffer& buf) {
203 AppendData(buf.data(), buf.size());
204 }
205
206 // Sets the size of the buffer. If the new size is smaller than the old, the
207 // buffer contents will be kept but truncated; if the new size is greater,
208 // the existing contents will be kept and the new space will be
209 // uninitialized.
210 void SetSize(size_t size);
211
212 // Ensure that the buffer size can be increased to at least capacity without
213 // further reallocation. (Of course, this operation might need to reallocate
214 // the buffer.)
215 void EnsureCapacity(size_t capacity);
216
217 // Resets the buffer to zero size without altering capacity. Works even if the
218 // buffer has been moved from.
219 void Clear();
220
221 // Swaps two buffers.
222 friend void swap(CopyOnWriteBuffer& a, CopyOnWriteBuffer& b) {
223 std::swap(a.buffer_, b.buffer_);
224 }
225
226 private:
227 // Create a copy of the underlying data if it is referenced from other Buffer
228 // objects.
229 void CloneDataIfReferenced(size_t new_capacity);
230
231 // Pre- and postcondition of all methods.
232 bool IsConsistent() const {
233 return (!buffer_ || buffer_->capacity() > 0);
234 }
235
236 // buffer_ is either null, or points to an rtc::Buffer with capacity > 0.
237 scoped_refptr<RefCountedObject<Buffer>> buffer_;
238};
239
240} // namespace rtc
jbauch13041cf2016-02-25 06:16:52 -0800241
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200242#endif // RTC_BASE_COPYONWRITEBUFFER_H_