Coverage Report

Created: 2026-06-09 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/sentencepiece/third_party/protobuf-lite/arenastring.cc
Line
Count
Source
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
10
const std::string& LazyString::Init() const {
51
10
  static WrappedMutex mu{GOOGLE_PROTOBUF_LINKER_INITIALIZED};
52
10
  mu.Lock();
53
10
  const std::string* res = inited_.load(std::memory_order_acquire);
54
10
  if (res == nullptr) {
55
10
    auto init_value = init_value_;
56
10
    res = ::new (static_cast<void*>(string_buf_))
57
10
        std::string(init_value.ptr, init_value.size);
58
10
    inited_.store(res, std::memory_order_release);
59
10
  }
60
10
  mu.Unlock();
61
10
  return *res;
62
10
}
63
64
65
void ArenaStringPtr::Set(const std::string* default_value,
66
468k
                         ConstStringParam value, ::google::protobuf::Arena* arena) {
67
468k
  if (IsDefault(default_value)) {
68
468k
    tagged_ptr_.Set(Arena::Create<std::string>(arena, value));
69
468k
  } else {
70
0
    UnsafeMutablePointer()->assign(value.data(), value.length());
71
0
  }
72
468k
}
73
74
void ArenaStringPtr::Set(const std::string* default_value, std::string&& value,
75
28.2M
                         ::google::protobuf::Arena* arena) {
76
28.2M
  if (IsDefault(default_value)) {
77
22.0M
    if (arena == nullptr) {
78
22.0M
      tagged_ptr_.Set(new std::string(std::move(value)));
79
22.0M
    } else {
80
0
      tagged_ptr_.Set(Arena::Create<std::string>(arena, std::move(value)));
81
0
    }
82
22.0M
  } else if (IsDonatedString()) {
83
0
    std::string* current = tagged_ptr_.Get();
84
0
    auto* s = new (current) std::string(std::move(value));
85
0
    arena->OwnDestructor(s);
86
0
    tagged_ptr_.Set(s);
87
6.16M
  } else /* !IsDonatedString() */ {
88
6.16M
    *UnsafeMutablePointer() = std::move(value);
89
6.16M
  }
90
28.2M
}
91
92
void ArenaStringPtr::Set(EmptyDefault, ConstStringParam value,
93
468k
                         ::google::protobuf::Arena* arena) {
94
468k
  Set(&GetEmptyStringAlreadyInited(), value, arena);
95
468k
}
96
97
void ArenaStringPtr::Set(EmptyDefault, std::string&& value,
98
28.2M
                         ::google::protobuf::Arena* arena) {
99
28.2M
  Set(&GetEmptyStringAlreadyInited(), std::move(value), arena);
100
28.2M
}
101
102
void ArenaStringPtr::Set(NonEmptyDefault, ConstStringParam value,
103
0
                         ::google::protobuf::Arena* arena) {
104
0
  Set(nullptr, value, arena);
105
0
}
106
107
void ArenaStringPtr::Set(NonEmptyDefault, std::string&& value,
108
0
                         ::google::protobuf::Arena* arena) {
109
0
  Set(nullptr, std::move(value), arena);
110
0
}
111
112
92.7M
std::string* ArenaStringPtr::Mutable(EmptyDefault, ::google::protobuf::Arena* arena) {
113
92.7M
  if (!IsDonatedString() && !IsDefault(&GetEmptyStringAlreadyInited())) {
114
89.5M
    return UnsafeMutablePointer();
115
89.5M
  } else {
116
3.20M
    return MutableSlow(arena);
117
3.20M
  }
118
92.7M
}
119
120
std::string* ArenaStringPtr::Mutable(const LazyString& default_value,
121
167
                                     ::google::protobuf::Arena* arena) {
122
167
  if (!IsDonatedString() && !IsDefault(nullptr)) {
123
54
    return UnsafeMutablePointer();
124
113
  } else {
125
113
    return MutableSlow(arena, default_value);
126
113
  }
127
167
}
128
129
std::string* ArenaStringPtr::MutableNoCopy(const std::string* default_value,
130
0
                                           ::google::protobuf::Arena* arena) {
131
0
  if (!IsDonatedString() && !IsDefault(default_value)) {
132
0
    return UnsafeMutablePointer();
133
0
  } else {
134
0
    GOOGLE_DCHECK(IsDefault(default_value));
135
    // Allocate empty. The contents are not relevant.
136
0
    std::string* new_string = Arena::Create<std::string>(arena);
137
0
    tagged_ptr_.Set(new_string);
138
0
    return new_string;
139
0
  }
140
0
}
141
142
template <typename... Lazy>
143
std::string* ArenaStringPtr::MutableSlow(::google::protobuf::Arena* arena,
144
3.20M
                                         const Lazy&... lazy_default) {
145
3.20M
  const std::string* const default_value =
146
3.20M
      sizeof...(Lazy) == 0 ? &GetEmptyStringAlreadyInited() : nullptr;
147
3.20M
  GOOGLE_DCHECK(IsDefault(default_value));
148
3.20M
  std::string* new_string =
149
3.20M
      Arena::Create<std::string>(arena, lazy_default.get()...);
150
3.20M
  tagged_ptr_.Set(new_string);
151
3.20M
  return new_string;
152
3.20M
}
std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >* google::protobuf::internal::ArenaStringPtr::MutableSlow<>(google::protobuf::Arena*)
Line
Count
Source
144
3.20M
                                         const Lazy&... lazy_default) {
145
3.20M
  const std::string* const default_value =
146
3.20M
      sizeof...(Lazy) == 0 ? &GetEmptyStringAlreadyInited() : nullptr;
147
3.20M
  GOOGLE_DCHECK(IsDefault(default_value));
148
3.20M
  std::string* new_string =
149
3.20M
      Arena::Create<std::string>(arena, lazy_default.get()...);
150
3.20M
  tagged_ptr_.Set(new_string);
151
3.20M
  return new_string;
152
3.20M
}
std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >* google::protobuf::internal::ArenaStringPtr::MutableSlow<google::protobuf::internal::LazyString>(google::protobuf::Arena*, google::protobuf::internal::LazyString const&)
Line
Count
Source
144
113
                                         const Lazy&... lazy_default) {
145
113
  const std::string* const default_value =
146
113
      sizeof...(Lazy) == 0 ? &GetEmptyStringAlreadyInited() : nullptr;
147
113
  GOOGLE_DCHECK(IsDefault(default_value));
148
113
  std::string* new_string =
149
113
      Arena::Create<std::string>(arena, lazy_default.get()...);
150
113
  tagged_ptr_.Set(new_string);
151
113
  return new_string;
152
113
}
153
154
std::string* ArenaStringPtr::Release(const std::string* default_value,
155
0
                                     ::google::protobuf::Arena* arena) {
156
0
  if (IsDefault(default_value)) {
157
0
    return nullptr;
158
0
  } else {
159
0
    return ReleaseNonDefault(default_value, arena);
160
0
  }
161
0
}
162
163
std::string* ArenaStringPtr::ReleaseNonDefault(const std::string* default_value,
164
0
                                               ::google::protobuf::Arena* arena) {
165
0
  GOOGLE_DCHECK(!IsDefault(default_value));
166
167
0
  if (!IsDonatedString()) {
168
0
    std::string* released;
169
0
    if (arena != nullptr) {
170
0
      released = new std::string;
171
0
      released->swap(*UnsafeMutablePointer());
172
0
    } else {
173
0
      released = UnsafeMutablePointer();
174
0
    }
175
0
    tagged_ptr_.Set(const_cast<std::string*>(default_value));
176
0
    return released;
177
0
  } else /* IsDonatedString() */ {
178
0
    GOOGLE_DCHECK(arena != nullptr);
179
0
    std::string* released = new std::string(Get());
180
0
    tagged_ptr_.Set(const_cast<std::string*>(default_value));
181
0
    return released;
182
0
  }
183
0
}
184
185
void ArenaStringPtr::SetAllocated(const std::string* default_value,
186
0
                                  std::string* value, ::google::protobuf::Arena* arena) {
187
  // Release what we have first.
188
0
  if (arena == nullptr && !IsDefault(default_value)) {
189
0
    delete UnsafeMutablePointer();
190
0
  }
191
0
  if (value == nullptr) {
192
0
    tagged_ptr_.Set(const_cast<std::string*>(default_value));
193
0
  } else {
194
0
#ifdef NDEBUG
195
0
    tagged_ptr_.Set(value);
196
0
    if (arena != nullptr) {
197
0
      arena->Own(value);
198
0
    }
199
#else
200
    // On debug builds, copy the string so the address differs.  delete will
201
    // fail if value was a stack-allocated temporary/etc., which would have
202
    // failed when arena ran its cleanup list.
203
    std::string* new_value = Arena::Create<std::string>(arena, *value);
204
    delete value;
205
    tagged_ptr_.Set(new_value);
206
#endif
207
0
  }
208
0
}
209
210
void ArenaStringPtr::Destroy(const std::string* default_value,
211
0
                             ::google::protobuf::Arena* arena) {
212
0
  if (arena == nullptr) {
213
0
    GOOGLE_DCHECK(!IsDonatedString());
214
0
    if (!IsDefault(default_value)) {
215
0
      delete UnsafeMutablePointer();
216
0
    }
217
0
  }
218
0
}
219
220
0
void ArenaStringPtr::Destroy(EmptyDefault, ::google::protobuf::Arena* arena) {
221
0
  Destroy(&GetEmptyStringAlreadyInited(), arena);
222
0
}
223
224
0
void ArenaStringPtr::Destroy(NonEmptyDefault, ::google::protobuf::Arena* arena) {
225
0
  Destroy(nullptr, arena);
226
0
}
227
228
0
void ArenaStringPtr::ClearToEmpty() {
229
0
  if (IsDefault(&GetEmptyStringAlreadyInited())) {
230
    // Already set to default -- do nothing.
231
0
  } else {
232
    // Unconditionally mask away the tag.
233
    //
234
    // UpdateDonatedString uses assign when capacity is larger than the new
235
    // value, which is trivially true in the donated string case.
236
    // const_cast<std::string*>(PtrValue<std::string>())->clear();
237
0
    tagged_ptr_.Get()->clear();
238
0
  }
239
0
}
240
241
void ArenaStringPtr::ClearToDefault(const LazyString& default_value,
242
0
                                    ::google::protobuf::Arena* arena) {
243
0
  (void)arena;
244
0
  if (IsDefault(nullptr)) {
245
    // Already set to default -- do nothing.
246
0
  } else if (!IsDonatedString()) {
247
0
    UnsafeMutablePointer()->assign(default_value.get());
248
0
  }
249
0
}
250
251
252
}  // namespace internal
253
}  // namespace protobuf
254
}  // namespace google