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 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
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 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 return &rep_->elements[current_size_]; | 51 return &rep_->elements[current_size_]; |
52 } | 52 } |
53 Rep* old_rep = rep_; | 53 Rep* old_rep = rep_; |
54 Arena* arena = GetArenaNoVirtual(); | 54 Arena* arena = GetArenaNoVirtual(); |
55 new_size = std::max(kMinRepeatedFieldAllocationSize, | 55 new_size = std::max(kMinRepeatedFieldAllocationSize, |
56 std::max(total_size_ * 2, new_size)); | 56 std::max(total_size_ * 2, new_size)); |
57 GOOGLE_CHECK_LE(new_size, | 57 GOOGLE_CHECK_LE(new_size, |
58 (std::numeric_limits<size_t>::max() - kRepHeaderSize) / | 58 (std::numeric_limits<size_t>::max() - kRepHeaderSize) / |
59 sizeof(old_rep->elements[0])) | 59 sizeof(old_rep->elements[0])) |
60 << "Requested size is too large to fit into size_t."; | 60 << "Requested size is too large to fit into size_t."; |
| 61 size_t bytes = kRepHeaderSize + sizeof(old_rep->elements[0]) * new_size; |
61 if (arena == NULL) { | 62 if (arena == NULL) { |
62 rep_ = reinterpret_cast<Rep*>( | 63 rep_ = reinterpret_cast<Rep*>(::operator new(bytes)); |
63 new char[kRepHeaderSize + sizeof(old_rep->elements[0]) * new_size]); | |
64 } else { | 64 } else { |
65 rep_ = reinterpret_cast<Rep*>( | 65 rep_ = reinterpret_cast<Rep*>( |
66 ::google::protobuf::Arena::CreateArray<char>(arena, | 66 ::google::protobuf::Arena::CreateArray<char>(arena, bytes)); |
67 kRepHeaderSize + sizeof(old_rep->elements[0]) * new_size)); | |
68 } | 67 } |
| 68 #if defined(__GXX_DELETE_WITH_SIZE__) || defined(__cpp_sized_deallocation) |
| 69 const int old_total_size = total_size_; |
| 70 #endif |
69 total_size_ = new_size; | 71 total_size_ = new_size; |
70 if (old_rep && old_rep->allocated_size > 0) { | 72 if (old_rep && old_rep->allocated_size > 0) { |
71 memcpy(rep_->elements, old_rep->elements, | 73 memcpy(rep_->elements, old_rep->elements, |
72 old_rep->allocated_size * sizeof(rep_->elements[0])); | 74 old_rep->allocated_size * sizeof(rep_->elements[0])); |
73 rep_->allocated_size = old_rep->allocated_size; | 75 rep_->allocated_size = old_rep->allocated_size; |
74 } else { | 76 } else { |
75 rep_->allocated_size = 0; | 77 rep_->allocated_size = 0; |
76 } | 78 } |
77 if (arena == NULL) { | 79 if (arena == NULL) { |
78 delete [] reinterpret_cast<char*>(old_rep); | 80 #if defined(__GXX_DELETE_WITH_SIZE__) || defined(__cpp_sized_deallocation) |
| 81 const size_t old_size = |
| 82 old_total_size * sizeof(rep_->elements[0]) + kRepHeaderSize; |
| 83 ::operator delete(static_cast<void*>(old_rep), old_size); |
| 84 #else |
| 85 ::operator delete(static_cast<void*>(old_rep)); |
| 86 #endif |
79 } | 87 } |
80 return &rep_->elements[current_size_]; | 88 return &rep_->elements[current_size_]; |
81 } | 89 } |
82 | 90 |
83 void RepeatedPtrFieldBase::Reserve(int new_size) { | 91 void RepeatedPtrFieldBase::Reserve(int new_size) { |
84 if (new_size > current_size_) { | 92 if (new_size > current_size_) { |
85 InternalExtend(new_size - current_size_); | 93 InternalExtend(new_size - current_size_); |
86 } | 94 } |
87 } | 95 } |
88 | 96 |
89 void RepeatedPtrFieldBase::CloseGap(int start, int num) { | 97 void RepeatedPtrFieldBase::CloseGap(int start, int num) { |
90 if (rep_ == NULL) return; | 98 if (rep_ == NULL) return; |
91 // Close up a gap of "num" elements starting at offset "start". | 99 // Close up a gap of "num" elements starting at offset "start". |
92 for (int i = start + num; i < rep_->allocated_size; ++i) | 100 for (int i = start + num; i < rep_->allocated_size; ++i) |
93 rep_->elements[i - num] = rep_->elements[i]; | 101 rep_->elements[i - num] = rep_->elements[i]; |
94 current_size_ -= num; | 102 current_size_ -= num; |
95 rep_->allocated_size -= num; | 103 rep_->allocated_size -= num; |
96 } | 104 } |
97 | 105 |
98 } // namespace internal | 106 } // namespace internal |
99 | 107 |
100 | 108 |
101 } // namespace protobuf | 109 } // namespace protobuf |
102 } // namespace google | 110 } // namespace google |
OLD | NEW |