xref: /aosp_15_r20/external/cronet/third_party/protobuf/src/google/protobuf/repeated_ptr_field.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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: [email protected] (Kenton Varda)
32 //  Based on original Protocol Buffers design by
33 //  Sanjay Ghemawat, Jeff Dean, and others.
34 
35 #include <algorithm>
36 
37 #include <google/protobuf/stubs/logging.h>
38 #include <google/protobuf/stubs/common.h>
39 #include <google/protobuf/implicit_weak_message.h>
40 #include <google/protobuf/repeated_field.h>
41 #include <google/protobuf/port.h>
42 
43 // Must be included last.
44 #include <google/protobuf/port_def.inc>
45 
46 namespace google {
47 namespace protobuf {
48 
49 namespace internal {
50 
InternalExtend(int extend_amount)51 void** RepeatedPtrFieldBase::InternalExtend(int extend_amount) {
52   int new_size = current_size_ + extend_amount;
53   if (total_size_ >= new_size) {
54     // N.B.: rep_ is non-nullptr because extend_amount is always > 0, hence
55     // total_size must be non-zero since it is lower-bounded by new_size.
56     return &rep_->elements[current_size_];
57   }
58   Rep* old_rep = rep_;
59   Arena* arena = GetOwningArena();
60   new_size = internal::CalculateReserveSize<void*, kRepHeaderSize>(total_size_,
61                                                                    new_size);
62   GOOGLE_CHECK_LE(static_cast<int64_t>(new_size),
63            static_cast<int64_t>(
64                (std::numeric_limits<size_t>::max() - kRepHeaderSize) /
65                sizeof(old_rep->elements[0])))
66       << "Requested size is too large to fit into size_t.";
67   size_t bytes = kRepHeaderSize + sizeof(old_rep->elements[0]) * new_size;
68   if (arena == nullptr) {
69     rep_ = reinterpret_cast<Rep*>(::operator new(bytes));
70   } else {
71     rep_ = reinterpret_cast<Rep*>(Arena::CreateArray<char>(arena, bytes));
72   }
73   const int old_total_size = total_size_;
74   total_size_ = new_size;
75   if (old_rep) {
76     if (old_rep->allocated_size > 0) {
77       memcpy(rep_->elements, old_rep->elements,
78              old_rep->allocated_size * sizeof(rep_->elements[0]));
79     }
80     rep_->allocated_size = old_rep->allocated_size;
81 
82     const size_t old_size =
83         old_total_size * sizeof(rep_->elements[0]) + kRepHeaderSize;
84     if (arena == nullptr) {
85       internal::SizedDelete(old_rep, old_size);
86     } else {
87       arena_->ReturnArrayMemory(old_rep, old_size);
88     }
89   } else {
90     rep_->allocated_size = 0;
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 
DestroyProtos()101 void RepeatedPtrFieldBase::DestroyProtos() {
102   GOOGLE_DCHECK(rep_);
103   GOOGLE_DCHECK(arena_ == nullptr);
104   int n = rep_->allocated_size;
105   void* const* elements = rep_->elements;
106   for (int i = 0; i < n; i++) {
107     delete static_cast<MessageLite*>(elements[i]);
108   }
109   const size_t size = total_size_ * sizeof(elements[0]) + kRepHeaderSize;
110   internal::SizedDelete(rep_, size);
111   rep_ = nullptr;
112 }
113 
AddOutOfLineHelper(void * obj)114 void* RepeatedPtrFieldBase::AddOutOfLineHelper(void* obj) {
115   if (!rep_ || rep_->allocated_size == total_size_) {
116     InternalExtend(1);  // Equivalent to "Reserve(total_size_ + 1)"
117   }
118   ++rep_->allocated_size;
119   rep_->elements[current_size_++] = obj;
120   return obj;
121 }
122 
CloseGap(int start,int num)123 void RepeatedPtrFieldBase::CloseGap(int start, int num) {
124   if (rep_ == nullptr) return;
125   // Close up a gap of "num" elements starting at offset "start".
126   for (int i = start + num; i < rep_->allocated_size; ++i)
127     rep_->elements[i - num] = rep_->elements[i];
128   current_size_ -= num;
129   rep_->allocated_size -= num;
130 }
131 
AddWeak(const MessageLite * prototype)132 MessageLite* RepeatedPtrFieldBase::AddWeak(const MessageLite* prototype) {
133   if (rep_ != nullptr && current_size_ < rep_->allocated_size) {
134     return reinterpret_cast<MessageLite*>(rep_->elements[current_size_++]);
135   }
136   if (!rep_ || rep_->allocated_size == total_size_) {
137     Reserve(total_size_ + 1);
138   }
139   ++rep_->allocated_size;
140   MessageLite* result = prototype
141                             ? prototype->New(arena_)
142                             : Arena::CreateMessage<ImplicitWeakMessage>(arena_);
143   rep_->elements[current_size_++] = result;
144   return result;
145 }
146 
147 }  // namespace internal
148 
149 }  // namespace protobuf
150 }  // namespace google
151 
152 #include <google/protobuf/port_undef.inc>
153