Coverage Report

Created: 2025-12-05 06:14

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
0
const std::string& LazyString::Init() const {
51
0
  static WrappedMutex mu{GOOGLE_PROTOBUF_LINKER_INITIALIZED};
52
0
  mu.Lock();
53
0
  const std::string* res = inited_.load(std::memory_order_acquire);
54
0
  if (res == nullptr) {
55
0
    auto init_value = init_value_;
56
0
    res = ::new (static_cast<void*>(string_buf_))
57
0
        std::string(init_value.ptr, init_value.size);
58
0
    inited_.store(res, std::memory_order_release);
59
0
  }
60
0
  mu.Unlock();
61
0
  return *res;
62
0
}
63
64
65
void ArenaStringPtr::Set(const std::string* default_value,
66
0
                         ConstStringParam value, ::google::protobuf::Arena* arena) {
67
0
  if (IsDefault(default_value)) {
68
0
    tagged_ptr_.Set(Arena::Create<std::string>(arena, value));
69
0
  } else {
70
0
    UnsafeMutablePointer()->assign(value.data(), value.length());
71
0
  }
72
0
}
73
74
void ArenaStringPtr::Set(const std::string* default_value, std::string&& value,
75
0
                         ::google::protobuf::Arena* arena) {
76
0
  if (IsDefault(default_value)) {
77
0
    if (arena == nullptr) {
78
0
      tagged_ptr_.Set(new std::string(std::move(value)));
79
0
    } else {
80
0
      tagged_ptr_.Set(Arena::Create<std::string>(arena, std::move(value)));
81
0
    }
82
0
  } 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
0
  } else /* !IsDonatedString() */ {
88
0
    *UnsafeMutablePointer() = std::move(value);
89
0
  }
90
0
}
91
92
void ArenaStringPtr::Set(EmptyDefault, ConstStringParam value,
93
0
                         ::google::protobuf::Arena* arena) {
94
0
  Set(&GetEmptyStringAlreadyInited(), value, arena);
95
0
}
96
97
void ArenaStringPtr::Set(EmptyDefault, std::string&& value,
98
0
                         ::google::protobuf::Arena* arena) {
99
0
  Set(&GetEmptyStringAlreadyInited(), std::move(value), arena);
100
0
}
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
0
std::string* ArenaStringPtr::Mutable(EmptyDefault, ::google::protobuf::Arena* arena) {
113
0
  if (!IsDonatedString() && !IsDefault(&GetEmptyStringAlreadyInited())) {
114
0
    return UnsafeMutablePointer();
115
0
  } else {
116
0
    return MutableSlow(arena);
117
0
  }
118
0
}
119
120
std::string* ArenaStringPtr::Mutable(const LazyString& default_value,
121
0
                                     ::google::protobuf::Arena* arena) {
122
0
  if (!IsDonatedString() && !IsDefault(nullptr)) {
123
0
    return UnsafeMutablePointer();
124
0
  } else {
125
0
    return MutableSlow(arena, default_value);
126
0
  }
127
0
}
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
0
                                         const Lazy&... lazy_default) {
145
0
  const std::string* const default_value =
146
0
      sizeof...(Lazy) == 0 ? &GetEmptyStringAlreadyInited() : nullptr;
147
0
  GOOGLE_DCHECK(IsDefault(default_value));
148
0
  std::string* new_string =
149
0
      Arena::Create<std::string>(arena, lazy_default.get()...);
150
0
  tagged_ptr_.Set(new_string);
151
0
  return new_string;
152
0
}
Unexecuted instantiation: std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >* google::protobuf::internal::ArenaStringPtr::MutableSlow<>(google::protobuf::Arena*)
Unexecuted instantiation: 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&)
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
#ifdef NDEBUG
195
    tagged_ptr_.Set(value);
196
    if (arena != nullptr) {
197
      arena->Own(value);
198
    }
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
0
    std::string* new_value = Arena::Create<std::string>(arena, *value);
204
0
    delete value;
205
0
    tagged_ptr_.Set(new_value);
206
0
#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