Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(31)

Side by Side Diff: webrtc/base/buffer.cc

Issue 1697743003: Add CopyOnWriteBuffer class (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Removed atomicops.h Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <utility>
12
13 #include "webrtc/base/checks.h"
11 #include "webrtc/base/buffer.h" 14 #include "webrtc/base/buffer.h"
12 15
13 #include <cassert>
14 #include <utility>
15
16 namespace rtc { 16 namespace rtc {
17 17
18 Buffer::Buffer() : size_(0), capacity_(0), data_(nullptr) { 18 namespace internal {
19 assert(IsConsistent()); 19
20 BufferData::BufferData()
21 : size_(0),
22 capacity_(0),
23 data_(nullptr) {
24 RTC_DCHECK(IsConsistent());
20 } 25 }
21 26
22 Buffer::Buffer(const Buffer& buf) : Buffer(buf.data(), buf.size()) { 27 BufferData::BufferData(const BufferData& data)
28 : size_(data.size_),
29 capacity_(data.capacity_),
30 data_(new uint8_t[capacity_]) {
31 RTC_DCHECK(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)
29 assert(IsConsistent()); 41 : size_(size),
42 capacity_(std::max(size, capacity)),
43 data_(new uint8_t[capacity_]) {
44 RTC_DCHECK(IsConsistent());
45 }
46
47 BufferData::BufferData(const void* data, size_t size)
48 : BufferData(data, size, size) {
49 }
50
51 BufferData::BufferData(const void* data, size_t size, size_t capacity)
52 : BufferData(size, capacity) {
53 std::memcpy(data_.get(), data, size);
54 }
55
56 } // namespace internal
57
58
59 Buffer::Buffer() : data_(new internal::BufferData()) {
60 }
kwiberg-webrtc 2016/02/14 14:43:45 Hmm. With this change, you no longer have the prop
joachim 2016/02/14 17:10:16 Changed to only allocate if necessary.
61
62 Buffer::Buffer(const Buffer& buf) : data_(buf.data_) {
63 }
64
65 Buffer::Buffer(Buffer&& buf) : data_(std::move(buf.data_)) {
30 buf.OnMovedFrom(); 66 buf.OnMovedFrom();
31 } 67 }
32 68
33 Buffer::Buffer(size_t size) : Buffer(size, size) { 69 Buffer::Buffer(size_t size) : Buffer(size, size) {
34 } 70 }
35 71
36 Buffer::Buffer(size_t size, size_t capacity) 72 Buffer::Buffer(size_t size, size_t capacity)
37 : size_(size), 73 : data_(new internal::BufferData(size, capacity)) {
38 capacity_(std::max(size, capacity)), 74 RTC_DCHECK(IsConsistent());
39 data_(new uint8_t[capacity_]) {
40 assert(IsConsistent());
41 } 75 }
42 76
43 // Note: The destructor works even if the buffer has been moved from. 77 Buffer::~Buffer() {
44 Buffer::~Buffer() = default; 78 }
kwiberg-webrtc 2016/02/14 14:43:45 Why not =default? It's one line shorter...
joachim 2016/02/14 17:10:16 Right, that was a leftover of some earlier patch s
45 79
46 }; // namespace rtc 80 }; // namespace rtc
OLDNEW
« webrtc/base/buffer.h ('K') | « webrtc/base/buffer.h ('k') | webrtc/base/buffer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698