Chromium Code Reviews| Index: base/pickle.cc |
| diff --git a/base/pickle.cc b/base/pickle.cc |
| index cf4a865d35c337421b0acea57a05211fa076b06f..ea8bee1a36015e86bac30bc16cfc07bc5a054c0b 100644 |
| --- a/base/pickle.cc |
| +++ b/base/pickle.cc |
| @@ -8,6 +8,9 @@ |
| #include <algorithm> // for max() |
| +#include "base/bits.h" |
| +#include "base/macros.h" |
| + |
| namespace base { |
| // static |
| @@ -34,7 +37,7 @@ inline bool PickleIterator::ReadBuiltinType(Type* result) { |
| } |
| inline void PickleIterator::Advance(size_t size) { |
| - size_t aligned_size = AlignInt(size, sizeof(uint32_t)); |
| + size_t aligned_size = bits::Align(size, sizeof(uint32_t)); |
| if (end_index_ - read_index_ < aligned_size) { |
| read_index_ = end_index_; |
| } else { |
| @@ -210,13 +213,15 @@ Pickle::Pickle() |
| header_size_(sizeof(Header)), |
| capacity_after_header_(0), |
| write_offset_(0) { |
| + COMPILE_ASSERT((Pickle::kPayloadUnit & (Pickle::kPayloadUnit - 1)) == 0, |
| + pickle_kpayloadunit_must_be_a_power_of_two); |
|
piman
2015/07/23 22:37:11
nit: static_assert
|
| Resize(kPayloadUnit); |
| header_->payload_size = 0; |
| } |
| Pickle::Pickle(int header_size) |
| : header_(NULL), |
| - header_size_(AlignInt(header_size, sizeof(uint32))), |
| + header_size_(bits::Align(header_size, sizeof(uint32))), |
| capacity_after_header_(0), |
| write_offset_(0) { |
| DCHECK_GE(static_cast<size_t>(header_size), sizeof(Header)); |
| @@ -236,7 +241,7 @@ Pickle::Pickle(const char* data, int data_len) |
| if (header_size_ > static_cast<unsigned int>(data_len)) |
| header_size_ = 0; |
| - if (header_size_ != AlignInt(header_size_, sizeof(uint32))) |
| + if (header_size_ != bits::Align(header_size_, sizeof(uint32))) |
| header_size_ = 0; |
| // If there is anything wrong with the data, we're not going to use it. |
| @@ -305,7 +310,7 @@ bool Pickle::WriteBytes(const void* data, int length) { |
| } |
| void Pickle::Reserve(size_t length) { |
| - size_t data_len = AlignInt(length, sizeof(uint32)); |
| + size_t data_len = bits::Align(length, sizeof(uint32)); |
| DCHECK_GE(data_len, length); |
| #ifdef ARCH_CPU_64_BITS |
| DCHECK_LE(data_len, kuint32max); |
| @@ -318,7 +323,7 @@ void Pickle::Reserve(size_t length) { |
| void Pickle::Resize(size_t new_capacity) { |
| CHECK_NE(capacity_after_header_, kCapacityReadOnly); |
| - capacity_after_header_ = AlignInt(new_capacity, kPayloadUnit); |
| + capacity_after_header_ = bits::Align(new_capacity, kPayloadUnit); |
| void* p = realloc(header_, GetTotalAllocatedSize()); |
| CHECK(p); |
| header_ = reinterpret_cast<Header*>(p); |
| @@ -334,7 +339,7 @@ size_t Pickle::GetTotalAllocatedSize() const { |
| const char* Pickle::FindNext(size_t header_size, |
| const char* start, |
| const char* end) { |
| - DCHECK_EQ(header_size, AlignInt(header_size, sizeof(uint32))); |
| + DCHECK_EQ(header_size, bits::Align(header_size, sizeof(uint32))); |
| DCHECK_LE(header_size, static_cast<size_t>(kPayloadUnit)); |
| size_t length = static_cast<size_t>(end - start); |
| @@ -359,7 +364,7 @@ inline void Pickle::WriteBytesCommon(const void* data, size_t length) { |
| DCHECK_NE(kCapacityReadOnly, capacity_after_header_) |
| << "oops: pickle is readonly"; |
| MSAN_CHECK_MEM_IS_INITIALIZED(data, length); |
| - size_t data_len = AlignInt(length, sizeof(uint32)); |
| + size_t data_len = bits::Align(length, sizeof(uint32)); |
| DCHECK_GE(data_len, length); |
| #ifdef ARCH_CPU_64_BITS |
| DCHECK_LE(data_len, kuint32max); |
| @@ -367,7 +372,11 @@ inline void Pickle::WriteBytesCommon(const void* data, size_t length) { |
| DCHECK_LE(write_offset_, kuint32max - data_len); |
| size_t new_size = write_offset_ + data_len; |
| if (new_size > capacity_after_header_) { |
| - Resize(std::max(capacity_after_header_ * 2, new_size)); |
| + size_t new_capacity = capacity_after_header_ * 2; |
| + const size_t kPickleHeapAlign = 4096; |
| + if (new_capacity > kPickleHeapAlign) |
| + new_capacity = bits::Align(new_capacity, kPickleHeapAlign) - kPayloadUnit; |
| + Resize(std::max(new_capacity, new_size)); |
|
piman
2015/07/23 22:37:11
You may want to do the Align after the max to get
|
| } |
| char* write = mutable_payload() + write_offset_; |