/work/obj-fuzz/dist/include/google/protobuf/arenastring.h
Line | Count | Source (jump to first uncovered line) |
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 | | #ifndef GOOGLE_PROTOBUF_ARENASTRING_H__ |
32 | | #define GOOGLE_PROTOBUF_ARENASTRING_H__ |
33 | | |
34 | | #include <string> |
35 | | |
36 | | #include <google/protobuf/stubs/logging.h> |
37 | | #include <google/protobuf/stubs/common.h> |
38 | | #include <google/protobuf/stubs/fastmem.h> |
39 | | #include <google/protobuf/arena.h> |
40 | | |
41 | | |
42 | | |
43 | | // This is the implementation of arena string fields written for the open-source |
44 | | // release. The ArenaStringPtr struct below is an internal implementation class |
45 | | // and *should not be used* by user code. It is used to collect string |
46 | | // operations together into one place and abstract away the underlying |
47 | | // string-field pointer representation, so that (for example) an alternate |
48 | | // implementation that knew more about ::std::string's internals could integrate more |
49 | | // closely with the arena allocator. |
50 | | |
51 | | namespace google { |
52 | | namespace protobuf { |
53 | | namespace internal { |
54 | | |
55 | | struct LIBPROTOBUF_EXPORT ArenaStringPtr { |
56 | | inline void Set(const ::std::string* default_value, |
57 | 0 | const ::std::string& value, ::google::protobuf::Arena* arena) { |
58 | 0 | if (ptr_ == default_value) { |
59 | 0 | CreateInstance(arena, &value); |
60 | 0 | } else { |
61 | 0 | *ptr_ = value; |
62 | 0 | } |
63 | 0 | } |
64 | | |
65 | | // Basic accessors. |
66 | 0 | inline const ::std::string& Get() const { return *ptr_; } |
67 | | |
68 | | inline ::std::string* Mutable(const ::std::string* default_value, |
69 | 0 | ::google::protobuf::Arena* arena) { |
70 | 0 | if (ptr_ == default_value) { |
71 | 0 | CreateInstance(arena, default_value); |
72 | 0 | } |
73 | 0 | return ptr_; |
74 | 0 | } |
75 | | |
76 | | // Release returns a ::std::string* instance that is heap-allocated and is not |
77 | | // Own()'d by any arena. If the field was not set, it returns NULL. The caller |
78 | | // retains ownership. Clears this field back to NULL state. Used to implement |
79 | | // release_<field>() methods on generated classes. |
80 | | inline ::std::string* Release(const ::std::string* default_value, |
81 | 0 | ::google::protobuf::Arena* arena) { |
82 | 0 | if (ptr_ == default_value) { |
83 | 0 | return NULL; |
84 | 0 | } |
85 | 0 | ::std::string* released = NULL; |
86 | 0 | if (arena != NULL) { |
87 | 0 | // ptr_ is owned by the arena -- we need to return a copy. |
88 | 0 | released = new ::std::string(*ptr_); |
89 | 0 | } else { |
90 | 0 | released = ptr_; |
91 | 0 | } |
92 | 0 | ptr_ = const_cast< ::std::string* >(default_value); |
93 | 0 | return released; |
94 | 0 | } |
95 | | |
96 | | // UnsafeArenaRelease returns a ::std::string*, but it may be arena-owned (i.e. |
97 | | // have its destructor already registered) if arena != NULL. If the field was |
98 | | // not set, this returns NULL. This method clears this field back to NULL |
99 | | // state. Used to implement unsafe_arena_release_<field>() methods on |
100 | | // generated classes. |
101 | | inline ::std::string* UnsafeArenaRelease(const ::std::string* default_value, |
102 | 0 | ::google::protobuf::Arena* /* arena */) { |
103 | 0 | if (ptr_ == default_value) { |
104 | 0 | return NULL; |
105 | 0 | } |
106 | 0 | ::std::string* released = ptr_; |
107 | 0 | ptr_ = const_cast< ::std::string* >(default_value); |
108 | 0 | return released; |
109 | 0 | } |
110 | | |
111 | | // Takes a string that is heap-allocated, and takes ownership. The string's |
112 | | // destructor is registered with the arena. Used to implement |
113 | | // set_allocated_<field> in generated classes. |
114 | | inline void SetAllocated(const ::std::string* default_value, |
115 | 0 | ::std::string* value, ::google::protobuf::Arena* arena) { |
116 | 0 | if (arena == NULL && ptr_ != default_value) { |
117 | 0 | Destroy(default_value, arena); |
118 | 0 | } |
119 | 0 | if (value != NULL) { |
120 | 0 | ptr_ = value; |
121 | 0 | if (arena != NULL) { |
122 | 0 | arena->Own(value); |
123 | 0 | } |
124 | 0 | } else { |
125 | 0 | ptr_ = const_cast< ::std::string* >(default_value); |
126 | 0 | } |
127 | 0 | } |
128 | | |
129 | | // Takes a string that has lifetime equal to the arena's lifetime. The arena |
130 | | // must be non-null. It is safe only to pass this method a value returned by |
131 | | // UnsafeArenaRelease() on another field of a message in the same arena. Used |
132 | | // to implement unsafe_arena_set_allocated_<field> in generated classes. |
133 | | inline void UnsafeArenaSetAllocated(const ::std::string* default_value, |
134 | | ::std::string* value, |
135 | 0 | ::google::protobuf::Arena* /* arena */) { |
136 | 0 | if (value != NULL) { |
137 | 0 | ptr_ = value; |
138 | 0 | } else { |
139 | 0 | ptr_ = const_cast< ::std::string* >(default_value); |
140 | 0 | } |
141 | 0 | } |
142 | | |
143 | | // Swaps internal pointers. Arena-safety semantics: this is guarded by the |
144 | | // logic in Swap()/UnsafeArenaSwap() at the message level, so this method is |
145 | | // 'unsafe' if called directly. |
146 | 0 | GOOGLE_ATTRIBUTE_ALWAYS_INLINE void Swap(ArenaStringPtr* other) { |
147 | 0 | std::swap(ptr_, other->ptr_); |
148 | 0 | } |
149 | | |
150 | | // Frees storage (if not on an arena). |
151 | | inline void Destroy(const ::std::string* default_value, |
152 | 0 | ::google::protobuf::Arena* arena) { |
153 | 0 | if (arena == NULL && ptr_ != default_value) { |
154 | 0 | delete ptr_; |
155 | 0 | } |
156 | 0 | } |
157 | | |
158 | | // Clears content, but keeps allocated string if arena != NULL, to avoid the |
159 | | // overhead of heap operations. After this returns, the content (as seen by |
160 | | // the user) will always be the empty string. Assumes that |default_value| |
161 | | // is an empty string. |
162 | | inline void ClearToEmpty(const ::std::string* default_value, |
163 | 0 | ::google::protobuf::Arena* /* arena */) { |
164 | 0 | if (ptr_ == default_value) { |
165 | 0 | // Already set to default (which is empty) -- do nothing. |
166 | 0 | } else { |
167 | 0 | ptr_->clear(); |
168 | 0 | } |
169 | 0 | } |
170 | | |
171 | | // Clears content, but keeps allocated string if arena != NULL, to avoid the |
172 | | // overhead of heap operations. After this returns, the content (as seen by |
173 | | // the user) will always be equal to |default_value|. |
174 | | inline void ClearToDefault(const ::std::string* default_value, |
175 | 0 | ::google::protobuf::Arena* /* arena */) { |
176 | 0 | if (ptr_ == default_value) { |
177 | 0 | // Already set to default -- do nothing. |
178 | 0 | } else { |
179 | 0 | // Have another allocated string -- rather than throwing this away and |
180 | 0 | // resetting ptr_ to the canonical default string instance, we just reuse |
181 | 0 | // this instance. |
182 | 0 | *ptr_ = *default_value; |
183 | 0 | } |
184 | 0 | } |
185 | | |
186 | | // Called from generated code / reflection runtime only. Resets value to point |
187 | | // to a default string pointer, with the semantics that this ArenaStringPtr |
188 | | // does not own the pointed-to memory. Disregards initial value of ptr_ (so |
189 | | // this is the *ONLY* safe method to call after construction or when |
190 | | // reinitializing after becoming the active field in a oneof union). |
191 | 0 | inline void UnsafeSetDefault(const ::std::string* default_value) { |
192 | 0 | // Casting away 'const' is safe here: accessors ensure that ptr_ is only |
193 | 0 | // returned as a const if it is equal to default_value. |
194 | 0 | ptr_ = const_cast< ::std::string* >(default_value); |
195 | 0 | } |
196 | | |
197 | | // The 'NoArena' variants of methods below assume arena == NULL and are |
198 | | // optimized to provide very little overhead relative to a raw string pointer |
199 | | // (while still being in-memory compatible with other code that assumes |
200 | | // ArenaStringPtr). Note the invariant that a class instance that has only |
201 | | // ever been mutated by NoArena methods must *only* be in the String state |
202 | | // (i.e., tag bits are not used), *NEVER* ArenaString. This allows all |
203 | | // tagged-pointer manipulations to be avoided. |
204 | | inline void SetNoArena(const ::std::string* default_value, |
205 | 0 | const ::std::string& value) { |
206 | 0 | if (ptr_ == default_value) { |
207 | 0 | CreateInstanceNoArena(&value); |
208 | 0 | } else { |
209 | 0 | *ptr_ = value; |
210 | 0 | } |
211 | 0 | } |
212 | | |
213 | | #if LANG_CXX11 |
214 | 0 | void SetNoArena(const ::std::string* default_value, ::std::string&& value) { |
215 | 0 | if (IsDefault(default_value)) { |
216 | 0 | ptr_ = new ::std::string(std::move(value)); |
217 | 0 | } else { |
218 | 0 | *ptr_ = std::move(value); |
219 | 0 | } |
220 | 0 | } |
221 | | #endif |
222 | | |
223 | | void AssignWithDefault(const ::std::string* default_value, ArenaStringPtr value); |
224 | | |
225 | 0 | inline const ::std::string& GetNoArena() const { return *ptr_; } |
226 | | |
227 | 0 | inline ::std::string* MutableNoArena(const ::std::string* default_value) { |
228 | 0 | if (ptr_ == default_value) { |
229 | 0 | CreateInstanceNoArena(default_value); |
230 | 0 | } |
231 | 0 | return ptr_; |
232 | 0 | } |
233 | | |
234 | 0 | inline ::std::string* ReleaseNoArena(const ::std::string* default_value) { |
235 | 0 | if (ptr_ == default_value) { |
236 | 0 | return NULL; |
237 | 0 | } else { |
238 | 0 | ::std::string* released = ptr_; |
239 | 0 | ptr_ = const_cast< ::std::string* >(default_value); |
240 | 0 | return released; |
241 | 0 | } |
242 | 0 | } |
243 | | |
244 | | inline void SetAllocatedNoArena(const ::std::string* default_value, |
245 | 0 | ::std::string* value) { |
246 | 0 | if (ptr_ != default_value) { |
247 | 0 | delete ptr_; |
248 | 0 | } |
249 | 0 | if (value != NULL) { |
250 | 0 | ptr_ = value; |
251 | 0 | } else { |
252 | 0 | ptr_ = const_cast< ::std::string* >(default_value); |
253 | 0 | } |
254 | 0 | } |
255 | | |
256 | 0 | inline void DestroyNoArena(const ::std::string* default_value) { |
257 | 0 | if (ptr_ != default_value) { |
258 | 0 | delete ptr_; |
259 | 0 | } |
260 | 0 | } |
261 | | |
262 | 0 | inline void ClearToEmptyNoArena(const ::std::string* default_value) { |
263 | 0 | if (ptr_ == default_value) { |
264 | 0 | // Nothing: already equal to default (which is the empty string). |
265 | 0 | } else { |
266 | 0 | ptr_->clear(); |
267 | 0 | } |
268 | 0 | } |
269 | | |
270 | 0 | inline void ClearToDefaultNoArena(const ::std::string* default_value) { |
271 | 0 | if (ptr_ == default_value) { |
272 | 0 | // Nothing: already set to default. |
273 | 0 | } else { |
274 | 0 | // Reuse existing allocated instance. |
275 | 0 | *ptr_ = *default_value; |
276 | 0 | } |
277 | 0 | } |
278 | | |
279 | | // Internal accessor used only at parse time to provide direct access to the |
280 | | // raw pointer from the shared parse routine (in the non-arenas case). The |
281 | | // parse routine does the string allocation in order to save code size in the |
282 | | // generated parsing code. |
283 | 0 | inline ::std::string** UnsafeRawStringPointer() { |
284 | 0 | return &ptr_; |
285 | 0 | } |
286 | | |
287 | 0 | inline bool IsDefault(const ::std::string* default_value) const { |
288 | 0 | return ptr_ == default_value; |
289 | 0 | } |
290 | | |
291 | | private: |
292 | | ::std::string* ptr_; |
293 | | |
294 | | GOOGLE_ATTRIBUTE_NOINLINE void CreateInstance(::google::protobuf::Arena* arena, |
295 | 0 | const ::std::string* initial_value) { |
296 | 0 | GOOGLE_DCHECK(initial_value != NULL); |
297 | 0 | ptr_ = new ::std::string(*initial_value); |
298 | 0 | if (arena != NULL) { |
299 | 0 | arena->Own(ptr_); |
300 | 0 | } |
301 | 0 | } |
302 | 0 | GOOGLE_ATTRIBUTE_NOINLINE void CreateInstanceNoArena(const ::std::string* initial_value) { |
303 | 0 | GOOGLE_DCHECK(initial_value != NULL); |
304 | 0 | ptr_ = new ::std::string(*initial_value); |
305 | 0 | } |
306 | | }; |
307 | | |
308 | | } // namespace internal |
309 | | } // namespace protobuf |
310 | | |
311 | | |
312 | | |
313 | | namespace protobuf { |
314 | | namespace internal { |
315 | | |
316 | | inline void ArenaStringPtr::AssignWithDefault(const ::std::string* default_value, |
317 | 0 | ArenaStringPtr value) { |
318 | 0 | const ::std::string* me = *UnsafeRawStringPointer(); |
319 | 0 | const ::std::string* other = *value.UnsafeRawStringPointer(); |
320 | 0 | // If the pointers are the same then do nothing. |
321 | 0 | if (me != other) { |
322 | 0 | SetNoArena(default_value, value.GetNoArena()); |
323 | 0 | } |
324 | 0 | } |
325 | | |
326 | | } // namespace internal |
327 | | } // namespace protobuf |
328 | | |
329 | | } // namespace google |
330 | | #endif // GOOGLE_PROTOBUF_ARENASTRING_H__ |