OLD | NEW |
---|---|
(Empty) | |
1 /* | |
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/refcount.h" | |
19 #include "webrtc/base/scoped_ref_ptr.h" | |
20 | |
21 namespace rtc { | |
22 | |
23 class CopyOnWriteBuffer { | |
24 public: | |
25 // An empty buffer. | |
26 CopyOnWriteBuffer(); | |
27 // Copy size and contents of an existing buffer. | |
28 CopyOnWriteBuffer(const CopyOnWriteBuffer& buf); | |
29 // Move contents from an existing buffer. | |
30 CopyOnWriteBuffer(CopyOnWriteBuffer&& buf); | |
31 | |
32 // Construct a buffer with the specified number of uninitialized bytes. | |
33 explicit CopyOnWriteBuffer(size_t size); | |
34 CopyOnWriteBuffer(size_t size, size_t capacity); | |
35 | |
36 // Construct a buffer and copy the specified number of bytes into it. The | |
37 // source array may be (const) uint8_t*, int8_t*, or char*. | |
38 template <typename T, typename internal::ByteType<T>::t = 0> | |
39 CopyOnWriteBuffer(const T* data, size_t size) | |
40 : CopyOnWriteBuffer(data, size, size) {} | |
41 template <typename T, typename internal::ByteType<T>::t = 0> | |
42 CopyOnWriteBuffer(const T* data, size_t size, size_t capacity) | |
43 : CopyOnWriteBuffer(size, capacity) { | |
44 std::memcpy(buffer_->data(), data, size); | |
45 } | |
46 | |
47 // Construct a buffer from the contents of an array. | |
48 template <typename T, size_t N, typename internal::ByteType<T>::t = 0> | |
49 CopyOnWriteBuffer(const T(&array)[N]) // NOLINT: runtime/explicit | |
50 : CopyOnWriteBuffer(array, N) {} | |
51 | |
52 ~CopyOnWriteBuffer(); | |
53 | |
54 // Get a pointer to the data. Just .data() will give you a (const) uint8_t*, | |
55 // but you may also use .data<int8_t>() and .data<char>(). | |
56 template <typename T = uint8_t, typename internal::ByteType<T>::t = 0> | |
57 const T* data() const { | |
58 if (!buffer_) { | |
59 return nullptr; | |
60 } | |
61 return buffer_->data<T>(); | |
kwiberg-webrtc
2016/02/20 06:43:32
For clarity and brevity, perhaps replace this func
joachim
2016/02/23 23:25:35
Done.
| |
62 } | |
63 | |
64 // Get writable pointer to the data. This will create a copy of the underlying | |
65 // data if it is shared with other buffers. | |
66 template <typename T = uint8_t, typename internal::ByteType<T>::t = 0> | |
67 T* data() { | |
68 if (!buffer_) { | |
69 return nullptr; | |
70 } | |
71 CloneDataIfReferenced(buffer_->capacity()); | |
72 return buffer_->data<T>(); | |
73 } | |
74 | |
75 // Get const pointer to the data. This will not create a copy of the | |
76 // underlying data if it is shared with other buffers. | |
77 template <typename T = uint8_t, typename internal::ByteType<T>::t = 0> | |
78 T* cdata() const { | |
79 if (!buffer_) { | |
80 return nullptr; | |
81 } | |
82 return buffer_->data<T>(); | |
83 } | |
84 | |
85 size_t size() const { | |
86 return buffer_ ? buffer_->size() : 0; | |
87 } | |
88 | |
89 size_t capacity() const { | |
90 return buffer_ ? buffer_->capacity() : 0; | |
91 } | |
92 | |
93 CopyOnWriteBuffer& operator=(const CopyOnWriteBuffer& buf) { | |
94 if (&buf != this) { | |
95 buffer_ = buf.buffer_; | |
96 } | |
97 return *this; | |
98 } | |
99 | |
100 CopyOnWriteBuffer& operator=(CopyOnWriteBuffer&& buf) { | |
101 // TODO(jbauch): use std::move once scoped_refptr supports it (issue 5556) | |
102 buffer_.swap(buf.buffer_); | |
103 buf.buffer_ = nullptr; | |
104 return *this; | |
105 } | |
106 | |
107 bool operator==(const CopyOnWriteBuffer& buf) const { | |
108 // Must either use the same buffer internally or have the same contents. | |
109 return buffer_.get() == buf.buffer_.get() || | |
110 (buffer_.get() && buf.buffer_.get() && | |
111 *buffer_.get() == *buf.buffer_.get()); | |
112 } | |
113 | |
114 bool operator!=(const CopyOnWriteBuffer& buf) const { | |
115 return !(*this == buf); | |
116 } | |
117 | |
118 // Replace the contents of the buffer. Accepts the same types as the | |
119 // constructors. | |
120 template <typename T, typename internal::ByteType<T>::t = 0> | |
121 void SetData(const T* data, size_t size) { | |
122 if (!buffer_ || !buffer_->HasOneRef()) { | |
123 buffer_ = new RefCountedObject<Buffer>(data, size, size); | |
124 } else { | |
125 buffer_->SetData(data, size); | |
126 } | |
127 } | |
128 | |
129 template <typename T, size_t N, typename internal::ByteType<T>::t = 0> | |
130 void SetData(const T(&array)[N]) { | |
131 SetData(array, N); | |
132 } | |
133 | |
134 void SetData(const CopyOnWriteBuffer& buf) { | |
135 if (&buf != this) { | |
136 buffer_ = buf.buffer_; | |
137 } | |
138 } | |
139 | |
140 // Append data to the buffer. Accepts the same types as the constructors. | |
141 template <typename T, typename internal::ByteType<T>::t = 0> | |
142 void AppendData(const T* data, size_t size) { | |
143 if (!buffer_) { | |
144 buffer_ = new RefCountedObject<Buffer>(data, size); | |
145 return; | |
146 } | |
147 | |
148 CloneDataIfReferenced(std::max(buffer_->capacity(), | |
149 buffer_->size() + size)); | |
150 buffer_->AppendData(data, size); | |
151 } | |
152 | |
153 template <typename T, size_t N, typename internal::ByteType<T>::t = 0> | |
154 void AppendData(const T(&array)[N]) { | |
155 AppendData(array, N); | |
156 } | |
157 | |
158 void AppendData(const CopyOnWriteBuffer& buf) { | |
159 AppendData(buf.data(), buf.size()); | |
160 } | |
161 | |
162 // Sets the size of the buffer. If the new size is smaller than the old, the | |
163 // buffer contents will be kept but truncated; if the new size is greater, | |
164 // the existing contents will be kept and the new space will be | |
165 // uninitialized. | |
166 void SetSize(size_t size) { | |
167 if (!buffer_) { | |
168 if (size > 0) { | |
169 buffer_ = new RefCountedObject<Buffer>(size); | |
170 } | |
171 return; | |
172 } | |
173 | |
174 CloneDataIfReferenced(std::max(buffer_->capacity(), size)); | |
175 buffer_->SetSize(size); | |
176 } | |
177 | |
178 // Ensure that the buffer size can be increased to at least capacity without | |
179 // further reallocation. (Of course, this operation might need to reallocate | |
180 // the buffer.) | |
181 void EnsureCapacity(size_t capacity) { | |
182 if (!buffer_) { | |
183 if (capacity > 0) { | |
184 buffer_ = new RefCountedObject<Buffer>(0, capacity); | |
185 } | |
186 return; | |
187 } else if (capacity <= buffer_->capacity()) { | |
188 return; | |
189 } | |
190 | |
191 CloneDataIfReferenced(std::max(buffer_->capacity(), capacity)); | |
192 buffer_->EnsureCapacity(capacity); | |
193 } | |
194 | |
195 // Resets the buffer to zero size and capacity. | |
196 void Clear() { | |
197 if (!buffer_ || !buffer_->HasOneRef()) { | |
198 buffer_ = nullptr; | |
199 } else { | |
200 buffer_->Clear(); | |
201 } | |
202 } | |
203 | |
204 // Swaps two buffers. | |
205 friend void swap(CopyOnWriteBuffer& a, CopyOnWriteBuffer& b) { | |
206 std::swap(a.buffer_, b.buffer_); | |
207 } | |
208 | |
209 private: | |
210 // Create a copy of the underlying data if it is referenced from other Buffer | |
211 // objects. | |
212 void CloneDataIfReferenced(size_t new_capacity) { | |
213 if (buffer_->HasOneRef()) { | |
214 return; | |
215 } | |
216 | |
217 buffer_ = new RefCountedObject<Buffer>(buffer_->data(), buffer_->size(), | |
218 new_capacity); | |
219 } | |
220 | |
221 // Will only be allocated if capacity has been set at least once. | |
kwiberg-webrtc
2016/02/20 06:43:32
Perhaps be more explicit: "buffer_ is either null,
joachim
2016/02/23 23:25:35
Done
| |
222 scoped_refptr<RefCountedObject<Buffer>> buffer_; | |
223 }; | |
224 | |
225 } // namespace rtc | |
226 | |
227 #endif // WEBRTC_BASE_COPYONWRITEBUFFER_H_ | |
OLD | NEW |