| 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::ClaimUninitializedBytes(size_t num_bytes) { |
| 333 void* p = ClaimUninitializedBytesInternal(num_bytes); |
| 334 CHECK(p); |
| 335 return p; |
| 336 } |
| 337 |
| 338 void* Pickle::ClaimBytes(size_t num_bytes) { |
| 339 void* p = ClaimUninitializedBytesInternal(num_bytes); |
| 340 CHECK(p); |
| 341 memset(p, 0, num_bytes); |
| 342 return p; |
| 343 } |
| 344 |
| 332 size_t Pickle::GetTotalAllocatedSize() const { | 345 size_t Pickle::GetTotalAllocatedSize() const { |
| 333 if (capacity_after_header_ == kCapacityReadOnly) | 346 if (capacity_after_header_ == kCapacityReadOnly) |
| 334 return 0; | 347 return 0; |
| 335 return header_size_ + capacity_after_header_; | 348 return header_size_ + capacity_after_header_; |
| 336 } | 349 } |
| 337 | 350 |
| 338 // static | 351 // static |
| 339 const char* Pickle::FindNext(size_t header_size, | 352 const char* Pickle::FindNext(size_t header_size, |
| 340 const char* start, | 353 const char* start, |
| 341 const char* end) { | 354 const char* end) { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 377 } | 390 } |
| 378 | 391 |
| 379 template <size_t length> void Pickle::WriteBytesStatic(const void* data) { | 392 template <size_t length> void Pickle::WriteBytesStatic(const void* data) { |
| 380 WriteBytesCommon(data, length); | 393 WriteBytesCommon(data, length); |
| 381 } | 394 } |
| 382 | 395 |
| 383 template void Pickle::WriteBytesStatic<2>(const void* data); | 396 template void Pickle::WriteBytesStatic<2>(const void* data); |
| 384 template void Pickle::WriteBytesStatic<4>(const void* data); | 397 template void Pickle::WriteBytesStatic<4>(const void* data); |
| 385 template void Pickle::WriteBytesStatic<8>(const void* data); | 398 template void Pickle::WriteBytesStatic<8>(const void* data); |
| 386 | 399 |
| 387 inline void Pickle::WriteBytesCommon(const void* data, size_t length) { | 400 inline void* Pickle::ClaimUninitializedBytesInternal(size_t length) { |
| 388 DCHECK_NE(kCapacityReadOnly, capacity_after_header_) | 401 DCHECK_NE(kCapacityReadOnly, capacity_after_header_) |
| 389 << "oops: pickle is readonly"; | 402 << "oops: pickle is readonly"; |
| 390 MSAN_CHECK_MEM_IS_INITIALIZED(data, length); | |
| 391 size_t data_len = bits::Align(length, sizeof(uint32_t)); | 403 size_t data_len = bits::Align(length, sizeof(uint32_t)); |
| 392 DCHECK_GE(data_len, length); | 404 DCHECK_GE(data_len, length); |
| 393 #ifdef ARCH_CPU_64_BITS | 405 #ifdef ARCH_CPU_64_BITS |
| 394 DCHECK_LE(data_len, std::numeric_limits<uint32_t>::max()); | 406 DCHECK_LE(data_len, std::numeric_limits<uint32_t>::max()); |
| 395 #endif | 407 #endif |
| 396 DCHECK_LE(write_offset_, std::numeric_limits<uint32_t>::max() - data_len); | 408 DCHECK_LE(write_offset_, std::numeric_limits<uint32_t>::max() - data_len); |
| 397 size_t new_size = write_offset_ + data_len; | 409 size_t new_size = write_offset_ + data_len; |
| 398 if (new_size > capacity_after_header_) { | 410 if (new_size > capacity_after_header_) { |
| 399 size_t new_capacity = capacity_after_header_ * 2; | 411 size_t new_capacity = capacity_after_header_ * 2; |
| 400 const size_t kPickleHeapAlign = 4096; | 412 const size_t kPickleHeapAlign = 4096; |
| 401 if (new_capacity > kPickleHeapAlign) | 413 if (new_capacity > kPickleHeapAlign) |
| 402 new_capacity = bits::Align(new_capacity, kPickleHeapAlign) - kPayloadUnit; | 414 new_capacity = bits::Align(new_capacity, kPickleHeapAlign) - kPayloadUnit; |
| 403 Resize(std::max(new_capacity, new_size)); | 415 Resize(std::max(new_capacity, new_size)); |
| 404 } | 416 } |
| 405 | 417 |
| 406 char* write = mutable_payload() + write_offset_; | 418 char* write = mutable_payload() + write_offset_; |
| 407 memcpy(write, data, length); | 419 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); | 420 header_->payload_size = static_cast<uint32_t>(new_size); |
| 410 write_offset_ = new_size; | 421 write_offset_ = new_size; |
| 422 return write; |
| 423 } |
| 424 |
| 425 inline void Pickle::WriteBytesCommon(const void* data, size_t length) { |
| 426 DCHECK_NE(kCapacityReadOnly, capacity_after_header_) |
| 427 << "oops: pickle is readonly"; |
| 428 MSAN_CHECK_MEM_IS_INITIALIZED(data, length); |
| 429 void* write = ClaimUninitializedBytesInternal(length); |
| 430 memcpy(write, data, length); |
| 411 } | 431 } |
| 412 | 432 |
| 413 } // namespace base | 433 } // namespace base |
| OLD | NEW |