Chromium Code Reviews| Index: base/pickle.cc |
| diff --git a/base/pickle.cc b/base/pickle.cc |
| index af3191b9d86db2785fe375ecbcb045cf0a420870..e0cb47382137ae91bd3eabb2c8a0743d42426062 100644 |
| --- a/base/pickle.cc |
| +++ b/base/pickle.cc |
| @@ -195,8 +195,7 @@ Pickle::Pickle(const Pickle& other) |
| capacity_(0), |
| variable_buffer_offset_(other.variable_buffer_offset_) { |
| size_t payload_size = header_size_ + other.header_->payload_size; |
| - bool resized = Resize(payload_size); |
| - CHECK(resized); // Realloc failed. |
| + Resize(payload_size); |
| memcpy(header_, other.header_, payload_size); |
| } |
| @@ -219,8 +218,7 @@ Pickle& Pickle::operator=(const Pickle& other) { |
| header_ = NULL; |
| header_size_ = other.header_size_; |
| } |
| - bool resized = Resize(other.header_size_ + other.header_->payload_size); |
| - CHECK(resized); // Realloc failed. |
| + Resize(other.header_size_ + other.header_->payload_size); |
| memcpy(header_, other.header_, |
| other.header_size_ + other.header_->payload_size); |
| variable_buffer_offset_ = other.variable_buffer_offset_; |
| @@ -304,14 +302,24 @@ void Pickle::TrimWriteData(int new_length) { |
| *cur_length = new_length; |
| } |
| +void Pickle::Reserve(size_t length) { |
| + // write at a uint32-aligned offset from the beginning of the header |
| + size_t offset = AlignInt(header_->payload_size, sizeof(uint32)); |
| + |
| + size_t new_size = offset + length; |
| + size_t needed_size = header_size_ + new_size; |
| + if (needed_size > capacity_) |
| + Resize(capacity_ * 2 + needed_size); |
|
Tom Sepez
2013/10/24 17:48:06
Why 2x? Also, this should be a bool return value,
danakj
2013/10/24 17:51:38
Same reason as in BeginWrite, in order to amortize
|
| +} |
| + |
| char* Pickle::BeginWrite(size_t length) { |
| // write at a uint32-aligned offset from the beginning of the header |
| size_t offset = AlignInt(header_->payload_size, sizeof(uint32)); |
| size_t new_size = offset + length; |
| size_t needed_size = header_size_ + new_size; |
| - if (needed_size > capacity_ && !Resize(std::max(capacity_ * 2, needed_size))) |
| - return NULL; |
| + if (needed_size > capacity_) |
| + Resize(std::max(capacity_ * 2, needed_size)); |
| #ifdef ARCH_CPU_64_BITS |
| DCHECK_LE(length, kuint32max); |
| @@ -328,17 +336,13 @@ void Pickle::EndWrite(char* dest, int length) { |
| memset(dest + length, 0, sizeof(uint32) - (length % sizeof(uint32))); |
| } |
| -bool Pickle::Resize(size_t new_capacity) { |
| - new_capacity = AlignInt(new_capacity, kPayloadUnit); |
| - |
| +void Pickle::Resize(size_t new_capacity) { |
| CHECK_NE(capacity_, kCapacityReadOnly); |
|
Tom Sepez
2013/10/24 17:48:06
There's a minor problem here in that if realloc fa
danakj
2013/10/24 17:51:38
if realloc fails, we will crash inside tcmalloc no
|
| - void* p = realloc(header_, new_capacity); |
| - if (!p) |
| - return false; |
| + new_capacity = AlignInt(new_capacity, kPayloadUnit); |
| + void* p = realloc(header_, new_capacity); |
| header_ = reinterpret_cast<Header*>(p); |
| capacity_ = new_capacity; |
| - return true; |
| } |
| // static |