诸暨麻将添加redis
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

832 行
31 KiB

  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. // Author: kenton@google.com (Kenton Varda)
  31. // Based on original Protocol Buffers design by
  32. // Sanjay Ghemawat, Jeff Dean, and others.
  33. #include <google/protobuf/generated_message_util.h>
  34. #include <limits>
  35. #ifndef GOOGLE_PROTOBUF_SUPPORT_WINDOWS_XP
  36. // We're only using this as a standard way for getting the thread id.
  37. // We're not using any thread functionality.
  38. #include <thread> // NOLINT
  39. #endif // #ifndef GOOGLE_PROTOBUF_SUPPORT_WINDOWS_XP
  40. #include <vector>
  41. #include <google/protobuf/io/coded_stream.h>
  42. #include <google/protobuf/io/zero_copy_stream_impl_lite.h>
  43. #include <google/protobuf/arenastring.h>
  44. #include <google/protobuf/extension_set.h>
  45. #include <google/protobuf/generated_message_table_driven.h>
  46. #include <google/protobuf/message_lite.h>
  47. #include <google/protobuf/metadata_lite.h>
  48. #include <google/protobuf/stubs/mutex.h>
  49. #include <google/protobuf/port_def.inc>
  50. #include <google/protobuf/repeated_field.h>
  51. #include <google/protobuf/wire_format_lite.h>
  52. namespace google {
  53. namespace protobuf {
  54. namespace internal {
  55. void DestroyMessage(const void* message) {
  56. static_cast<const MessageLite*>(message)->~MessageLite();
  57. }
  58. void DestroyString(const void* s) {
  59. static_cast<const std::string*>(s)->~string();
  60. }
  61. ExplicitlyConstructed<std::string> fixed_address_empty_string;
  62. static bool InitProtobufDefaultsImpl() {
  63. fixed_address_empty_string.DefaultConstruct();
  64. OnShutdownDestroyString(fixed_address_empty_string.get_mutable());
  65. return true;
  66. }
  67. void InitProtobufDefaults() {
  68. static bool is_inited = InitProtobufDefaultsImpl();
  69. (void)is_inited;
  70. }
  71. size_t StringSpaceUsedExcludingSelfLong(const std::string& str) {
  72. const void* start = &str;
  73. const void* end = &str + 1;
  74. if (start <= str.data() && str.data() < end) {
  75. // The string's data is stored inside the string object itself.
  76. return 0;
  77. } else {
  78. return str.capacity();
  79. }
  80. }
  81. template <typename T>
  82. const T& Get(const void* ptr) {
  83. return *static_cast<const T*>(ptr);
  84. }
  85. // PrimitiveTypeHelper is a wrapper around the interface of WireFormatLite.
  86. // WireFormatLite has a very inconvenient interface with respect to template
  87. // meta-programming. This class wraps the different named functions into
  88. // a single Serialize / SerializeToArray interface.
  89. template <int type>
  90. struct PrimitiveTypeHelper;
  91. template <>
  92. struct PrimitiveTypeHelper<WireFormatLite::TYPE_BOOL> {
  93. typedef bool Type;
  94. static void Serialize(const void* ptr, io::CodedOutputStream* output) {
  95. WireFormatLite::WriteBoolNoTag(Get<bool>(ptr), output);
  96. }
  97. static uint8* SerializeToArray(const void* ptr, uint8* buffer) {
  98. return WireFormatLite::WriteBoolNoTagToArray(Get<Type>(ptr), buffer);
  99. }
  100. };
  101. template <>
  102. struct PrimitiveTypeHelper<WireFormatLite::TYPE_INT32> {
  103. typedef int32 Type;
  104. static void Serialize(const void* ptr, io::CodedOutputStream* output) {
  105. WireFormatLite::WriteInt32NoTag(Get<int32>(ptr), output);
  106. }
  107. static uint8* SerializeToArray(const void* ptr, uint8* buffer) {
  108. return WireFormatLite::WriteInt32NoTagToArray(Get<Type>(ptr), buffer);
  109. }
  110. };
  111. template <>
  112. struct PrimitiveTypeHelper<WireFormatLite::TYPE_SINT32> {
  113. typedef int32 Type;
  114. static void Serialize(const void* ptr, io::CodedOutputStream* output) {
  115. WireFormatLite::WriteSInt32NoTag(Get<int32>(ptr), output);
  116. }
  117. static uint8* SerializeToArray(const void* ptr, uint8* buffer) {
  118. return WireFormatLite::WriteSInt32NoTagToArray(Get<Type>(ptr), buffer);
  119. }
  120. };
  121. template <>
  122. struct PrimitiveTypeHelper<WireFormatLite::TYPE_UINT32> {
  123. typedef uint32 Type;
  124. static void Serialize(const void* ptr, io::CodedOutputStream* output) {
  125. WireFormatLite::WriteUInt32NoTag(Get<uint32>(ptr), output);
  126. }
  127. static uint8* SerializeToArray(const void* ptr, uint8* buffer) {
  128. return WireFormatLite::WriteUInt32NoTagToArray(Get<Type>(ptr), buffer);
  129. }
  130. };
  131. template <>
  132. struct PrimitiveTypeHelper<WireFormatLite::TYPE_INT64> {
  133. typedef int64 Type;
  134. static void Serialize(const void* ptr, io::CodedOutputStream* output) {
  135. WireFormatLite::WriteInt64NoTag(Get<int64>(ptr), output);
  136. }
  137. static uint8* SerializeToArray(const void* ptr, uint8* buffer) {
  138. return WireFormatLite::WriteInt64NoTagToArray(Get<Type>(ptr), buffer);
  139. }
  140. };
  141. template <>
  142. struct PrimitiveTypeHelper<WireFormatLite::TYPE_SINT64> {
  143. typedef int64 Type;
  144. static void Serialize(const void* ptr, io::CodedOutputStream* output) {
  145. WireFormatLite::WriteSInt64NoTag(Get<int64>(ptr), output);
  146. }
  147. static uint8* SerializeToArray(const void* ptr, uint8* buffer) {
  148. return WireFormatLite::WriteSInt64NoTagToArray(Get<Type>(ptr), buffer);
  149. }
  150. };
  151. template <>
  152. struct PrimitiveTypeHelper<WireFormatLite::TYPE_UINT64> {
  153. typedef uint64 Type;
  154. static void Serialize(const void* ptr, io::CodedOutputStream* output) {
  155. WireFormatLite::WriteUInt64NoTag(Get<uint64>(ptr), output);
  156. }
  157. static uint8* SerializeToArray(const void* ptr, uint8* buffer) {
  158. return WireFormatLite::WriteUInt64NoTagToArray(Get<Type>(ptr), buffer);
  159. }
  160. };
  161. template <>
  162. struct PrimitiveTypeHelper<WireFormatLite::TYPE_FIXED32> {
  163. typedef uint32 Type;
  164. static void Serialize(const void* ptr, io::CodedOutputStream* output) {
  165. WireFormatLite::WriteFixed32NoTag(Get<uint32>(ptr), output);
  166. }
  167. static uint8* SerializeToArray(const void* ptr, uint8* buffer) {
  168. return WireFormatLite::WriteFixed32NoTagToArray(Get<Type>(ptr), buffer);
  169. }
  170. };
  171. template <>
  172. struct PrimitiveTypeHelper<WireFormatLite::TYPE_FIXED64> {
  173. typedef uint64 Type;
  174. static void Serialize(const void* ptr, io::CodedOutputStream* output) {
  175. WireFormatLite::WriteFixed64NoTag(Get<uint64>(ptr), output);
  176. }
  177. static uint8* SerializeToArray(const void* ptr, uint8* buffer) {
  178. return WireFormatLite::WriteFixed64NoTagToArray(Get<Type>(ptr), buffer);
  179. }
  180. };
  181. template <>
  182. struct PrimitiveTypeHelper<WireFormatLite::TYPE_ENUM>
  183. : PrimitiveTypeHelper<WireFormatLite::TYPE_INT32> {};
  184. template <>
  185. struct PrimitiveTypeHelper<WireFormatLite::TYPE_SFIXED32>
  186. : PrimitiveTypeHelper<WireFormatLite::TYPE_FIXED32> {
  187. typedef int32 Type;
  188. };
  189. template <>
  190. struct PrimitiveTypeHelper<WireFormatLite::TYPE_SFIXED64>
  191. : PrimitiveTypeHelper<WireFormatLite::TYPE_FIXED64> {
  192. typedef int64 Type;
  193. };
  194. template <>
  195. struct PrimitiveTypeHelper<WireFormatLite::TYPE_FLOAT>
  196. : PrimitiveTypeHelper<WireFormatLite::TYPE_FIXED32> {
  197. typedef float Type;
  198. };
  199. template <>
  200. struct PrimitiveTypeHelper<WireFormatLite::TYPE_DOUBLE>
  201. : PrimitiveTypeHelper<WireFormatLite::TYPE_FIXED64> {
  202. typedef double Type;
  203. };
  204. template <>
  205. struct PrimitiveTypeHelper<WireFormatLite::TYPE_STRING> {
  206. typedef std::string Type;
  207. static void Serialize(const void* ptr, io::CodedOutputStream* output) {
  208. const Type& value = *static_cast<const Type*>(ptr);
  209. output->WriteVarint32(value.size());
  210. output->WriteRawMaybeAliased(value.data(), value.size());
  211. }
  212. static uint8* SerializeToArray(const void* ptr, uint8* buffer) {
  213. const Type& value = *static_cast<const Type*>(ptr);
  214. return io::CodedOutputStream::WriteStringWithSizeToArray(value, buffer);
  215. }
  216. };
  217. template <>
  218. struct PrimitiveTypeHelper<WireFormatLite::TYPE_BYTES>
  219. : PrimitiveTypeHelper<WireFormatLite::TYPE_STRING> {};
  220. template <>
  221. struct PrimitiveTypeHelper<FieldMetadata::kInlinedType>
  222. : PrimitiveTypeHelper<WireFormatLite::TYPE_STRING> {};
  223. // We want to serialize to both CodedOutputStream and directly into byte arrays
  224. // without duplicating the code. In fact we might want extra output channels in
  225. // the future.
  226. template <typename O, int type>
  227. struct OutputHelper;
  228. template <int type, typename O>
  229. void SerializeTo(const void* ptr, O* output) {
  230. OutputHelper<O, type>::Serialize(ptr, output);
  231. }
  232. template <typename O>
  233. void WriteTagTo(uint32 tag, O* output) {
  234. SerializeTo<WireFormatLite::TYPE_UINT32>(&tag, output);
  235. }
  236. template <typename O>
  237. void WriteLengthTo(uint32 length, O* output) {
  238. SerializeTo<WireFormatLite::TYPE_UINT32>(&length, output);
  239. }
  240. // Specialization for coded output stream
  241. template <int type>
  242. struct OutputHelper<io::CodedOutputStream, type> {
  243. static void Serialize(const void* ptr, io::CodedOutputStream* output) {
  244. PrimitiveTypeHelper<type>::Serialize(ptr, output);
  245. }
  246. };
  247. // Specialization for writing into a plain array
  248. struct ArrayOutput {
  249. uint8* ptr;
  250. bool is_deterministic;
  251. };
  252. template <int type>
  253. struct OutputHelper<ArrayOutput, type> {
  254. static void Serialize(const void* ptr, ArrayOutput* output) {
  255. output->ptr = PrimitiveTypeHelper<type>::SerializeToArray(ptr, output->ptr);
  256. }
  257. };
  258. void SerializeMessageNoTable(const MessageLite* msg,
  259. io::CodedOutputStream* output) {
  260. msg->SerializeWithCachedSizes(output);
  261. }
  262. void SerializeMessageNoTable(const MessageLite* msg, ArrayOutput* output) {
  263. io::ArrayOutputStream array_stream(output->ptr, INT_MAX);
  264. io::CodedOutputStream o(&array_stream);
  265. o.SetSerializationDeterministic(output->is_deterministic);
  266. msg->SerializeWithCachedSizes(&o);
  267. output->ptr += o.ByteCount();
  268. }
  269. // Helper to branch to fast path if possible
  270. void SerializeMessageDispatch(const MessageLite& msg,
  271. const FieldMetadata* field_table, int num_fields,
  272. int32 cached_size,
  273. io::CodedOutputStream* output) {
  274. const uint8* base = reinterpret_cast<const uint8*>(&msg);
  275. SerializeInternal(base, field_table, num_fields, output);
  276. }
  277. // Helper to branch to fast path if possible
  278. void SerializeMessageDispatch(const MessageLite& msg,
  279. const FieldMetadata* field_table, int num_fields,
  280. int32 cached_size, ArrayOutput* output) {
  281. const uint8* base = reinterpret_cast<const uint8*>(&msg);
  282. output->ptr = SerializeInternalToArray(base, field_table, num_fields,
  283. output->is_deterministic, output->ptr);
  284. }
  285. // Serializing messages is special as it's not a primitive type and needs an
  286. // explicit overload for each output type.
  287. template <typename O>
  288. void SerializeMessageTo(const MessageLite* msg, const void* table_ptr,
  289. O* output) {
  290. const SerializationTable* table =
  291. static_cast<const SerializationTable*>(table_ptr);
  292. if (!table) {
  293. // Proto1
  294. WriteLengthTo(msg->GetCachedSize(), output);
  295. SerializeMessageNoTable(msg, output);
  296. return;
  297. }
  298. const FieldMetadata* field_table = table->field_table;
  299. const uint8* base = reinterpret_cast<const uint8*>(msg);
  300. int cached_size = *reinterpret_cast<const int32*>(base + field_table->offset);
  301. WriteLengthTo(cached_size, output);
  302. int num_fields = table->num_fields - 1;
  303. SerializeMessageDispatch(*msg, field_table + 1, num_fields, cached_size,
  304. output);
  305. }
  306. // Almost the same as above only it doesn't output the length field.
  307. template <typename O>
  308. void SerializeGroupTo(const MessageLite* msg, const void* table_ptr,
  309. O* output) {
  310. const SerializationTable* table =
  311. static_cast<const SerializationTable*>(table_ptr);
  312. if (!table) {
  313. // Proto1
  314. SerializeMessageNoTable(msg, output);
  315. return;
  316. }
  317. const FieldMetadata* field_table = table->field_table;
  318. const uint8* base = reinterpret_cast<const uint8*>(msg);
  319. int cached_size = *reinterpret_cast<const int32*>(base + field_table->offset);
  320. int num_fields = table->num_fields - 1;
  321. SerializeMessageDispatch(*msg, field_table + 1, num_fields, cached_size,
  322. output);
  323. }
  324. template <int type>
  325. struct SingularFieldHelper {
  326. template <typename O>
  327. static void Serialize(const void* field, const FieldMetadata& md, O* output) {
  328. WriteTagTo(md.tag, output);
  329. SerializeTo<type>(field, output);
  330. }
  331. };
  332. template <>
  333. struct SingularFieldHelper<WireFormatLite::TYPE_STRING> {
  334. template <typename O>
  335. static void Serialize(const void* field, const FieldMetadata& md, O* output) {
  336. WriteTagTo(md.tag, output);
  337. SerializeTo<WireFormatLite::TYPE_STRING>(&Get<ArenaStringPtr>(field).Get(),
  338. output);
  339. }
  340. };
  341. template <>
  342. struct SingularFieldHelper<WireFormatLite::TYPE_BYTES>
  343. : SingularFieldHelper<WireFormatLite::TYPE_STRING> {};
  344. template <>
  345. struct SingularFieldHelper<WireFormatLite::TYPE_GROUP> {
  346. template <typename O>
  347. static void Serialize(const void* field, const FieldMetadata& md, O* output) {
  348. WriteTagTo(md.tag, output);
  349. SerializeGroupTo(Get<const MessageLite*>(field),
  350. static_cast<const SerializationTable*>(md.ptr), output);
  351. WriteTagTo(md.tag + 1, output);
  352. }
  353. };
  354. template <>
  355. struct SingularFieldHelper<WireFormatLite::TYPE_MESSAGE> {
  356. template <typename O>
  357. static void Serialize(const void* field, const FieldMetadata& md, O* output) {
  358. WriteTagTo(md.tag, output);
  359. SerializeMessageTo(Get<const MessageLite*>(field),
  360. static_cast<const SerializationTable*>(md.ptr), output);
  361. }
  362. };
  363. template <>
  364. struct SingularFieldHelper<FieldMetadata::kInlinedType> {
  365. template <typename O>
  366. static void Serialize(const void* field, const FieldMetadata& md, O* output) {
  367. WriteTagTo(md.tag, output);
  368. SerializeTo<FieldMetadata::kInlinedType>(&Get<std::string>(field), output);
  369. }
  370. };
  371. template <int type>
  372. struct RepeatedFieldHelper {
  373. template <typename O>
  374. static void Serialize(const void* field, const FieldMetadata& md, O* output) {
  375. typedef typename PrimitiveTypeHelper<type>::Type T;
  376. const RepeatedField<T>& array = Get<RepeatedField<T> >(field);
  377. for (int i = 0; i < array.size(); i++) {
  378. WriteTagTo(md.tag, output);
  379. SerializeTo<type>(&array[i], output);
  380. }
  381. }
  382. };
  383. // We need to use a helper class to get access to the private members
  384. class AccessorHelper {
  385. public:
  386. static int Size(const RepeatedPtrFieldBase& x) { return x.size(); }
  387. static void const* Get(const RepeatedPtrFieldBase& x, int idx) {
  388. return x.raw_data()[idx];
  389. }
  390. };
  391. template <>
  392. struct RepeatedFieldHelper<WireFormatLite::TYPE_STRING> {
  393. template <typename O>
  394. static void Serialize(const void* field, const FieldMetadata& md, O* output) {
  395. const internal::RepeatedPtrFieldBase& array =
  396. Get<internal::RepeatedPtrFieldBase>(field);
  397. for (int i = 0; i < AccessorHelper::Size(array); i++) {
  398. WriteTagTo(md.tag, output);
  399. SerializeTo<WireFormatLite::TYPE_STRING>(AccessorHelper::Get(array, i),
  400. output);
  401. }
  402. }
  403. };
  404. template <>
  405. struct RepeatedFieldHelper<WireFormatLite::TYPE_BYTES>
  406. : RepeatedFieldHelper<WireFormatLite::TYPE_STRING> {};
  407. template <>
  408. struct RepeatedFieldHelper<WireFormatLite::TYPE_GROUP> {
  409. template <typename O>
  410. static void Serialize(const void* field, const FieldMetadata& md, O* output) {
  411. const internal::RepeatedPtrFieldBase& array =
  412. Get<internal::RepeatedPtrFieldBase>(field);
  413. for (int i = 0; i < AccessorHelper::Size(array); i++) {
  414. WriteTagTo(md.tag, output);
  415. SerializeGroupTo(
  416. static_cast<const MessageLite*>(AccessorHelper::Get(array, i)),
  417. static_cast<const SerializationTable*>(md.ptr), output);
  418. WriteTagTo(md.tag + 1, output);
  419. }
  420. }
  421. };
  422. template <>
  423. struct RepeatedFieldHelper<WireFormatLite::TYPE_MESSAGE> {
  424. template <typename O>
  425. static void Serialize(const void* field, const FieldMetadata& md, O* output) {
  426. const internal::RepeatedPtrFieldBase& array =
  427. Get<internal::RepeatedPtrFieldBase>(field);
  428. for (int i = 0; i < AccessorHelper::Size(array); i++) {
  429. WriteTagTo(md.tag, output);
  430. SerializeMessageTo(
  431. static_cast<const MessageLite*>(AccessorHelper::Get(array, i)),
  432. md.ptr, output);
  433. }
  434. }
  435. };
  436. template <>
  437. struct RepeatedFieldHelper<FieldMetadata::kInlinedType>
  438. : RepeatedFieldHelper<WireFormatLite::TYPE_STRING> {};
  439. template <int type>
  440. struct PackedFieldHelper {
  441. template <typename O>
  442. static void Serialize(const void* field, const FieldMetadata& md, O* output) {
  443. typedef typename PrimitiveTypeHelper<type>::Type T;
  444. const RepeatedField<T>& array = Get<RepeatedField<T> >(field);
  445. if (array.empty()) return;
  446. WriteTagTo(md.tag, output);
  447. int cached_size =
  448. Get<int>(static_cast<const uint8*>(field) + sizeof(RepeatedField<T>));
  449. WriteLengthTo(cached_size, output);
  450. for (int i = 0; i < array.size(); i++) {
  451. SerializeTo<type>(&array[i], output);
  452. }
  453. }
  454. };
  455. template <>
  456. struct PackedFieldHelper<WireFormatLite::TYPE_STRING> {
  457. template <typename O>
  458. static void Serialize(const void* field, const FieldMetadata& md, O* output) {
  459. GOOGLE_LOG(FATAL) << "Not implemented field number " << md.tag << " with type "
  460. << md.type;
  461. }
  462. };
  463. template <>
  464. struct PackedFieldHelper<WireFormatLite::TYPE_BYTES>
  465. : PackedFieldHelper<WireFormatLite::TYPE_STRING> {};
  466. template <>
  467. struct PackedFieldHelper<WireFormatLite::TYPE_GROUP>
  468. : PackedFieldHelper<WireFormatLite::TYPE_STRING> {};
  469. template <>
  470. struct PackedFieldHelper<WireFormatLite::TYPE_MESSAGE>
  471. : PackedFieldHelper<WireFormatLite::TYPE_STRING> {};
  472. template <>
  473. struct PackedFieldHelper<FieldMetadata::kInlinedType>
  474. : PackedFieldHelper<WireFormatLite::TYPE_STRING> {};
  475. template <int type>
  476. struct OneOfFieldHelper {
  477. template <typename O>
  478. static void Serialize(const void* field, const FieldMetadata& md, O* output) {
  479. SingularFieldHelper<type>::Serialize(field, md, output);
  480. }
  481. };
  482. template <>
  483. struct OneOfFieldHelper<FieldMetadata::kInlinedType> {
  484. template <typename O>
  485. static void Serialize(const void* field, const FieldMetadata& md, O* output) {
  486. SingularFieldHelper<FieldMetadata::kInlinedType>::Serialize(
  487. Get<const std::string*>(field), md, output);
  488. }
  489. };
  490. void SerializeNotImplemented(int field) {
  491. GOOGLE_LOG(FATAL) << "Not implemented field number " << field;
  492. }
  493. // When switching to c++11 we should make these constexpr functions
  494. #define SERIALIZE_TABLE_OP(type, type_class) \
  495. ((type - 1) + static_cast<int>(type_class) * FieldMetadata::kNumTypes)
  496. int FieldMetadata::CalculateType(int type,
  497. FieldMetadata::FieldTypeClass type_class) {
  498. return SERIALIZE_TABLE_OP(type, type_class);
  499. }
  500. template <int type>
  501. bool IsNull(const void* ptr) {
  502. return *static_cast<const typename PrimitiveTypeHelper<type>::Type*>(ptr) ==
  503. 0;
  504. }
  505. template <>
  506. bool IsNull<WireFormatLite::TYPE_STRING>(const void* ptr) {
  507. return static_cast<const ArenaStringPtr*>(ptr)->Get().size() == 0;
  508. }
  509. template <>
  510. bool IsNull<WireFormatLite::TYPE_BYTES>(const void* ptr) {
  511. return static_cast<const ArenaStringPtr*>(ptr)->Get().size() == 0;
  512. }
  513. template <>
  514. bool IsNull<WireFormatLite::TYPE_GROUP>(const void* ptr) {
  515. return Get<const MessageLite*>(ptr) == NULL;
  516. }
  517. template <>
  518. bool IsNull<WireFormatLite::TYPE_MESSAGE>(const void* ptr) {
  519. return Get<const MessageLite*>(ptr) == NULL;
  520. }
  521. template <>
  522. bool IsNull<FieldMetadata::kInlinedType>(const void* ptr) {
  523. return static_cast<const std::string*>(ptr)->empty();
  524. }
  525. #define SERIALIZERS_FOR_TYPE(type) \
  526. case SERIALIZE_TABLE_OP(type, FieldMetadata::kPresence): \
  527. if (!IsPresent(base, field_metadata.has_offset)) continue; \
  528. SingularFieldHelper<type>::Serialize(ptr, field_metadata, output); \
  529. break; \
  530. case SERIALIZE_TABLE_OP(type, FieldMetadata::kNoPresence): \
  531. if (IsNull<type>(ptr)) continue; \
  532. SingularFieldHelper<type>::Serialize(ptr, field_metadata, output); \
  533. break; \
  534. case SERIALIZE_TABLE_OP(type, FieldMetadata::kRepeated): \
  535. RepeatedFieldHelper<type>::Serialize(ptr, field_metadata, output); \
  536. break; \
  537. case SERIALIZE_TABLE_OP(type, FieldMetadata::kPacked): \
  538. PackedFieldHelper<type>::Serialize(ptr, field_metadata, output); \
  539. break; \
  540. case SERIALIZE_TABLE_OP(type, FieldMetadata::kOneOf): \
  541. if (!IsOneofPresent(base, field_metadata.has_offset, field_metadata.tag)) \
  542. continue; \
  543. OneOfFieldHelper<type>::Serialize(ptr, field_metadata, output); \
  544. break
  545. void SerializeInternal(const uint8* base,
  546. const FieldMetadata* field_metadata_table,
  547. int32 num_fields, io::CodedOutputStream* output) {
  548. SpecialSerializer func = nullptr;
  549. for (int i = 0; i < num_fields; i++) {
  550. const FieldMetadata& field_metadata = field_metadata_table[i];
  551. const uint8* ptr = base + field_metadata.offset;
  552. switch (field_metadata.type) {
  553. SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_DOUBLE);
  554. SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_FLOAT);
  555. SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_INT64);
  556. SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_UINT64);
  557. SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_INT32);
  558. SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_FIXED64);
  559. SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_FIXED32);
  560. SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_BOOL);
  561. SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_STRING);
  562. SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_GROUP);
  563. SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_MESSAGE);
  564. SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_BYTES);
  565. SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_UINT32);
  566. SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_ENUM);
  567. SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_SFIXED32);
  568. SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_SFIXED64);
  569. SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_SINT32);
  570. SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_SINT64);
  571. SERIALIZERS_FOR_TYPE(FieldMetadata::kInlinedType);
  572. // Special cases
  573. case FieldMetadata::kSpecial:
  574. func = reinterpret_cast<SpecialSerializer>(
  575. const_cast<void*>(field_metadata.ptr));
  576. func(base, field_metadata.offset, field_metadata.tag,
  577. field_metadata.has_offset, output);
  578. break;
  579. default:
  580. // __builtin_unreachable()
  581. SerializeNotImplemented(field_metadata.type);
  582. }
  583. }
  584. }
  585. uint8* SerializeInternalToArray(const uint8* base,
  586. const FieldMetadata* field_metadata_table,
  587. int32 num_fields, bool is_deterministic,
  588. uint8* buffer) {
  589. ArrayOutput array_output = {buffer, is_deterministic};
  590. ArrayOutput* output = &array_output;
  591. SpecialSerializer func = nullptr;
  592. for (int i = 0; i < num_fields; i++) {
  593. const FieldMetadata& field_metadata = field_metadata_table[i];
  594. const uint8* ptr = base + field_metadata.offset;
  595. switch (field_metadata.type) {
  596. SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_DOUBLE);
  597. SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_FLOAT);
  598. SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_INT64);
  599. SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_UINT64);
  600. SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_INT32);
  601. SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_FIXED64);
  602. SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_FIXED32);
  603. SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_BOOL);
  604. SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_STRING);
  605. SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_GROUP);
  606. SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_MESSAGE);
  607. SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_BYTES);
  608. SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_UINT32);
  609. SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_ENUM);
  610. SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_SFIXED32);
  611. SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_SFIXED64);
  612. SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_SINT32);
  613. SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_SINT64);
  614. SERIALIZERS_FOR_TYPE(FieldMetadata::kInlinedType);
  615. // Special cases
  616. case FieldMetadata::kSpecial: {
  617. io::ArrayOutputStream array_stream(array_output.ptr, INT_MAX);
  618. io::CodedOutputStream output(&array_stream);
  619. output.SetSerializationDeterministic(is_deterministic);
  620. func = reinterpret_cast<SpecialSerializer>(
  621. const_cast<void*>(field_metadata.ptr));
  622. func(base, field_metadata.offset, field_metadata.tag,
  623. field_metadata.has_offset, &output);
  624. array_output.ptr += output.ByteCount();
  625. } break;
  626. default:
  627. // __builtin_unreachable()
  628. SerializeNotImplemented(field_metadata.type);
  629. }
  630. }
  631. return array_output.ptr;
  632. }
  633. #undef SERIALIZERS_FOR_TYPE
  634. void ExtensionSerializer(const uint8* ptr, uint32 offset, uint32 tag,
  635. uint32 has_offset, io::CodedOutputStream* output) {
  636. reinterpret_cast<const ExtensionSet*>(ptr + offset)
  637. ->SerializeWithCachedSizes(tag, has_offset, output);
  638. }
  639. void UnknownFieldSerializerLite(const uint8* ptr, uint32 offset, uint32 tag,
  640. uint32 has_offset,
  641. io::CodedOutputStream* output) {
  642. output->WriteString(
  643. reinterpret_cast<const InternalMetadataWithArenaLite*>(ptr + offset)
  644. ->unknown_fields());
  645. }
  646. MessageLite* DuplicateIfNonNullInternal(MessageLite* message) {
  647. if (message) {
  648. MessageLite* ret = message->New();
  649. ret->CheckTypeAndMergeFrom(*message);
  650. return ret;
  651. } else {
  652. return NULL;
  653. }
  654. }
  655. void GenericSwap(MessageLite* m1, MessageLite* m2) {
  656. std::unique_ptr<MessageLite> tmp(m1->New());
  657. tmp->CheckTypeAndMergeFrom(*m1);
  658. m1->Clear();
  659. m1->CheckTypeAndMergeFrom(*m2);
  660. m2->Clear();
  661. m2->CheckTypeAndMergeFrom(*tmp);
  662. }
  663. // Returns a message owned by this Arena. This may require Own()ing or
  664. // duplicating the message.
  665. MessageLite* GetOwnedMessageInternal(Arena* message_arena,
  666. MessageLite* submessage,
  667. Arena* submessage_arena) {
  668. GOOGLE_DCHECK(submessage->GetArena() == submessage_arena);
  669. GOOGLE_DCHECK(message_arena != submessage_arena);
  670. if (message_arena != NULL && submessage_arena == NULL) {
  671. message_arena->Own(submessage);
  672. return submessage;
  673. } else {
  674. MessageLite* ret = submessage->New(message_arena);
  675. ret->CheckTypeAndMergeFrom(*submessage);
  676. return ret;
  677. }
  678. }
  679. namespace {
  680. void InitSCC_DFS(SCCInfoBase* scc) {
  681. if (scc->visit_status.load(std::memory_order_relaxed) !=
  682. SCCInfoBase::kUninitialized)
  683. return;
  684. scc->visit_status.store(SCCInfoBase::kRunning, std::memory_order_relaxed);
  685. // Each base is followed by an array of void*, containing first pointers to
  686. // SCCInfoBase and then pointers-to-pointers to SCCInfoBase.
  687. auto deps = reinterpret_cast<void**>(scc + 1);
  688. auto strong_deps = reinterpret_cast<SCCInfoBase* const*>(deps);
  689. for (int i = 0; i < scc->num_deps; ++i) {
  690. if (strong_deps[i]) InitSCC_DFS(strong_deps[i]);
  691. }
  692. auto implicit_weak_deps =
  693. reinterpret_cast<SCCInfoBase** const*>(deps + scc->num_deps);
  694. for (int i = 0; i < scc->num_implicit_weak_deps; ++i) {
  695. if (*implicit_weak_deps[i]) {
  696. InitSCC_DFS(*implicit_weak_deps[i]);
  697. }
  698. }
  699. scc->init_func();
  700. // Mark done (note we use memory order release here), other threads could
  701. // now see this as initialized and thus the initialization must have happened
  702. // before.
  703. scc->visit_status.store(SCCInfoBase::kInitialized, std::memory_order_release);
  704. }
  705. } // namespace
  706. void InitSCCImpl(SCCInfoBase* scc) {
  707. static WrappedMutex mu{GOOGLE_PROTOBUF_LINKER_INITIALIZED};
  708. // Either the default in case no initialization is running or the id of the
  709. // thread that is currently initializing.
  710. #ifndef GOOGLE_PROTOBUF_SUPPORT_WINDOWS_XP
  711. static std::atomic<std::thread::id> runner;
  712. auto me = std::this_thread::get_id();
  713. #else
  714. // This is a lightweight replacement for std::thread::id. std::thread does not
  715. // work on Windows XP SP2 with the latest VC++ libraries, because it utilizes
  716. // the Concurrency Runtime that is only supported on Windows XP SP3 and above.
  717. static std::atomic_llong runner(-1);
  718. auto me = ::GetCurrentThreadId();
  719. #endif // #ifndef GOOGLE_PROTOBUF_SUPPORT_WINDOWS_XP
  720. // This will only happen because the constructor will call InitSCC while
  721. // constructing the default instance.
  722. if (runner.load(std::memory_order_relaxed) == me) {
  723. // Because we're in the process of constructing the default instance.
  724. // We can be assured that we're already exploring this SCC.
  725. GOOGLE_CHECK_EQ(scc->visit_status.load(std::memory_order_relaxed),
  726. SCCInfoBase::kRunning);
  727. return;
  728. }
  729. InitProtobufDefaults();
  730. mu.Lock();
  731. runner.store(me, std::memory_order_relaxed);
  732. InitSCC_DFS(scc);
  733. #ifndef GOOGLE_PROTOBUF_SUPPORT_WINDOWS_XP
  734. runner.store(std::thread::id{}, std::memory_order_relaxed);
  735. #else
  736. runner.store(-1, std::memory_order_relaxed);
  737. #endif // #ifndef GOOGLE_PROTOBUF_SUPPORT_WINDOWS_XP
  738. mu.Unlock();
  739. }
  740. } // namespace internal
  741. } // namespace protobuf
  742. } // namespace google