metadata_lite.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 <google/protobuf/stubs/common.h>
  33. #include <google/protobuf/arena.h>
  34. #include <google/protobuf/message_lite.h>
  35. #include <google/protobuf/stubs/port.h>
  36. namespace google {
  37. namespace protobuf {
  38. namespace internal {
  39. // This is the representation for messages that support arena allocation. It
  40. // uses a tagged pointer to either store the Arena pointer, if there are no
  41. // unknown fields, or a pointer to a block of memory with both the Arena pointer
  42. // and the UnknownFieldSet, if there are unknown fields. This optimization
  43. // allows for "zero-overhead" storage of the Arena pointer, relative to the
  44. // above baseline implementation.
  45. //
  46. // The tagged pointer uses the LSB to disambiguate cases, and uses bit 0 == 0 to
  47. // indicate an arena pointer and bit 0 == 1 to indicate a UFS+Arena-container
  48. // pointer.
  49. template <class T, class Derived>
  50. class InternalMetadataWithArenaBase {
  51. public:
  52. InternalMetadataWithArenaBase() : ptr_(NULL) {}
  53. explicit InternalMetadataWithArenaBase(Arena* arena) : ptr_(arena) {}
  54. ~InternalMetadataWithArenaBase() {
  55. if (have_unknown_fields() && arena() == NULL) {
  56. delete PtrValue<Container>();
  57. }
  58. ptr_ = NULL;
  59. }
  60. GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE const T& unknown_fields() const {
  61. if (GOOGLE_PREDICT_FALSE(have_unknown_fields())) {
  62. return PtrValue<Container>()->unknown_fields;
  63. } else {
  64. return Derived::default_instance();
  65. }
  66. }
  67. GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE T* mutable_unknown_fields() {
  68. if (GOOGLE_PREDICT_TRUE(have_unknown_fields())) {
  69. return &PtrValue<Container>()->unknown_fields;
  70. } else {
  71. return mutable_unknown_fields_slow();
  72. }
  73. }
  74. GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE Arena* arena() const {
  75. if (GOOGLE_PREDICT_FALSE(have_unknown_fields())) {
  76. return PtrValue<Container>()->arena;
  77. } else {
  78. return PtrValue<Arena>();
  79. }
  80. }
  81. GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE bool have_unknown_fields() const {
  82. return PtrTag() == kTagContainer;
  83. }
  84. GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE void Swap(Derived* other) {
  85. // Semantics here are that we swap only the unknown fields, not the arena
  86. // pointer. We cannot simply swap ptr_ with other->ptr_ because we need to
  87. // maintain our own arena ptr. Also, our ptr_ and other's ptr_ may be in
  88. // different states (direct arena pointer vs. container with UFS) so we
  89. // cannot simply swap ptr_ and then restore the arena pointers. We reuse
  90. // UFS's swap implementation instead.
  91. if (have_unknown_fields() || other->have_unknown_fields()) {
  92. static_cast<Derived*>(this)->DoSwap(other->mutable_unknown_fields());
  93. }
  94. }
  95. GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE void MergeFrom(const Derived& other) {
  96. if (other.have_unknown_fields()) {
  97. static_cast<Derived*>(this)->DoMergeFrom(other.unknown_fields());
  98. }
  99. }
  100. GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE void Clear() {
  101. if (have_unknown_fields()) {
  102. static_cast<Derived*>(this)->DoClear();
  103. }
  104. }
  105. GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE void* raw_arena_ptr() const {
  106. return ptr_;
  107. }
  108. private:
  109. void* ptr_;
  110. // Tagged pointer implementation.
  111. enum {
  112. // ptr_ is an Arena*.
  113. kTagArena = 0,
  114. // ptr_ is a Container*.
  115. kTagContainer = 1,
  116. };
  117. static const intptr_t kPtrTagMask = 1;
  118. static const intptr_t kPtrValueMask = ~kPtrTagMask;
  119. // Accessors for pointer tag and pointer value.
  120. GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE int PtrTag() const {
  121. return reinterpret_cast<intptr_t>(ptr_) & kPtrTagMask;
  122. }
  123. template<typename U> U* PtrValue() const {
  124. return reinterpret_cast<U*>(
  125. reinterpret_cast<intptr_t>(ptr_) & kPtrValueMask);
  126. }
  127. // If ptr_'s tag is kTagContainer, it points to an instance of this struct.
  128. struct Container {
  129. T unknown_fields;
  130. Arena* arena;
  131. };
  132. GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE T* mutable_unknown_fields_slow() {
  133. Arena* my_arena = arena();
  134. Container* container = Arena::Create<Container>(my_arena);
  135. // Two-step assignment works around a bug in clang's static analyzer:
  136. // https://bugs.llvm.org/show_bug.cgi?id=34198.
  137. ptr_ = container;
  138. ptr_ = reinterpret_cast<void*>(
  139. reinterpret_cast<intptr_t>(ptr_) | kTagContainer);
  140. container->arena = my_arena;
  141. return &(container->unknown_fields);
  142. }
  143. };
  144. // We store unknown fields as a string right now, because there is currently no
  145. // good interface for reading unknown fields into an ArenaString. We may want
  146. // to revisit this to allow unknown fields to be parsed onto the Arena.
  147. class InternalMetadataWithArenaLite
  148. : public InternalMetadataWithArenaBase<string,
  149. InternalMetadataWithArenaLite> {
  150. public:
  151. InternalMetadataWithArenaLite() {}
  152. explicit InternalMetadataWithArenaLite(Arena* arena)
  153. : InternalMetadataWithArenaBase<string,
  154. InternalMetadataWithArenaLite>(arena) {}
  155. void DoSwap(string* other) {
  156. mutable_unknown_fields()->swap(*other);
  157. }
  158. void DoMergeFrom(const string& other) {
  159. mutable_unknown_fields()->append(other);
  160. }
  161. void DoClear() {
  162. mutable_unknown_fields()->clear();
  163. }
  164. static const string& default_instance() {
  165. return GetEmptyStringAlreadyInited();
  166. }
  167. };
  168. // This helper RAII class is needed to efficiently parse unknown fields. We
  169. // should only call mutable_unknown_fields if there are actual unknown fields.
  170. // The obvious thing to just use a stack string and swap it at the end of the
  171. // parse won't work, because the destructor of StringOutputStream needs to be
  172. // called before we can modify the string (it check-fails). Using
  173. // LiteUnknownFieldSetter setter(&_internal_metadata_);
  174. // StringOutputStream stream(setter.buffer());
  175. // guarantees that the string is only swapped after stream is destroyed.
  176. class LIBPROTOBUF_EXPORT LiteUnknownFieldSetter {
  177. public:
  178. explicit LiteUnknownFieldSetter(InternalMetadataWithArenaLite* metadata)
  179. : metadata_(metadata) {
  180. if (metadata->have_unknown_fields()) {
  181. buffer_.swap(*metadata->mutable_unknown_fields());
  182. }
  183. }
  184. ~LiteUnknownFieldSetter() {
  185. if (!buffer_.empty()) metadata_->mutable_unknown_fields()->swap(buffer_);
  186. }
  187. string* buffer() { return &buffer_; }
  188. private:
  189. InternalMetadataWithArenaLite* metadata_;
  190. string buffer_;
  191. };
  192. } // namespace internal
  193. } // namespace protobuf
  194. } // namespace google
  195. #endif // GOOGLE_PROTOBUF_METADATA_LITE_H__