Move CopyOnWriteBuffer functions definitions from .h to .cc
R=kwiberg@webrtc.org Review URL: https://codereview.webrtc.org/2338843002 . Cr-Commit-Position: refs/heads/master@{#14217}
This commit is contained in:
parent
2e164c6b53
commit
3626d7e247
@ -38,4 +38,75 @@ CopyOnWriteBuffer::CopyOnWriteBuffer(size_t size, size_t capacity)
|
||||
|
||||
CopyOnWriteBuffer::~CopyOnWriteBuffer() = default;
|
||||
|
||||
bool CopyOnWriteBuffer::operator==(const CopyOnWriteBuffer& buf) const {
|
||||
// Must either use the same buffer internally or have the same contents.
|
||||
RTC_DCHECK(IsConsistent());
|
||||
RTC_DCHECK(buf.IsConsistent());
|
||||
return buffer_.get() == buf.buffer_.get() ||
|
||||
(buffer_.get() && buf.buffer_.get() &&
|
||||
*buffer_.get() == *buf.buffer_.get());
|
||||
}
|
||||
|
||||
void CopyOnWriteBuffer::SetSize(size_t size) {
|
||||
RTC_DCHECK(IsConsistent());
|
||||
if (!buffer_) {
|
||||
if (size > 0) {
|
||||
buffer_ = new RefCountedObject<Buffer>(size);
|
||||
}
|
||||
RTC_DCHECK(IsConsistent());
|
||||
return;
|
||||
}
|
||||
|
||||
// Clone data if referenced.
|
||||
if (!buffer_->HasOneRef()) {
|
||||
buffer_ = new RefCountedObject<Buffer>(
|
||||
buffer_->data(),
|
||||
std::min(buffer_->size(), size),
|
||||
std::max(buffer_->capacity(), size));
|
||||
}
|
||||
buffer_->SetSize(size);
|
||||
RTC_DCHECK(IsConsistent());
|
||||
}
|
||||
|
||||
void CopyOnWriteBuffer::EnsureCapacity(size_t capacity) {
|
||||
RTC_DCHECK(IsConsistent());
|
||||
if (!buffer_) {
|
||||
if (capacity > 0) {
|
||||
buffer_ = new RefCountedObject<Buffer>(0, capacity);
|
||||
}
|
||||
RTC_DCHECK(IsConsistent());
|
||||
return;
|
||||
} else if (capacity <= buffer_->capacity()) {
|
||||
return;
|
||||
}
|
||||
|
||||
CloneDataIfReferenced(std::max(buffer_->capacity(), capacity));
|
||||
buffer_->EnsureCapacity(capacity);
|
||||
RTC_DCHECK(IsConsistent());
|
||||
}
|
||||
|
||||
void CopyOnWriteBuffer::Clear() {
|
||||
if (!buffer_)
|
||||
return;
|
||||
|
||||
if (buffer_->HasOneRef()) {
|
||||
buffer_->Clear();
|
||||
} else {
|
||||
buffer_ = new RefCountedObject<Buffer>(0, buffer_->capacity());
|
||||
}
|
||||
RTC_DCHECK(IsConsistent());
|
||||
}
|
||||
|
||||
void CopyOnWriteBuffer::CloneDataIfReferenced(size_t new_capacity) {
|
||||
if (buffer_->HasOneRef()) {
|
||||
return;
|
||||
}
|
||||
|
||||
buffer_ = new RefCountedObject<Buffer>(buffer_->data(), buffer_->size(),
|
||||
new_capacity);
|
||||
RTC_DCHECK(IsConsistent());
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace rtc
|
||||
|
||||
@ -123,14 +123,7 @@ class CopyOnWriteBuffer {
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool operator==(const CopyOnWriteBuffer& buf) const {
|
||||
// Must either use the same buffer internally or have the same contents.
|
||||
RTC_DCHECK(IsConsistent());
|
||||
RTC_DCHECK(buf.IsConsistent());
|
||||
return buffer_.get() == buf.buffer_.get() ||
|
||||
(buffer_.get() && buf.buffer_.get() &&
|
||||
*buffer_.get() == *buf.buffer_.get());
|
||||
}
|
||||
bool operator==(const CopyOnWriteBuffer& buf) const;
|
||||
|
||||
bool operator!=(const CopyOnWriteBuffer& buf) const {
|
||||
return !(*this == buf);
|
||||
@ -213,60 +206,16 @@ class CopyOnWriteBuffer {
|
||||
// buffer contents will be kept but truncated; if the new size is greater,
|
||||
// the existing contents will be kept and the new space will be
|
||||
// uninitialized.
|
||||
void SetSize(size_t size) {
|
||||
RTC_DCHECK(IsConsistent());
|
||||
if (!buffer_) {
|
||||
if (size > 0) {
|
||||
buffer_ = new RefCountedObject<Buffer>(size);
|
||||
}
|
||||
RTC_DCHECK(IsConsistent());
|
||||
return;
|
||||
}
|
||||
|
||||
// Clone data if referenced.
|
||||
if (!buffer_->HasOneRef()) {
|
||||
buffer_ = new RefCountedObject<Buffer>(
|
||||
buffer_->data(),
|
||||
std::min(buffer_->size(), size),
|
||||
std::max(buffer_->capacity(), size));
|
||||
}
|
||||
buffer_->SetSize(size);
|
||||
RTC_DCHECK(IsConsistent());
|
||||
}
|
||||
void SetSize(size_t size);
|
||||
|
||||
// Ensure that the buffer size can be increased to at least capacity without
|
||||
// further reallocation. (Of course, this operation might need to reallocate
|
||||
// the buffer.)
|
||||
void EnsureCapacity(size_t capacity) {
|
||||
RTC_DCHECK(IsConsistent());
|
||||
if (!buffer_) {
|
||||
if (capacity > 0) {
|
||||
buffer_ = new RefCountedObject<Buffer>(0, capacity);
|
||||
}
|
||||
RTC_DCHECK(IsConsistent());
|
||||
return;
|
||||
} else if (capacity <= buffer_->capacity()) {
|
||||
return;
|
||||
}
|
||||
|
||||
CloneDataIfReferenced(std::max(buffer_->capacity(), capacity));
|
||||
buffer_->EnsureCapacity(capacity);
|
||||
RTC_DCHECK(IsConsistent());
|
||||
}
|
||||
void EnsureCapacity(size_t capacity);
|
||||
|
||||
// Resets the buffer to zero size without altering capacity. Works even if the
|
||||
// buffer has been moved from.
|
||||
void Clear() {
|
||||
if (!buffer_)
|
||||
return;
|
||||
|
||||
if (buffer_->HasOneRef()) {
|
||||
buffer_->Clear();
|
||||
} else {
|
||||
buffer_ = new RefCountedObject<Buffer>(0, buffer_->capacity());
|
||||
}
|
||||
RTC_DCHECK(IsConsistent());
|
||||
}
|
||||
void Clear();
|
||||
|
||||
// Swaps two buffers.
|
||||
friend void swap(CopyOnWriteBuffer& a, CopyOnWriteBuffer& b) {
|
||||
@ -276,15 +225,7 @@ class CopyOnWriteBuffer {
|
||||
private:
|
||||
// Create a copy of the underlying data if it is referenced from other Buffer
|
||||
// objects.
|
||||
void CloneDataIfReferenced(size_t new_capacity) {
|
||||
if (buffer_->HasOneRef()) {
|
||||
return;
|
||||
}
|
||||
|
||||
buffer_ = new RefCountedObject<Buffer>(buffer_->data(), buffer_->size(),
|
||||
new_capacity);
|
||||
RTC_DCHECK(IsConsistent());
|
||||
}
|
||||
void CloneDataIfReferenced(size_t new_capacity);
|
||||
|
||||
// Pre- and postcondition of all methods.
|
||||
bool IsConsistent() const {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user