metadata.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. // This header file defines an internal class that encapsulates internal message
  31. // metadata (Unknown-field set, Arena pointer, ...) and allows its
  32. // representation to be made more space-efficient via various optimizations.
  33. //
  34. // Note that this is distinct from google::protobuf::Metadata, which encapsulates
  35. // Descriptor and Reflection pointers.
  36. #ifndef GOOGLE_PROTOBUF_METADATA_H__
  37. #define GOOGLE_PROTOBUF_METADATA_H__
  38. #include <google/protobuf/stubs/common.h>
  39. #include <google/protobuf/arena.h>
  40. #include <google/protobuf/unknown_field_set.h>
  41. namespace google {
  42. namespace protobuf {
  43. namespace internal {
  44. // This is the representation for messages that support arena allocation. It
  45. // uses a tagged pointer to either store the Arena pointer, if there are no
  46. // unknown fields, or a pointer to a block of memory with both the Arena pointer
  47. // and the UnknownFieldSet, if there are unknown fields. This optimization
  48. // allows for "zero-overhead" storage of the Arena pointer, relative to the
  49. // above baseline implementation.
  50. //
  51. // The tagged pointer uses the LSB to disambiguate cases, and uses bit 0 == 0 to
  52. // indicate an arena pointer and bit 0 == 1 to indicate a UFS+Arena-container
  53. // pointer.
  54. class InternalMetadataWithArena {
  55. public:
  56. InternalMetadataWithArena() : ptr_(NULL) {}
  57. explicit InternalMetadataWithArena(Arena* arena)
  58. : ptr_ (arena) {}
  59. ~InternalMetadataWithArena() {
  60. if (have_unknown_fields() && arena() == NULL) {
  61. delete PtrValue<Container>();
  62. }
  63. ptr_ = NULL;
  64. }
  65. GOOGLE_ATTRIBUTE_ALWAYS_INLINE const UnknownFieldSet& unknown_fields() const {
  66. if (GOOGLE_PREDICT_FALSE(have_unknown_fields())) {
  67. return PtrValue<Container>()->unknown_fields_;
  68. } else {
  69. return *UnknownFieldSet::default_instance();
  70. }
  71. }
  72. GOOGLE_ATTRIBUTE_ALWAYS_INLINE UnknownFieldSet* mutable_unknown_fields() {
  73. if (GOOGLE_PREDICT_TRUE(have_unknown_fields())) {
  74. return &PtrValue<Container>()->unknown_fields_;
  75. } else {
  76. return mutable_unknown_fields_slow();
  77. }
  78. }
  79. GOOGLE_ATTRIBUTE_ALWAYS_INLINE Arena* arena() const {
  80. if (GOOGLE_PREDICT_FALSE(have_unknown_fields())) {
  81. return PtrValue<Container>()->arena_;
  82. } else {
  83. return PtrValue<Arena>();
  84. }
  85. }
  86. GOOGLE_ATTRIBUTE_ALWAYS_INLINE bool have_unknown_fields() const {
  87. return PtrTag() == kTagContainer;
  88. }
  89. GOOGLE_ATTRIBUTE_ALWAYS_INLINE void Swap(InternalMetadataWithArena* other) {
  90. // Semantics here are that we swap only the unknown fields, not the arena
  91. // pointer. We cannot simply swap ptr_ with other->ptr_ because we need to
  92. // maintain our own arena ptr. Also, our ptr_ and other's ptr_ may be in
  93. // different states (direct arena pointer vs. container with UFS) so we
  94. // cannot simply swap ptr_ and then restore the arena pointers. We reuse
  95. // UFS's swap implementation instead.
  96. if (have_unknown_fields() || other->have_unknown_fields()) {
  97. mutable_unknown_fields()->Swap(other->mutable_unknown_fields());
  98. }
  99. }
  100. GOOGLE_ATTRIBUTE_ALWAYS_INLINE void* raw_arena_ptr() const {
  101. return ptr_;
  102. }
  103. private:
  104. void* ptr_;
  105. // Tagged pointer implementation.
  106. enum {
  107. // ptr_ is an Arena*.
  108. kTagArena = 0,
  109. // ptr_ is a Container*.
  110. kTagContainer = 1,
  111. };
  112. static const intptr_t kPtrTagMask = 1;
  113. static const intptr_t kPtrValueMask = ~kPtrTagMask;
  114. // Accessors for pointer tag and pointer value.
  115. GOOGLE_ATTRIBUTE_ALWAYS_INLINE int PtrTag() const {
  116. return reinterpret_cast<intptr_t>(ptr_) & kPtrTagMask;
  117. }
  118. template<typename T> T* PtrValue() const {
  119. return reinterpret_cast<T*>(
  120. reinterpret_cast<intptr_t>(ptr_) & kPtrValueMask);
  121. }
  122. // If ptr_'s tag is kTagContainer, it points to an instance of this struct.
  123. struct Container {
  124. UnknownFieldSet unknown_fields_;
  125. Arena* arena_;
  126. };
  127. GOOGLE_ATTRIBUTE_NOINLINE UnknownFieldSet* mutable_unknown_fields_slow() {
  128. Arena* my_arena = arena();
  129. Container* container = Arena::Create<Container>(my_arena);
  130. ptr_ = reinterpret_cast<void*>(
  131. reinterpret_cast<intptr_t>(container) | kTagContainer);
  132. container->arena_ = my_arena;
  133. return &(container->unknown_fields_);
  134. }
  135. };
  136. // Temporary compatibility typedef. Remove once this is released in components
  137. // and upb CL is submitted.
  138. typedef InternalMetadataWithArena InternalMetadata;
  139. } // namespace internal
  140. } // namespace protobuf
  141. } // namespace google
  142. #endif // GOOGLE_PROTOBUF_METADATA_H__