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

Unified Diff: base/pickle.cc

Issue 1249643007: Align base::Pickle allocations to 4k boundaries (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@accounting_fix
Patch Set: Nits test Created 5 years, 5 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: base/pickle.cc
diff --git a/base/pickle.cc b/base/pickle.cc
index cf4a865d35c337421b0acea57a05211fa076b06f..d4487d45b24d61238309d3db48b1f17f4f2e0cca 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) {
+ static_assert((Pickle::kPayloadUnit & (Pickle::kPayloadUnit - 1)) == 0,
+ "Pickle::kPayloadUnit must be a power of two");
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));
}
char* write = mutable_payload() + write_offset_;

Powered by Google App Engine
This is Rietveld 408576698