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 
31 // Author: kenton@google.com (Kenton Varda)
32 //  Based on original Protocol Buffers design by
33 //  Sanjay Ghemawat, Jeff Dean, and others.
34 
35 #include <google/protobuf/repeated_field.h>
36 
37 #include <algorithm>
38 
39 #include <google/protobuf/stubs/logging.h>
40 #include <google/protobuf/stubs/common.h>
41 #include <google/protobuf/implicit_weak_message.h>
42 
43 #include <google/protobuf/port_def.inc>
44 
45 namespace google {
46 namespace protobuf {
47 
48 namespace internal {
49 
InternalExtend(int extend_amount)50 void** RepeatedPtrFieldBase::InternalExtend(int extend_amount) {
51   int new_size = current_size_ + extend_amount;
52   if (total_size_ >= new_size) {
53     // N.B.: rep_ is non-NULL because extend_amount is always > 0, hence
54     // total_size must be non-zero since it is lower-bounded by new_size.
55     return &rep_->elements[current_size_];
56   }
57   Rep* old_rep = rep_;
58   Arena* arena = GetArena();
59   new_size = std::max(internal::kRepeatedFieldLowerClampLimit,
60                       std::max(total_size_ * 2, new_size));
61   GOOGLE_CHECK_LE(
62       static_cast<int64>(new_size),
63       static_cast<int64>((std::numeric_limits<size_t>::max() - kRepHeaderSize) /
64                          sizeof(old_rep->elements[0])))
65       << "Requested size is too large to fit into size_t.";
66   size_t bytes = kRepHeaderSize + sizeof(old_rep->elements[0]) * new_size;
67   if (arena == NULL) {
68     rep_ = reinterpret_cast<Rep*>(::operator new(bytes));
69   } else {
70     rep_ = reinterpret_cast<Rep*>(Arena::CreateArray<char>(arena, bytes));
71   }
72 #if defined(__GXX_DELETE_WITH_SIZE__) || defined(__cpp_sized_deallocation)
73   const int old_total_size = total_size_;
74 #endif
75   total_size_ = new_size;
76   if (old_rep && old_rep->allocated_size > 0) {
77     memcpy(rep_->elements, old_rep->elements,
78            old_rep->allocated_size * sizeof(rep_->elements[0]));
79     rep_->allocated_size = old_rep->allocated_size;
80   } else {
81     rep_->allocated_size = 0;
82   }
83   if (arena == NULL) {
84 #if defined(__GXX_DELETE_WITH_SIZE__) || defined(__cpp_sized_deallocation)
85     const size_t old_size =
86         old_total_size * sizeof(rep_->elements[0]) + kRepHeaderSize;
87     ::operator delete(static_cast<void*>(old_rep), old_size);
88 #else
89     ::operator delete(static_cast<void*>(old_rep));
90 #endif
91   }
92   return &rep_->elements[current_size_];
93 }
94 
Reserve(int new_size)95 void RepeatedPtrFieldBase::Reserve(int new_size) {
96   if (new_size > current_size_) {
97     InternalExtend(new_size - current_size_);
98   }
99 }
100 
AddOutOfLineHelper(void * obj)101 void* RepeatedPtrFieldBase::AddOutOfLineHelper(void* obj) {
102   if (!rep_ || rep_->allocated_size == total_size_) {
103     InternalExtend(1);  // Equivalent to "Reserve(total_size_ + 1)"
104   }
105   ++rep_->allocated_size;
106   rep_->elements[current_size_++] = obj;
107   return obj;
108 }
109 
CloseGap(int start,int num)110 void RepeatedPtrFieldBase::CloseGap(int start, int num) {
111   if (rep_ == NULL) return;
112   // Close up a gap of "num" elements starting at offset "start".
113   for (int i = start + num; i < rep_->allocated_size; ++i)
114     rep_->elements[i - num] = rep_->elements[i];
115   current_size_ -= num;
116   rep_->allocated_size -= num;
117 }
118 
AddWeak(const MessageLite * prototype)119 MessageLite* RepeatedPtrFieldBase::AddWeak(const MessageLite* prototype) {
120   if (rep_ != NULL && current_size_ < rep_->allocated_size) {
121     return reinterpret_cast<MessageLite*>(rep_->elements[current_size_++]);
122   }
123   if (!rep_ || rep_->allocated_size == total_size_) {
124     Reserve(total_size_ + 1);
125   }
126   ++rep_->allocated_size;
127   MessageLite* result = prototype
128                             ? prototype->New(arena_)
129                             : Arena::CreateMessage<ImplicitWeakMessage>(arena_);
130   rep_->elements[current_size_++] = result;
131   return result;
132 }
133 
134 }  // namespace internal
135 
136 
137 template class PROTOBUF_EXPORT_TEMPLATE_DEFINE RepeatedField<bool>;
138 template class PROTOBUF_EXPORT_TEMPLATE_DEFINE RepeatedField<int32>;
139 template class PROTOBUF_EXPORT_TEMPLATE_DEFINE RepeatedField<uint32>;
140 template class PROTOBUF_EXPORT_TEMPLATE_DEFINE RepeatedField<int64>;
141 template class PROTOBUF_EXPORT_TEMPLATE_DEFINE RepeatedField<uint64>;
142 template class PROTOBUF_EXPORT_TEMPLATE_DEFINE RepeatedField<float>;
143 template class PROTOBUF_EXPORT_TEMPLATE_DEFINE RepeatedField<double>;
144 template class PROTOBUF_EXPORT_TEMPLATE_DEFINE RepeatedPtrField<std::string>;
145 
146 }  // namespace protobuf
147 }  // namespace google
148 
149 #include <google/protobuf/port_undef.inc>
150