repeated_ptr_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 <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 
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-nullptr 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();
61  GOOGLE_CHECK_LE(static_cast<int64_t>(new_size),
62  static_cast<int64_t>(
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 == nullptr) {
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
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 == nullptr) {
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 
96  if (new_size > current_size_) {
98  }
99 }
100 
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 #if defined(__GXX_DELETE_WITH_SIZE__) || defined(__cpp_sized_deallocation)
110  const size_t size = total_size_ * sizeof(elements[0]) + kRepHeaderSize;
111  ::operator delete(static_cast<void*>(rep_), size);
112  rep_ = nullptr;
113 #else
114  ::operator delete(static_cast<void*>(rep_));
115  rep_ = nullptr;
116 #endif
117 }
118 
120  if (!rep_ || rep_->allocated_size == total_size_) {
121  InternalExtend(1); // Equivalent to "Reserve(total_size_ + 1)"
122  }
123  ++rep_->allocated_size;
125  return obj;
126 }
127 
129  if (rep_ == nullptr) return;
130  // Close up a gap of "num" elements starting at offset "start".
131  for (int i = start + num; i < rep_->allocated_size; ++i)
132  rep_->elements[i - num] = rep_->elements[i];
133  current_size_ -= num;
134  rep_->allocated_size -= num;
135 }
136 
138  if (rep_ != nullptr && current_size_ < rep_->allocated_size) {
139  return reinterpret_cast<MessageLite*>(rep_->elements[current_size_++]);
140  }
141  if (!rep_ || rep_->allocated_size == total_size_) {
142  Reserve(total_size_ + 1);
143  }
144  ++rep_->allocated_size;
145  MessageLite* result = prototype
146  ? prototype->New(arena_)
147  : Arena::CreateMessage<ImplicitWeakMessage>(arena_);
149  return result;
150 }
151 
152 } // namespace internal
153 
154 } // namespace protobuf
155 } // namespace google
156 
157 #include <google/protobuf/port_undef.inc>
google::protobuf.internal::RepeatedPtrFieldBase::current_size_
int current_size_
Definition: bloaty/third_party/protobuf/src/google/protobuf/repeated_field.h:608
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
obj
OPENSSL_EXPORT const ASN1_OBJECT * obj
Definition: x509.h:1671
google::protobuf.internal::RepeatedPtrFieldBase::total_size_
int total_size_
Definition: bloaty/third_party/protobuf/src/google/protobuf/repeated_field.h:609
GOOGLE_DCHECK
#define GOOGLE_DCHECK
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/logging.h:194
Arena
Definition: arena.c:39
google::protobuf.internal::RepeatedPtrFieldBase::Rep::elements
void * elements[1]
Definition: bloaty/third_party/protobuf/src/google/protobuf/repeated_field.h:612
binary_size.new_size
def new_size
Definition: binary_size.py:124
google::protobuf
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:12
google::protobuf::MessageLite
Definition: bloaty/third_party/protobuf/src/google/protobuf/message_lite.h:184
arena
grpc_core::ScopedArenaPtr arena
Definition: binder_transport_test.cc:237
memcpy
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
start
static uint64_t start
Definition: benchmark-pound.c:74
google::protobuf.internal::RepeatedPtrFieldBase::AddWeak
MessageLite * AddWeak(const MessageLite *prototype)
Definition: bloaty/third_party/protobuf/src/google/protobuf/repeated_field.cc:108
int64_t
signed __int64 int64_t
Definition: stdint-msvc2008.h:89
google::protobuf.internal::RepeatedPtrFieldBase::AddOutOfLineHelper
void * AddOutOfLineHelper(void *obj)
Definition: repeated_ptr_field.cc:119
max
int max
Definition: bloaty/third_party/zlib/examples/enough.c:170
grpc::protobuf::MessageLite
GRPC_CUSTOM_MESSAGELITE MessageLite
Definition: include/grpcpp/impl/codegen/config_protobuf.h:79
google::protobuf.internal::RepeatedPtrFieldBase::rep_
Rep * rep_
Definition: bloaty/third_party/protobuf/src/google/protobuf/repeated_field.h:617
google::protobuf.internal::RepeatedPtrFieldBase::Rep::allocated_size
int allocated_size
Definition: bloaty/third_party/protobuf/src/google/protobuf/repeated_field.h:611
google::protobuf.internal::RepeatedPtrFieldBase::InternalExtend
void ** InternalExtend(int extend_amount)
Definition: bloaty/third_party/protobuf/src/google/protobuf/repeated_field.cc:50
google::protobuf.internal::RepeatedPtrFieldBase::kRepHeaderSize
static const size_t kRepHeaderSize
Definition: bloaty/third_party/protobuf/src/google/protobuf/repeated_field.h:614
n
int n
Definition: abseil-cpp/absl/container/btree_test.cc:1080
google::protobuf.internal::RepeatedPtrFieldBase::arena_
Arena * arena_
Definition: bloaty/third_party/protobuf/src/google/protobuf/repeated_field.h:607
bytes
uint8 bytes[10]
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/coded_stream_unittest.cc:153
xds_manager.num
num
Definition: xds_manager.py:56
google::protobuf.internal::RepeatedPtrFieldBase::Reserve
void Reserve(int new_size)
Definition: bloaty/third_party/protobuf/src/google/protobuf/repeated_field.cc:93
internal
Definition: benchmark/test/output_test_helper.cc:20
google::protobuf.internal::kRepeatedFieldLowerClampLimit
constexpr int kRepeatedFieldLowerClampLimit
Definition: protobuf/src/google/protobuf/repeated_field.h:86
google::protobuf.internal::RepeatedPtrFieldBase::DestroyProtos
void DestroyProtos()
Definition: repeated_ptr_field.cc:101
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
google::protobuf.internal::RepeatedPtrFieldBase::CloseGap
void CloseGap(int start, int num)
Definition: bloaty/third_party/protobuf/src/google/protobuf/repeated_field.cc:99
google
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:11
binary_size.old_size
old_size
Definition: binary_size.py:125
GOOGLE_CHECK_LE
#define GOOGLE_CHECK_LE(A, B)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/logging.h:159
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
google::protobuf.internal::RepeatedPtrFieldBase::GetArena
Arena * GetArena() const
Definition: repeated_ptr_field.h:317
google::protobuf::MessageLite::New
virtual MessageLite * New() const =0


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:04