| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/pickle.h" | 5 #include "base/pickle.h" |
| 6 | 6 |
| 7 #include <stdlib.h> | 7 #include <stdlib.h> |
| 8 | 8 |
| 9 #include <algorithm> // for max() | 9 #include <algorithm> // for max() |
| 10 #include <limits> | 10 #include <limits> |
| (...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 322 } | 322 } |
| 323 | 323 |
| 324 void Pickle::Resize(size_t new_capacity) { | 324 void Pickle::Resize(size_t new_capacity) { |
| 325 CHECK_NE(capacity_after_header_, kCapacityReadOnly); | 325 CHECK_NE(capacity_after_header_, kCapacityReadOnly); |
| 326 capacity_after_header_ = bits::Align(new_capacity, kPayloadUnit); | 326 capacity_after_header_ = bits::Align(new_capacity, kPayloadUnit); |
| 327 void* p = realloc(header_, GetTotalAllocatedSize()); | 327 void* p = realloc(header_, GetTotalAllocatedSize()); |
| 328 CHECK(p); | 328 CHECK(p); |
| 329 header_ = reinterpret_cast<Header*>(p); | 329 header_ = reinterpret_cast<Header*>(p); |
| 330 } | 330 } |
| 331 | 331 |
| 332 void* Pickle::ClaimBytes(size_t num_bytes) { |
| 333 void* p = ClaimUninitializedBytesInternal(num_bytes); |
| 334 CHECK(p); |
| 335 memset(p, 0, num_bytes); |
| 336 return p; |
| 337 } |
| 338 |
| 332 size_t Pickle::GetTotalAllocatedSize() const { | 339 size_t Pickle::GetTotalAllocatedSize() const { |
| 333 if (capacity_after_header_ == kCapacityReadOnly) | 340 if (capacity_after_header_ == kCapacityReadOnly) |
| 334 return 0; | 341 return 0; |
| 335 return header_size_ + capacity_after_header_; | 342 return header_size_ + capacity_after_header_; |
| 336 } | 343 } |
| 337 | 344 |
| 338 // static | 345 // static |
| 339 const char* Pickle::FindNext(size_t header_size, | 346 const char* Pickle::FindNext(size_t header_size, |
| 340 const char* start, | 347 const char* start, |
| 341 const char* end) { | 348 const char* end) { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 377 } | 384 } |
| 378 | 385 |
| 379 template <size_t length> void Pickle::WriteBytesStatic(const void* data) { | 386 template <size_t length> void Pickle::WriteBytesStatic(const void* data) { |
| 380 WriteBytesCommon(data, length); | 387 WriteBytesCommon(data, length); |
| 381 } | 388 } |
| 382 | 389 |
| 383 template void Pickle::WriteBytesStatic<2>(const void* data); | 390 template void Pickle::WriteBytesStatic<2>(const void* data); |
| 384 template void Pickle::WriteBytesStatic<4>(const void* data); | 391 template void Pickle::WriteBytesStatic<4>(const void* data); |
| 385 template void Pickle::WriteBytesStatic<8>(const void* data); | 392 template void Pickle::WriteBytesStatic<8>(const void* data); |
| 386 | 393 |
| 387 inline void Pickle::WriteBytesCommon(const void* data, size_t length) { | 394 inline void* Pickle::ClaimUninitializedBytesInternal(size_t length) { |
| 388 DCHECK_NE(kCapacityReadOnly, capacity_after_header_) | 395 DCHECK_NE(kCapacityReadOnly, capacity_after_header_) |
| 389 << "oops: pickle is readonly"; | 396 << "oops: pickle is readonly"; |
| 390 MSAN_CHECK_MEM_IS_INITIALIZED(data, length); | |
| 391 size_t data_len = bits::Align(length, sizeof(uint32_t)); | 397 size_t data_len = bits::Align(length, sizeof(uint32_t)); |
| 392 DCHECK_GE(data_len, length); | 398 DCHECK_GE(data_len, length); |
| 393 #ifdef ARCH_CPU_64_BITS | 399 #ifdef ARCH_CPU_64_BITS |
| 394 DCHECK_LE(data_len, std::numeric_limits<uint32_t>::max()); | 400 DCHECK_LE(data_len, std::numeric_limits<uint32_t>::max()); |
| 395 #endif | 401 #endif |
| 396 DCHECK_LE(write_offset_, std::numeric_limits<uint32_t>::max() - data_len); | 402 DCHECK_LE(write_offset_, std::numeric_limits<uint32_t>::max() - data_len); |
| 397 size_t new_size = write_offset_ + data_len; | 403 size_t new_size = write_offset_ + data_len; |
| 398 if (new_size > capacity_after_header_) { | 404 if (new_size > capacity_after_header_) { |
| 399 size_t new_capacity = capacity_after_header_ * 2; | 405 size_t new_capacity = capacity_after_header_ * 2; |
| 400 const size_t kPickleHeapAlign = 4096; | 406 const size_t kPickleHeapAlign = 4096; |
| 401 if (new_capacity > kPickleHeapAlign) | 407 if (new_capacity > kPickleHeapAlign) |
| 402 new_capacity = bits::Align(new_capacity, kPickleHeapAlign) - kPayloadUnit; | 408 new_capacity = bits::Align(new_capacity, kPickleHeapAlign) - kPayloadUnit; |
| 403 Resize(std::max(new_capacity, new_size)); | 409 Resize(std::max(new_capacity, new_size)); |
| 404 } | 410 } |
| 405 | 411 |
| 406 char* write = mutable_payload() + write_offset_; | 412 char* write = mutable_payload() + write_offset_; |
| 407 memcpy(write, data, length); | 413 memset(write + length, 0, data_len - length); // Always initialize padding |
| 408 memset(write + length, 0, data_len - length); | |
| 409 header_->payload_size = static_cast<uint32_t>(new_size); | 414 header_->payload_size = static_cast<uint32_t>(new_size); |
| 410 write_offset_ = new_size; | 415 write_offset_ = new_size; |
| 416 return write; |
| 417 } |
| 418 |
| 419 inline void Pickle::WriteBytesCommon(const void* data, size_t length) { |
| 420 DCHECK_NE(kCapacityReadOnly, capacity_after_header_) |
| 421 << "oops: pickle is readonly"; |
| 422 MSAN_CHECK_MEM_IS_INITIALIZED(data, length); |
| 423 void* write = ClaimUninitializedBytesInternal(length); |
| 424 memcpy(write, data, length); |
| 411 } | 425 } |
| 412 | 426 |
| 413 } // namespace base | 427 } // namespace base |
| OLD | NEW |