repeated_field.cc
Go to the documentation of this file.
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 <algorithm>
36 
40 
41 #include <google/protobuf/port_def.inc>
42 
43 namespace google {
44 namespace protobuf {
45 
46 namespace internal {
47 
48 void** RepeatedPtrFieldBase::InternalExtend(int extend_amount) {
49  int new_size = current_size_ + extend_amount;
50  if (total_size_ >= new_size) {
51  // N.B.: rep_ is non-NULL because extend_amount is always > 0, hence
52  // total_size must be non-zero since it is lower-bounded by new_size.
53  return &rep_->elements[current_size_];
54  }
55  Rep* old_rep = rep_;
56  Arena* arena = GetArenaNoVirtual();
57  new_size = std::max(kMinRepeatedFieldAllocationSize,
58  std::max(total_size_ * 2, new_size));
59  GOOGLE_CHECK_LE(new_size, (std::numeric_limits<size_t>::max() - kRepHeaderSize) /
60  sizeof(old_rep->elements[0]))
61  << "Requested size is too large to fit into size_t.";
62  size_t bytes = kRepHeaderSize + sizeof(old_rep->elements[0]) * new_size;
63  if (arena == NULL) {
64  rep_ = reinterpret_cast<Rep*>(::operator new(bytes));
65  } else {
66  rep_ = reinterpret_cast<Rep*>(Arena::CreateArray<char>(arena, bytes));
67  }
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;
72  if (old_rep && old_rep->allocated_size > 0) {
73  memcpy(rep_->elements, old_rep->elements,
74  old_rep->allocated_size * sizeof(rep_->elements[0]));
76  } else {
77  rep_->allocated_size = 0;
78  }
79  if (arena == NULL) {
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
87  }
88  return &rep_->elements[current_size_];
89 }
90 
91 void RepeatedPtrFieldBase::Reserve(int new_size) {
92  if (new_size > current_size_) {
93  InternalExtend(new_size - current_size_);
94  }
95 }
96 
98  if (rep_ == NULL) return;
99  // Close up a gap of "num" elements starting at offset "start".
100  for (int i = start + num; i < rep_->allocated_size; ++i)
101  rep_->elements[i - num] = rep_->elements[i];
102  current_size_ -= num;
103  rep_->allocated_size -= num;
104 }
105 
107  if (rep_ != NULL && current_size_ < rep_->allocated_size) {
108  return reinterpret_cast<MessageLite*>(rep_->elements[current_size_++]);
109  }
110  if (!rep_ || rep_->allocated_size == total_size_) {
111  Reserve(total_size_ + 1);
112  }
113  ++rep_->allocated_size;
114  MessageLite* result = prototype
115  ? prototype->New(arena_)
116  : Arena::CreateMessage<ImplicitWeakMessage>(arena_);
117  rep_->elements[current_size_++] = result;
118  return result;
119 }
120 
121 } // namespace internal
122 
123 
124 template class PROTOBUF_EXPORT RepeatedField<bool>;
125 template class PROTOBUF_EXPORT RepeatedField<int32>;
126 template class PROTOBUF_EXPORT RepeatedField<uint32>;
127 template class PROTOBUF_EXPORT RepeatedField<int64>;
128 template class PROTOBUF_EXPORT RepeatedField<uint64>;
129 template class PROTOBUF_EXPORT RepeatedField<float>;
130 template class PROTOBUF_EXPORT RepeatedField<double>;
131 template class PROTOBUF_EXPORT RepeatedPtrField<std::string>;
132 
133 } // namespace protobuf
134 } // namespace google
google::protobuf.internal::RepeatedPtrFieldBase::current_size_
int current_size_
Definition: repeated_field.h:608
google::protobuf::RepeatedPtrField< std::string >
NULL
NULL
Definition: test_security_zap.cpp:405
google::protobuf.internal::RepeatedPtrFieldBase::total_size_
int total_size_
Definition: repeated_field.h:609
google::protobuf.internal::RepeatedPtrFieldBase::GetArenaNoVirtual
Arena * GetArenaNoVirtual() const
Definition: repeated_field.h:592
google::protobuf.internal::RepeatedPtrFieldBase::rep_
Rep * rep_
Definition: repeated_field.h:617
google::protobuf::RepeatedField< bool >
google::protobuf::MessageLite
Definition: message_lite.h:183
bytes
uint8 bytes[10]
Definition: coded_stream_unittest.cc:153
google::protobuf.internal::RepeatedPtrFieldBase::AddWeak
MessageLite * AddWeak(const MessageLite *prototype)
Definition: repeated_field.cc:106
start
GLuint start
Definition: glcorearb.h:2858
google::protobuf.internal::RepeatedPtrFieldBase::Rep::allocated_size
int allocated_size
Definition: repeated_field.h:611
google::protobuf.internal::RepeatedPtrFieldBase::Rep::elements
void * elements[1]
Definition: repeated_field.h:612
repeated_field.h
google::protobuf.internal::RepeatedPtrFieldBase::InternalExtend
void ** InternalExtend(int extend_amount)
Definition: repeated_field.cc:48
google::protobuf.internal::RepeatedPtrFieldBase::kRepHeaderSize
static const size_t kRepHeaderSize
Definition: repeated_field.h:614
GOOGLE_CHECK_LE
#define GOOGLE_CHECK_LE(A, B)
Definition: logging.h:159
i
int i
Definition: gmock-matchers_test.cc:764
google::protobuf.internal::RepeatedPtrFieldBase::Rep
Definition: repeated_field.h:610
common.h
google::protobuf.internal::RepeatedPtrFieldBase::arena_
Arena * arena_
Definition: repeated_field.h:607
google::protobuf.internal::kMinRepeatedFieldAllocationSize
static const int kMinRepeatedFieldAllocationSize
Definition: repeated_field.h:92
logging.h
google::protobuf.internal::RepeatedPtrFieldBase::Reserve
void Reserve(int new_size)
Definition: repeated_field.cc:91
internal
Definition: any.pb.h:40
google::protobuf.internal::RepeatedPtrFieldBase::CloseGap
void CloseGap(int start, int num)
Definition: repeated_field.cc:97
google
Definition: data_proto2_to_proto3_util.h:11
google::protobuf::MessageLite::New
virtual MessageLite * New() const =0


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:58