repeated_field.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // Author: kenton@google.com (Kenton Varda)
  31. // Based on original Protocol Buffers design by
  32. // Sanjay Ghemawat, Jeff Dean, and others.
  33. #include <algorithm>
  34. #include <google/protobuf/repeated_field.h>
  35. #include <google/protobuf/stubs/logging.h>
  36. #include <google/protobuf/stubs/common.h>
  37. namespace google {
  38. namespace protobuf {
  39. namespace internal {
  40. void** RepeatedPtrFieldBase::InternalExtend(int extend_amount) {
  41. int new_size = current_size_ + extend_amount;
  42. if (total_size_ >= new_size) {
  43. // N.B.: rep_ is non-NULL because extend_amount is always > 0, hence
  44. // total_size must be non-zero since it is lower-bounded by new_size.
  45. return &rep_->elements[current_size_];
  46. }
  47. Rep* old_rep = rep_;
  48. Arena* arena = GetArenaNoVirtual();
  49. new_size = std::max(kMinRepeatedFieldAllocationSize,
  50. std::max(total_size_ * 2, new_size));
  51. GOOGLE_CHECK_LE(new_size,
  52. (std::numeric_limits<size_t>::max() - kRepHeaderSize) /
  53. sizeof(old_rep->elements[0]))
  54. << "Requested size is too large to fit into size_t.";
  55. size_t bytes = kRepHeaderSize + sizeof(old_rep->elements[0]) * new_size;
  56. if (arena == NULL) {
  57. rep_ = reinterpret_cast<Rep*>(::operator new(bytes));
  58. } else {
  59. rep_ = reinterpret_cast<Rep*>(
  60. ::google::protobuf::Arena::CreateArray<char>(arena, bytes));
  61. }
  62. #if defined(__GXX_DELETE_WITH_SIZE__) || defined(__cpp_sized_deallocation)
  63. const int old_total_size = total_size_;
  64. #endif
  65. total_size_ = new_size;
  66. if (old_rep && old_rep->allocated_size > 0) {
  67. memcpy(rep_->elements, old_rep->elements,
  68. old_rep->allocated_size * sizeof(rep_->elements[0]));
  69. rep_->allocated_size = old_rep->allocated_size;
  70. } else {
  71. rep_->allocated_size = 0;
  72. }
  73. if (arena == NULL) {
  74. #if defined(__GXX_DELETE_WITH_SIZE__) || defined(__cpp_sized_deallocation)
  75. const size_t old_size =
  76. old_total_size * sizeof(rep_->elements[0]) + kRepHeaderSize;
  77. ::operator delete(static_cast<void*>(old_rep), old_size);
  78. #else
  79. ::operator delete(static_cast<void*>(old_rep));
  80. #endif
  81. }
  82. return &rep_->elements[current_size_];
  83. }
  84. void RepeatedPtrFieldBase::Reserve(int new_size) {
  85. if (new_size > current_size_) {
  86. InternalExtend(new_size - current_size_);
  87. }
  88. }
  89. void RepeatedPtrFieldBase::CloseGap(int start, int num) {
  90. if (rep_ == NULL) return;
  91. // Close up a gap of "num" elements starting at offset "start".
  92. for (int i = start + num; i < rep_->allocated_size; ++i)
  93. rep_->elements[i - num] = rep_->elements[i];
  94. current_size_ -= num;
  95. rep_->allocated_size -= num;
  96. }
  97. google::protobuf::MessageLite* RepeatedPtrFieldBase::AddWeak(
  98. const google::protobuf::MessageLite* prototype) {
  99. if (rep_ != NULL && current_size_ < rep_->allocated_size) {
  100. return reinterpret_cast<google::protobuf::MessageLite*>(
  101. rep_->elements[current_size_++]);
  102. }
  103. if (!rep_ || rep_->allocated_size == total_size_) {
  104. Reserve(total_size_ + 1);
  105. }
  106. ++rep_->allocated_size;
  107. google::protobuf::MessageLite* result = prototype ? prototype->New(arena_) :
  108. Arena::CreateMessage<ImplicitWeakMessage>(arena_);
  109. rep_->elements[current_size_++] = result;
  110. return result;
  111. }
  112. } // namespace internal
  113. } // namespace protobuf
  114. } // namespace google