You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

metadata_lite.h 15 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. #ifndef GOOGLE_PROTOBUF_METADATA_LITE_H__
  31. #define GOOGLE_PROTOBUF_METADATA_LITE_H__
  32. #include <string>
  33. #include <google/protobuf/stubs/common.h>
  34. #include <google/protobuf/arena.h>
  35. #include <google/protobuf/port.h>
  36. // Must be included last.
  37. #include <google/protobuf/port_def.inc>
  38. #ifdef SWIG
  39. #error "You cannot SWIG proto headers"
  40. #endif
  41. namespace google
  42. {
  43. namespace protobuf
  44. {
  45. namespace internal
  46. {
  47. // This is the representation for messages that support arena allocation. It
  48. // uses a tagged pointer to either store the owning Arena pointer, if there are
  49. // no unknown fields, or a pointer to a block of memory with both the owning
  50. // Arena pointer and the UnknownFieldSet, if there are unknown fields. Besides,
  51. // it also uses the tag to distinguish whether the owning Arena pointer is also
  52. // used by sub-structure allocation. This optimization allows for
  53. // "zero-overhead" storage of the Arena pointer, relative to the above baseline
  54. // implementation.
  55. //
  56. // The tagged pointer uses the least two significant bits to disambiguate cases.
  57. // It uses bit 0 == 0 to indicate an arena pointer and bit 0 == 1 to indicate a
  58. // UFS+Arena-container pointer. Besides it uses bit 1 == 0 to indicate arena
  59. // allocation and bit 1 == 1 to indicate heap allocation.
  60. class PROTOBUF_EXPORT InternalMetadata
  61. {
  62. public:
  63. constexpr InternalMetadata() :
  64. ptr_(0)
  65. {
  66. }
  67. explicit InternalMetadata(Arena* arena, bool is_message_owned = false)
  68. {
  69. SetArena(arena, is_message_owned);
  70. }
  71. void SetArena(Arena* arena, bool is_message_owned)
  72. {
  73. ptr_ = is_message_owned ? reinterpret_cast<intptr_t>(arena) | kMessageOwnedArenaTagMask : reinterpret_cast<intptr_t>(arena);
  74. GOOGLE_DCHECK(!is_message_owned || arena != nullptr);
  75. }
  76. // To keep the ABI identical between debug and non-debug builds,
  77. // the destructor is always defined here even though it may delegate
  78. // to a non-inline private method.
  79. // (see https://github.com/protocolbuffers/protobuf/issues/9947)
  80. ~InternalMetadata()
  81. {
  82. #if defined(NDEBUG) || defined(_MSC_VER)
  83. if (HasMessageOwnedArenaTag())
  84. {
  85. delete reinterpret_cast<Arena*>(ptr_ - kMessageOwnedArenaTagMask);
  86. }
  87. #else
  88. CheckedDestruct();
  89. #endif
  90. }
  91. template<typename T>
  92. void Delete()
  93. {
  94. // Note that Delete<> should be called not more than once.
  95. if (have_unknown_fields())
  96. {
  97. DeleteOutOfLineHelper<T>();
  98. }
  99. }
  100. // DeleteReturnArena will delete the unknown fields only if they weren't
  101. // allocated on an arena. Then it updates the flags so that if you call
  102. // have_unknown_fields(), it will return false. Finally, it returns the
  103. // current value of arena(). It is designed to be used as part of a
  104. // Message class's destructor call, so that when control eventually gets
  105. // to ~InternalMetadata(), we don't need to check for have_unknown_fields()
  106. // again.
  107. template<typename T>
  108. Arena* DeleteReturnArena()
  109. {
  110. if (have_unknown_fields())
  111. {
  112. return DeleteOutOfLineHelper<T>();
  113. }
  114. else
  115. {
  116. return PtrValue<Arena>();
  117. }
  118. }
  119. PROTOBUF_NDEBUG_INLINE Arena* owning_arena() const
  120. {
  121. return HasMessageOwnedArenaTag() ? nullptr : arena();
  122. }
  123. PROTOBUF_NDEBUG_INLINE Arena* user_arena() const
  124. {
  125. Arena* a = arena();
  126. return a && !a->IsMessageOwned() ? a : nullptr;
  127. }
  128. PROTOBUF_NDEBUG_INLINE Arena* arena() const
  129. {
  130. if (PROTOBUF_PREDICT_FALSE(have_unknown_fields()))
  131. {
  132. return PtrValue<ContainerBase>()->arena;
  133. }
  134. else
  135. {
  136. return PtrValue<Arena>();
  137. }
  138. }
  139. PROTOBUF_NDEBUG_INLINE bool have_unknown_fields() const
  140. {
  141. return HasUnknownFieldsTag();
  142. }
  143. PROTOBUF_NDEBUG_INLINE void* raw_arena_ptr() const
  144. {
  145. return reinterpret_cast<void*>(ptr_);
  146. }
  147. template<typename T>
  148. PROTOBUF_NDEBUG_INLINE const T& unknown_fields(
  149. const T& (*default_instance)()
  150. ) const
  151. {
  152. if (PROTOBUF_PREDICT_FALSE(have_unknown_fields()))
  153. {
  154. return PtrValue<Container<T>>()->unknown_fields;
  155. }
  156. else
  157. {
  158. return default_instance();
  159. }
  160. }
  161. template<typename T>
  162. PROTOBUF_NDEBUG_INLINE T* mutable_unknown_fields()
  163. {
  164. if (PROTOBUF_PREDICT_TRUE(have_unknown_fields()))
  165. {
  166. return &PtrValue<Container<T>>()->unknown_fields;
  167. }
  168. else
  169. {
  170. return mutable_unknown_fields_slow<T>();
  171. }
  172. }
  173. template<typename T>
  174. PROTOBUF_NDEBUG_INLINE void Swap(InternalMetadata* other)
  175. {
  176. // Semantics here are that we swap only the unknown fields, not the arena
  177. // pointer. We cannot simply swap ptr_ with other->ptr_ because we need to
  178. // maintain our own arena ptr. Also, our ptr_ and other's ptr_ may be in
  179. // different states (direct arena pointer vs. container with UFS) so we
  180. // cannot simply swap ptr_ and then restore the arena pointers. We reuse
  181. // UFS's swap implementation instead.
  182. if (have_unknown_fields() || other->have_unknown_fields())
  183. {
  184. DoSwap<T>(other->mutable_unknown_fields<T>());
  185. }
  186. }
  187. PROTOBUF_NDEBUG_INLINE void InternalSwap(InternalMetadata* other)
  188. {
  189. std::swap(ptr_, other->ptr_);
  190. }
  191. template<typename T>
  192. PROTOBUF_NDEBUG_INLINE void MergeFrom(const InternalMetadata& other)
  193. {
  194. if (other.have_unknown_fields())
  195. {
  196. DoMergeFrom<T>(other.unknown_fields<T>(nullptr));
  197. }
  198. }
  199. template<typename T>
  200. PROTOBUF_NDEBUG_INLINE void Clear()
  201. {
  202. if (have_unknown_fields())
  203. {
  204. DoClear<T>();
  205. }
  206. }
  207. private:
  208. intptr_t ptr_;
  209. // Tagged pointer implementation.
  210. static constexpr intptr_t kUnknownFieldsTagMask = 1;
  211. static constexpr intptr_t kMessageOwnedArenaTagMask = 2;
  212. static constexpr intptr_t kPtrTagMask =
  213. kUnknownFieldsTagMask | kMessageOwnedArenaTagMask;
  214. static constexpr intptr_t kPtrValueMask = ~kPtrTagMask;
  215. // Accessors for pointer tag and pointer value.
  216. PROTOBUF_ALWAYS_INLINE bool HasUnknownFieldsTag() const
  217. {
  218. return ptr_ & kUnknownFieldsTagMask;
  219. }
  220. PROTOBUF_ALWAYS_INLINE bool HasMessageOwnedArenaTag() const
  221. {
  222. return ptr_ & kMessageOwnedArenaTagMask;
  223. }
  224. template<typename U>
  225. U* PtrValue() const
  226. {
  227. return reinterpret_cast<U*>(ptr_ & kPtrValueMask);
  228. }
  229. // If ptr_'s tag is kTagContainer, it points to an instance of this struct.
  230. struct ContainerBase
  231. {
  232. Arena* arena;
  233. };
  234. template<typename T>
  235. struct Container : public ContainerBase
  236. {
  237. T unknown_fields;
  238. };
  239. template<typename T>
  240. PROTOBUF_NOINLINE Arena* DeleteOutOfLineHelper()
  241. {
  242. if (auto* a = arena())
  243. {
  244. // Subtle: we want to preserve the message-owned arena flag, while at the
  245. // same time replacing the pointer to Container<T> with a pointer to the
  246. // arena.
  247. intptr_t message_owned_arena_tag = ptr_ & kMessageOwnedArenaTagMask;
  248. ptr_ = reinterpret_cast<intptr_t>(a) | message_owned_arena_tag;
  249. return a;
  250. }
  251. else
  252. {
  253. delete PtrValue<Container<T>>();
  254. ptr_ = 0;
  255. return nullptr;
  256. }
  257. }
  258. template<typename T>
  259. PROTOBUF_NOINLINE T* mutable_unknown_fields_slow()
  260. {
  261. Arena* my_arena = arena();
  262. Container<T>* container = Arena::Create<Container<T>>(my_arena);
  263. intptr_t message_owned_arena_tag = ptr_ & kMessageOwnedArenaTagMask;
  264. // Two-step assignment works around a bug in clang's static analyzer:
  265. // https://bugs.llvm.org/show_bug.cgi?id=34198.
  266. ptr_ = reinterpret_cast<intptr_t>(container);
  267. ptr_ |= kUnknownFieldsTagMask | message_owned_arena_tag;
  268. container->arena = my_arena;
  269. return &(container->unknown_fields);
  270. }
  271. // Templated functions.
  272. template<typename T>
  273. PROTOBUF_NOINLINE void DoClear()
  274. {
  275. mutable_unknown_fields<T>()->Clear();
  276. }
  277. template<typename T>
  278. PROTOBUF_NOINLINE void DoMergeFrom(const T& other)
  279. {
  280. mutable_unknown_fields<T>()->MergeFrom(other);
  281. }
  282. template<typename T>
  283. PROTOBUF_NOINLINE void DoSwap(T* other)
  284. {
  285. mutable_unknown_fields<T>()->Swap(other);
  286. }
  287. // Private helper with debug checks for ~InternalMetadata()
  288. void CheckedDestruct();
  289. };
  290. // String Template specializations.
  291. template<>
  292. PROTOBUF_EXPORT void InternalMetadata::DoClear<std::string>();
  293. template<>
  294. PROTOBUF_EXPORT void InternalMetadata::DoMergeFrom<std::string>(
  295. const std::string& other
  296. );
  297. template<>
  298. PROTOBUF_EXPORT void InternalMetadata::DoSwap<std::string>(std::string* other);
  299. // This helper RAII class is needed to efficiently parse unknown fields. We
  300. // should only call mutable_unknown_fields if there are actual unknown fields.
  301. // The obvious thing to just use a stack string and swap it at the end of
  302. // the parse won't work, because the destructor of StringOutputStream needs to
  303. // be called before we can modify the string (it check-fails). Using
  304. // LiteUnknownFieldSetter setter(&_internal_metadata_);
  305. // StringOutputStream stream(setter.buffer());
  306. // guarantees that the string is only swapped after stream is destroyed.
  307. class PROTOBUF_EXPORT LiteUnknownFieldSetter
  308. {
  309. public:
  310. explicit LiteUnknownFieldSetter(InternalMetadata* metadata) :
  311. metadata_(metadata)
  312. {
  313. if (metadata->have_unknown_fields())
  314. {
  315. buffer_.swap(*metadata->mutable_unknown_fields<std::string>());
  316. }
  317. }
  318. ~LiteUnknownFieldSetter()
  319. {
  320. if (!buffer_.empty())
  321. metadata_->mutable_unknown_fields<std::string>()->swap(buffer_);
  322. }
  323. std::string* buffer()
  324. {
  325. return &buffer_;
  326. }
  327. private:
  328. InternalMetadata* metadata_;
  329. std::string buffer_;
  330. };
  331. } // namespace internal
  332. } // namespace protobuf
  333. } // namespace google
  334. #include <google/protobuf/port_undef.inc>
  335. #endif // GOOGLE_PROTOBUF_METADATA_LITE_H__