OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/base/buffer.h" | 11 #include "webrtc/base/buffer.h" |
12 | 12 |
13 #include <cassert> | 13 #include <cassert> |
14 #include <utility> | 14 #include <utility> |
15 | 15 |
16 namespace rtc { | 16 namespace rtc { |
17 | 17 |
18 Buffer::Buffer() : size_(0), capacity_(0), data_(nullptr) { | 18 BufferData::BufferData() |
| 19 : ref_count_(1), |
| 20 size_(0), |
| 21 capacity_(0), |
| 22 data_(nullptr) { |
19 assert(IsConsistent()); | 23 assert(IsConsistent()); |
20 } | 24 } |
21 | 25 |
22 Buffer::Buffer(const Buffer& buf) : Buffer(buf.data(), buf.size()) { | 26 BufferData::BufferData(const BufferData& data) |
| 27 : ref_count_(1), |
| 28 size_(data.size_), |
| 29 capacity_(data.capacity_), |
| 30 data_(new uint8_t[capacity_]) { |
| 31 assert(IsConsistent()); |
| 32 if (data.size_) { |
| 33 std::memcpy(data_.get(), data.data_.get(), data.size_); |
| 34 } |
23 } | 35 } |
24 | 36 |
25 Buffer::Buffer(Buffer&& buf) | 37 BufferData::BufferData(size_t size) : BufferData(size, size) { |
26 : size_(buf.size()), | 38 } |
27 capacity_(buf.capacity()), | 39 |
28 data_(std::move(buf.data_)) { | 40 BufferData::BufferData(size_t size, size_t capacity) |
| 41 : ref_count_(1), |
| 42 size_(size), |
| 43 capacity_(std::max(size, capacity)), |
| 44 data_(new uint8_t[capacity_]) { |
29 assert(IsConsistent()); | 45 assert(IsConsistent()); |
| 46 } |
| 47 |
| 48 BufferData::BufferData(const void* data, size_t size) |
| 49 : BufferData(data, size, size) { |
| 50 } |
| 51 |
| 52 BufferData::BufferData(const void* data, size_t size, size_t capacity) |
| 53 : BufferData(size, capacity) { |
| 54 std::memcpy(data_.get(), data, size); |
| 55 } |
| 56 |
| 57 BufferData::~BufferData() { |
| 58 } |
| 59 |
| 60 |
| 61 Buffer::Buffer() : data_(new BufferData()) { |
| 62 } |
| 63 |
| 64 Buffer::Buffer(const Buffer& buf) : data_(buf.data_) { |
| 65 data_->AddRef(); |
| 66 } |
| 67 |
| 68 Buffer::Buffer(Buffer&& buf) : data_(std::move(buf.data_)) { |
30 buf.OnMovedFrom(); | 69 buf.OnMovedFrom(); |
31 } | 70 } |
32 | 71 |
33 Buffer::Buffer(size_t size) : Buffer(size, size) { | 72 Buffer::Buffer(size_t size) : Buffer(size, size) { |
34 } | 73 } |
35 | 74 |
36 Buffer::Buffer(size_t size, size_t capacity) | 75 Buffer::Buffer(size_t size, size_t capacity) |
37 : size_(size), | 76 : data_(new BufferData(size, capacity)) { |
38 capacity_(std::max(size, capacity)), | |
39 data_(new uint8_t[capacity_]) { | |
40 assert(IsConsistent()); | 77 assert(IsConsistent()); |
41 } | 78 } |
42 | 79 |
43 // Note: The destructor works even if the buffer has been moved from. | 80 Buffer::~Buffer() { |
44 Buffer::~Buffer() = default; | 81 data_->Release(); |
| 82 } |
45 | 83 |
46 }; // namespace rtc | 84 }; // namespace rtc |
OLD | NEW |