blob: cc174dfc7c969e728995de3d34daa4ac8d2833cd [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
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <stdint.h>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020015#include <algorithm>
Yves Gerey988cc082018-10-23 12:03:01 +020016#include <cstring>
17#include <string>
18#include <type_traits>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020019#include <utility>
jbauch13041cf2016-02-25 06:16:52 -080020
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/buffer.h"
22#include "rtc_base/checks.h"
Niels Möller84255bb2017-10-06 13:43:23 +020023#include "rtc_base/refcountedobject.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "rtc_base/scoped_ref_ptr.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020025
26namespace rtc {
27
28class CopyOnWriteBuffer {
29 public:
30 // An empty buffer.
31 CopyOnWriteBuffer();
32 // Copy size and contents of an existing buffer.
33 CopyOnWriteBuffer(const CopyOnWriteBuffer& buf);
34 // Move contents from an existing buffer.
35 CopyOnWriteBuffer(CopyOnWriteBuffer&& buf);
36
Jeroen de Borst4f6d2332018-07-18 11:25:12 -070037 // Construct a buffer from a string, convenient for unittests.
38 CopyOnWriteBuffer(const std::string& s);
39
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020040 // Construct a buffer with the specified number of uninitialized bytes.
41 explicit CopyOnWriteBuffer(size_t size);
42 CopyOnWriteBuffer(size_t size, size_t capacity);
43
44 // Construct a buffer and copy the specified number of bytes into it. The
45 // source array may be (const) uint8_t*, int8_t*, or char*.
46 template <typename T,
47 typename std::enable_if<
48 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
49 CopyOnWriteBuffer(const T* data, size_t size)
50 : CopyOnWriteBuffer(data, size, size) {}
51 template <typename T,
52 typename std::enable_if<
53 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
54 CopyOnWriteBuffer(const T* data, size_t size, size_t capacity)
55 : CopyOnWriteBuffer(size, capacity) {
56 if (buffer_) {
57 std::memcpy(buffer_->data(), data, size);
58 }
59 }
60
61 // Construct a buffer from the contents of an array.
62 template <typename T,
63 size_t N,
64 typename std::enable_if<
65 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
66 CopyOnWriteBuffer(const T (&array)[N]) // NOLINT: runtime/explicit
67 : CopyOnWriteBuffer(array, N) {}
68
69 ~CopyOnWriteBuffer();
70
71 // Get a pointer to the data. Just .data() will give you a (const) uint8_t*,
72 // but you may also use .data<int8_t>() and .data<char>().
73 template <typename T = uint8_t,
74 typename std::enable_if<
75 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
76 const T* data() const {
77 return cdata<T>();
78 }
79
80 // Get writable pointer to the data. This will create a copy of the underlying
81 // data if it is shared with other buffers.
82 template <typename T = uint8_t,
83 typename std::enable_if<
84 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
85 T* data() {
86 RTC_DCHECK(IsConsistent());
87 if (!buffer_) {
88 return nullptr;
89 }
90 CloneDataIfReferenced(buffer_->capacity());
91 return buffer_->data<T>();
92 }
93
94 // Get const pointer to the data. This will not create a copy of the
95 // underlying data if it is shared with other buffers.
96 template <typename T = uint8_t,
97 typename std::enable_if<
98 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
99 const T* cdata() const {
100 RTC_DCHECK(IsConsistent());
101 if (!buffer_) {
102 return nullptr;
103 }
104 return buffer_->data<T>();
105 }
106
107 size_t size() const {
108 RTC_DCHECK(IsConsistent());
109 return buffer_ ? buffer_->size() : 0;
110 }
111
112 size_t capacity() const {
113 RTC_DCHECK(IsConsistent());
114 return buffer_ ? buffer_->capacity() : 0;
115 }
116
117 CopyOnWriteBuffer& operator=(const CopyOnWriteBuffer& buf) {
118 RTC_DCHECK(IsConsistent());
119 RTC_DCHECK(buf.IsConsistent());
120 if (&buf != this) {
121 buffer_ = buf.buffer_;
122 }
123 return *this;
124 }
125
126 CopyOnWriteBuffer& operator=(CopyOnWriteBuffer&& buf) {
127 RTC_DCHECK(IsConsistent());
128 RTC_DCHECK(buf.IsConsistent());
129 buffer_ = std::move(buf.buffer_);
130 return *this;
131 }
132
133 bool operator==(const CopyOnWriteBuffer& buf) const;
134
135 bool operator!=(const CopyOnWriteBuffer& buf) const {
136 return !(*this == buf);
137 }
138
139 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
149 // Replace the contents of the buffer. Accepts the same types as the
150 // constructors.
151 template <typename T,
152 typename std::enable_if<
153 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
154 void SetData(const T* data, size_t size) {
155 RTC_DCHECK(IsConsistent());
156 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());
160 } else {
161 buffer_->SetData(data, size);
162 }
163 RTC_DCHECK(IsConsistent());
164 }
165
166 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]) {
171 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.
183 template <typename T,
184 typename std::enable_if<
185 internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
186 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
Yves Gerey665174f2018-06-19 15:03:05 +0200194 CloneDataIfReferenced(
195 std::max(buffer_->capacity(), buffer_->size() + size));
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200196 buffer_->AppendData(data, size);
197 RTC_DCHECK(IsConsistent());
198 }
199
200 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]) {
205 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
218 // Ensure that the buffer size can be increased to at least capacity without
219 // further reallocation. (Of course, this operation might need to reallocate
220 // the buffer.)
221 void EnsureCapacity(size_t capacity);
222
223 // Resets the buffer to zero size without altering capacity. Works even if the
224 // buffer has been moved from.
225 void Clear();
226
227 // Swaps two buffers.
228 friend void swap(CopyOnWriteBuffer& a, CopyOnWriteBuffer& b) {
229 std::swap(a.buffer_, b.buffer_);
230 }
231
232 private:
233 // Create a copy of the underlying data if it is referenced from other Buffer
234 // objects.
235 void CloneDataIfReferenced(size_t new_capacity);
236
237 // Pre- and postcondition of all methods.
Yves Gerey665174f2018-06-19 15:03:05 +0200238 bool IsConsistent() const { return (!buffer_ || buffer_->capacity() > 0); }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200239
240 // buffer_ is either null, or points to an rtc::Buffer with capacity > 0.
241 scoped_refptr<RefCountedObject<Buffer>> buffer_;
242};
243
244} // namespace rtc
jbauch13041cf2016-02-25 06:16:52 -0800245
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200246#endif // RTC_BASE_COPYONWRITEBUFFER_H_