arenastring.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 #include <google/protobuf/arenastring.h>
32 
33 #include <google/protobuf/stubs/logging.h>
34 #include <google/protobuf/stubs/common.h>
35 #include <google/protobuf/parse_context.h>
36 #include <google/protobuf/io/coded_stream.h>
37 #include <google/protobuf/message_lite.h>
38 #include <google/protobuf/stubs/mutex.h>
39 #include <google/protobuf/stubs/strutil.h>
40 #include <google/protobuf/stubs/stl_util.h>
41 
42 // clang-format off
43 #include <google/protobuf/port_def.inc>
44 // clang-format on
45 
46 namespace google {
47 namespace protobuf {
48 namespace internal {
49 
50 const std::string& LazyString::Init() const {
51  static WrappedMutex mu{GOOGLE_PROTOBUF_LINKER_INITIALIZED};
52  mu.Lock();
53  const std::string* res = inited_.load(std::memory_order_acquire);
54  if (res == nullptr) {
55  auto init_value = init_value_;
56  res = ::new (static_cast<void*>(string_buf_))
57  std::string(init_value.ptr, init_value.size);
58  inited_.store(res, std::memory_order_release);
59  }
60  mu.Unlock();
61  return *res;
62 }
63 
64 
66  std::string* new_string = new std::string();
67  tagged_ptr_.Set(new_string);
68  return new_string;
69 }
70 
72 
73 void ArenaStringPtr::Set(const std::string* default_value,
75  if (IsDefault(default_value)) {
76  tagged_ptr_.Set(Arena::Create<std::string>(arena, value));
77  } else {
78  UnsafeMutablePointer()->assign(value.data(), value.length());
79  }
80 }
81 
82 void ArenaStringPtr::Set(const std::string* default_value, std::string&& value,
84  if (IsDefault(default_value)) {
85  if (arena == nullptr) {
87  } else {
88  tagged_ptr_.Set(Arena::Create<std::string>(arena, std::move(value)));
89  }
90  } else if (IsDonatedString()) {
91  std::string* current = tagged_ptr_.Get();
92  auto* s = new (current) std::string(std::move(value));
93  arena->OwnDestructor(s);
94  tagged_ptr_.Set(s);
95  } else /* !IsDonatedString() */ {
97  }
98 }
99 
103 }
104 
108 }
109 
112  Set(nullptr, value, arena);
113 }
114 
117  Set(nullptr, std::move(value), arena);
118 }
119 
122  return UnsafeMutablePointer();
123  } else {
124  return MutableSlow(arena);
125  }
126 }
127 
130  if (!IsDonatedString() && !IsDefault(nullptr)) {
131  return UnsafeMutablePointer();
132  } else {
133  return MutableSlow(arena, default_value);
134  }
135 }
136 
139  if (!IsDonatedString() && !IsDefault(default_value)) {
140  return UnsafeMutablePointer();
141  } else {
142  GOOGLE_DCHECK(IsDefault(default_value));
143  // Allocate empty. The contents are not relevant.
144  std::string* new_string = Arena::Create<std::string>(arena);
145  tagged_ptr_.Set(new_string);
146  return new_string;
147  }
148 }
149 
150 template <typename... Lazy>
152  const Lazy&... lazy_default) {
153  const std::string* const default_value =
154  sizeof...(Lazy) == 0 ? &GetEmptyStringAlreadyInited() : nullptr;
155  GOOGLE_DCHECK(IsDefault(default_value));
156  std::string* new_string =
157  Arena::Create<std::string>(arena, lazy_default.get()...);
158  tagged_ptr_.Set(new_string);
159  return new_string;
160 }
161 
164  if (IsDefault(default_value)) {
165  return nullptr;
166  } else {
167  return ReleaseNonDefault(default_value, arena);
168  }
169 }
170 
173  GOOGLE_DCHECK(!IsDefault(default_value));
174 
175  if (!IsDonatedString()) {
176  std::string* released;
177  if (arena != nullptr) {
178  released = new std::string;
179  released->swap(*UnsafeMutablePointer());
180  } else {
181  released = UnsafeMutablePointer();
182  }
183  tagged_ptr_.Set(const_cast<std::string*>(default_value));
184  return released;
185  } else /* IsDonatedString() */ {
186  GOOGLE_DCHECK(arena != nullptr);
187  std::string* released = new std::string(Get());
188  tagged_ptr_.Set(const_cast<std::string*>(default_value));
189  return released;
190  }
191 }
192 
193 void ArenaStringPtr::SetAllocated(const std::string* default_value,
195  // Release what we have first.
196  if (arena == nullptr && !IsDefault(default_value)) {
197  delete UnsafeMutablePointer();
198  }
199  if (value == nullptr) {
200  tagged_ptr_.Set(const_cast<std::string*>(default_value));
201  } else {
202 #ifdef NDEBUG
204  if (arena != nullptr) {
205  arena->Own(value);
206  }
207 #else
208  // On debug builds, copy the string so the address differs. delete will
209  // fail if value was a stack-allocated temporary/etc., which would have
210  // failed when arena ran its cleanup list.
211  std::string* new_value = Arena::Create<std::string>(arena, *value);
212  delete value;
213  tagged_ptr_.Set(new_value);
214 #endif
215  }
216 }
217 
218 void ArenaStringPtr::Destroy(const std::string* default_value,
220  if (arena == nullptr) {
222  if (!IsDefault(default_value)) {
223  delete UnsafeMutablePointer();
224  }
225  }
226 }
227 
230 }
231 
233  Destroy(nullptr, arena);
234 }
235 
238  // Already set to default -- do nothing.
239  } else {
240  // Unconditionally mask away the tag.
241  //
242  // UpdateDonatedString uses assign when capacity is larger than the new
243  // value, which is trivially true in the donated string case.
244  // const_cast<std::string*>(PtrValue<std::string>())->clear();
245  tagged_ptr_.Get()->clear();
246  }
247 }
248 
249 void ArenaStringPtr::ClearToDefault(const LazyString& default_value,
251  (void)arena;
252  if (IsDefault(nullptr)) {
253  // Already set to default -- do nothing.
254  } else if (!IsDonatedString()) {
255  UnsafeMutablePointer()->assign(default_value.get());
256  }
257 }
258 
261  res.Set(str);
262  s->UnsafeSetTaggedPointer(res);
263 }
264 
265 const char* EpsCopyInputStream::ReadArenaString(const char* ptr,
266  ArenaStringPtr* s,
267  Arena* arena) {
268  GOOGLE_DCHECK(arena != nullptr);
269 
270  int size = ReadSize(&ptr);
271  if (!ptr) return nullptr;
272 
273  auto* str = Arena::Create<std::string>(arena);
274  ptr = ReadString(ptr, size, str);
276 
278 
279  return ptr;
280 }
281 
282 } // namespace internal
283 } // namespace protobuf
284 } // namespace google
285 
286 #include <google/protobuf/port_undef.inc>
xds_interop_client.str
str
Definition: xds_interop_client.py:487
ptr
char * ptr
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:45
google::protobuf.internal::LazyString::Init
const std::string & Init() const
Definition: arenastring.cc:50
Arena
struct Arena Arena
Definition: third_party/bloaty/third_party/protobuf/src/google/protobuf/arena.h:189
google::protobuf::value
const Descriptor::ReservedRange value
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:1954
google::protobuf.internal::ArenaStringPtr::ReleaseNonDefault
inline ::std::string * ReleaseNonDefault(const ::std::string *default_value, Arena *arena)
Definition: bloaty/third_party/protobuf/src/google/protobuf/arenastring.h:113
google::protobuf.internal::ReadSize
uint32 ReadSize(const char **pp)
Definition: bloaty/third_party/protobuf/src/google/protobuf/parse_context.h:561
google::protobuf.internal::TaggedPtr::Set
void Set(T *p)
Definition: bloaty/third_party/protobuf/src/google/protobuf/arenastring.h:65
google::protobuf.internal::ArenaStringPtr::IsDonatedString
bool IsDonatedString() const
Definition: protobuf/src/google/protobuf/arenastring.h:325
google::protobuf.internal::LazyString::init_value_
InitValue init_value_
Definition: protobuf/src/google/protobuf/arenastring.h:74
google::protobuf.internal::ArenaStringPtr::MutableSlow
std::string * MutableSlow(::google::protobuf::Arena *arena, const Lazy &... lazy_default)
Definition: arenastring.cc:151
GOOGLE_DCHECK
#define GOOGLE_DCHECK
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/logging.h:194
google::protobuf.internal::ArenaStringPtr::SetAllocated
void SetAllocated(const ::std::string *default_value, ::std::string *value, Arena *arena)
Definition: bloaty/third_party/protobuf/src/google/protobuf/arenastring.h:146
Arena
Definition: arena.c:39
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
google::protobuf
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:12
google::protobuf.internal::ArenaStringPtr::Get
const ::std::string & Get() const
Definition: bloaty/third_party/protobuf/src/google/protobuf/arenastring.h:90
google::protobuf.internal::ArenaStringPtr::UnsafeMutablePointer
::std::string * UnsafeMutablePointer()
Definition: bloaty/third_party/protobuf/src/google/protobuf/arenastring.h:363
env.new
def new
Definition: env.py:51
google::protobuf.internal::ArenaStringPtr::tagged_ptr_
TaggedPtr< std::string > tagged_ptr_
Definition: protobuf/src/google/protobuf/arenastring.h:323
google::protobuf.internal::ArenaStringPtr::Destroy
void Destroy(const ::std::string *default_value, Arena *arena)
Definition: bloaty/third_party/protobuf/src/google/protobuf/arenastring.h:209
arena
grpc_core::ScopedArenaPtr arena
Definition: binder_transport_test.cc:237
google::protobuf.internal::ArenaStringPtr::Release
inline ::std::string * Release(const ::std::string *default_value, Arena *arena)
Definition: bloaty/third_party/protobuf/src/google/protobuf/arenastring.h:104
google::protobuf.internal::ArenaStringPtr::Mutable
inline ::std::string * Mutable(const ::std::string *default_value, Arena *arena)
Definition: bloaty/third_party/protobuf/src/google/protobuf/arenastring.h:92
google::protobuf::ConstStringParam
const std::string & ConstStringParam
Definition: third_party/protobuf/src/google/protobuf/stubs/port.h:129
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
mu
Mutex mu
Definition: server_config_selector_filter.cc:74
google::protobuf.internal::LazyString
Definition: protobuf/src/google/protobuf/arenastring.h:61
google::protobuf.internal::GetEmptyStringAlreadyInited
const PROTOBUF_EXPORT std::string & GetEmptyStringAlreadyInited()
Definition: bloaty/third_party/protobuf/src/google/protobuf/message_lite.h:153
GOOGLE_PROTOBUF_PARSER_ASSERT
#define GOOGLE_PROTOBUF_PARSER_ASSERT(predicate)
Definition: bloaty/third_party/protobuf/src/google/protobuf/parse_context.h:660
google::protobuf.internal::ArenaStringPtr::MutableNoCopy
std::string * MutableNoCopy(const std::string *default_value, ::google::protobuf::Arena *arena)
Definition: arenastring.cc:137
google::protobuf.internal::LazyString::string_buf_
char string_buf_[sizeof(std::string)]
Definition: protobuf/src/google/protobuf/arenastring.h:75
google::protobuf.internal::TaggedPtr::Get
T * Get() const
Definition: bloaty/third_party/protobuf/src/google/protobuf/arenastring.h:66
google::protobuf.internal::EpsCopyInputStream::ReadString
const PROTOBUF_MUST_USE_RESULT char * ReadString(const char *ptr, int size, std::string *s)
Definition: bloaty/third_party/protobuf/src/google/protobuf/parse_context.h:152
google::protobuf.internal::ArenaStringPtr::IsDefault
bool IsDefault(const ::std::string *default_value) const
Definition: bloaty/third_party/protobuf/src/google/protobuf/arenastring.h:352
google::protobuf.internal::ArenaStringPtr::DestroyNoArenaSlowPath
void DestroyNoArenaSlowPath()
Definition: arenastring.cc:71
google::protobuf.internal::ArenaStringPtr::ClearToEmpty
void ClearToEmpty()
Definition: arenastring.cc:236
google::protobuf.internal::TaggedPtr< std::string >
google::protobuf.internal::ArenaStringPtr::NonEmptyDefault
Definition: protobuf/src/google/protobuf/arenastring.h:188
google::protobuf.internal::SetStrWithHeapBuffer
void SetStrWithHeapBuffer(std::string *str, ArenaStringPtr *s)
Definition: arenastring.cc:259
google::protobuf.internal::ArenaStringPtr::EmptyDefault
Definition: protobuf/src/google/protobuf/arenastring.h:187
google::protobuf.internal::ArenaStringPtr::ClearToDefault
void ClearToDefault(const ::std::string *default_value, Arena *)
Definition: bloaty/third_party/protobuf/src/google/protobuf/arenastring.h:236
google::protobuf.internal::LazyString::inited_
std::atomic< const std::string * > inited_
Definition: protobuf/src/google/protobuf/arenastring.h:77
GOOGLE_PROTOBUF_LINKER_INITIALIZED
#define GOOGLE_PROTOBUF_LINKER_INITIALIZED
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/mutex.h:70
google::protobuf.internal::EpsCopyInputStream::ReadArenaString
const PROTOBUF_NODISCARD char * ReadArenaString(const char *ptr, ArenaStringPtr *s, Arena *arena)
Definition: arenastring.cc:265
internal
Definition: benchmark/test/output_test_helper.cc:20
google::protobuf.internal::LazyString::get
const std::string & get() const
Definition: protobuf/src/google/protobuf/arenastring.h:79
google::protobuf.internal::ArenaStringPtr::SetAndReturnNewString
std::string * SetAndReturnNewString()
Definition: arenastring.cc:65
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
google
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:11
google::protobuf.internal::ArenaStringPtr
Definition: bloaty/third_party/protobuf/src/google/protobuf/arenastring.h:74
google::protobuf.internal::ArenaStringPtr::Set
void Set(const ::std::string *default_value, const ::std::string &value, Arena *arena)
Definition: bloaty/third_party/protobuf/src/google/protobuf/arenastring.h:75


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:42