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 | 10 |
(...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
310 #ifdef ARCH_CPU_64_BITS | 310 #ifdef ARCH_CPU_64_BITS |
311 DCHECK_LE(data_len, kuint32max); | 311 DCHECK_LE(data_len, kuint32max); |
312 #endif | 312 #endif |
313 DCHECK_LE(write_offset_, kuint32max - data_len); | 313 DCHECK_LE(write_offset_, kuint32max - data_len); |
314 size_t new_size = write_offset_ + data_len; | 314 size_t new_size = write_offset_ + data_len; |
315 if (new_size > capacity_after_header_) | 315 if (new_size > capacity_after_header_) |
316 Resize(capacity_after_header_ * 2 + new_size); | 316 Resize(capacity_after_header_ * 2 + new_size); |
317 } | 317 } |
318 | 318 |
319 void Pickle::Resize(size_t new_capacity) { | 319 void Pickle::Resize(size_t new_capacity) { |
320 new_capacity = AlignInt(new_capacity, kPayloadUnit); | |
321 | |
322 CHECK_NE(capacity_after_header_, kCapacityReadOnly); | 320 CHECK_NE(capacity_after_header_, kCapacityReadOnly); |
323 void* p = realloc(header_, header_size_ + new_capacity); | 321 capacity_after_header_ = AlignInt(new_capacity, kPayloadUnit); |
| 322 void* p = realloc(header_, GetTotalAllocatedSize()); |
324 CHECK(p); | 323 CHECK(p); |
325 header_ = reinterpret_cast<Header*>(p); | 324 header_ = reinterpret_cast<Header*>(p); |
326 capacity_after_header_ = new_capacity; | 325 } |
| 326 |
| 327 size_t Pickle::GetTotalAllocatedSize() const { |
| 328 if (capacity_after_header_ == kCapacityReadOnly) |
| 329 return 0; |
| 330 return header_size_ + capacity_after_header_; |
327 } | 331 } |
328 | 332 |
329 // static | 333 // static |
330 const char* Pickle::FindNext(size_t header_size, | 334 const char* Pickle::FindNext(size_t header_size, |
331 const char* start, | 335 const char* start, |
332 const char* end) { | 336 const char* end) { |
333 DCHECK_EQ(header_size, AlignInt(header_size, sizeof(uint32))); | 337 DCHECK_EQ(header_size, AlignInt(header_size, sizeof(uint32))); |
334 DCHECK_LE(header_size, static_cast<size_t>(kPayloadUnit)); | 338 DCHECK_LE(header_size, static_cast<size_t>(kPayloadUnit)); |
335 | 339 |
336 size_t length = static_cast<size_t>(end - start); | 340 size_t length = static_cast<size_t>(end - start); |
(...skipping 30 matching lines...) Expand all Loading... |
367 } | 371 } |
368 | 372 |
369 char* write = mutable_payload() + write_offset_; | 373 char* write = mutable_payload() + write_offset_; |
370 memcpy(write, data, length); | 374 memcpy(write, data, length); |
371 memset(write + length, 0, data_len - length); | 375 memset(write + length, 0, data_len - length); |
372 header_->payload_size = static_cast<uint32>(new_size); | 376 header_->payload_size = static_cast<uint32>(new_size); |
373 write_offset_ = new_size; | 377 write_offset_ = new_size; |
374 } | 378 } |
375 | 379 |
376 } // namespace base | 380 } // namespace base |
OLD | NEW |