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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: webrtc/base/buffer.cc
diff --git a/webrtc/base/buffer.cc b/webrtc/base/buffer.cc
index 62855f1620dfeac0c9f797c96492cbf7a99404f1..b61ff040e0e8fd404c81e0687242ee8519796394 100644
--- a/webrtc/base/buffer.cc
+++ b/webrtc/base/buffer.cc
@@ -8,25 +8,61 @@
* be found in the AUTHORS file in the root of the source tree.
*/
-#include "webrtc/base/buffer.h"
-
-#include <cassert>
#include <utility>
+#include "webrtc/base/checks.h"
+#include "webrtc/base/buffer.h"
+
namespace rtc {
-Buffer::Buffer() : size_(0), capacity_(0), data_(nullptr) {
- assert(IsConsistent());
+namespace internal {
+
+BufferData::BufferData()
+ : size_(0),
+ capacity_(0),
+ data_(nullptr) {
+ RTC_DCHECK(IsConsistent());
+}
+
+BufferData::BufferData(const BufferData& data)
+ : size_(data.size_),
+ capacity_(data.capacity_),
+ data_(new uint8_t[capacity_]) {
+ RTC_DCHECK(IsConsistent());
+ if (data.size_) {
+ std::memcpy(data_.get(), data.data_.get(), data.size_);
+ }
}
-Buffer::Buffer(const Buffer& buf) : Buffer(buf.data(), buf.size()) {
+BufferData::BufferData(size_t size) : BufferData(size, size) {
}
-Buffer::Buffer(Buffer&& buf)
- : size_(buf.size()),
- capacity_(buf.capacity()),
- data_(std::move(buf.data_)) {
- assert(IsConsistent());
+BufferData::BufferData(size_t size, size_t capacity)
+ : size_(size),
+ capacity_(std::max(size, capacity)),
+ data_(new uint8_t[capacity_]) {
+ RTC_DCHECK(IsConsistent());
+}
+
+BufferData::BufferData(const void* data, size_t size)
+ : BufferData(data, size, size) {
+}
+
+BufferData::BufferData(const void* data, size_t size, size_t capacity)
+ : BufferData(size, capacity) {
+ std::memcpy(data_.get(), data, size);
+}
+
+} // namespace internal
+
+
+Buffer::Buffer() : data_(new internal::BufferData()) {
+}
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.
+
+Buffer::Buffer(const Buffer& buf) : data_(buf.data_) {
+}
+
+Buffer::Buffer(Buffer&& buf) : data_(std::move(buf.data_)) {
buf.OnMovedFrom();
}
@@ -34,13 +70,11 @@ Buffer::Buffer(size_t size) : Buffer(size, size) {
}
Buffer::Buffer(size_t size, size_t capacity)
- : size_(size),
- capacity_(std::max(size, capacity)),
- data_(new uint8_t[capacity_]) {
- assert(IsConsistent());
+ : data_(new internal::BufferData(size, capacity)) {
+ RTC_DCHECK(IsConsistent());
}
-// Note: The destructor works even if the buffer has been moved from.
-Buffer::~Buffer() = default;
+Buffer::~Buffer() {
+}
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
}; // namespace rtc
« 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