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 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
297 if (new_length < 0 || new_length > *cur_length) { | 297 if (new_length < 0 || new_length > *cur_length) { |
298 NOTREACHED() << "Invalid length in TrimWriteData."; | 298 NOTREACHED() << "Invalid length in TrimWriteData."; |
299 return; | 299 return; |
300 } | 300 } |
301 | 301 |
302 // Update the payload size and variable buffer size | 302 // Update the payload size and variable buffer size |
303 header_->payload_size -= (*cur_length - new_length); | 303 header_->payload_size -= (*cur_length - new_length); |
304 *cur_length = new_length; | 304 *cur_length = new_length; |
305 } | 305 } |
306 | 306 |
| 307 void Pickle::Reserve(size_t additional_capacity) { |
| 308 // Write at a uint32-aligned offset from the beginning of the header. |
| 309 size_t offset = AlignInt(header_->payload_size, sizeof(uint32)); |
| 310 |
| 311 size_t new_size = offset + additional_capacity; |
| 312 size_t needed_size = header_size_ + new_size; |
| 313 if (needed_size > capacity_) |
| 314 Resize(capacity_ * 2 + needed_size); |
| 315 } |
| 316 |
307 char* Pickle::BeginWrite(size_t length) { | 317 char* Pickle::BeginWrite(size_t length) { |
308 // write at a uint32-aligned offset from the beginning of the header | 318 // Write at a uint32-aligned offset from the beginning of the header. |
309 size_t offset = AlignInt(header_->payload_size, sizeof(uint32)); | 319 size_t offset = AlignInt(header_->payload_size, sizeof(uint32)); |
310 | 320 |
311 size_t new_size = offset + length; | 321 size_t new_size = offset + length; |
312 size_t needed_size = header_size_ + new_size; | 322 size_t needed_size = header_size_ + new_size; |
313 if (needed_size > capacity_ && !Resize(std::max(capacity_ * 2, needed_size))) | 323 if (needed_size > capacity_ && !Resize(std::max(capacity_ * 2, needed_size))) |
314 return NULL; | 324 return NULL; |
315 | 325 |
316 #ifdef ARCH_CPU_64_BITS | 326 #ifdef ARCH_CPU_64_BITS |
317 DCHECK_LE(length, kuint32max); | 327 DCHECK_LE(length, kuint32max); |
318 #endif | 328 #endif |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
352 return NULL; | 362 return NULL; |
353 | 363 |
354 const Header* hdr = reinterpret_cast<const Header*>(start); | 364 const Header* hdr = reinterpret_cast<const Header*>(start); |
355 const char* payload_base = start + header_size; | 365 const char* payload_base = start + header_size; |
356 const char* payload_end = payload_base + hdr->payload_size; | 366 const char* payload_end = payload_base + hdr->payload_size; |
357 if (payload_end < payload_base) | 367 if (payload_end < payload_base) |
358 return NULL; | 368 return NULL; |
359 | 369 |
360 return (payload_end > end) ? NULL : payload_end; | 370 return (payload_end > end) ? NULL : payload_end; |
361 } | 371 } |
OLD | NEW |