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

Side by Side Diff: base/pickle.cc

Issue 290173008: Refactor PickleIterator. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 6 years, 7 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
11 //------------------------------------------------------------------------------ 11 //------------------------------------------------------------------------------
12 12
13 using base::char16; 13 using base::char16;
14 using base::string16; 14 using base::string16;
15 15
16 // static 16 // static
17 const int Pickle::kPayloadUnit = 64; 17 const int Pickle::kPayloadUnit = 64;
18 18
19 static const size_t kCapacityReadOnly = static_cast<size_t>(-1); 19 static const size_t kCapacityReadOnly = static_cast<size_t>(-1);
20 20
21 PickleIterator::PickleIterator(const Pickle& pickle) 21 PickleIterator::PickleIterator(const Pickle& pickle)
22 : read_ptr_(pickle.payload()), 22 : payload_ptr_(pickle.payload()),
23 read_end_ptr_(pickle.end_of_payload()) { 23 read_index_(0),
24 end_index_(pickle.payload_size()) {
24 } 25 }
25 26
26 template <typename Type> 27 template <typename Type>
27 inline bool PickleIterator::ReadBuiltinType(Type* result) { 28 inline bool PickleIterator::ReadBuiltinType(Type* result) {
28 const char* read_from = GetReadPointerAndAdvance<Type>(); 29 const char* read_from = GetReadPointerAndAdvance<Type>();
29 if (!read_from) 30 if (!read_from)
30 return false; 31 return false;
31 if (sizeof(Type) > sizeof(uint32)) 32 if (sizeof(Type) > sizeof(uint32))
32 memcpy(result, read_from, sizeof(*result)); 33 memcpy(result, read_from, sizeof(*result));
33 else 34 else
34 *result = *reinterpret_cast<const Type*>(read_from); 35 *result = *reinterpret_cast<const Type*>(read_from);
35 return true; 36 return true;
36 } 37 }
37 38
39 inline void PickleIterator::Advance(size_t size) {
jar (doing other things) 2014/05/23 21:22:04 I *think* all (both?) calls to this method pass in
halyavin 2014/05/26 07:43:02 Done.
40 if (end_index_ - read_index_ < size) {
41 read_index_ = end_index_;
42 } else {
43 read_index_ += size;
44 }
45 }
46
38 template<typename Type> 47 template<typename Type>
39 inline const char* PickleIterator::GetReadPointerAndAdvance() { 48 inline const char* PickleIterator::GetReadPointerAndAdvance() {
40 const char* current_read_ptr = read_ptr_; 49 if (sizeof(Type) > end_index_ - read_index_)
41 if (read_ptr_ + sizeof(Type) > read_end_ptr_)
42 return NULL; 50 return NULL;
jar (doing other things) 2014/05/23 21:22:04 I agree that you perfectly translate the existing
halyavin 2014/05/26 07:43:02 Looks reasonable. Let us see what happens.
43 if (sizeof(Type) < sizeof(uint32)) 51 const char* current_read_ptr = payload_ptr_ + read_index_;
44 read_ptr_ += AlignInt(sizeof(Type), sizeof(uint32)); 52 Advance(AlignInt(sizeof(Type), sizeof(uint32_t)));
jar (doing other things) 2014/05/23 21:22:04 I think the precise translation of the old code wo
halyavin 2014/05/26 07:43:02 It was a bug since sizeof(Type) may not be divisib
45 else
46 read_ptr_ += sizeof(Type);
47 return current_read_ptr; 53 return current_read_ptr;
48 } 54 }
49 55
50 const char* PickleIterator::GetReadPointerAndAdvance(int num_bytes) { 56 const char* PickleIterator::GetReadPointerAndAdvance(int num_bytes) {
51 if (num_bytes < 0 || read_end_ptr_ - read_ptr_ < num_bytes) 57 if (num_bytes < 0 ||
58 end_index_ - read_index_ < static_cast<size_t>(num_bytes))
52 return NULL; 59 return NULL;
jar (doing other things) 2014/05/23 21:22:04 Here again I *wonder* if we should advance, making
halyavin 2014/05/26 07:43:02 Done.
53 const char* current_read_ptr = read_ptr_; 60 const char* current_read_ptr = payload_ptr_ + read_index_;
54 read_ptr_ += AlignInt(num_bytes, sizeof(uint32)); 61 Advance(AlignInt(num_bytes, sizeof(uint32_t)));
55 return current_read_ptr; 62 return current_read_ptr;
56 } 63 }
57 64
58 inline const char* PickleIterator::GetReadPointerAndAdvance(int num_elements, 65 inline const char* PickleIterator::GetReadPointerAndAdvance(int num_elements,
59 size_t size_element) { 66 size_t size_element) {
jar (doing other things) 2014/05/23 21:22:04 nit: While you're here.... all arguments should be
halyavin 2014/05/26 07:43:02 Done.
60 // Check for int32 overflow. 67 // Check for int32 overflow.
61 int64 num_bytes = static_cast<int64>(num_elements) * size_element; 68 int64 num_bytes = static_cast<int64>(num_elements) * size_element;
62 int num_bytes32 = static_cast<int>(num_bytes); 69 int num_bytes32 = static_cast<int>(num_bytes);
63 if (num_bytes != static_cast<int64>(num_bytes32)) 70 if (num_bytes != static_cast<int64>(num_bytes32))
64 return NULL; 71 return NULL;
65 return GetReadPointerAndAdvance(num_bytes32); 72 return GetReadPointerAndAdvance(num_bytes32);
66 } 73 }
67 74
68 bool PickleIterator::ReadBool(bool* result) { 75 bool PickleIterator::ReadBool(bool* result) {
69 return ReadBuiltinType(result); 76 return ReadBuiltinType(result);
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after
325 #endif 332 #endif
326 DCHECK_LE(write_offset_, kuint32max - data_len); 333 DCHECK_LE(write_offset_, kuint32max - data_len);
327 size_t new_size = write_offset_ + data_len; 334 size_t new_size = write_offset_ + data_len;
328 if (new_size > capacity_after_header_) { 335 if (new_size > capacity_after_header_) {
329 Resize(std::max(capacity_after_header_ * 2, new_size)); 336 Resize(std::max(capacity_after_header_ * 2, new_size));
330 } 337 }
331 338
332 char* write = mutable_payload() + write_offset_; 339 char* write = mutable_payload() + write_offset_;
333 memcpy(write, data, length); 340 memcpy(write, data, length);
334 memset(write + length, 0, data_len - length); 341 memset(write + length, 0, data_len - length);
335 header_->payload_size = static_cast<uint32>(write_offset_ + length); 342 header_->payload_size = static_cast<uint32>(new_size);
jar (doing other things) 2014/05/23 21:22:04 Would this change alone, which keeps payload_size
halyavin 2014/05/26 07:43:02 No, because the other side of IPC connection might
jar (doing other things) 2014/05/27 23:29:34 Isn't this the side that is writing into a pickle,
halyavin 2014/05/28 07:45:12 It doesn't. I meant that we still need to check al
jar (doing other things) 2014/05/29 01:51:16 FWIW: I checked... PNaCL uses its own IPC, and han
336 write_offset_ = new_size; 343 write_offset_ = new_size;
337 } 344 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698