| 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; | |
| 62 if (arena == NULL) { | 61 if (arena == NULL) { |
| 63 rep_ = reinterpret_cast<Rep*>(::operator new(bytes)); | 62 rep_ = reinterpret_cast<Rep*>( |
| 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, bytes)); | 66 ::google::protobuf::Arena::CreateArray<char>(arena, |
| 67 kRepHeaderSize + sizeof(old_rep->elements[0]) * new_size)); |
| 67 } | 68 } |
| 68 #if defined(__GXX_DELETE_WITH_SIZE__) || defined(__cpp_sized_deallocation) | |
| 69 const int old_total_size = total_size_; | |
| 70 #endif | |
| 71 total_size_ = new_size; | 69 total_size_ = new_size; |
| 72 if (old_rep && old_rep->allocated_size > 0) { | 70 if (old_rep && old_rep->allocated_size > 0) { |
| 73 memcpy(rep_->elements, old_rep->elements, | 71 memcpy(rep_->elements, old_rep->elements, |
| 74 old_rep->allocated_size * sizeof(rep_->elements[0])); | 72 old_rep->allocated_size * sizeof(rep_->elements[0])); |
| 75 rep_->allocated_size = old_rep->allocated_size; | 73 rep_->allocated_size = old_rep->allocated_size; |
| 76 } else { | 74 } else { |
| 77 rep_->allocated_size = 0; | 75 rep_->allocated_size = 0; |
| 78 } | 76 } |
| 79 if (arena == NULL) { | 77 if (arena == NULL) { |
| 80 #if defined(__GXX_DELETE_WITH_SIZE__) || defined(__cpp_sized_deallocation) | 78 delete [] reinterpret_cast<char*>(old_rep); |
| 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 | |
| 87 } | 79 } |
| 88 return &rep_->elements[current_size_]; | 80 return &rep_->elements[current_size_]; |
| 89 } | 81 } |
| 90 | 82 |
| 91 void RepeatedPtrFieldBase::Reserve(int new_size) { | 83 void RepeatedPtrFieldBase::Reserve(int new_size) { |
| 92 if (new_size > current_size_) { | 84 if (new_size > current_size_) { |
| 93 InternalExtend(new_size - current_size_); | 85 InternalExtend(new_size - current_size_); |
| 94 } | 86 } |
| 95 } | 87 } |
| 96 | 88 |
| 97 void RepeatedPtrFieldBase::CloseGap(int start, int num) { | 89 void RepeatedPtrFieldBase::CloseGap(int start, int num) { |
| 98 if (rep_ == NULL) return; | 90 if (rep_ == NULL) return; |
| 99 // Close up a gap of "num" elements starting at offset "start". | 91 // Close up a gap of "num" elements starting at offset "start". |
| 100 for (int i = start + num; i < rep_->allocated_size; ++i) | 92 for (int i = start + num; i < rep_->allocated_size; ++i) |
| 101 rep_->elements[i - num] = rep_->elements[i]; | 93 rep_->elements[i - num] = rep_->elements[i]; |
| 102 current_size_ -= num; | 94 current_size_ -= num; |
| 103 rep_->allocated_size -= num; | 95 rep_->allocated_size -= num; |
| 104 } | 96 } |
| 105 | 97 |
| 106 } // namespace internal | 98 } // namespace internal |
| 107 | 99 |
| 108 | 100 |
| 109 } // namespace protobuf | 101 } // namespace protobuf |
| 110 } // namespace google | 102 } // namespace google |
| OLD | NEW |