blob: c60e78be1d481196e3b95847c99c9aced80ee94f [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);
59 }
60 }
61
62 // Construct a buffer from the contents of an array.
63 template <typename T,
64 size_t N,
65 typename std::enable_if<
66 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
67 CopyOnWriteBuffer(const T (&array)[N]) // NOLINT: runtime/explicit
68 : CopyOnWriteBuffer(array, N) {}
69
70 ~CopyOnWriteBuffer();
71
72 // Get a pointer to the data. Just .data() will give you a (const) uint8_t*,
73 // but you may also use .data<int8_t>() and .data<char>().
74 template <typename T = uint8_t,
75 typename std::enable_if<
76 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
77 const T* data() const {
78 return cdata<T>();
79 }
80
81 // Get writable pointer to the data. This will create a copy of the underlying
82 // data if it is shared with other buffers.
83 template <typename T = uint8_t,
84 typename std::enable_if<
85 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
86 T* data() {
87 RTC_DCHECK(IsConsistent());
88 if (!buffer_) {
89 return nullptr;
90 }
91 CloneDataIfReferenced(buffer_->capacity());
92 return buffer_->data<T>();
93 }
94
95 // Get const pointer to the data. This will not create a copy of the
96 // underlying data if it is shared with other buffers.
97 template <typename T = uint8_t,
98 typename std::enable_if<
99 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
100 const T* cdata() const {
101 RTC_DCHECK(IsConsistent());
102 if (!buffer_) {
103 return nullptr;
104 }
105 return buffer_->data<T>();
106 }
107
108 size_t size() const {
109 RTC_DCHECK(IsConsistent());
110 return buffer_ ? buffer_->size() : 0;
111 }
112
113 size_t capacity() const {
114 RTC_DCHECK(IsConsistent());
115 return buffer_ ? buffer_->capacity() : 0;
116 }
117
118 CopyOnWriteBuffer& operator=(const CopyOnWriteBuffer& buf) {
119 RTC_DCHECK(IsConsistent());
120 RTC_DCHECK(buf.IsConsistent());
121 if (&buf != this) {
122 buffer_ = buf.buffer_;
123 }
124 return *this;
125 }
126
127 CopyOnWriteBuffer& operator=(CopyOnWriteBuffer&& buf) {
128 RTC_DCHECK(IsConsistent());
129 RTC_DCHECK(buf.IsConsistent());
130 buffer_ = std::move(buf.buffer_);
131 return *this;
132 }
133
134 bool operator==(const CopyOnWriteBuffer& buf) const;
135
136 bool operator!=(const CopyOnWriteBuffer& buf) const {
137 return !(*this == buf);
138 }
139
140 uint8_t& operator[](size_t index) {
141 RTC_DCHECK_LT(index, size());
142 return data()[index];
143 }
144
145 uint8_t operator[](size_t index) const {
146 RTC_DCHECK_LT(index, size());
147 return cdata()[index];
148 }
149
150 // Replace the contents of the buffer. Accepts the same types as the
151 // constructors.
152 template <typename T,
153 typename std::enable_if<
154 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
155 void SetData(const T* data, size_t size) {
156 RTC_DCHECK(IsConsistent());
157 if (!buffer_) {
158 buffer_ = size > 0 ? new RefCountedObject<Buffer>(data, size) : nullptr;
159 } else if (!buffer_->HasOneRef()) {
160 buffer_ = new RefCountedObject<Buffer>(data, size, buffer_->capacity());
161 } else {
162 buffer_->SetData(data, size);
163 }
164 RTC_DCHECK(IsConsistent());
165 }
166
167 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]) {
172 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.
184 template <typename T,
185 typename std::enable_if<
186 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
187 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
Yves Gerey665174f2018-06-19 15:03:05 +0200195 CloneDataIfReferenced(
196 std::max(buffer_->capacity(), buffer_->size() + size));
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200197 buffer_->AppendData(data, size);
198 RTC_DCHECK(IsConsistent());
199 }
200
201 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]) {
206 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
219 // Ensure that the buffer size can be increased to at least capacity without
220 // further reallocation. (Of course, this operation might need to reallocate
221 // the buffer.)
222 void EnsureCapacity(size_t capacity);
223
224 // Resets the buffer to zero size without altering capacity. Works even if the
225 // buffer has been moved from.
226 void Clear();
227
228 // Swaps two buffers.
229 friend void swap(CopyOnWriteBuffer& a, CopyOnWriteBuffer& b) {
230 std::swap(a.buffer_, b.buffer_);
231 }
232
233 private:
234 // Create a copy of the underlying data if it is referenced from other Buffer
235 // objects.
236 void CloneDataIfReferenced(size_t new_capacity);
237
238 // Pre- and postcondition of all methods.
Yves Gerey665174f2018-06-19 15:03:05 +0200239 bool IsConsistent() const { return (!buffer_ || buffer_->capacity() > 0); }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200240
241 // buffer_ is either null, or points to an rtc::Buffer with capacity > 0.
242 scoped_refptr<RefCountedObject<Buffer>> buffer_;
243};
244
245} // namespace rtc
jbauch13041cf2016-02-25 06:16:52 -0800246
Steve Anton10542f22019-01-11 09:11:00 -0800247#endif // RTC_BASE_COPY_ON_WRITE_BUFFER_H_