metadata_lite.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. #include <google/protobuf/port_def.inc>
  37. #ifdef SWIG
  38. #error "You cannot SWIG proto headers"
  39. #endif
  40. namespace google {
  41. namespace protobuf {
  42. namespace internal {
  43. // This is the representation for messages that support arena allocation. It
  44. // uses a tagged pointer to either store the owning Arena pointer, if there are
  45. // no unknown fields, or a pointer to a block of memory with both the owning
  46. // Arena pointer and the UnknownFieldSet, if there are unknown fields. Besides,
  47. // it also uses the tag to distinguish whether the owning Arena pointer is also
  48. // used by sub-structure allocation. This optimization allows for
  49. // "zero-overhead" storage of the Arena pointer, relative to the above baseline
  50. // implementation.
  51. //
  52. // The tagged pointer uses the least two significant bits to disambiguate cases.
  53. // It uses bit 0 == 0 to indicate an arena pointer and bit 0 == 1 to indicate a
  54. // UFS+Arena-container pointer. Besides it uses bit 1 == 0 to indicate arena
  55. // allocation and bit 1 == 1 to indicate heap allocation.
  56. class InternalMetadata {
  57. public:
  58. constexpr InternalMetadata() : ptr_(0) {}
  59. explicit InternalMetadata(Arena* arena, bool is_message_owned = false)
  60. : ptr_(is_message_owned
  61. ? reinterpret_cast<intptr_t>(arena) | kMessageOwnedArenaTagMask
  62. : reinterpret_cast<intptr_t>(arena)) {
  63. GOOGLE_DCHECK(!is_message_owned || arena != nullptr);
  64. }
  65. ~InternalMetadata() {
  66. if (HasMessageOwnedArenaTag()) {
  67. delete arena();
  68. }
  69. }
  70. template <typename T>
  71. void Delete() {
  72. // Note that Delete<> should be called not more than once.
  73. if (have_unknown_fields()) {
  74. DeleteOutOfLineHelper<T>();
  75. }
  76. }
  77. PROTOBUF_NDEBUG_INLINE Arena* owning_arena() const {
  78. return HasMessageOwnedArenaTag() ? nullptr : arena();
  79. }
  80. PROTOBUF_NDEBUG_INLINE Arena* arena() const {
  81. if (PROTOBUF_PREDICT_FALSE(have_unknown_fields())) {
  82. return PtrValue<ContainerBase>()->arena;
  83. } else {
  84. return PtrValue<Arena>();
  85. }
  86. }
  87. PROTOBUF_NDEBUG_INLINE bool have_unknown_fields() const {
  88. return HasUnknownFieldsTag();
  89. }
  90. PROTOBUF_NDEBUG_INLINE void* raw_arena_ptr() const {
  91. return reinterpret_cast<void*>(ptr_);
  92. }
  93. template <typename T>
  94. PROTOBUF_NDEBUG_INLINE const T& unknown_fields(
  95. const T& (*default_instance)()) const {
  96. if (PROTOBUF_PREDICT_FALSE(have_unknown_fields())) {
  97. return PtrValue<Container<T>>()->unknown_fields;
  98. } else {
  99. return default_instance();
  100. }
  101. }
  102. template <typename T>
  103. PROTOBUF_NDEBUG_INLINE T* mutable_unknown_fields() {
  104. if (PROTOBUF_PREDICT_TRUE(have_unknown_fields())) {
  105. return &PtrValue<Container<T>>()->unknown_fields;
  106. } else {
  107. return mutable_unknown_fields_slow<T>();
  108. }
  109. }
  110. template <typename T>
  111. PROTOBUF_NDEBUG_INLINE void Swap(InternalMetadata* other) {
  112. // Semantics here are that we swap only the unknown fields, not the arena
  113. // pointer. We cannot simply swap ptr_ with other->ptr_ because we need to
  114. // maintain our own arena ptr. Also, our ptr_ and other's ptr_ may be in
  115. // different states (direct arena pointer vs. container with UFS) so we
  116. // cannot simply swap ptr_ and then restore the arena pointers. We reuse
  117. // UFS's swap implementation instead.
  118. if (have_unknown_fields() || other->have_unknown_fields()) {
  119. DoSwap<T>(other->mutable_unknown_fields<T>());
  120. }
  121. }
  122. PROTOBUF_NDEBUG_INLINE void InternalSwap(InternalMetadata* other) {
  123. std::swap(ptr_, other->ptr_);
  124. }
  125. template <typename T>
  126. PROTOBUF_NDEBUG_INLINE void MergeFrom(const InternalMetadata& other) {
  127. if (other.have_unknown_fields()) {
  128. DoMergeFrom<T>(other.unknown_fields<T>(nullptr));
  129. }
  130. }
  131. template <typename T>
  132. PROTOBUF_NDEBUG_INLINE void Clear() {
  133. if (have_unknown_fields()) {
  134. DoClear<T>();
  135. }
  136. }
  137. private:
  138. intptr_t ptr_;
  139. // Tagged pointer implementation.
  140. static constexpr intptr_t kUnknownFieldsTagMask = 1;
  141. static constexpr intptr_t kMessageOwnedArenaTagMask = 2;
  142. static constexpr intptr_t kPtrTagMask =
  143. kUnknownFieldsTagMask | kMessageOwnedArenaTagMask;
  144. static constexpr intptr_t kPtrValueMask = ~kPtrTagMask;
  145. // Accessors for pointer tag and pointer value.
  146. PROTOBUF_ALWAYS_INLINE bool HasUnknownFieldsTag() const {
  147. return ptr_ & kUnknownFieldsTagMask;
  148. }
  149. PROTOBUF_ALWAYS_INLINE bool HasMessageOwnedArenaTag() const {
  150. return ptr_ & kMessageOwnedArenaTagMask;
  151. }
  152. template <typename U>
  153. U* PtrValue() const {
  154. return reinterpret_cast<U*>(ptr_ & kPtrValueMask);
  155. }
  156. // If ptr_'s tag is kTagContainer, it points to an instance of this struct.
  157. struct ContainerBase {
  158. Arena* arena;
  159. };
  160. template <typename T>
  161. struct Container : public ContainerBase {
  162. T unknown_fields;
  163. };
  164. template <typename T>
  165. PROTOBUF_NOINLINE void DeleteOutOfLineHelper() {
  166. if (arena() == NULL) {
  167. delete PtrValue<Container<T>>();
  168. }
  169. }
  170. template <typename T>
  171. PROTOBUF_NOINLINE T* mutable_unknown_fields_slow() {
  172. Arena* my_arena = arena();
  173. Container<T>* container = Arena::Create<Container<T>>(my_arena);
  174. intptr_t message_owned_arena_tag = ptr_ & kMessageOwnedArenaTagMask;
  175. // Two-step assignment works around a bug in clang's static analyzer:
  176. // https://bugs.llvm.org/show_bug.cgi?id=34198.
  177. ptr_ = reinterpret_cast<intptr_t>(container);
  178. ptr_ |= kUnknownFieldsTagMask | message_owned_arena_tag;
  179. container->arena = my_arena;
  180. return &(container->unknown_fields);
  181. }
  182. // Templated functions.
  183. template <typename T>
  184. PROTOBUF_NOINLINE void DoClear() {
  185. mutable_unknown_fields<T>()->Clear();
  186. }
  187. template <typename T>
  188. PROTOBUF_NOINLINE void DoMergeFrom(const T& other) {
  189. mutable_unknown_fields<T>()->MergeFrom(other);
  190. }
  191. template <typename T>
  192. PROTOBUF_NOINLINE void DoSwap(T* other) {
  193. mutable_unknown_fields<T>()->Swap(other);
  194. }
  195. };
  196. // String Template specializations.
  197. template <>
  198. PROTOBUF_EXPORT void InternalMetadata::DoClear<std::string>();
  199. template <>
  200. PROTOBUF_EXPORT void InternalMetadata::DoMergeFrom<std::string>(
  201. const std::string& other);
  202. template <>
  203. PROTOBUF_EXPORT void InternalMetadata::DoSwap<std::string>(std::string* other);
  204. // This helper RAII class is needed to efficiently parse unknown fields. We
  205. // should only call mutable_unknown_fields if there are actual unknown fields.
  206. // The obvious thing to just use a stack string and swap it at the end of
  207. // the parse won't work, because the destructor of StringOutputStream needs to
  208. // be called before we can modify the string (it check-fails). Using
  209. // LiteUnknownFieldSetter setter(&_internal_metadata_);
  210. // StringOutputStream stream(setter.buffer());
  211. // guarantees that the string is only swapped after stream is destroyed.
  212. class PROTOBUF_EXPORT LiteUnknownFieldSetter {
  213. public:
  214. explicit LiteUnknownFieldSetter(InternalMetadata* metadata)
  215. : metadata_(metadata) {
  216. if (metadata->have_unknown_fields()) {
  217. buffer_.swap(*metadata->mutable_unknown_fields<std::string>());
  218. }
  219. }
  220. ~LiteUnknownFieldSetter() {
  221. if (!buffer_.empty())
  222. metadata_->mutable_unknown_fields<std::string>()->swap(buffer_);
  223. }
  224. std::string* buffer() { return &buffer_; }
  225. private:
  226. InternalMetadata* metadata_;
  227. std::string buffer_;
  228. };
  229. } // namespace internal
  230. } // namespace protobuf
  231. } // namespace google
  232. #include <google/protobuf/port_undef.inc>
  233. #endif // GOOGLE_PROTOBUF_METADATA_LITE_H__