OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
3 // http://code.google.com/p/protobuf/ | 3 // http://code.google.com/p/protobuf/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
(...skipping 28 matching lines...) Expand all Loading... |
39 | 39 |
40 namespace google { | 40 namespace google { |
41 namespace protobuf { | 41 namespace protobuf { |
42 | 42 |
43 namespace internal { | 43 namespace internal { |
44 | 44 |
45 void RepeatedPtrFieldBase::Reserve(int new_size) { | 45 void RepeatedPtrFieldBase::Reserve(int new_size) { |
46 if (total_size_ >= new_size) return; | 46 if (total_size_ >= new_size) return; |
47 | 47 |
48 void** old_elements = elements_; | 48 void** old_elements = elements_; |
49 total_size_ = max(total_size_ * 2, new_size); | 49 total_size_ = max(kMinRepeatedFieldAllocationSize, |
| 50 max(total_size_ * 2, new_size)); |
50 elements_ = new void*[total_size_]; | 51 elements_ = new void*[total_size_]; |
51 memcpy(elements_, old_elements, allocated_size_ * sizeof(elements_[0])); | 52 if (old_elements != NULL) { |
52 if (old_elements != initial_space_) { | 53 memcpy(elements_, old_elements, allocated_size_ * sizeof(elements_[0])); |
53 delete [] old_elements; | 54 delete [] old_elements; |
54 } | 55 } |
55 } | 56 } |
56 | 57 |
57 void RepeatedPtrFieldBase::Swap(RepeatedPtrFieldBase* other) { | 58 void RepeatedPtrFieldBase::Swap(RepeatedPtrFieldBase* other) { |
| 59 if (this == other) return; |
58 void** swap_elements = elements_; | 60 void** swap_elements = elements_; |
59 int swap_current_size = current_size_; | 61 int swap_current_size = current_size_; |
60 int swap_allocated_size = allocated_size_; | 62 int swap_allocated_size = allocated_size_; |
61 int swap_total_size = total_size_; | 63 int swap_total_size = total_size_; |
62 // We may not be using initial_space_ but it's not worth checking. Just | |
63 // copy it anyway. | |
64 void* swap_initial_space[kInitialSize]; | |
65 memcpy(swap_initial_space, initial_space_, sizeof(initial_space_)); | |
66 | 64 |
67 elements_ = other->elements_; | 65 elements_ = other->elements_; |
68 current_size_ = other->current_size_; | 66 current_size_ = other->current_size_; |
69 allocated_size_ = other->allocated_size_; | 67 allocated_size_ = other->allocated_size_; |
70 total_size_ = other->total_size_; | 68 total_size_ = other->total_size_; |
71 memcpy(initial_space_, other->initial_space_, sizeof(initial_space_)); | |
72 | 69 |
73 other->elements_ = swap_elements; | 70 other->elements_ = swap_elements; |
74 other->current_size_ = swap_current_size; | 71 other->current_size_ = swap_current_size; |
75 other->allocated_size_ = swap_allocated_size; | 72 other->allocated_size_ = swap_allocated_size; |
76 other->total_size_ = swap_total_size; | 73 other->total_size_ = swap_total_size; |
77 memcpy(other->initial_space_, swap_initial_space, sizeof(swap_initial_space)); | |
78 | |
79 if (elements_ == other->initial_space_) { | |
80 elements_ = initial_space_; | |
81 } | |
82 if (other->elements_ == initial_space_) { | |
83 other->elements_ = other->initial_space_; | |
84 } | |
85 } | 74 } |
86 | 75 |
87 string* StringTypeHandlerBase::New() { | 76 string* StringTypeHandlerBase::New() { |
88 return new string; | 77 return new string; |
89 } | 78 } |
90 void StringTypeHandlerBase::Delete(string* value) { | 79 void StringTypeHandlerBase::Delete(string* value) { |
91 delete value; | 80 delete value; |
92 } | 81 } |
93 | 82 |
94 } // namespace internal | 83 } // namespace internal |
95 | 84 |
96 | 85 |
97 } // namespace protobuf | 86 } // namespace protobuf |
98 } // namespace google | 87 } // namespace google |
OLD | NEW |