• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // This file contains miscellaneous helper code used by generated code --
36 // including lite types -- but which should not be used directly by users.
37 
38 #ifndef GOOGLE_PROTOBUF_GENERATED_MESSAGE_UTIL_H__
39 #define GOOGLE_PROTOBUF_GENERATED_MESSAGE_UTIL_H__
40 
41 #include <assert.h>
42 
43 #include <atomic>
44 #include <climits>
45 #include <string>
46 #include <vector>
47 
48 #include <google/protobuf/stubs/common.h>
49 #include <google/protobuf/any.h>
50 #include <google/protobuf/has_bits.h>
51 #include <google/protobuf/implicit_weak_message.h>
52 #include <google/protobuf/message_lite.h>
53 #include <google/protobuf/stubs/once.h>  // Add direct dep on port for pb.cc
54 #include <google/protobuf/port.h>
55 #include <google/protobuf/repeated_field.h>
56 #include <google/protobuf/wire_format_lite.h>
57 #include <google/protobuf/stubs/strutil.h>
58 #include <google/protobuf/stubs/casts.h>
59 
60 #include <google/protobuf/port_def.inc>
61 
62 #ifdef SWIG
63 #error "You cannot SWIG proto headers"
64 #endif
65 
66 namespace google {
67 namespace protobuf {
68 
69 class Arena;
70 class Message;
71 
72 namespace io {
73 class CodedInputStream;
74 }
75 
76 namespace internal {
77 
78 template <typename To, typename From>
DownCast(From * f)79 inline To DownCast(From* f) {
80   return PROTOBUF_NAMESPACE_ID::internal::down_cast<To>(f);
81 }
82 template <typename To, typename From>
DownCast(From & f)83 inline To DownCast(From& f) {
84   return PROTOBUF_NAMESPACE_ID::internal::down_cast<To>(f);
85 }
86 
87 
88 // This fastpath inlines a single branch instead of having to make the
89 // InitProtobufDefaults function call.
90 // It also generates less inlined code than a function-scope static initializer.
91 PROTOBUF_EXPORT extern std::atomic<bool> init_protobuf_defaults_state;
92 PROTOBUF_EXPORT void InitProtobufDefaultsSlow();
InitProtobufDefaults()93 PROTOBUF_EXPORT inline void InitProtobufDefaults() {
94   if (PROTOBUF_PREDICT_FALSE(
95           !init_protobuf_defaults_state.load(std::memory_order_acquire))) {
96     InitProtobufDefaultsSlow();
97   }
98 }
99 
100 // This used by proto1
GetEmptyString()101 PROTOBUF_EXPORT inline const std::string& GetEmptyString() {
102   InitProtobufDefaults();
103   return GetEmptyStringAlreadyInited();
104 }
105 
106 
107 // True if IsInitialized() is true for all elements of t.  Type is expected
108 // to be a RepeatedPtrField<some message type>.  It's useful to have this
109 // helper here to keep the protobuf compiler from ever having to emit loops in
110 // IsInitialized() methods.  We want the C++ compiler to inline this or not
111 // as it sees fit.
112 template <typename Msg>
AllAreInitialized(const RepeatedPtrField<Msg> & t)113 bool AllAreInitialized(const RepeatedPtrField<Msg>& t) {
114   for (int i = t.size(); --i >= 0;) {
115     if (!t.Get(i).IsInitialized()) return false;
116   }
117   return true;
118 }
119 
120 // "Weak" variant of AllAreInitialized, used to implement implicit weak fields.
121 // This version operates on MessageLite to avoid introducing a dependency on the
122 // concrete message type.
123 template <class T>
AllAreInitializedWeak(const RepeatedPtrField<T> & t)124 bool AllAreInitializedWeak(const RepeatedPtrField<T>& t) {
125   for (int i = t.size(); --i >= 0;) {
126     if (!reinterpret_cast<const RepeatedPtrFieldBase&>(t)
127              .Get<ImplicitWeakTypeHandler<T> >(i)
128              .IsInitialized()) {
129       return false;
130     }
131   }
132   return true;
133 }
134 
IsPresent(const void * base,uint32 hasbit)135 inline bool IsPresent(const void* base, uint32 hasbit) {
136   const uint32* has_bits_array = static_cast<const uint32*>(base);
137   return (has_bits_array[hasbit / 32] & (1u << (hasbit & 31))) != 0;
138 }
139 
IsOneofPresent(const void * base,uint32 offset,uint32 tag)140 inline bool IsOneofPresent(const void* base, uint32 offset, uint32 tag) {
141   const uint32* oneof =
142       reinterpret_cast<const uint32*>(static_cast<const uint8*>(base) + offset);
143   return *oneof == tag >> 3;
144 }
145 
146 typedef void (*SpecialSerializer)(const uint8* base, uint32 offset, uint32 tag,
147                                   uint32 has_offset,
148                                   io::CodedOutputStream* output);
149 
150 PROTOBUF_EXPORT void ExtensionSerializer(const uint8* base, uint32 offset,
151                                          uint32 tag, uint32 has_offset,
152                                          io::CodedOutputStream* output);
153 PROTOBUF_EXPORT void UnknownFieldSerializerLite(const uint8* base,
154                                                 uint32 offset, uint32 tag,
155                                                 uint32 has_offset,
156                                                 io::CodedOutputStream* output);
157 
158 PROTOBUF_EXPORT MessageLite* DuplicateIfNonNullInternal(MessageLite* message);
159 PROTOBUF_EXPORT MessageLite* GetOwnedMessageInternal(Arena* message_arena,
160                                                      MessageLite* submessage,
161                                                      Arena* submessage_arena);
162 PROTOBUF_EXPORT void GenericSwap(MessageLite* m1, MessageLite* m2);
163 // We specialize GenericSwap for non-lite messages to benefit from reflection.
164 PROTOBUF_EXPORT void GenericSwap(Message* m1, Message* m2);
165 
166 template <typename T>
DuplicateIfNonNull(T * message)167 T* DuplicateIfNonNull(T* message) {
168   // The casts must be reinterpret_cast<> because T might be a forward-declared
169   // type that the compiler doesn't know is related to MessageLite.
170   return reinterpret_cast<T*>(
171       DuplicateIfNonNullInternal(reinterpret_cast<MessageLite*>(message)));
172 }
173 
174 template <typename T>
GetOwnedMessage(Arena * message_arena,T * submessage,Arena * submessage_arena)175 T* GetOwnedMessage(Arena* message_arena, T* submessage,
176                    Arena* submessage_arena) {
177   // The casts must be reinterpret_cast<> because T might be a forward-declared
178   // type that the compiler doesn't know is related to MessageLite.
179   return reinterpret_cast<T*>(GetOwnedMessageInternal(
180       message_arena, reinterpret_cast<MessageLite*>(submessage),
181       submessage_arena));
182 }
183 
184 // Hide atomic from the public header and allow easy change to regular int
185 // on platforms where the atomic might have a perf impact.
186 class PROTOBUF_EXPORT CachedSize {
187  public:
Get()188   int Get() const { return size_.load(std::memory_order_relaxed); }
Set(int size)189   void Set(int size) { size_.store(size, std::memory_order_relaxed); }
190 
191  private:
192   std::atomic<int> size_{0};
193 };
194 
195 // SCCInfo represents information of a strongly connected component of
196 // mutual dependent messages.
197 struct PROTOBUF_EXPORT SCCInfoBase {
198   // We use 0 for the Initialized state, because test eax,eax, jnz is smaller
199   // and is subject to macro fusion.
200   enum {
201     kInitialized = 0,  // final state
202     kRunning = 1,
203     kUninitialized = -1,  // initial state
204   };
205 #if defined(_MSC_VER) && !defined(__clang__)
206   // MSVC doesn't make std::atomic constant initialized. This union trick
207   // makes it so.
208   union {
209     int visit_status_to_make_linker_init;
210     std::atomic<int> visit_status;
211   };
212 #else
213   std::atomic<int> visit_status;
214 #endif
215   int num_deps;
216   int num_implicit_weak_deps;
217   void (*init_func)();
218   // This is followed by an array  of num_deps
219   // const SCCInfoBase* deps[];
220 };
221 
222 // Zero-length arrays are a language extension available in GCC and Clang but
223 // not MSVC.
224 #ifdef __GNUC__
225 #define PROTOBUF_ARRAY_SIZE(n) (n)
226 #else
227 #define PROTOBUF_ARRAY_SIZE(n) ((n) ? (n) : 1)
228 #endif
229 
230 template <int N>
231 struct SCCInfo {
232   SCCInfoBase base;
233   // Semantically this is const SCCInfo<T>* which is is a templated type.
234   // The obvious inheriting from SCCInfoBase mucks with struct initialization.
235   // Attempts showed the compiler was generating dynamic initialization code.
236   // This deps array consists of base.num_deps pointers to SCCInfoBase followed
237   // by base.num_implicit_weak_deps pointers to SCCInfoBase*. We need the extra
238   // pointer indirection for implicit weak fields. We cannot use a union type
239   // here, since that would prevent the array from being linker-initialized.
240   void* deps[PROTOBUF_ARRAY_SIZE(N)];
241 };
242 
243 #undef PROTOBUF_ARRAY_SIZE
244 
245 PROTOBUF_EXPORT void InitSCCImpl(SCCInfoBase* scc);
246 
InitSCC(SCCInfoBase * scc)247 inline void InitSCC(SCCInfoBase* scc) {
248   auto status = scc->visit_status.load(std::memory_order_acquire);
249   if (PROTOBUF_PREDICT_FALSE(status != SCCInfoBase::kInitialized))
250     InitSCCImpl(scc);
251 }
252 
253 PROTOBUF_EXPORT void DestroyMessage(const void* message);
254 PROTOBUF_EXPORT void DestroyString(const void* s);
255 // Destroy (not delete) the message
OnShutdownDestroyMessage(const void * ptr)256 inline void OnShutdownDestroyMessage(const void* ptr) {
257   OnShutdownRun(DestroyMessage, ptr);
258 }
259 // Destroy the string (call std::string destructor)
OnShutdownDestroyString(const std::string * ptr)260 inline void OnShutdownDestroyString(const std::string* ptr) {
261   OnShutdownRun(DestroyString, ptr);
262 }
263 
264 }  // namespace internal
265 }  // namespace protobuf
266 }  // namespace google
267 
268 #include <google/protobuf/port_undef.inc>
269 
270 #endif  // GOOGLE_PROTOBUF_GENERATED_MESSAGE_UTIL_H__
271