From 6e099b510cf72f5fa62b9e5a2c14fdc39e89d4e4 Mon Sep 17 00:00:00 2001 From: Pedro Holanda Date: Mon, 6 Jun 2022 14:46:45 +0200 Subject: [PATCH] Add duckdb namespace to protobuf inlined dependency --- third_party/google/protobuf/any.cc | 4 +- third_party/google/protobuf/any.h | 157 +- third_party/google/protobuf/any.pb.h | 586 +-- third_party/google/protobuf/any_lite.cc | 75 +- third_party/google/protobuf/arena.cc | 684 ++- third_party/google/protobuf/arena.h | 1263 +++-- third_party/google/protobuf/arena_impl.h | 909 ++-- third_party/google/protobuf/arenastring.cc | 352 +- third_party/google/protobuf/arenastring.h | 553 +- third_party/google/protobuf/descriptor.cc | 3 +- third_party/google/protobuf/descriptor.h | 3800 +++++++------- .../google/protobuf/descriptor_database.cc | 1591 +++--- .../google/protobuf/descriptor_database.h | 522 +- .../google/protobuf/dynamic_message.cc | 1246 +++-- third_party/google/protobuf/dynamic_message.h | 267 +- .../google/protobuf/explicitly_constructed.h | 3 +- third_party/google/protobuf/extension_set.cc | 3 +- third_party/google/protobuf/extension_set.h | 6 +- .../google/protobuf/extension_set_heavy.cc | 745 ++- .../google/protobuf/extension_set_inl.h | 4 +- .../protobuf/generated_enum_reflection.h | 37 +- .../google/protobuf/generated_enum_util.cc | 78 +- .../google/protobuf/generated_enum_util.h | 26 +- .../protobuf/generated_message_bases.cc | 93 +- .../google/protobuf/generated_message_bases.h | 57 +- .../protobuf/generated_message_reflection.cc | 34 +- .../protobuf/generated_message_reflection.h | 423 +- .../generated_message_table_driven.cc | 2 + .../protobuf/generated_message_table_driven.h | 408 +- .../generated_message_table_driven_lite.cc | 100 +- .../generated_message_table_driven_lite.h | 4 +- .../protobuf/generated_message_tctable_decl.h | 115 +- .../protobuf/generated_message_tctable_impl.h | 367 +- .../google/protobuf/generated_message_util.cc | 864 ++-- .../google/protobuf/generated_message_util.h | 154 +- third_party/google/protobuf/has_bits.h | 75 +- .../google/protobuf/implicit_weak_message.cc | 26 +- .../google/protobuf/implicit_weak_message.h | 238 +- .../google/protobuf/inlined_string_field.cc | 85 +- .../google/protobuf/inlined_string_field.h | 471 +- .../google/protobuf/io/coded_stream.cc | 1595 +++--- third_party/google/protobuf/io/coded_stream.h | 2631 +++++----- third_party/google/protobuf/io/io_win32.h | 52 +- third_party/google/protobuf/io/strtod.cc | 57 +- third_party/google/protobuf/io/strtod.h | 12 +- third_party/google/protobuf/io/tokenizer.cc | 1746 ++++--- third_party/google/protobuf/io/tokenizer.h | 717 +-- .../google/protobuf/io/zero_copy_stream.cc | 20 +- .../google/protobuf/io/zero_copy_stream.h | 238 +- .../protobuf/io/zero_copy_stream_impl.cc | 419 +- .../protobuf/io/zero_copy_stream_impl.h | 442 +- .../protobuf/io/zero_copy_stream_impl_lite.cc | 613 ++- .../protobuf/io/zero_copy_stream_impl_lite.h | 475 +- third_party/google/protobuf/map.cc | 11 +- third_party/google/protobuf/map.h | 4 +- third_party/google/protobuf/map_entry.h | 122 +- third_party/google/protobuf/map_entry_lite.h | 1034 ++-- third_party/google/protobuf/map_field.cc | 1039 ++-- third_party/google/protobuf/map_field.h | 1568 +++--- third_party/google/protobuf/map_field_inl.h | 440 +- third_party/google/protobuf/map_field_lite.h | 204 +- .../google/protobuf/map_type_handler.h | 4 +- third_party/google/protobuf/message.cc | 419 +- third_party/google/protobuf/message.h | 2091 ++++---- third_party/google/protobuf/message_lite.cc | 721 ++- third_party/google/protobuf/message_lite.h | 752 ++- third_party/google/protobuf/metadata_lite.h | 370 +- third_party/google/protobuf/parse_context.cc | 916 ++-- third_party/google/protobuf/parse_context.h | 1363 +++-- third_party/google/protobuf/port_def.inc | 16 +- third_party/google/protobuf/reflection.h | 856 ++-- .../google/protobuf/reflection_internal.h | 536 +- third_party/google/protobuf/reflection_ops.cc | 702 ++- third_party/google/protobuf/reflection_ops.h | 42 +- third_party/google/protobuf/repeated_field.cc | 8 +- third_party/google/protobuf/repeated_field.h | 6 +- .../google/protobuf/repeated_ptr_field.cc | 162 +- .../google/protobuf/repeated_ptr_field.h | 4 +- third_party/google/protobuf/stubs/callback.h | 714 ++- third_party/google/protobuf/stubs/casts.h | 74 +- third_party/google/protobuf/stubs/common.cc | 3 +- third_party/google/protobuf/stubs/common.h | 72 +- third_party/google/protobuf/stubs/hash.h | 91 +- third_party/google/protobuf/stubs/int128.cc | 228 +- third_party/google/protobuf/stubs/int128.h | 484 +- third_party/google/protobuf/stubs/logging.h | 176 +- third_party/google/protobuf/stubs/macros.h | 20 +- third_party/google/protobuf/stubs/map_util.h | 695 ++- third_party/google/protobuf/stubs/mutex.h | 195 +- third_party/google/protobuf/stubs/once.h | 14 +- third_party/google/protobuf/stubs/port.h | 4 +- third_party/google/protobuf/stubs/status.cc | 264 +- third_party/google/protobuf/stubs/status.h | 225 +- third_party/google/protobuf/stubs/stl_util.h | 33 +- .../google/protobuf/stubs/stringpiece.cc | 319 +- .../google/protobuf/stubs/stringpiece.h | 403 +- .../google/protobuf/stubs/stringprintf.cc | 203 +- .../google/protobuf/stubs/stringprintf.h | 23 +- .../protobuf/stubs/structurally_valid.cc | 3036 +++++++++-- third_party/google/protobuf/stubs/strutil.cc | 2 + third_party/google/protobuf/stubs/strutil.h | 651 ++- .../google/protobuf/stubs/substitute.cc | 153 +- .../google/protobuf/stubs/substitute.h | 171 +- third_party/google/protobuf/text_format.cc | 4552 ++++++++--------- third_party/google/protobuf/text_format.h | 1195 +++-- .../google/protobuf/unknown_field_set.cc | 417 +- .../google/protobuf/unknown_field_set.h | 522 +- third_party/google/protobuf/wire_format.cc | 3 +- third_party/google/protobuf/wire_format.h | 554 +- .../google/protobuf/wire_format_lite.cc | 3 +- .../google/protobuf/wire_format_lite.h | 4 +- 111 files changed, 27683 insertions(+), 26657 deletions(-) diff --git a/third_party/google/protobuf/any.cc b/third_party/google/protobuf/any.cc index 73c002f6040..1815e2b6ac5 100644 --- a/third_party/google/protobuf/any.cc +++ b/third_party/google/protobuf/any.cc @@ -36,7 +36,7 @@ #include #include - +namespace duckdb { namespace google { namespace protobuf { namespace internal { @@ -79,5 +79,5 @@ bool GetAnyFieldDescriptors(const Message& message, } // namespace internal } // namespace protobuf } // namespace google - +} // namespace duckdb #include diff --git a/third_party/google/protobuf/any.h b/third_party/google/protobuf/any.h index e8336fa14a3..9f17d2c06c4 100644 --- a/third_party/google/protobuf/any.h +++ b/third_party/google/protobuf/any.h @@ -38,7 +38,7 @@ #include #include - +namespace duckdb { namespace google { namespace protobuf { @@ -47,81 +47,74 @@ class Message; namespace internal { -extern const char kAnyFullTypeName[]; // "google.protobuf.Any". -extern const char kTypeGoogleApisComPrefix[]; // "type.googleapis.com/". -extern const char kTypeGoogleProdComPrefix[]; // "type.googleprod.com/". +extern const char kAnyFullTypeName[]; // "google.protobuf.Any". +extern const char kTypeGoogleApisComPrefix[]; // "type.googleapis.com/". +extern const char kTypeGoogleProdComPrefix[]; // "type.googleprod.com/". -std::string GetTypeUrl(StringPiece message_name, - StringPiece type_url_prefix); +std::string GetTypeUrl(StringPiece message_name, StringPiece type_url_prefix); // Helper class used to implement google::protobuf::Any. class PROTOBUF_EXPORT AnyMetadata { - typedef ArenaStringPtr UrlType; - typedef ArenaStringPtr ValueType; - public: - // AnyMetadata does not take ownership of "type_url" and "value". - constexpr AnyMetadata(UrlType* type_url, ValueType* value) - : type_url_(type_url), value_(value) {} - - // Packs a message using the default type URL prefix: "type.googleapis.com". - // The resulted type URL will be "type.googleapis.com/". - // Returns false if serializing the message failed. - template - bool PackFrom(Arena* arena, const T& message) { - return InternalPackFrom(arena, message, kTypeGoogleApisComPrefix, - T::FullMessageName()); - } - - bool PackFrom(Arena* arena, const Message& message); - - // Packs a message using the given type URL prefix. The type URL will be - // constructed by concatenating the message type's full name to the prefix - // with an optional "/" separator if the prefix doesn't already end with "/". - // For example, both PackFrom(message, "type.googleapis.com") and - // PackFrom(message, "type.googleapis.com/") yield the same result type - // URL: "type.googleapis.com/". - // Returns false if serializing the message failed. - template - bool PackFrom(Arena* arena, const T& message, - StringPiece type_url_prefix) { - return InternalPackFrom(arena, message, type_url_prefix, - T::FullMessageName()); - } - - bool PackFrom(Arena* arena, const Message& message, - StringPiece type_url_prefix); - - // Unpacks the payload into the given message. Returns false if the message's - // type doesn't match the type specified in the type URL (i.e., the full - // name after the last "/" of the type URL doesn't match the message's actual - // full name) or parsing the payload has failed. - template - bool UnpackTo(T* message) const { - return InternalUnpackTo(T::FullMessageName(), message); - } - - bool UnpackTo(Message* message) const; - - // Checks whether the type specified in the type URL matches the given type. - // A type is considered matching if its full name matches the full name after - // the last "/" in the type URL. - template - bool Is() const { - return InternalIs(T::FullMessageName()); - } - - private: - bool InternalPackFrom(Arena* arena, const MessageLite& message, - StringPiece type_url_prefix, - StringPiece type_name); - bool InternalUnpackTo(StringPiece type_name, - MessageLite* message) const; - bool InternalIs(StringPiece type_name) const; - - UrlType* type_url_; - ValueType* value_; - - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(AnyMetadata); + typedef ArenaStringPtr UrlType; + typedef ArenaStringPtr ValueType; + +public: + // AnyMetadata does not take ownership of "type_url" and "value". + constexpr AnyMetadata(UrlType *type_url, ValueType *value) : type_url_(type_url), value_(value) { + } + + // Packs a message using the default type URL prefix: "type.googleapis.com". + // The resulted type URL will be "type.googleapis.com/". + // Returns false if serializing the message failed. + template + bool PackFrom(Arena *arena, const T &message) { + return InternalPackFrom(arena, message, kTypeGoogleApisComPrefix, T::FullMessageName()); + } + + bool PackFrom(Arena *arena, const Message &message); + + // Packs a message using the given type URL prefix. The type URL will be + // constructed by concatenating the message type's full name to the prefix + // with an optional "/" separator if the prefix doesn't already end with "/". + // For example, both PackFrom(message, "type.googleapis.com") and + // PackFrom(message, "type.googleapis.com/") yield the same result type + // URL: "type.googleapis.com/". + // Returns false if serializing the message failed. + template + bool PackFrom(Arena *arena, const T &message, StringPiece type_url_prefix) { + return InternalPackFrom(arena, message, type_url_prefix, T::FullMessageName()); + } + + bool PackFrom(Arena *arena, const Message &message, StringPiece type_url_prefix); + + // Unpacks the payload into the given message. Returns false if the message's + // type doesn't match the type specified in the type URL (i.e., the full + // name after the last "/" of the type URL doesn't match the message's actual + // full name) or parsing the payload has failed. + template + bool UnpackTo(T *message) const { + return InternalUnpackTo(T::FullMessageName(), message); + } + + bool UnpackTo(Message *message) const; + + // Checks whether the type specified in the type URL matches the given type. + // A type is considered matching if its full name matches the full name after + // the last "/" in the type URL. + template + bool Is() const { + return InternalIs(T::FullMessageName()); + } + +private: + bool InternalPackFrom(Arena *arena, const MessageLite &message, StringPiece type_url_prefix, StringPiece type_name); + bool InternalUnpackTo(StringPiece type_name, MessageLite *message) const; + bool InternalIs(StringPiece type_name) const; + + UrlType *type_url_; + ValueType *value_; + + GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(AnyMetadata); }; // Get the proto type name from Any::type_url value. For example, passing @@ -131,26 +124,24 @@ class PROTOBUF_EXPORT AnyMetadata { // // NOTE: this function is available publicly as: // google::protobuf::Any() // static method on the generated message type. -bool ParseAnyTypeUrl(StringPiece type_url, std::string* full_type_name); +bool ParseAnyTypeUrl(StringPiece type_url, std::string *full_type_name); // Get the proto type name and prefix from Any::type_url value. For example, // passing "type.googleapis.com/rpc.QueryOrigin" will return // "type.googleapis.com/" in *url_prefix and "rpc.QueryOrigin" in // *full_type_name. Returns false if the type_url does not have a "/" in the // type url separating the full type name. -bool ParseAnyTypeUrl(StringPiece type_url, std::string* url_prefix, - std::string* full_type_name); +bool ParseAnyTypeUrl(StringPiece type_url, std::string *url_prefix, std::string *full_type_name); // See if message is of type google.protobuf.Any, if so, return the descriptors // for "type_url" and "value" fields. -bool GetAnyFieldDescriptors(const Message& message, - const FieldDescriptor** type_url_field, - const FieldDescriptor** value_field); - -} // namespace internal -} // namespace protobuf -} // namespace google +bool GetAnyFieldDescriptors(const Message &message, const FieldDescriptor **type_url_field, + const FieldDescriptor **value_field); +} // namespace internal +} // namespace protobuf +} // namespace google +} // namespace duckdb #include -#endif // GOOGLE_PROTOBUF_ANY_H__ +#endif // GOOGLE_PROTOBUF_ANY_H__ diff --git a/third_party/google/protobuf/any.pb.h b/third_party/google/protobuf/any.pb.h index ff7ef9cfa02..3323ab33afc 100644 --- a/third_party/google/protobuf/any.pb.h +++ b/third_party/google/protobuf/any.pb.h @@ -37,349 +37,357 @@ PROTOBUF_NAMESPACE_OPEN namespace internal { class AnyMetadata; -} // namespace internal +} // namespace internal PROTOBUF_NAMESPACE_CLOSE // Internal implementation detail -- do not use these members. struct PROTOBUF_EXPORT TableStruct_google_2fprotobuf_2fany_2eproto { - static const ::PROTOBUF_NAMESPACE_ID::internal::ParseTableField entries[] - PROTOBUF_SECTION_VARIABLE(protodesc_cold); - static const ::PROTOBUF_NAMESPACE_ID::internal::AuxiliaryParseTableField aux[] - PROTOBUF_SECTION_VARIABLE(protodesc_cold); - static const ::PROTOBUF_NAMESPACE_ID::internal::ParseTable schema[1] - PROTOBUF_SECTION_VARIABLE(protodesc_cold); - static const ::PROTOBUF_NAMESPACE_ID::internal::FieldMetadata field_metadata[]; - static const ::PROTOBUF_NAMESPACE_ID::internal::SerializationTable serialization_table[]; - static const uint32_t offsets[]; + static const ::PROTOBUF_NAMESPACE_ID::internal::ParseTableField entries[] PROTOBUF_SECTION_VARIABLE(protodesc_cold); + static const ::PROTOBUF_NAMESPACE_ID::internal::AuxiliaryParseTableField + aux[] PROTOBUF_SECTION_VARIABLE(protodesc_cold); + static const ::PROTOBUF_NAMESPACE_ID::internal::ParseTable schema[1] PROTOBUF_SECTION_VARIABLE(protodesc_cold); + static const ::PROTOBUF_NAMESPACE_ID::internal::FieldMetadata field_metadata[]; + static const ::PROTOBUF_NAMESPACE_ID::internal::SerializationTable serialization_table[]; + static const uint32_t offsets[]; }; -PROTOBUF_EXPORT extern const ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable descriptor_table_google_2fprotobuf_2fany_2eproto; +PROTOBUF_EXPORT extern const ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable + descriptor_table_google_2fprotobuf_2fany_2eproto; PROTOBUF_NAMESPACE_OPEN class Any; struct AnyDefaultTypeInternal; PROTOBUF_EXPORT extern AnyDefaultTypeInternal _Any_default_instance_; PROTOBUF_NAMESPACE_CLOSE PROTOBUF_NAMESPACE_OPEN -template<> PROTOBUF_EXPORT ::PROTOBUF_NAMESPACE_ID::Any* Arena::CreateMaybeMessage<::PROTOBUF_NAMESPACE_ID::Any>(Arena*); +template <> +PROTOBUF_EXPORT Any *Arena::CreateMaybeMessage<::PROTOBUF_NAMESPACE_ID::Any>(Arena *); PROTOBUF_NAMESPACE_CLOSE PROTOBUF_NAMESPACE_OPEN // =================================================================== -class PROTOBUF_EXPORT Any final : - public ::PROTOBUF_NAMESPACE_ID::Message /* @@protoc_insertion_point(class_definition:google.protobuf.Any) */ { - public: - inline Any() : Any(nullptr) {} - ~Any() override; - explicit constexpr Any(::PROTOBUF_NAMESPACE_ID::internal::ConstantInitialized); - - Any(const Any& from); - Any(Any&& from) noexcept - : Any() { - *this = ::std::move(from); - } - - inline Any& operator=(const Any& from) { - CopyFrom(from); - return *this; - } - inline Any& operator=(Any&& from) noexcept { - if (this == &from) return *this; - if (GetOwningArena() == from.GetOwningArena() - #ifdef PROTOBUF_FORCE_COPY_IN_MOVE - && GetOwningArena() != nullptr - #endif // !PROTOBUF_FORCE_COPY_IN_MOVE - ) { - InternalSwap(&from); - } else { - CopyFrom(from); - } - return *this; - } - - static const ::PROTOBUF_NAMESPACE_ID::Descriptor* descriptor() { - return GetDescriptor(); - } - static const ::PROTOBUF_NAMESPACE_ID::Descriptor* GetDescriptor() { - return default_instance().GetMetadata().descriptor; - } - static const ::PROTOBUF_NAMESPACE_ID::Reflection* GetReflection() { - return default_instance().GetMetadata().reflection; - } - static const Any& default_instance() { - return *internal_default_instance(); - } - static inline const Any* internal_default_instance() { - return reinterpret_cast( - &_Any_default_instance_); - } - static constexpr int kIndexInFileMessages = - 0; - - // implements Any ----------------------------------------------- - - bool PackFrom(const ::PROTOBUF_NAMESPACE_ID::Message& message) { - return _any_metadata_.PackFrom(GetArena(), message); - } - bool PackFrom(const ::PROTOBUF_NAMESPACE_ID::Message& message, - ::PROTOBUF_NAMESPACE_ID::ConstStringParam type_url_prefix) { - return _any_metadata_.PackFrom(GetArena(), message, type_url_prefix); - } - bool UnpackTo(::PROTOBUF_NAMESPACE_ID::Message* message) const { - return _any_metadata_.UnpackTo(message); - } - static bool GetAnyFieldDescriptors( - const ::PROTOBUF_NAMESPACE_ID::Message& message, - const ::PROTOBUF_NAMESPACE_ID::FieldDescriptor** type_url_field, - const ::PROTOBUF_NAMESPACE_ID::FieldDescriptor** value_field); - template ::value>::type> - bool PackFrom(const T& message) { - return _any_metadata_.PackFrom(GetArena(), message); - } - template ::value>::type> - bool PackFrom(const T& message, - ::PROTOBUF_NAMESPACE_ID::ConstStringParam type_url_prefix) { - return _any_metadata_.PackFrom(GetArena(), message, type_url_prefix);} - template ::value>::type> - bool UnpackTo(T* message) const { - return _any_metadata_.UnpackTo(message); - } - template bool Is() const { - return _any_metadata_.Is(); - } - static bool ParseAnyTypeUrl(::PROTOBUF_NAMESPACE_ID::ConstStringParam type_url, - std::string* full_type_name); - friend void swap(Any& a, Any& b) { - a.Swap(&b); - } - inline void Swap(Any* other) { - if (other == this) return; - #ifdef PROTOBUF_FORCE_COPY_IN_SWAP - if (GetOwningArena() != nullptr && - GetOwningArena() == other->GetOwningArena()) { - #else // PROTOBUF_FORCE_COPY_IN_SWAP - if (GetOwningArena() == other->GetOwningArena()) { - #endif // !PROTOBUF_FORCE_COPY_IN_SWAP - InternalSwap(other); - } else { - ::PROTOBUF_NAMESPACE_ID::internal::GenericSwap(this, other); - } - } - void UnsafeArenaSwap(Any* other) { - if (other == this) return; - GOOGLE_DCHECK(GetOwningArena() == other->GetOwningArena()); - InternalSwap(other); - } - - // implements Message ---------------------------------------------- - - Any* New(::PROTOBUF_NAMESPACE_ID::Arena* arena = nullptr) const final { - return CreateMaybeMessage(arena); - } - using ::PROTOBUF_NAMESPACE_ID::Message::CopyFrom; - void CopyFrom(const Any& from); - using ::PROTOBUF_NAMESPACE_ID::Message::MergeFrom; - void MergeFrom(const Any& from); - private: - static void MergeImpl(::PROTOBUF_NAMESPACE_ID::Message* to, const ::PROTOBUF_NAMESPACE_ID::Message& from); - public: - PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; - bool IsInitialized() const final; - - size_t ByteSizeLong() const final; - const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; - uint8_t* _InternalSerialize( - uint8_t* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; - int GetCachedSize() const final { return _cached_size_.Get(); } - - private: - void SharedCtor(); - void SharedDtor(); - void SetCachedSize(int size) const final; - void InternalSwap(Any* other); - - private: - friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { - return "google.protobuf.Any"; - } - protected: - explicit Any(::PROTOBUF_NAMESPACE_ID::Arena* arena, - bool is_message_owned = false); - private: - static void ArenaDtor(void* object); - inline void RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena* arena); - public: - - static const ClassData _class_data_; - const ::PROTOBUF_NAMESPACE_ID::Message::ClassData*GetClassData() const final; - - ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final; - - // nested types ---------------------------------------------------- - - // accessors ------------------------------------------------------- - - enum : int { - kTypeUrlFieldNumber = 1, - kValueFieldNumber = 2, - }; - // string type_url = 1; - void clear_type_url(); - const std::string& type_url() const; - template - void set_type_url(ArgT0&& arg0, ArgT... args); - std::string* mutable_type_url(); - PROTOBUF_NODISCARD std::string* release_type_url(); - void set_allocated_type_url(std::string* type_url); - private: - const std::string& _internal_type_url() const; - inline PROTOBUF_ALWAYS_INLINE void _internal_set_type_url(const std::string& value); - std::string* _internal_mutable_type_url(); - public: - - // bytes value = 2; - void clear_value(); - const std::string& value() const; - template - void set_value(ArgT0&& arg0, ArgT... args); - std::string* mutable_value(); - PROTOBUF_NODISCARD std::string* release_value(); - void set_allocated_value(std::string* value); - private: - const std::string& _internal_value() const; - inline PROTOBUF_ALWAYS_INLINE void _internal_set_value(const std::string& value); - std::string* _internal_mutable_value(); - public: - - // @@protoc_insertion_point(class_scope:google.protobuf.Any) - private: - class _Internal; - - template friend class ::PROTOBUF_NAMESPACE_ID::Arena::InternalHelper; - typedef void InternalArenaConstructable_; - typedef void DestructorSkippable_; - ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr type_url_; - ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr value_; - mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_; - ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata _any_metadata_; - friend struct ::TableStruct_google_2fprotobuf_2fany_2eproto; +class PROTOBUF_EXPORT Any final + : public ::PROTOBUF_NAMESPACE_ID::Message /* @@protoc_insertion_point(class_definition:google.protobuf.Any) */ { +public: + inline Any() : Any(nullptr) { + } + ~Any() override; + explicit constexpr Any(::PROTOBUF_NAMESPACE_ID::internal::ConstantInitialized); + + Any(const Any &from); + Any(Any &&from) noexcept : Any() { + *this = ::std::move(from); + } + + inline Any &operator=(const Any &from) { + CopyFrom(from); + return *this; + } + inline Any &operator=(Any &&from) noexcept { + if (this == &from) + return *this; + if (GetOwningArena() == from.GetOwningArena() +#ifdef PROTOBUF_FORCE_COPY_IN_MOVE + && GetOwningArena() != nullptr +#endif // !PROTOBUF_FORCE_COPY_IN_MOVE + ) { + InternalSwap(&from); + } else { + CopyFrom(from); + } + return *this; + } + + static const ::PROTOBUF_NAMESPACE_ID::Descriptor *descriptor() { + return GetDescriptor(); + } + static const ::PROTOBUF_NAMESPACE_ID::Descriptor *GetDescriptor() { + return default_instance().GetMetadata().descriptor; + } + static const ::PROTOBUF_NAMESPACE_ID::Reflection *GetReflection() { + return default_instance().GetMetadata().reflection; + } + static const Any &default_instance() { + return *internal_default_instance(); + } + static inline const Any *internal_default_instance() { + return reinterpret_cast(&_Any_default_instance_); + } + static constexpr int kIndexInFileMessages = 0; + + // implements Any ----------------------------------------------- + + bool PackFrom(const ::PROTOBUF_NAMESPACE_ID::Message &message) { + return _any_metadata_.PackFrom(GetArena(), message); + } + bool PackFrom(const ::PROTOBUF_NAMESPACE_ID::Message &message, + ::PROTOBUF_NAMESPACE_ID::ConstStringParam type_url_prefix) { + return _any_metadata_.PackFrom(GetArena(), message, type_url_prefix); + } + bool UnpackTo(::PROTOBUF_NAMESPACE_ID::Message *message) const { + return _any_metadata_.UnpackTo(message); + } + static bool GetAnyFieldDescriptors(const ::PROTOBUF_NAMESPACE_ID::Message &message, + const ::PROTOBUF_NAMESPACE_ID::FieldDescriptor **type_url_field, + const ::PROTOBUF_NAMESPACE_ID::FieldDescriptor **value_field); + template ::value>::type> + bool PackFrom(const T &message) { + return _any_metadata_.PackFrom(GetArena(), message); + } + template ::value>::type> + bool PackFrom(const T &message, ::PROTOBUF_NAMESPACE_ID::ConstStringParam type_url_prefix) { + return _any_metadata_.PackFrom(GetArena(), message, type_url_prefix); + } + template ::value>::type> + bool UnpackTo(T *message) const { + return _any_metadata_.UnpackTo(message); + } + template + bool Is() const { + return _any_metadata_.Is(); + } + static bool ParseAnyTypeUrl(::PROTOBUF_NAMESPACE_ID::ConstStringParam type_url, std::string *full_type_name); + friend void swap(Any &a, Any &b) { + a.Swap(&b); + } + inline void Swap(Any *other) { + if (other == this) + return; +#ifdef PROTOBUF_FORCE_COPY_IN_SWAP + if (GetOwningArena() != nullptr && GetOwningArena() == other->GetOwningArena()) { +#else // PROTOBUF_FORCE_COPY_IN_SWAP + if (GetOwningArena() == other->GetOwningArena()) { +#endif // !PROTOBUF_FORCE_COPY_IN_SWAP + InternalSwap(other); + } else { + ::PROTOBUF_NAMESPACE_ID::internal::GenericSwap(this, other); + } + } + void UnsafeArenaSwap(Any *other) { + if (other == this) + return; + GOOGLE_DCHECK(GetOwningArena() == other->GetOwningArena()); + InternalSwap(other); + } + + // implements Message ---------------------------------------------- + + Any *New(::PROTOBUF_NAMESPACE_ID::Arena *arena = nullptr) const final { + return CreateMaybeMessage(arena); + } + using ::PROTOBUF_NAMESPACE_ID::Message::CopyFrom; + void CopyFrom(const Any &from); + using ::PROTOBUF_NAMESPACE_ID::Message::MergeFrom; + void MergeFrom(const Any &from); + +private: + static void MergeImpl(::PROTOBUF_NAMESPACE_ID::Message *to, const ::PROTOBUF_NAMESPACE_ID::Message &from); + +public: + PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; + bool IsInitialized() const final; + + size_t ByteSizeLong() const final; + const char *_InternalParse(const char *ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext *ctx) final; + uint8_t *_InternalSerialize(uint8_t *target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream *stream) const final; + int GetCachedSize() const final { + return _cached_size_.Get(); + } + +private: + void SharedCtor(); + void SharedDtor(); + void SetCachedSize(int size) const final; + void InternalSwap(Any *other); + +private: + friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; + static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + return "google.protobuf.Any"; + } + +protected: + explicit Any(::PROTOBUF_NAMESPACE_ID::Arena *arena, bool is_message_owned = false); + +private: + static void ArenaDtor(void *object); + inline void RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena *arena); + +public: + static const ClassData _class_data_; + const ::PROTOBUF_NAMESPACE_ID::Message::ClassData *GetClassData() const final; + + ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final; + + // nested types ---------------------------------------------------- + + // accessors ------------------------------------------------------- + + enum : int { + kTypeUrlFieldNumber = 1, + kValueFieldNumber = 2, + }; + // string type_url = 1; + void clear_type_url(); + const std::string &type_url() const; + template + void set_type_url(ArgT0 &&arg0, ArgT... args); + std::string *mutable_type_url(); + PROTOBUF_NODISCARD std::string *release_type_url(); + void set_allocated_type_url(std::string *type_url); + +private: + const std::string &_internal_type_url() const; + inline PROTOBUF_ALWAYS_INLINE void _internal_set_type_url(const std::string &value); + std::string *_internal_mutable_type_url(); + +public: + // bytes value = 2; + void clear_value(); + const std::string &value() const; + template + void set_value(ArgT0 &&arg0, ArgT... args); + std::string *mutable_value(); + PROTOBUF_NODISCARD std::string *release_value(); + void set_allocated_value(std::string *value); + +private: + const std::string &_internal_value() const; + inline PROTOBUF_ALWAYS_INLINE void _internal_set_value(const std::string &value); + std::string *_internal_mutable_value(); + +public: + // @@protoc_insertion_point(class_scope:google.protobuf.Any) +private: + class _Internal; + + template + friend class ::PROTOBUF_NAMESPACE_ID::Arena::InternalHelper; + typedef void InternalArenaConstructable_; + typedef void DestructorSkippable_; + ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr type_url_; + ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr value_; + mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_; + ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata _any_metadata_; + friend struct ::TableStruct_google_2fprotobuf_2fany_2eproto; }; // =================================================================== - // =================================================================== #ifdef __GNUC__ - #pragma GCC diagnostic push - #pragma GCC diagnostic ignored "-Wstrict-aliasing" -#endif // __GNUC__ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wstrict-aliasing" +#endif // __GNUC__ // Any // string type_url = 1; inline void Any::clear_type_url() { - type_url_.ClearToEmpty(); + type_url_.ClearToEmpty(); } -inline const std::string& Any::type_url() const { - // @@protoc_insertion_point(field_get:google.protobuf.Any.type_url) - return _internal_type_url(); +inline const std::string &Any::type_url() const { + // @@protoc_insertion_point(field_get:google.protobuf.Any.type_url) + return _internal_type_url(); } template -inline PROTOBUF_ALWAYS_INLINE -void Any::set_type_url(ArgT0&& arg0, ArgT... args) { - - type_url_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, static_cast(arg0), args..., GetArenaForAllocation()); - // @@protoc_insertion_point(field_set:google.protobuf.Any.type_url) +inline PROTOBUF_ALWAYS_INLINE void Any::set_type_url(ArgT0 &&arg0, ArgT... args) { + + type_url_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault {}, static_cast(arg0), + args..., GetArenaForAllocation()); + // @@protoc_insertion_point(field_set:google.protobuf.Any.type_url) } -inline std::string* Any::mutable_type_url() { - std::string* _s = _internal_mutable_type_url(); - // @@protoc_insertion_point(field_mutable:google.protobuf.Any.type_url) - return _s; +inline std::string *Any::mutable_type_url() { + std::string *_s = _internal_mutable_type_url(); + // @@protoc_insertion_point(field_mutable:google.protobuf.Any.type_url) + return _s; } -inline const std::string& Any::_internal_type_url() const { - return type_url_.Get(); +inline const std::string &Any::_internal_type_url() const { + return type_url_.Get(); } -inline void Any::_internal_set_type_url(const std::string& value) { - - type_url_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, value, GetArenaForAllocation()); +inline void Any::_internal_set_type_url(const std::string &value) { + + type_url_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault {}, value, GetArenaForAllocation()); } -inline std::string* Any::_internal_mutable_type_url() { - - return type_url_.Mutable(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, GetArenaForAllocation()); +inline std::string *Any::_internal_mutable_type_url() { + + return type_url_.Mutable(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault {}, + GetArenaForAllocation()); } -inline std::string* Any::release_type_url() { - // @@protoc_insertion_point(field_release:google.protobuf.Any.type_url) - return type_url_.Release(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaForAllocation()); +inline std::string *Any::release_type_url() { + // @@protoc_insertion_point(field_release:google.protobuf.Any.type_url) + return type_url_.Release(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), + GetArenaForAllocation()); } -inline void Any::set_allocated_type_url(std::string* type_url) { - if (type_url != nullptr) { - - } else { - - } - type_url_.SetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), type_url, - GetArenaForAllocation()); +inline void Any::set_allocated_type_url(std::string *type_url) { + if (type_url != nullptr) { + + } else { + } + type_url_.SetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), type_url, + GetArenaForAllocation()); #ifdef PROTOBUF_FORCE_COPY_DEFAULT_STRING - if (type_url_.IsDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited())) { - type_url_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), "", GetArenaForAllocation()); - } + if (type_url_.IsDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited())) { + type_url_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), "", GetArenaForAllocation()); + } #endif // PROTOBUF_FORCE_COPY_DEFAULT_STRING - // @@protoc_insertion_point(field_set_allocated:google.protobuf.Any.type_url) + // @@protoc_insertion_point(field_set_allocated:google.protobuf.Any.type_url) } // bytes value = 2; inline void Any::clear_value() { - value_.ClearToEmpty(); + value_.ClearToEmpty(); } -inline const std::string& Any::value() const { - // @@protoc_insertion_point(field_get:google.protobuf.Any.value) - return _internal_value(); +inline const std::string &Any::value() const { + // @@protoc_insertion_point(field_get:google.protobuf.Any.value) + return _internal_value(); } template -inline PROTOBUF_ALWAYS_INLINE -void Any::set_value(ArgT0&& arg0, ArgT... args) { - - value_.SetBytes(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, static_cast(arg0), args..., GetArenaForAllocation()); - // @@protoc_insertion_point(field_set:google.protobuf.Any.value) +inline PROTOBUF_ALWAYS_INLINE void Any::set_value(ArgT0 &&arg0, ArgT... args) { + + value_.SetBytes(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault {}, static_cast(arg0), + args..., GetArenaForAllocation()); + // @@protoc_insertion_point(field_set:google.protobuf.Any.value) } -inline std::string* Any::mutable_value() { - std::string* _s = _internal_mutable_value(); - // @@protoc_insertion_point(field_mutable:google.protobuf.Any.value) - return _s; +inline std::string *Any::mutable_value() { + std::string *_s = _internal_mutable_value(); + // @@protoc_insertion_point(field_mutable:google.protobuf.Any.value) + return _s; } -inline const std::string& Any::_internal_value() const { - return value_.Get(); +inline const std::string &Any::_internal_value() const { + return value_.Get(); } -inline void Any::_internal_set_value(const std::string& value) { - - value_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, value, GetArenaForAllocation()); +inline void Any::_internal_set_value(const std::string &value) { + + value_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault {}, value, GetArenaForAllocation()); } -inline std::string* Any::_internal_mutable_value() { - - return value_.Mutable(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, GetArenaForAllocation()); +inline std::string *Any::_internal_mutable_value() { + + return value_.Mutable(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault {}, GetArenaForAllocation()); } -inline std::string* Any::release_value() { - // @@protoc_insertion_point(field_release:google.protobuf.Any.value) - return value_.Release(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaForAllocation()); +inline std::string *Any::release_value() { + // @@protoc_insertion_point(field_release:google.protobuf.Any.value) + return value_.Release(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaForAllocation()); } -inline void Any::set_allocated_value(std::string* value) { - if (value != nullptr) { - - } else { - - } - value_.SetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value, - GetArenaForAllocation()); +inline void Any::set_allocated_value(std::string *value) { + if (value != nullptr) { + + } else { + } + value_.SetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value, + GetArenaForAllocation()); #ifdef PROTOBUF_FORCE_COPY_DEFAULT_STRING - if (value_.IsDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited())) { - value_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), "", GetArenaForAllocation()); - } + if (value_.IsDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited())) { + value_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), "", GetArenaForAllocation()); + } #endif // PROTOBUF_FORCE_COPY_DEFAULT_STRING - // @@protoc_insertion_point(field_set_allocated:google.protobuf.Any.value) + // @@protoc_insertion_point(field_set_allocated:google.protobuf.Any.value) } #ifdef __GNUC__ - #pragma GCC diagnostic pop -#endif // __GNUC__ +#pragma GCC diagnostic pop +#endif // __GNUC__ // @@protoc_insertion_point(namespace_scope) @@ -388,4 +396,4 @@ PROTOBUF_NAMESPACE_CLOSE // @@protoc_insertion_point(global_scope) #include -#endif // GOOGLE_PROTOBUF_INCLUDED_GOOGLE_PROTOBUF_INCLUDED_google_2fprotobuf_2fany_2eproto +#endif // GOOGLE_PROTOBUF_INCLUDED_GOOGLE_PROTOBUF_INCLUDED_google_2fprotobuf_2fany_2eproto diff --git a/third_party/google/protobuf/any_lite.cc b/third_party/google/protobuf/any_lite.cc index a98559da140..a374feb858f 100644 --- a/third_party/google/protobuf/any_lite.cc +++ b/third_party/google/protobuf/any_lite.cc @@ -35,65 +35,60 @@ #include #include +namespace duckdb { namespace google { namespace protobuf { namespace internal { -std::string GetTypeUrl(StringPiece message_name, - StringPiece type_url_prefix) { - if (!type_url_prefix.empty() && - type_url_prefix[type_url_prefix.size() - 1] == '/') { - return StrCat(type_url_prefix, message_name); - } else { - return StrCat(type_url_prefix, "/", message_name); - } +std::string GetTypeUrl(StringPiece message_name, StringPiece type_url_prefix) { + if (!type_url_prefix.empty() && type_url_prefix[type_url_prefix.size() - 1] == '/') { + return StrCat(type_url_prefix, message_name); + } else { + return StrCat(type_url_prefix, "/", message_name); + } } const char kAnyFullTypeName[] = "google.protobuf.Any"; const char kTypeGoogleApisComPrefix[] = "type.googleapis.com/"; const char kTypeGoogleProdComPrefix[] = "type.googleprod.com/"; -bool AnyMetadata::InternalPackFrom(Arena* arena, const MessageLite& message, - StringPiece type_url_prefix, +bool AnyMetadata::InternalPackFrom(Arena *arena, const MessageLite &message, StringPiece type_url_prefix, StringPiece type_name) { - type_url_->Set(&::google::protobuf::internal::GetEmptyString(), - GetTypeUrl(type_name, type_url_prefix), arena); - return message.SerializeToString( - value_->Mutable(ArenaStringPtr::EmptyDefault{}, arena)); + type_url_->Set(&duckdb::google::protobuf::internal::GetEmptyString(), GetTypeUrl(type_name, type_url_prefix), + arena); + return message.SerializeToString(value_->Mutable(ArenaStringPtr::EmptyDefault {}, arena)); } -bool AnyMetadata::InternalUnpackTo(StringPiece type_name, - MessageLite* message) const { - if (!InternalIs(type_name)) { - return false; - } - return message->ParseFromString(value_->Get()); +bool AnyMetadata::InternalUnpackTo(StringPiece type_name, MessageLite *message) const { + if (!InternalIs(type_name)) { + return false; + } + return message->ParseFromString(value_->Get()); } bool AnyMetadata::InternalIs(StringPiece type_name) const { - StringPiece type_url = type_url_->Get(); - return type_url.size() >= type_name.size() + 1 && - type_url[type_url.size() - type_name.size() - 1] == '/' && - HasSuffixString(type_url, type_name); + StringPiece type_url = type_url_->Get(); + return type_url.size() >= type_name.size() + 1 && type_url[type_url.size() - type_name.size() - 1] == '/' && + HasSuffixString(type_url, type_name); } -bool ParseAnyTypeUrl(StringPiece type_url, std::string* url_prefix, - std::string* full_type_name) { - size_t pos = type_url.find_last_of('/'); - if (pos == std::string::npos || pos + 1 == type_url.size()) { - return false; - } - if (url_prefix) { - *url_prefix = std::string(type_url.substr(0, pos + 1)); - } - *full_type_name = std::string(type_url.substr(pos + 1)); - return true; +bool ParseAnyTypeUrl(StringPiece type_url, std::string *url_prefix, std::string *full_type_name) { + size_t pos = type_url.find_last_of('/'); + if (pos == std::string::npos || pos + 1 == type_url.size()) { + return false; + } + if (url_prefix) { + *url_prefix = std::string(type_url.substr(0, pos + 1)); + } + *full_type_name = std::string(type_url.substr(pos + 1)); + return true; } -bool ParseAnyTypeUrl(StringPiece type_url, std::string* full_type_name) { - return ParseAnyTypeUrl(type_url, nullptr, full_type_name); +bool ParseAnyTypeUrl(StringPiece type_url, std::string *full_type_name) { + return ParseAnyTypeUrl(type_url, nullptr, full_type_name); } -} // namespace internal -} // namespace protobuf -} // namespace google +} // namespace internal +} // namespace protobuf +} // namespace google +} // namespace duckdb \ No newline at end of file diff --git a/third_party/google/protobuf/arena.cc b/third_party/google/protobuf/arena.cc index 7624e0b2f3f..f0bffa673eb 100644 --- a/third_party/google/protobuf/arena.cc +++ b/third_party/google/protobuf/arena.cc @@ -45,467 +45,451 @@ #endif // ADDRESS_SANITIZER #include - +namespace duckdb { namespace google { namespace protobuf { namespace internal { -static SerialArena::Memory AllocateMemory(const AllocationPolicy* policy_ptr, - size_t last_size, size_t min_bytes) { - AllocationPolicy policy; // default policy - if (policy_ptr) policy = *policy_ptr; - size_t size; - if (last_size != 0) { - // Double the current block size, up to a limit. - auto max_size = policy.max_block_size; - size = std::min(2 * last_size, max_size); - } else { - size = policy.start_block_size; - } - // Verify that min_bytes + kBlockHeaderSize won't overflow. - GOOGLE_CHECK_LE(min_bytes, - std::numeric_limits::max() - SerialArena::kBlockHeaderSize); - size = std::max(size, SerialArena::kBlockHeaderSize + min_bytes); - - void* mem; - if (policy.block_alloc == nullptr) { - mem = ::operator new(size); - } else { - mem = policy.block_alloc(size); - } - return {mem, size}; +static SerialArena::Memory AllocateMemory(const AllocationPolicy *policy_ptr, size_t last_size, size_t min_bytes) { + AllocationPolicy policy; // default policy + if (policy_ptr) + policy = *policy_ptr; + size_t size; + if (last_size != 0) { + // Double the current block size, up to a limit. + auto max_size = policy.max_block_size; + size = std::min(2 * last_size, max_size); + } else { + size = policy.start_block_size; + } + // Verify that min_bytes + kBlockHeaderSize won't overflow. + GOOGLE_CHECK_LE(min_bytes, std::numeric_limits::max() - SerialArena::kBlockHeaderSize); + size = std::max(size, SerialArena::kBlockHeaderSize + min_bytes); + + void *mem; + if (policy.block_alloc == nullptr) { + mem = ::operator new(size); + } else { + mem = policy.block_alloc(size); + } + return {mem, size}; } class GetDeallocator { - public: - GetDeallocator(const AllocationPolicy* policy, size_t* space_allocated) - : dealloc_(policy ? policy->block_dealloc : nullptr), - space_allocated_(space_allocated) {} +public: + GetDeallocator(const AllocationPolicy *policy, size_t *space_allocated) + : dealloc_(policy ? policy->block_dealloc : nullptr), space_allocated_(space_allocated) { + } - void operator()(SerialArena::Memory mem) const { + void operator()(SerialArena::Memory mem) const { #ifdef ADDRESS_SANITIZER - // This memory was provided by the underlying allocator as unpoisoned, - // so return it in an unpoisoned state. - ASAN_UNPOISON_MEMORY_REGION(mem.ptr, mem.size); -#endif // ADDRESS_SANITIZER - if (dealloc_) { - dealloc_(mem.ptr, mem.size); - } else { + // This memory was provided by the underlying allocator as unpoisoned, + // so return it in an unpoisoned state. + ASAN_UNPOISON_MEMORY_REGION(mem.ptr, mem.size); +#endif // ADDRESS_SANITIZER + if (dealloc_) { + dealloc_(mem.ptr, mem.size); + } else { #if defined(__GXX_DELETE_WITH_SIZE__) || defined(__cpp_sized_deallocation) - ::operator delete(mem.ptr, mem.size); + ::operator delete(mem.ptr, mem.size); #else - ::operator delete(mem.ptr); + ::operator delete(mem.ptr); #endif - } - *space_allocated_ += mem.size; - } + } + *space_allocated_ += mem.size; + } - private: - void (*dealloc_)(void*, size_t); - size_t* space_allocated_; +private: + void (*dealloc_)(void *, size_t); + size_t *space_allocated_; }; -SerialArena::SerialArena(Block* b, void* owner) : space_allocated_(b->size) { - owner_ = owner; - head_ = b; - ptr_ = b->Pointer(kBlockHeaderSize + ThreadSafeArena::kSerialArenaSize); - limit_ = b->Pointer(b->size & static_cast(-8)); +SerialArena::SerialArena(Block *b, void *owner) : space_allocated_(b->size) { + owner_ = owner; + head_ = b; + ptr_ = b->Pointer(kBlockHeaderSize + ThreadSafeArena::kSerialArenaSize); + limit_ = b->Pointer(b->size & static_cast(-8)); } -SerialArena* SerialArena::New(Memory mem, void* owner) { - GOOGLE_DCHECK_LE(kBlockHeaderSize + ThreadSafeArena::kSerialArenaSize, mem.size); +SerialArena *SerialArena::New(Memory mem, void *owner) { + GOOGLE_DCHECK_LE(kBlockHeaderSize + ThreadSafeArena::kSerialArenaSize, mem.size); - auto b = new (mem.ptr) Block{nullptr, mem.size}; - return new (b->Pointer(kBlockHeaderSize)) SerialArena(b, owner); + auto b = new (mem.ptr) Block {nullptr, mem.size}; + return new (b->Pointer(kBlockHeaderSize)) SerialArena(b, owner); } template SerialArena::Memory SerialArena::Free(Deallocator deallocator) { - Block* b = head_; - Memory mem = {b, b->size}; - while (b->next) { - b = b->next; // We must first advance before deleting this block - deallocator(mem); - mem = {b, b->size}; - } - return mem; + Block *b = head_; + Memory mem = {b, b->size}; + while (b->next) { + b = b->next; // We must first advance before deleting this block + deallocator(mem); + mem = {b, b->size}; + } + return mem; } PROTOBUF_NOINLINE -std::pair -SerialArena::AllocateAlignedWithCleanupFallback( - size_t n, const AllocationPolicy* policy) { - AllocateNewBlock(n + kCleanupSize, policy); - return AllocateFromExistingWithCleanupFallback(n); +std::pair +SerialArena::AllocateAlignedWithCleanupFallback(size_t n, const AllocationPolicy *policy) { + AllocateNewBlock(n + kCleanupSize, policy); + return AllocateFromExistingWithCleanupFallback(n); } PROTOBUF_NOINLINE -void* SerialArena::AllocateAlignedFallback(size_t n, - const AllocationPolicy* policy) { - AllocateNewBlock(n, policy); - return AllocateFromExisting(n); +void *SerialArena::AllocateAlignedFallback(size_t n, const AllocationPolicy *policy) { + AllocateNewBlock(n, policy); + return AllocateFromExisting(n); } -void SerialArena::AllocateNewBlock(size_t n, const AllocationPolicy* policy) { - // Sync limit to block - head_->start = reinterpret_cast(limit_); +void SerialArena::AllocateNewBlock(size_t n, const AllocationPolicy *policy) { + // Sync limit to block + head_->start = reinterpret_cast(limit_); - // Record how much used in this block. - space_used_ += ptr_ - head_->Pointer(kBlockHeaderSize); + // Record how much used in this block. + space_used_ += ptr_ - head_->Pointer(kBlockHeaderSize); - auto mem = AllocateMemory(policy, head_->size, n); - // We don't want to emit an expensive RMW instruction that requires - // exclusive access to a cacheline. Hence we write it in terms of a - // regular add. - auto relaxed = std::memory_order_relaxed; - space_allocated_.store(space_allocated_.load(relaxed) + mem.size, relaxed); - head_ = new (mem.ptr) Block{head_, mem.size}; - ptr_ = head_->Pointer(kBlockHeaderSize); - limit_ = head_->Pointer(head_->size); + auto mem = AllocateMemory(policy, head_->size, n); + // We don't want to emit an expensive RMW instruction that requires + // exclusive access to a cacheline. Hence we write it in terms of a + // regular add. + auto relaxed = std::memory_order_relaxed; + space_allocated_.store(space_allocated_.load(relaxed) + mem.size, relaxed); + head_ = new (mem.ptr) Block {head_, mem.size}; + ptr_ = head_->Pointer(kBlockHeaderSize); + limit_ = head_->Pointer(head_->size); #ifdef ADDRESS_SANITIZER - ASAN_POISON_MEMORY_REGION(ptr_, limit_ - ptr_); -#endif // ADDRESS_SANITIZER + ASAN_POISON_MEMORY_REGION(ptr_, limit_ - ptr_); +#endif // ADDRESS_SANITIZER } uint64_t SerialArena::SpaceUsed() const { - uint64_t space_used = ptr_ - head_->Pointer(kBlockHeaderSize); - space_used += space_used_; - // Remove the overhead of the SerialArena itself. - space_used -= ThreadSafeArena::kSerialArenaSize; - return space_used; + uint64_t space_used = ptr_ - head_->Pointer(kBlockHeaderSize); + space_used += space_used_; + // Remove the overhead of the SerialArena itself. + space_used -= ThreadSafeArena::kSerialArenaSize; + return space_used; } void SerialArena::CleanupList() { - Block* b = head_; - b->start = reinterpret_cast(limit_); - do { - auto* limit = reinterpret_cast( - b->Pointer(b->size & static_cast(-8))); - auto it = b->start; - auto num = limit - it; - if (num > 0) { - for (; it < limit; it++) { - it->cleanup(it->elem); - } - } - b = b->next; - } while (b); + Block *b = head_; + b->start = reinterpret_cast(limit_); + do { + auto *limit = reinterpret_cast(b->Pointer(b->size & static_cast(-8))); + auto it = b->start; + auto num = limit - it; + if (num > 0) { + for (; it < limit; it++) { + it->cleanup(it->elem); + } + } + b = b->next; + } while (b); } - -ThreadSafeArena::CacheAlignedLifecycleIdGenerator - ThreadSafeArena::lifecycle_id_generator_; +ThreadSafeArena::CacheAlignedLifecycleIdGenerator ThreadSafeArena::lifecycle_id_generator_; #if defined(GOOGLE_PROTOBUF_NO_THREADLOCAL) -ThreadSafeArena::ThreadCache& ThreadSafeArena::thread_cache() { - static internal::ThreadLocalStorage* thread_cache_ = - new internal::ThreadLocalStorage(); - return *thread_cache_->Get(); +ThreadSafeArena::ThreadCache &ThreadSafeArena::thread_cache() { + static internal::ThreadLocalStorage *thread_cache_ = new internal::ThreadLocalStorage(); + return *thread_cache_->Get(); } #elif defined(PROTOBUF_USE_DLLS) -ThreadSafeArena::ThreadCache& ThreadSafeArena::thread_cache() { - static PROTOBUF_THREAD_LOCAL ThreadCache thread_cache_ = { - 0, static_cast(-1), nullptr}; - return thread_cache_; +ThreadSafeArena::ThreadCache &ThreadSafeArena::thread_cache() { + static PROTOBUF_THREAD_LOCAL ThreadCache thread_cache_ = {0, static_cast(-1), nullptr}; + return thread_cache_; } #else -PROTOBUF_THREAD_LOCAL ThreadSafeArena::ThreadCache - ThreadSafeArena::thread_cache_ = {0, static_cast(-1), - nullptr}; +PROTOBUF_THREAD_LOCAL ThreadSafeArena::ThreadCache ThreadSafeArena::thread_cache_ = { + 0, static_cast(-1), nullptr}; #endif -void ThreadSafeArena::InitializeFrom(void* mem, size_t size) { - GOOGLE_DCHECK_EQ(reinterpret_cast(mem) & 7, 0u); - GOOGLE_DCHECK(!AllocPolicy()); // Reset should call InitializeWithPolicy instead. - Init(); +void ThreadSafeArena::InitializeFrom(void *mem, size_t size) { + GOOGLE_DCHECK_EQ(reinterpret_cast(mem) & 7, 0u); + GOOGLE_DCHECK(!AllocPolicy()); // Reset should call InitializeWithPolicy instead. + Init(); - // Ignore initial block if it is too small. - if (mem != nullptr && size >= kBlockHeaderSize + kSerialArenaSize) { - alloc_policy_.set_is_user_owned_initial_block(true); - SetInitialBlock(mem, size); - } + // Ignore initial block if it is too small. + if (mem != nullptr && size >= kBlockHeaderSize + kSerialArenaSize) { + alloc_policy_.set_is_user_owned_initial_block(true); + SetInitialBlock(mem, size); + } } -void ThreadSafeArena::InitializeWithPolicy(void* mem, size_t size, - AllocationPolicy policy) { +void ThreadSafeArena::InitializeWithPolicy(void *mem, size_t size, AllocationPolicy policy) { #ifndef NDEBUG - const uint64_t old_alloc_policy = alloc_policy_.get_raw(); - // If there was a policy (e.g., in Reset()), make sure flags were preserved. + const uint64_t old_alloc_policy = alloc_policy_.get_raw(); + // If there was a policy (e.g., in Reset()), make sure flags were preserved. #define GOOGLE_DCHECK_POLICY_FLAGS_() \ if (old_alloc_policy > 3) \ GOOGLE_CHECK_EQ(old_alloc_policy & 3, alloc_policy_.get_raw() & 3) #else #define GOOGLE_DCHECK_POLICY_FLAGS_() -#endif // NDEBUG - - if (policy.IsDefault()) { - // Legacy code doesn't use the API above, but provides the initial block - // through ArenaOptions. I suspect most do not touch the allocation - // policy parameters. - InitializeFrom(mem, size); - GOOGLE_DCHECK_POLICY_FLAGS_(); - return; - } - GOOGLE_DCHECK_EQ(reinterpret_cast(mem) & 7, 0u); - Init(); - - // Ignore initial block if it is too small. We include an optional - // AllocationPolicy in this check, so that this can be allocated on the - // first block. - constexpr size_t kAPSize = internal::AlignUpTo8(sizeof(AllocationPolicy)); - constexpr size_t kMinimumSize = kBlockHeaderSize + kSerialArenaSize + kAPSize; - - // The value for alloc_policy_ stores whether or not allocations should be - // recorded. - alloc_policy_.set_should_record_allocs( - policy.metrics_collector != nullptr && - policy.metrics_collector->RecordAllocs()); - // Make sure we have an initial block to store the AllocationPolicy. - if (mem != nullptr && size >= kMinimumSize) { - alloc_policy_.set_is_user_owned_initial_block(true); - } else { - auto tmp = AllocateMemory(&policy, 0, kMinimumSize); - mem = tmp.ptr; - size = tmp.size; - } - SetInitialBlock(mem, size); - - auto sa = threads_.load(std::memory_order_relaxed); - // We ensured enough space so this cannot fail. - void* p; - if (!sa || !sa->MaybeAllocateAligned(kAPSize, &p)) { - GOOGLE_LOG(FATAL) << "MaybeAllocateAligned cannot fail here."; - return; - } - new (p) AllocationPolicy{policy}; - // Low bits store flags, so they mustn't be overwritten. - GOOGLE_DCHECK_EQ(0, reinterpret_cast(p) & 3); - alloc_policy_.set_policy(reinterpret_cast(p)); - GOOGLE_DCHECK_POLICY_FLAGS_(); +#endif // NDEBUG + + if (policy.IsDefault()) { + // Legacy code doesn't use the API above, but provides the initial block + // through ArenaOptions. I suspect most do not touch the allocation + // policy parameters. + InitializeFrom(mem, size); + GOOGLE_DCHECK_POLICY_FLAGS_(); + return; + } + GOOGLE_DCHECK_EQ(reinterpret_cast(mem) & 7, 0u); + Init(); + + // Ignore initial block if it is too small. We include an optional + // AllocationPolicy in this check, so that this can be allocated on the + // first block. + constexpr size_t kAPSize = internal::AlignUpTo8(sizeof(AllocationPolicy)); + constexpr size_t kMinimumSize = kBlockHeaderSize + kSerialArenaSize + kAPSize; + + // The value for alloc_policy_ stores whether or not allocations should be + // recorded. + alloc_policy_.set_should_record_allocs(policy.metrics_collector != nullptr && + policy.metrics_collector->RecordAllocs()); + // Make sure we have an initial block to store the AllocationPolicy. + if (mem != nullptr && size >= kMinimumSize) { + alloc_policy_.set_is_user_owned_initial_block(true); + } else { + auto tmp = AllocateMemory(&policy, 0, kMinimumSize); + mem = tmp.ptr; + size = tmp.size; + } + SetInitialBlock(mem, size); + + auto sa = threads_.load(std::memory_order_relaxed); + // We ensured enough space so this cannot fail. + void *p; + if (!sa || !sa->MaybeAllocateAligned(kAPSize, &p)) { + GOOGLE_LOG(FATAL) << "MaybeAllocateAligned cannot fail here."; + return; + } + new (p) AllocationPolicy {policy}; + // Low bits store flags, so they mustn't be overwritten. + GOOGLE_DCHECK_EQ(0, reinterpret_cast(p) & 3); + alloc_policy_.set_policy(reinterpret_cast(p)); + GOOGLE_DCHECK_POLICY_FLAGS_(); #undef GOOGLE_DCHECK_POLICY_FLAGS_ } void ThreadSafeArena::Init() { #ifndef NDEBUG - const bool was_message_owned = IsMessageOwned(); -#endif // NDEBUG - ThreadCache& tc = thread_cache(); - auto id = tc.next_lifecycle_id; - // We increment lifecycle_id's by multiples of two so we can use bit 0 as - // a tag. - constexpr uint64_t kDelta = 2; - constexpr uint64_t kInc = ThreadCache::kPerThreadIds * kDelta; - if (PROTOBUF_PREDICT_FALSE((id & (kInc - 1)) == 0)) { - constexpr auto relaxed = std::memory_order_relaxed; - // On platforms that don't support uint64_t atomics we can certainly not - // afford to increment by large intervals and expect uniqueness due to - // wrapping, hence we only add by 1. - id = lifecycle_id_generator_.id.fetch_add(1, relaxed) * kInc; - } - tc.next_lifecycle_id = id + kDelta; - // Message ownership is stored in tag_and_id_, and is set in the constructor. - // This flag bit must be preserved, even across calls to Reset(). - tag_and_id_ = id | (tag_and_id_ & kMessageOwnedArena); - hint_.store(nullptr, std::memory_order_relaxed); - threads_.store(nullptr, std::memory_order_relaxed); + const bool was_message_owned = IsMessageOwned(); +#endif // NDEBUG + ThreadCache &tc = thread_cache(); + auto id = tc.next_lifecycle_id; + // We increment lifecycle_id's by multiples of two so we can use bit 0 as + // a tag. + constexpr uint64_t kDelta = 2; + constexpr uint64_t kInc = ThreadCache::kPerThreadIds * kDelta; + if (PROTOBUF_PREDICT_FALSE((id & (kInc - 1)) == 0)) { + constexpr auto relaxed = std::memory_order_relaxed; + // On platforms that don't support uint64_t atomics we can certainly not + // afford to increment by large intervals and expect uniqueness due to + // wrapping, hence we only add by 1. + id = lifecycle_id_generator_.id.fetch_add(1, relaxed) * kInc; + } + tc.next_lifecycle_id = id + kDelta; + // Message ownership is stored in tag_and_id_, and is set in the constructor. + // This flag bit must be preserved, even across calls to Reset(). + tag_and_id_ = id | (tag_and_id_ & kMessageOwnedArena); + hint_.store(nullptr, std::memory_order_relaxed); + threads_.store(nullptr, std::memory_order_relaxed); #ifndef NDEBUG - GOOGLE_CHECK_EQ(was_message_owned, IsMessageOwned()); -#endif // NDEBUG + GOOGLE_CHECK_EQ(was_message_owned, IsMessageOwned()); +#endif // NDEBUG } -void ThreadSafeArena::SetInitialBlock(void* mem, size_t size) { - SerialArena* serial = SerialArena::New({mem, size}, &thread_cache()); - serial->set_next(NULL); - threads_.store(serial, std::memory_order_relaxed); - CacheSerialArena(serial); +void ThreadSafeArena::SetInitialBlock(void *mem, size_t size) { + SerialArena *serial = SerialArena::New({mem, size}, &thread_cache()); + serial->set_next(NULL); + threads_.store(serial, std::memory_order_relaxed); + CacheSerialArena(serial); } ThreadSafeArena::~ThreadSafeArena() { - // Have to do this in a first pass, because some of the destructors might - // refer to memory in other blocks. - CleanupList(); + // Have to do this in a first pass, because some of the destructors might + // refer to memory in other blocks. + CleanupList(); - size_t space_allocated = 0; - auto mem = Free(&space_allocated); + size_t space_allocated = 0; + auto mem = Free(&space_allocated); - // Policy is about to get deleted. - auto* p = alloc_policy_.get(); - ArenaMetricsCollector* collector = p ? p->metrics_collector : nullptr; + // Policy is about to get deleted. + auto *p = alloc_policy_.get(); + ArenaMetricsCollector *collector = p ? p->metrics_collector : nullptr; - if (alloc_policy_.is_user_owned_initial_block()) { - space_allocated += mem.size; - } else { - GetDeallocator(alloc_policy_.get(), &space_allocated)(mem); - } + if (alloc_policy_.is_user_owned_initial_block()) { + space_allocated += mem.size; + } else { + GetDeallocator(alloc_policy_.get(), &space_allocated)(mem); + } - if (collector) collector->OnDestroy(space_allocated); + if (collector) + collector->OnDestroy(space_allocated); } -SerialArena::Memory ThreadSafeArena::Free(size_t* space_allocated) { - SerialArena::Memory mem = {nullptr, 0}; - auto deallocator = GetDeallocator(alloc_policy_.get(), space_allocated); - PerSerialArena([deallocator, &mem](SerialArena* a) { - if (mem.ptr) deallocator(mem); - mem = a->Free(deallocator); - }); - return mem; +SerialArena::Memory ThreadSafeArena::Free(size_t *space_allocated) { + SerialArena::Memory mem = {nullptr, 0}; + auto deallocator = GetDeallocator(alloc_policy_.get(), space_allocated); + PerSerialArena([deallocator, &mem](SerialArena *a) { + if (mem.ptr) + deallocator(mem); + mem = a->Free(deallocator); + }); + return mem; } uint64_t ThreadSafeArena::Reset() { - // Have to do this in a first pass, because some of the destructors might - // refer to memory in other blocks. - CleanupList(); - - // Discard all blocks except the special block (if present). - size_t space_allocated = 0; - auto mem = Free(&space_allocated); - - AllocationPolicy* policy = alloc_policy_.get(); - if (policy) { - auto saved_policy = *policy; - if (alloc_policy_.is_user_owned_initial_block()) { - space_allocated += mem.size; - } else { - GetDeallocator(alloc_policy_.get(), &space_allocated)(mem); - mem.ptr = nullptr; - mem.size = 0; - } - ArenaMetricsCollector* collector = saved_policy.metrics_collector; - if (collector) collector->OnReset(space_allocated); - InitializeWithPolicy(mem.ptr, mem.size, saved_policy); - } else { - GOOGLE_DCHECK(!alloc_policy_.should_record_allocs()); - // Nullptr policy - if (alloc_policy_.is_user_owned_initial_block()) { - space_allocated += mem.size; - InitializeFrom(mem.ptr, mem.size); - } else { - GetDeallocator(alloc_policy_.get(), &space_allocated)(mem); - Init(); - } - } - - return space_allocated; + // Have to do this in a first pass, because some of the destructors might + // refer to memory in other blocks. + CleanupList(); + + // Discard all blocks except the special block (if present). + size_t space_allocated = 0; + auto mem = Free(&space_allocated); + + AllocationPolicy *policy = alloc_policy_.get(); + if (policy) { + auto saved_policy = *policy; + if (alloc_policy_.is_user_owned_initial_block()) { + space_allocated += mem.size; + } else { + GetDeallocator(alloc_policy_.get(), &space_allocated)(mem); + mem.ptr = nullptr; + mem.size = 0; + } + ArenaMetricsCollector *collector = saved_policy.metrics_collector; + if (collector) + collector->OnReset(space_allocated); + InitializeWithPolicy(mem.ptr, mem.size, saved_policy); + } else { + GOOGLE_DCHECK(!alloc_policy_.should_record_allocs()); + // Nullptr policy + if (alloc_policy_.is_user_owned_initial_block()) { + space_allocated += mem.size; + InitializeFrom(mem.ptr, mem.size); + } else { + GetDeallocator(alloc_policy_.get(), &space_allocated)(mem); + Init(); + } + } + + return space_allocated; } -std::pair -ThreadSafeArena::AllocateAlignedWithCleanup(size_t n, - const std::type_info* type) { - SerialArena* arena; - if (PROTOBUF_PREDICT_TRUE(!alloc_policy_.should_record_allocs() && - GetSerialArenaFast(&arena))) { - return arena->AllocateAlignedWithCleanup(n, alloc_policy_.get()); - } else { - return AllocateAlignedWithCleanupFallback(n, type); - } +std::pair ThreadSafeArena::AllocateAlignedWithCleanup(size_t n, + const std::type_info *type) { + SerialArena *arena; + if (PROTOBUF_PREDICT_TRUE(!alloc_policy_.should_record_allocs() && GetSerialArenaFast(&arena))) { + return arena->AllocateAlignedWithCleanup(n, alloc_policy_.get()); + } else { + return AllocateAlignedWithCleanupFallback(n, type); + } } -void ThreadSafeArena::AddCleanup(void* elem, void (*cleanup)(void*)) { - SerialArena* arena; - if (PROTOBUF_PREDICT_FALSE(!GetSerialArenaFast(&arena))) { - arena = GetSerialArenaFallback(&thread_cache()); - } - arena->AddCleanup(elem, cleanup, AllocPolicy()); +void ThreadSafeArena::AddCleanup(void *elem, void (*cleanup)(void *)) { + SerialArena *arena; + if (PROTOBUF_PREDICT_FALSE(!GetSerialArenaFast(&arena))) { + arena = GetSerialArenaFallback(&thread_cache()); + } + arena->AddCleanup(elem, cleanup, AllocPolicy()); } PROTOBUF_NOINLINE -void* ThreadSafeArena::AllocateAlignedFallback(size_t n, - const std::type_info* type) { - if (alloc_policy_.should_record_allocs()) { - alloc_policy_.RecordAlloc(type, n); - SerialArena* arena; - if (PROTOBUF_PREDICT_TRUE(GetSerialArenaFast(&arena))) { - return arena->AllocateAligned(n, alloc_policy_.get()); - } - } - return GetSerialArenaFallback(&thread_cache()) - ->AllocateAligned(n, alloc_policy_.get()); +void *ThreadSafeArena::AllocateAlignedFallback(size_t n, const std::type_info *type) { + if (alloc_policy_.should_record_allocs()) { + alloc_policy_.RecordAlloc(type, n); + SerialArena *arena; + if (PROTOBUF_PREDICT_TRUE(GetSerialArenaFast(&arena))) { + return arena->AllocateAligned(n, alloc_policy_.get()); + } + } + return GetSerialArenaFallback(&thread_cache())->AllocateAligned(n, alloc_policy_.get()); } PROTOBUF_NOINLINE -std::pair -ThreadSafeArena::AllocateAlignedWithCleanupFallback( - size_t n, const std::type_info* type) { - if (alloc_policy_.should_record_allocs()) { - alloc_policy_.RecordAlloc(type, n); - SerialArena* arena; - if (GetSerialArenaFast(&arena)) { - return arena->AllocateAlignedWithCleanup(n, alloc_policy_.get()); - } - } - return GetSerialArenaFallback(&thread_cache()) - ->AllocateAlignedWithCleanup(n, alloc_policy_.get()); +std::pair +ThreadSafeArena::AllocateAlignedWithCleanupFallback(size_t n, const std::type_info *type) { + if (alloc_policy_.should_record_allocs()) { + alloc_policy_.RecordAlloc(type, n); + SerialArena *arena; + if (GetSerialArenaFast(&arena)) { + return arena->AllocateAlignedWithCleanup(n, alloc_policy_.get()); + } + } + return GetSerialArenaFallback(&thread_cache())->AllocateAlignedWithCleanup(n, alloc_policy_.get()); } uint64_t ThreadSafeArena::SpaceAllocated() const { - SerialArena* serial = threads_.load(std::memory_order_acquire); - uint64_t res = 0; - for (; serial; serial = serial->next()) { - res += serial->SpaceAllocated(); - } - return res; + SerialArena *serial = threads_.load(std::memory_order_acquire); + uint64_t res = 0; + for (; serial; serial = serial->next()) { + res += serial->SpaceAllocated(); + } + return res; } uint64_t ThreadSafeArena::SpaceUsed() const { - SerialArena* serial = threads_.load(std::memory_order_acquire); - uint64_t space_used = 0; - for (; serial; serial = serial->next()) { - space_used += serial->SpaceUsed(); - } - return space_used - (alloc_policy_.get() ? sizeof(AllocationPolicy) : 0); + SerialArena *serial = threads_.load(std::memory_order_acquire); + uint64_t space_used = 0; + for (; serial; serial = serial->next()) { + space_used += serial->SpaceUsed(); + } + return space_used - (alloc_policy_.get() ? sizeof(AllocationPolicy) : 0); } void ThreadSafeArena::CleanupList() { - PerSerialArena([](SerialArena* a) { a->CleanupList(); }); + PerSerialArena([](SerialArena *a) { a->CleanupList(); }); } PROTOBUF_NOINLINE -SerialArena* ThreadSafeArena::GetSerialArenaFallback(void* me) { - // Look for this SerialArena in our linked list. - SerialArena* serial = threads_.load(std::memory_order_acquire); - for (; serial; serial = serial->next()) { - if (serial->owner() == me) { - break; - } - } - - if (!serial) { - // This thread doesn't have any SerialArena, which also means it doesn't - // have any blocks yet. So we'll allocate its first block now. - serial = SerialArena::New( - AllocateMemory(alloc_policy_.get(), 0, kSerialArenaSize), me); - - SerialArena* head = threads_.load(std::memory_order_relaxed); - do { - serial->set_next(head); - } while (!threads_.compare_exchange_weak( - head, serial, std::memory_order_release, std::memory_order_relaxed)); - } - - CacheSerialArena(serial); - return serial; +SerialArena *ThreadSafeArena::GetSerialArenaFallback(void *me) { + // Look for this SerialArena in our linked list. + SerialArena *serial = threads_.load(std::memory_order_acquire); + for (; serial; serial = serial->next()) { + if (serial->owner() == me) { + break; + } + } + + if (!serial) { + // This thread doesn't have any SerialArena, which also means it doesn't + // have any blocks yet. So we'll allocate its first block now. + serial = SerialArena::New(AllocateMemory(alloc_policy_.get(), 0, kSerialArenaSize), me); + + SerialArena *head = threads_.load(std::memory_order_relaxed); + do { + serial->set_next(head); + } while (!threads_.compare_exchange_weak(head, serial, std::memory_order_release, std::memory_order_relaxed)); + } + + CacheSerialArena(serial); + return serial; } -} // namespace internal +} // namespace internal PROTOBUF_FUNC_ALIGN(32) -void* Arena::AllocateAlignedNoHook(size_t n) { - return impl_.AllocateAligned(n, nullptr); +void *Arena::AllocateAlignedNoHook(size_t n) { + return impl_.AllocateAligned(n, nullptr); } PROTOBUF_FUNC_ALIGN(32) -void* Arena::AllocateAlignedWithHook(size_t n, const std::type_info* type) { - return impl_.AllocateAligned(n, type); +void *Arena::AllocateAlignedWithHook(size_t n, const std::type_info *type) { + return impl_.AllocateAligned(n, type); } PROTOBUF_FUNC_ALIGN(32) -std::pair -Arena::AllocateAlignedWithCleanup(size_t n, const std::type_info* type) { - return impl_.AllocateAlignedWithCleanup(n, type); +std::pair Arena::AllocateAlignedWithCleanup(size_t n, + const std::type_info *type) { + return impl_.AllocateAlignedWithCleanup(n, type); } -} // namespace protobuf -} // namespace google - +} // namespace protobuf +} // namespace google +} // namespace duckdb #include diff --git a/third_party/google/protobuf/arena.h b/third_party/google/protobuf/arena.h index 6dd6467f58b..324a8f83a7c 100644 --- a/third_party/google/protobuf/arena.h +++ b/third_party/google/protobuf/arena.h @@ -60,131 +60,126 @@ using type_info = ::type_info; #ifdef SWIG #error "You cannot SWIG proto headers" #endif - +namespace duckdb { namespace google { namespace protobuf { -struct ArenaOptions; // defined below -class Arena; // defined below -class Message; // defined in message.h +struct ArenaOptions; // defined below +class Arena; // defined below +class Message; // defined in message.h class MessageLite; template class Map; namespace arena_metrics { -void EnableArenaMetrics(ArenaOptions* options); +void EnableArenaMetrics(ArenaOptions *options); -} // namespace arena_metrics +} // namespace arena_metrics namespace TestUtil { -class ReflectionTester; // defined in test_util.h -} // namespace TestUtil +class ReflectionTester; // defined in test_util.h +} // namespace TestUtil namespace internal { -struct ArenaStringPtr; // defined in arenastring.h -class InlinedStringField; // defined in inlined_string_field.h -class LazyField; // defined in lazy_field.h -class EpsCopyInputStream; // defined in parse_context.h +struct ArenaStringPtr; // defined in arenastring.h +class InlinedStringField; // defined in inlined_string_field.h +class LazyField; // defined in lazy_field.h +class EpsCopyInputStream; // defined in parse_context.h template -class GenericTypeHandler; // defined in repeated_field.h +class GenericTypeHandler; // defined in repeated_field.h -inline PROTOBUF_ALWAYS_INLINE -void* AlignTo(void* ptr, size_t align) { - return reinterpret_cast( - (reinterpret_cast(ptr) + align - 1) & (~align + 1)); +inline PROTOBUF_ALWAYS_INLINE void *AlignTo(void *ptr, size_t align) { + return reinterpret_cast((reinterpret_cast(ptr) + align - 1) & (~align + 1)); } // Templated cleanup methods. template -void arena_destruct_object(void* object) { - reinterpret_cast(object)->~T(); +void arena_destruct_object(void *object) { + reinterpret_cast(object)->~T(); } template struct ObjectDestructor { - constexpr static void (*destructor)(void*) = &arena_destruct_object; + constexpr static void (*destructor)(void *) = &arena_destruct_object; }; template struct ObjectDestructor { - constexpr static void (*destructor)(void*) = nullptr; + constexpr static void (*destructor)(void *) = nullptr; }; template -void arena_delete_object(void* object) { - delete reinterpret_cast(object); +void arena_delete_object(void *object) { + delete reinterpret_cast(object); } -} // namespace internal +} // namespace internal // ArenaOptions provides optional additional parameters to arena construction // that control its block-allocation behavior. struct ArenaOptions { - // This defines the size of the first block requested from the system malloc. - // Subsequent block sizes will increase in a geometric series up to a maximum. - size_t start_block_size; - - // This defines the maximum block size requested from system malloc (unless an - // individual arena allocation request occurs with a size larger than this - // maximum). Requested block sizes increase up to this value, then remain - // here. - size_t max_block_size; - - // An initial block of memory for the arena to use, or NULL for none. If - // provided, the block must live at least as long as the arena itself. The - // creator of the Arena retains ownership of the block after the Arena is - // destroyed. - char* initial_block; - - // The size of the initial block, if provided. - size_t initial_block_size; - - // A function pointer to an alloc method that returns memory blocks of size - // requested. By default, it contains a ptr to the malloc function. - // - // NOTE: block_alloc and dealloc functions are expected to behave like - // malloc and free, including Asan poisoning. - void* (*block_alloc)(size_t); - // A function pointer to a dealloc method that takes ownership of the blocks - // from the arena. By default, it contains a ptr to a wrapper function that - // calls free. - void (*block_dealloc)(void*, size_t); - - ArenaOptions() - : start_block_size(internal::AllocationPolicy::kDefaultStartBlockSize), - max_block_size(internal::AllocationPolicy::kDefaultMaxBlockSize), - initial_block(NULL), - initial_block_size(0), - block_alloc(nullptr), - block_dealloc(nullptr), - make_metrics_collector(nullptr) {} - - private: - // If make_metrics_collector is not nullptr, it will be called at Arena init - // time. It may return a pointer to a collector instance that will be notified - // of interesting events related to the arena. - internal::ArenaMetricsCollector* (*make_metrics_collector)(); - - internal::ArenaMetricsCollector* MetricsCollector() const { - return make_metrics_collector ? (*make_metrics_collector)() : nullptr; - } - - internal::AllocationPolicy AllocationPolicy() const { - internal::AllocationPolicy res; - res.start_block_size = start_block_size; - res.max_block_size = max_block_size; - res.block_alloc = block_alloc; - res.block_dealloc = block_dealloc; - res.metrics_collector = MetricsCollector(); - return res; - } - - friend void arena_metrics::EnableArenaMetrics(ArenaOptions*); - - friend class Arena; - friend class ArenaOptionsTestFriend; + // This defines the size of the first block requested from the system malloc. + // Subsequent block sizes will increase in a geometric series up to a maximum. + size_t start_block_size; + + // This defines the maximum block size requested from system malloc (unless an + // individual arena allocation request occurs with a size larger than this + // maximum). Requested block sizes increase up to this value, then remain + // here. + size_t max_block_size; + + // An initial block of memory for the arena to use, or NULL for none. If + // provided, the block must live at least as long as the arena itself. The + // creator of the Arena retains ownership of the block after the Arena is + // destroyed. + char *initial_block; + + // The size of the initial block, if provided. + size_t initial_block_size; + + // A function pointer to an alloc method that returns memory blocks of size + // requested. By default, it contains a ptr to the malloc function. + // + // NOTE: block_alloc and dealloc functions are expected to behave like + // malloc and free, including Asan poisoning. + void *(*block_alloc)(size_t); + // A function pointer to a dealloc method that takes ownership of the blocks + // from the arena. By default, it contains a ptr to a wrapper function that + // calls free. + void (*block_dealloc)(void *, size_t); + + ArenaOptions() + : start_block_size(internal::AllocationPolicy::kDefaultStartBlockSize), + max_block_size(internal::AllocationPolicy::kDefaultMaxBlockSize), initial_block(NULL), initial_block_size(0), + block_alloc(nullptr), block_dealloc(nullptr), make_metrics_collector(nullptr) { + } + +private: + // If make_metrics_collector is not nullptr, it will be called at Arena init + // time. It may return a pointer to a collector instance that will be notified + // of interesting events related to the arena. + internal::ArenaMetricsCollector *(*make_metrics_collector)(); + + internal::ArenaMetricsCollector *MetricsCollector() const { + return make_metrics_collector ? (*make_metrics_collector)() : nullptr; + } + + internal::AllocationPolicy AllocationPolicy() const { + internal::AllocationPolicy res; + res.start_block_size = start_block_size; + res.max_block_size = max_block_size; + res.block_alloc = block_alloc; + res.block_dealloc = block_dealloc; + res.metrics_collector = MetricsCollector(); + return res; + } + + friend void arena_metrics::EnableArenaMetrics(ArenaOptions *); + + friend class Arena; + friend class ArenaOptionsTestFriend; }; // Support for non-RTTI environments. (The metrics hooks API uses type @@ -241,581 +236,535 @@ struct ArenaOptions { // is internal to protobuf and is not guaranteed to be stable. Non-proto types // should not rely on this protocol. class PROTOBUF_EXPORT PROTOBUF_ALIGNAS(8) Arena final { - public: - // Default constructor with sensible default options, tuned for average - // use-cases. - inline Arena() : impl_() {} - - // Construct an arena with default options, except for the supplied - // initial block. It is more efficient to use this constructor - // instead of passing ArenaOptions if the only configuration needed - // by the caller is supplying an initial block. - inline Arena(char* initial_block, size_t initial_block_size) - : impl_(initial_block, initial_block_size) {} - - // Arena constructor taking custom options. See ArenaOptions above for - // descriptions of the options available. - explicit Arena(const ArenaOptions& options) - : impl_(options.initial_block, options.initial_block_size, - options.AllocationPolicy()) {} - - // Block overhead. Use this as a guide for how much to over-allocate the - // initial block if you want an allocation of size N to fit inside it. - // - // WARNING: if you allocate multiple objects, it is difficult to guarantee - // that a series of allocations will fit in the initial block, especially if - // Arena changes its alignment guarantees in the future! - static const size_t kBlockOverhead = - internal::ThreadSafeArena::kBlockHeaderSize + - internal::ThreadSafeArena::kSerialArenaSize; - - inline ~Arena() {} - - // TODO(protobuf-team): Fix callers to use constructor and delete this method. - void Init(const ArenaOptions&) {} - - // API to create proto2 message objects on the arena. If the arena passed in - // is NULL, then a heap allocated object is returned. Type T must be a message - // defined in a .proto file with cc_enable_arenas set to true, otherwise a - // compilation error will occur. - // - // RepeatedField and RepeatedPtrField may also be instantiated directly on an - // arena with this method. - // - // This function also accepts any type T that satisfies the arena message - // allocation protocol, documented above. - template - PROTOBUF_ALWAYS_INLINE static T* CreateMessage(Arena* arena, Args&&... args) { - static_assert( - InternalHelper::is_arena_constructable::value, - "CreateMessage can only construct types that are ArenaConstructable"); - // We must delegate to CreateMaybeMessage() and NOT CreateMessageInternal() - // because protobuf generated classes specialize CreateMaybeMessage() and we - // need to use that specialization for code size reasons. - return Arena::CreateMaybeMessage(arena, static_cast(args)...); - } - - // API to create any objects on the arena. Note that only the object will - // be created on the arena; the underlying ptrs (in case of a proto2 message) - // will be still heap allocated. Proto messages should usually be allocated - // with CreateMessage() instead. - // - // Note that even if T satisfies the arena message construction protocol - // (InternalArenaConstructable_ trait and optional DestructorSkippable_ - // trait), as described above, this function does not follow the protocol; - // instead, it treats T as a black-box type, just as if it did not have these - // traits. Specifically, T's constructor arguments will always be only those - // passed to Create() -- no additional arena pointer is implicitly added. - // Furthermore, the destructor will always be called at arena destruction time - // (unless the destructor is trivial). Hence, from T's point of view, it is as - // if the object were allocated on the heap (except that the underlying memory - // is obtained from the arena). - template - PROTOBUF_NDEBUG_INLINE static T* Create(Arena* arena, Args&&... args) { - return CreateInternal(arena, std::is_convertible(), - static_cast(args)...); - } - - // Create an array of object type T on the arena *without* invoking the - // constructor of T. If `arena` is null, then the return value should be freed - // with `delete[] x;` (or `::operator delete[](x);`). - // To ensure safe uses, this function checks at compile time - // (when compiled as C++11) that T is trivially default-constructible and - // trivially destructible. - template - PROTOBUF_NDEBUG_INLINE static T* CreateArray(Arena* arena, - size_t num_elements) { - static_assert(std::is_trivial::value, - "CreateArray requires a trivially constructible type"); - static_assert(std::is_trivially_destructible::value, - "CreateArray requires a trivially destructible type"); - GOOGLE_CHECK_LE(num_elements, std::numeric_limits::max() / sizeof(T)) - << "Requested size is too large to fit into size_t."; - if (arena == NULL) { - return static_cast(::operator new[](num_elements * sizeof(T))); - } else { - return arena->CreateInternalRawArray(num_elements); - } - } - - // The following are routines are for monitoring. They will approximate the - // total sum allocated and used memory, but the exact value is an - // implementation deal. For instance allocated space depends on growth - // policies. Do not use these in unit tests. - // Returns the total space allocated by the arena, which is the sum of the - // sizes of the underlying blocks. - uint64_t SpaceAllocated() const { return impl_.SpaceAllocated(); } - // Returns the total space used by the arena. Similar to SpaceAllocated but - // does not include free space and block overhead. The total space returned - // may not include space used by other threads executing concurrently with - // the call to this method. - uint64_t SpaceUsed() const { return impl_.SpaceUsed(); } - - // Frees all storage allocated by this arena after calling destructors - // registered with OwnDestructor() and freeing objects registered with Own(). - // Any objects allocated on this arena are unusable after this call. It also - // returns the total space used by the arena which is the sums of the sizes - // of the allocated blocks. This method is not thread-safe. - uint64_t Reset() { return impl_.Reset(); } - - // Adds |object| to a list of heap-allocated objects to be freed with |delete| - // when the arena is destroyed or reset. - template - PROTOBUF_ALWAYS_INLINE void Own(T* object) { - OwnInternal(object, std::is_convertible()); - } - - // Adds |object| to a list of objects whose destructors will be manually - // called when the arena is destroyed or reset. This differs from Own() in - // that it does not free the underlying memory with |delete|; hence, it is - // normally only used for objects that are placement-newed into - // arena-allocated memory. - template - PROTOBUF_ALWAYS_INLINE void OwnDestructor(T* object) { - if (object != NULL) { - impl_.AddCleanup(object, &internal::arena_destruct_object); - } - } - - // Adds a custom member function on an object to the list of destructors that - // will be manually called when the arena is destroyed or reset. This differs - // from OwnDestructor() in that any member function may be specified, not only - // the class destructor. - PROTOBUF_ALWAYS_INLINE void OwnCustomDestructor(void* object, - void (*destruct)(void*)) { - impl_.AddCleanup(object, destruct); - } - - // Retrieves the arena associated with |value| if |value| is an arena-capable - // message, or NULL otherwise. If possible, the call resolves at compile time. - // Note that we can often devirtualize calls to `value->GetArena()` so usually - // calling this method is unnecessary. - template - PROTOBUF_ALWAYS_INLINE static Arena* GetArena(const T* value) { - return GetArenaInternal(value); - } - - template - class InternalHelper { - public: - // Provides access to protected GetOwningArena to generated messages. - static Arena* GetOwningArena(const T* p) { return p->GetOwningArena(); } - - // Provides access to protected GetArenaForAllocation to generated messages. - static Arena* GetArenaForAllocation(const T* p) { - return GetArenaForAllocationInternal( - p, std::is_convertible()); - } - - // Creates message-owned arena. - static Arena* CreateMessageOwnedArena() { - return new Arena(internal::MessageOwned{}); - } - - // Checks whether the given arena is message-owned. - static bool IsMessageOwnedArena(Arena* arena) { - return arena->IsMessageOwned(); - } - - private: - static Arena* GetArenaForAllocationInternal( - const T* p, std::true_type /*is_derived_from*/) { - return p->GetArenaForAllocation(); - } - - static Arena* GetArenaForAllocationInternal( - const T* p, std::false_type /*is_derived_from*/) { - return GetArenaForAllocationForNonMessage( - p, typename is_arena_constructable::type()); - } - - static Arena* GetArenaForAllocationForNonMessage( - const T* p, std::true_type /*is_arena_constructible*/) { - return p->GetArena(); - } - - static Arena* GetArenaForAllocationForNonMessage( - const T* p, std::false_type /*is_arena_constructible*/) { - return GetArenaForAllocationForNonMessageNonArenaConstructible( - p, typename has_get_arena::type()); - } - - static Arena* GetArenaForAllocationForNonMessageNonArenaConstructible( - const T* p, std::true_type /*has_get_arena*/) { - return p->GetArena(); - } - - static Arena* GetArenaForAllocationForNonMessageNonArenaConstructible( - const T* /* p */, std::false_type /*has_get_arena*/) { - return nullptr; - } - - template - static char DestructorSkippable(const typename U::DestructorSkippable_*); - template - static double DestructorSkippable(...); - - typedef std::integral_constant< - bool, sizeof(DestructorSkippable(static_cast(0))) == - sizeof(char) || - std::is_trivially_destructible::value> - is_destructor_skippable; - - template - static char ArenaConstructable( - const typename U::InternalArenaConstructable_*); - template - static double ArenaConstructable(...); - - typedef std::integral_constant( - static_cast(0))) == - sizeof(char)> - is_arena_constructable; - - template () - .GetArena())>::value, - int>::type = 0> - static char HasGetArena(decltype(&U::GetArena)); - template - static double HasGetArena(...); - - typedef std::integral_constant(nullptr)) == - sizeof(char)> - has_get_arena; - - template - static T* Construct(void* ptr, Args&&... args) { - return new (ptr) T(static_cast(args)...); - } - - static inline PROTOBUF_ALWAYS_INLINE T* New() { - return new T(nullptr); - } - - static Arena* GetArena(const T* p) { return p->GetArena(); } - - friend class Arena; - friend class TestUtil::ReflectionTester; - }; - - // Helper typetraits that indicates support for arenas in a type T at compile - // time. This is public only to allow construction of higher-level templated - // utilities. - // - // is_arena_constructable::value is true if the message type T has arena - // support enabled, and false otherwise. - // - // is_destructor_skippable::value is true if the message type T has told - // the arena that it is safe to skip the destructor, and false otherwise. - // - // This is inside Arena because only Arena has the friend relationships - // necessary to see the underlying generated code traits. - template - struct is_arena_constructable : InternalHelper::is_arena_constructable {}; - template - struct is_destructor_skippable : InternalHelper::is_destructor_skippable { - }; - - private: - internal::ThreadSafeArena impl_; - - template - struct has_get_arena : InternalHelper::has_get_arena {}; - - // Constructor solely used by message-owned arena. - inline Arena(internal::MessageOwned) : impl_(internal::MessageOwned{}) {} - - // Checks whether this arena is message-owned. - PROTOBUF_ALWAYS_INLINE bool IsMessageOwned() const { - return impl_.IsMessageOwned(); - } - - template - PROTOBUF_NDEBUG_INLINE static T* CreateMessageInternal(Arena* arena, - Args&&... args) { - static_assert( - InternalHelper::is_arena_constructable::value, - "CreateMessage can only construct types that are ArenaConstructable"); - if (arena == NULL) { - return new T(nullptr, static_cast(args)...); - } else { - return arena->DoCreateMessage(static_cast(args)...); - } - } - - // This specialization for no arguments is necessary, because its behavior is - // slightly different. When the arena pointer is nullptr, it calls T() - // instead of T(nullptr). - template - PROTOBUF_NDEBUG_INLINE static T* CreateMessageInternal(Arena* arena) { - static_assert( - InternalHelper::is_arena_constructable::value, - "CreateMessage can only construct types that are ArenaConstructable"); - if (arena == NULL) { - // Generated arena constructor T(Arena*) is protected. Call via - // InternalHelper. - return InternalHelper::New(); - } else { - return arena->DoCreateMessage(); - } - } - - // Allocate and also optionally call collector with the allocated type info - // when allocation recording is enabled. - PROTOBUF_NDEBUG_INLINE void* AllocateInternal(size_t size, size_t align, - void (*destructor)(void*), - const std::type_info* type) { - // Monitor allocation if needed. - if (destructor == nullptr) { - return AllocateAlignedWithHook(size, align, type); - } else { - if (align <= 8) { - auto res = AllocateAlignedWithCleanup(internal::AlignUpTo8(size), type); - res.second->elem = res.first; - res.second->cleanup = destructor; - return res.first; - } else { - auto res = AllocateAlignedWithCleanup(size + align - 8, type); - auto ptr = internal::AlignTo(res.first, align); - res.second->elem = ptr; - res.second->cleanup = destructor; - return ptr; - } - } - } - - // CreateMessage requires that T supports arenas, but this private method - // works whether or not T supports arenas. These are not exposed to user code - // as it can cause confusing API usages, and end up having double free in - // user code. These are used only internally from LazyField and Repeated - // fields, since they are designed to work in all mode combinations. - template - PROTOBUF_ALWAYS_INLINE static Msg* DoCreateMaybeMessage(Arena* arena, - std::true_type, - Args&&... args) { - return CreateMessageInternal(arena, std::forward(args)...); - } - - template - PROTOBUF_ALWAYS_INLINE static T* DoCreateMaybeMessage(Arena* arena, - std::false_type, - Args&&... args) { - return Create(arena, std::forward(args)...); - } - - template - PROTOBUF_ALWAYS_INLINE static T* CreateMaybeMessage(Arena* arena, - Args&&... args) { - return DoCreateMaybeMessage(arena, is_arena_constructable(), - std::forward(args)...); - } - - // Just allocate the required size for the given type assuming the - // type has a trivial constructor. - template - PROTOBUF_NDEBUG_INLINE T* CreateInternalRawArray(size_t num_elements) { - GOOGLE_CHECK_LE(num_elements, std::numeric_limits::max() / sizeof(T)) - << "Requested size is too large to fit into size_t."; - // We count on compiler to realize that if sizeof(T) is a multiple of - // 8 AlignUpTo can be elided. - const size_t n = sizeof(T) * num_elements; - return static_cast( - AllocateAlignedWithHook(n, alignof(T), RTTI_TYPE_ID(T))); - } - - template - PROTOBUF_NDEBUG_INLINE T* DoCreateMessage(Args&&... args) { - return InternalHelper::Construct( - AllocateInternal(sizeof(T), alignof(T), - internal::ObjectDestructor< - InternalHelper::is_destructor_skippable::value, - T>::destructor, - RTTI_TYPE_ID(T)), - this, std::forward(args)...); - } - - // CreateInArenaStorage is used to implement map field. Without it, - // Map need to call generated message's protected arena constructor, - // which needs to declare Map as friend of generated message. - template - static void CreateInArenaStorage(T* ptr, Arena* arena, Args&&... args) { - CreateInArenaStorageInternal(ptr, arena, - typename is_arena_constructable::type(), - std::forward(args)...); - if (arena != nullptr) { - RegisterDestructorInternal( - ptr, arena, - typename InternalHelper::is_destructor_skippable::type()); - } - } - - template - static void CreateInArenaStorageInternal(T* ptr, Arena* arena, - std::true_type, Args&&... args) { - InternalHelper::Construct(ptr, arena, std::forward(args)...); - } - template - static void CreateInArenaStorageInternal(T* ptr, Arena* /* arena */, - std::false_type, Args&&... args) { - new (ptr) T(std::forward(args)...); - } - - template - static void RegisterDestructorInternal(T* /* ptr */, Arena* /* arena */, - std::true_type) {} - template - static void RegisterDestructorInternal(T* ptr, Arena* arena, - std::false_type) { - arena->OwnDestructor(ptr); - } - - // These implement Create(). The second parameter has type 'true_type' if T is - // a subtype of Message and 'false_type' otherwise. - template - PROTOBUF_ALWAYS_INLINE static T* CreateInternal(Arena* arena, std::true_type, - Args&&... args) { - if (arena == nullptr) { - return new T(std::forward(args)...); - } else { - auto destructor = - internal::ObjectDestructor::value, - T>::destructor; - T* result = - new (arena->AllocateInternal(sizeof(T), alignof(T), destructor, - RTTI_TYPE_ID(T))) - T(std::forward(args)...); - return result; - } - } - template - PROTOBUF_ALWAYS_INLINE static T* CreateInternal(Arena* arena, std::false_type, - Args&&... args) { - if (arena == nullptr) { - return new T(std::forward(args)...); - } else { - auto destructor = - internal::ObjectDestructor::value, - T>::destructor; - return new (arena->AllocateInternal(sizeof(T), alignof(T), destructor, - RTTI_TYPE_ID(T))) - T(std::forward(args)...); - } - } - - // These implement Own(), which registers an object for deletion (destructor - // call and operator delete()). The second parameter has type 'true_type' if T - // is a subtype of Message and 'false_type' otherwise. Collapsing - // all template instantiations to one for generic Message reduces code size, - // using the virtual destructor instead. - template - PROTOBUF_ALWAYS_INLINE void OwnInternal(T* object, std::true_type) { - if (object != NULL) { - impl_.AddCleanup(object, &internal::arena_delete_object); - } - } - template - PROTOBUF_ALWAYS_INLINE void OwnInternal(T* object, std::false_type) { - if (object != NULL) { - impl_.AddCleanup(object, &internal::arena_delete_object); - } - } - - // Implementation for GetArena(). Only message objects with - // InternalArenaConstructable_ tags can be associated with an arena, and such - // objects must implement a GetArena() method. - template ::value, int>::type = 0> - PROTOBUF_ALWAYS_INLINE static Arena* GetArenaInternal(const T* value) { - return InternalHelper::GetArena(value); - } - template ::value && - has_get_arena::value, - int>::type = 0> - PROTOBUF_ALWAYS_INLINE static Arena* GetArenaInternal(const T* value) { - return value->GetArena(); - } - template ::value && - !has_get_arena::value, - int>::type = 0> - PROTOBUF_ALWAYS_INLINE static Arena* GetArenaInternal(const T* value) { - (void)value; - return nullptr; - } - - template - PROTOBUF_ALWAYS_INLINE static Arena* GetOwningArena(const T* value) { - return GetOwningArenaInternal( - value, std::is_convertible()); - } - - // Implementation for GetOwningArena(). All and only message objects have - // GetOwningArena() method. - template - PROTOBUF_ALWAYS_INLINE static Arena* GetOwningArenaInternal( - const T* value, std::true_type) { - return InternalHelper::GetOwningArena(value); - } - template - PROTOBUF_ALWAYS_INLINE static Arena* GetOwningArenaInternal( - const T* /* value */, std::false_type) { - return nullptr; - } - - // For friends of arena. - void* AllocateAligned(size_t n, size_t align = 8) { - if (align <= 8) { - return AllocateAlignedNoHook(internal::AlignUpTo8(n)); - } else { - // We are wasting space by over allocating align - 8 bytes. Compared - // to a dedicated function that takes current alignment in consideration. - // Such a scheme would only waste (align - 8)/2 bytes on average, but - // requires a dedicated function in the outline arena allocation - // functions. Possibly re-evaluate tradeoffs later. - return internal::AlignTo(AllocateAlignedNoHook(n + align - 8), align); - } - } - - void* AllocateAlignedWithHook(size_t n, size_t align, - const std::type_info* type) { - if (align <= 8) { - return AllocateAlignedWithHook(internal::AlignUpTo8(n), type); - } else { - // We are wasting space by over allocating align - 8 bytes. Compared - // to a dedicated function that takes current alignment in consideration. - // Such a schemee would only waste (align - 8)/2 bytes on average, but - // requires a dedicated function in the outline arena allocation - // functions. Possibly re-evaluate tradeoffs later. - return internal::AlignTo(AllocateAlignedWithHook(n + align - 8, type), - align); - } - } - - void* AllocateAlignedNoHook(size_t n); - void* AllocateAlignedWithHook(size_t n, const std::type_info* type); - std::pair - AllocateAlignedWithCleanup(size_t n, const std::type_info* type); - - template - friend class internal::GenericTypeHandler; - friend struct internal::ArenaStringPtr; // For AllocateAligned. - friend class internal::InlinedStringField; // For AllocateAligned. - friend class internal::LazyField; // For CreateMaybeMessage. - friend class internal::EpsCopyInputStream; // For parser performance - friend class MessageLite; - template - friend class Map; +public: + // Default constructor with sensible default options, tuned for average + // use-cases. + inline Arena() : impl_() { + } + + // Construct an arena with default options, except for the supplied + // initial block. It is more efficient to use this constructor + // instead of passing ArenaOptions if the only configuration needed + // by the caller is supplying an initial block. + inline Arena(char *initial_block, size_t initial_block_size) : impl_(initial_block, initial_block_size) { + } + + // Arena constructor taking custom options. See ArenaOptions above for + // descriptions of the options available. + explicit Arena(const ArenaOptions &options) + : impl_(options.initial_block, options.initial_block_size, options.AllocationPolicy()) { + } + + // Block overhead. Use this as a guide for how much to over-allocate the + // initial block if you want an allocation of size N to fit inside it. + // + // WARNING: if you allocate multiple objects, it is difficult to guarantee + // that a series of allocations will fit in the initial block, especially if + // Arena changes its alignment guarantees in the future! + static const size_t kBlockOverhead = + internal::ThreadSafeArena::kBlockHeaderSize + internal::ThreadSafeArena::kSerialArenaSize; + + inline ~Arena() { + } + + // TODO(protobuf-team): Fix callers to use constructor and delete this method. + void Init(const ArenaOptions &) { + } + + // API to create proto2 message objects on the arena. If the arena passed in + // is NULL, then a heap allocated object is returned. Type T must be a message + // defined in a .proto file with cc_enable_arenas set to true, otherwise a + // compilation error will occur. + // + // RepeatedField and RepeatedPtrField may also be instantiated directly on an + // arena with this method. + // + // This function also accepts any type T that satisfies the arena message + // allocation protocol, documented above. + template + PROTOBUF_ALWAYS_INLINE static T *CreateMessage(Arena *arena, Args &&...args) { + static_assert(InternalHelper::is_arena_constructable::value, + "CreateMessage can only construct types that are ArenaConstructable"); + // We must delegate to CreateMaybeMessage() and NOT CreateMessageInternal() + // because protobuf generated classes specialize CreateMaybeMessage() and we + // need to use that specialization for code size reasons. + return Arena::CreateMaybeMessage(arena, static_cast(args)...); + } + + // API to create any objects on the arena. Note that only the object will + // be created on the arena; the underlying ptrs (in case of a proto2 message) + // will be still heap allocated. Proto messages should usually be allocated + // with CreateMessage() instead. + // + // Note that even if T satisfies the arena message construction protocol + // (InternalArenaConstructable_ trait and optional DestructorSkippable_ + // trait), as described above, this function does not follow the protocol; + // instead, it treats T as a black-box type, just as if it did not have these + // traits. Specifically, T's constructor arguments will always be only those + // passed to Create() -- no additional arena pointer is implicitly added. + // Furthermore, the destructor will always be called at arena destruction time + // (unless the destructor is trivial). Hence, from T's point of view, it is as + // if the object were allocated on the heap (except that the underlying memory + // is obtained from the arena). + template + PROTOBUF_NDEBUG_INLINE static T *Create(Arena *arena, Args &&...args) { + return CreateInternal(arena, std::is_convertible(), static_cast(args)...); + } + + // Create an array of object type T on the arena *without* invoking the + // constructor of T. If `arena` is null, then the return value should be freed + // with `delete[] x;` (or `::operator delete[](x);`). + // To ensure safe uses, this function checks at compile time + // (when compiled as C++11) that T is trivially default-constructible and + // trivially destructible. + template + PROTOBUF_NDEBUG_INLINE static T *CreateArray(Arena *arena, size_t num_elements) { + static_assert(std::is_trivial::value, "CreateArray requires a trivially constructible type"); + static_assert(std::is_trivially_destructible::value, "CreateArray requires a trivially destructible type"); + GOOGLE_CHECK_LE(num_elements, std::numeric_limits::max() / sizeof(T)) + << "Requested size is too large to fit into size_t."; + if (arena == NULL) { + return static_cast(::operator new[](num_elements * sizeof(T))); + } else { + return arena->CreateInternalRawArray(num_elements); + } + } + + // The following are routines are for monitoring. They will approximate the + // total sum allocated and used memory, but the exact value is an + // implementation deal. For instance allocated space depends on growth + // policies. Do not use these in unit tests. + // Returns the total space allocated by the arena, which is the sum of the + // sizes of the underlying blocks. + uint64_t SpaceAllocated() const { + return impl_.SpaceAllocated(); + } + // Returns the total space used by the arena. Similar to SpaceAllocated but + // does not include free space and block overhead. The total space returned + // may not include space used by other threads executing concurrently with + // the call to this method. + uint64_t SpaceUsed() const { + return impl_.SpaceUsed(); + } + + // Frees all storage allocated by this arena after calling destructors + // registered with OwnDestructor() and freeing objects registered with Own(). + // Any objects allocated on this arena are unusable after this call. It also + // returns the total space used by the arena which is the sums of the sizes + // of the allocated blocks. This method is not thread-safe. + uint64_t Reset() { + return impl_.Reset(); + } + + // Adds |object| to a list of heap-allocated objects to be freed with |delete| + // when the arena is destroyed or reset. + template + PROTOBUF_ALWAYS_INLINE void Own(T *object) { + OwnInternal(object, std::is_convertible()); + } + + // Adds |object| to a list of objects whose destructors will be manually + // called when the arena is destroyed or reset. This differs from Own() in + // that it does not free the underlying memory with |delete|; hence, it is + // normally only used for objects that are placement-newed into + // arena-allocated memory. + template + PROTOBUF_ALWAYS_INLINE void OwnDestructor(T *object) { + if (object != NULL) { + impl_.AddCleanup(object, &internal::arena_destruct_object); + } + } + + // Adds a custom member function on an object to the list of destructors that + // will be manually called when the arena is destroyed or reset. This differs + // from OwnDestructor() in that any member function may be specified, not only + // the class destructor. + PROTOBUF_ALWAYS_INLINE void OwnCustomDestructor(void *object, void (*destruct)(void *)) { + impl_.AddCleanup(object, destruct); + } + + // Retrieves the arena associated with |value| if |value| is an arena-capable + // message, or NULL otherwise. If possible, the call resolves at compile time. + // Note that we can often devirtualize calls to `value->GetArena()` so usually + // calling this method is unnecessary. + template + PROTOBUF_ALWAYS_INLINE static Arena *GetArena(const T *value) { + return GetArenaInternal(value); + } + + template + class InternalHelper { + public: + // Provides access to protected GetOwningArena to generated messages. + static Arena *GetOwningArena(const T *p) { + return p->GetOwningArena(); + } + + // Provides access to protected GetArenaForAllocation to generated messages. + static Arena *GetArenaForAllocation(const T *p) { + return GetArenaForAllocationInternal(p, std::is_convertible()); + } + + // Creates message-owned arena. + static Arena *CreateMessageOwnedArena() { + return new Arena(internal::MessageOwned {}); + } + + // Checks whether the given arena is message-owned. + static bool IsMessageOwnedArena(Arena *arena) { + return arena->IsMessageOwned(); + } + + private: + static Arena *GetArenaForAllocationInternal(const T *p, std::true_type /*is_derived_from*/) { + return p->GetArenaForAllocation(); + } + + static Arena *GetArenaForAllocationInternal(const T *p, std::false_type /*is_derived_from*/) { + return GetArenaForAllocationForNonMessage(p, typename is_arena_constructable::type()); + } + + static Arena *GetArenaForAllocationForNonMessage(const T *p, std::true_type /*is_arena_constructible*/) { + return p->GetArena(); + } + + static Arena *GetArenaForAllocationForNonMessage(const T *p, std::false_type /*is_arena_constructible*/) { + return GetArenaForAllocationForNonMessageNonArenaConstructible(p, typename has_get_arena::type()); + } + + static Arena *GetArenaForAllocationForNonMessageNonArenaConstructible(const T *p, + std::true_type /*has_get_arena*/) { + return p->GetArena(); + } + + static Arena *GetArenaForAllocationForNonMessageNonArenaConstructible(const T * /* p */, + std::false_type /*has_get_arena*/) { + return nullptr; + } + + template + static char DestructorSkippable(const typename U::DestructorSkippable_ *); + template + static double DestructorSkippable(...); + + typedef std::integral_constant(static_cast(0))) == sizeof(char) || + std::is_trivially_destructible::value> + is_destructor_skippable; + + template + static char ArenaConstructable(const typename U::InternalArenaConstructable_ *); + template + static double ArenaConstructable(...); + + typedef std::integral_constant(static_cast(0))) == sizeof(char)> + is_arena_constructable; + + template ().GetArena())>::value, + int>::type = 0> + static char HasGetArena(decltype(&U::GetArena)); + template + static double HasGetArena(...); + + typedef std::integral_constant(nullptr)) == sizeof(char)> has_get_arena; + + template + static T *Construct(void *ptr, Args &&...args) { + return new (ptr) T(static_cast(args)...); + } + + static inline PROTOBUF_ALWAYS_INLINE T *New() { + return new T(nullptr); + } + + static Arena *GetArena(const T *p) { + return p->GetArena(); + } + + friend class Arena; + friend class TestUtil::ReflectionTester; + }; + + // Helper typetraits that indicates support for arenas in a type T at compile + // time. This is public only to allow construction of higher-level templated + // utilities. + // + // is_arena_constructable::value is true if the message type T has arena + // support enabled, and false otherwise. + // + // is_destructor_skippable::value is true if the message type T has told + // the arena that it is safe to skip the destructor, and false otherwise. + // + // This is inside Arena because only Arena has the friend relationships + // necessary to see the underlying generated code traits. + template + struct is_arena_constructable : InternalHelper::is_arena_constructable {}; + template + struct is_destructor_skippable : InternalHelper::is_destructor_skippable {}; + +private: + internal::ThreadSafeArena impl_; + + template + struct has_get_arena : InternalHelper::has_get_arena {}; + + // Constructor solely used by message-owned arena. + inline Arena(internal::MessageOwned) : impl_(internal::MessageOwned {}) { + } + + // Checks whether this arena is message-owned. + PROTOBUF_ALWAYS_INLINE bool IsMessageOwned() const { + return impl_.IsMessageOwned(); + } + + template + PROTOBUF_NDEBUG_INLINE static T *CreateMessageInternal(Arena *arena, Args &&...args) { + static_assert(InternalHelper::is_arena_constructable::value, + "CreateMessage can only construct types that are ArenaConstructable"); + if (arena == NULL) { + return new T(nullptr, static_cast(args)...); + } else { + return arena->DoCreateMessage(static_cast(args)...); + } + } + + // This specialization for no arguments is necessary, because its behavior is + // slightly different. When the arena pointer is nullptr, it calls T() + // instead of T(nullptr). + template + PROTOBUF_NDEBUG_INLINE static T *CreateMessageInternal(Arena *arena) { + static_assert(InternalHelper::is_arena_constructable::value, + "CreateMessage can only construct types that are ArenaConstructable"); + if (arena == NULL) { + // Generated arena constructor T(Arena*) is protected. Call via + // InternalHelper. + return InternalHelper::New(); + } else { + return arena->DoCreateMessage(); + } + } + + // Allocate and also optionally call collector with the allocated type info + // when allocation recording is enabled. + PROTOBUF_NDEBUG_INLINE void *AllocateInternal(size_t size, size_t align, void (*destructor)(void *), + const std::type_info *type) { + // Monitor allocation if needed. + if (destructor == nullptr) { + return AllocateAlignedWithHook(size, align, type); + } else { + if (align <= 8) { + auto res = AllocateAlignedWithCleanup(internal::AlignUpTo8(size), type); + res.second->elem = res.first; + res.second->cleanup = destructor; + return res.first; + } else { + auto res = AllocateAlignedWithCleanup(size + align - 8, type); + auto ptr = internal::AlignTo(res.first, align); + res.second->elem = ptr; + res.second->cleanup = destructor; + return ptr; + } + } + } + + // CreateMessage requires that T supports arenas, but this private method + // works whether or not T supports arenas. These are not exposed to user code + // as it can cause confusing API usages, and end up having double free in + // user code. These are used only internally from LazyField and Repeated + // fields, since they are designed to work in all mode combinations. + template + PROTOBUF_ALWAYS_INLINE static Msg *DoCreateMaybeMessage(Arena *arena, std::true_type, Args &&...args) { + return CreateMessageInternal(arena, std::forward(args)...); + } + + template + PROTOBUF_ALWAYS_INLINE static T *DoCreateMaybeMessage(Arena *arena, std::false_type, Args &&...args) { + return Create(arena, std::forward(args)...); + } + + template + PROTOBUF_ALWAYS_INLINE static T *CreateMaybeMessage(Arena *arena, Args &&...args) { + return DoCreateMaybeMessage(arena, is_arena_constructable(), std::forward(args)...); + } + + // Just allocate the required size for the given type assuming the + // type has a trivial constructor. + template + PROTOBUF_NDEBUG_INLINE T *CreateInternalRawArray(size_t num_elements) { + GOOGLE_CHECK_LE(num_elements, std::numeric_limits::max() / sizeof(T)) + << "Requested size is too large to fit into size_t."; + // We count on compiler to realize that if sizeof(T) is a multiple of + // 8 AlignUpTo can be elided. + const size_t n = sizeof(T) * num_elements; + return static_cast(AllocateAlignedWithHook(n, alignof(T), RTTI_TYPE_ID(T))); + } + + template + PROTOBUF_NDEBUG_INLINE T *DoCreateMessage(Args &&...args) { + return InternalHelper::Construct( + AllocateInternal( + sizeof(T), alignof(T), + internal::ObjectDestructor::is_destructor_skippable::value, T>::destructor, + RTTI_TYPE_ID(T)), + this, std::forward(args)...); + } + + // CreateInArenaStorage is used to implement map field. Without it, + // Map need to call generated message's protected arena constructor, + // which needs to declare Map as friend of generated message. + template + static void CreateInArenaStorage(T *ptr, Arena *arena, Args &&...args) { + CreateInArenaStorageInternal(ptr, arena, typename is_arena_constructable::type(), + std::forward(args)...); + if (arena != nullptr) { + RegisterDestructorInternal(ptr, arena, typename InternalHelper::is_destructor_skippable::type()); + } + } + + template + static void CreateInArenaStorageInternal(T *ptr, Arena *arena, std::true_type, Args &&...args) { + InternalHelper::Construct(ptr, arena, std::forward(args)...); + } + template + static void CreateInArenaStorageInternal(T *ptr, Arena * /* arena */, std::false_type, Args &&...args) { + new (ptr) T(std::forward(args)...); + } + + template + static void RegisterDestructorInternal(T * /* ptr */, Arena * /* arena */, std::true_type) { + } + template + static void RegisterDestructorInternal(T *ptr, Arena *arena, std::false_type) { + arena->OwnDestructor(ptr); + } + + // These implement Create(). The second parameter has type 'true_type' if T is + // a subtype of Message and 'false_type' otherwise. + template + PROTOBUF_ALWAYS_INLINE static T *CreateInternal(Arena *arena, std::true_type, Args &&...args) { + if (arena == nullptr) { + return new T(std::forward(args)...); + } else { + auto destructor = internal::ObjectDestructor::value, T>::destructor; + T *result = new (arena->AllocateInternal(sizeof(T), alignof(T), destructor, RTTI_TYPE_ID(T))) + T(std::forward(args)...); + return result; + } + } + template + PROTOBUF_ALWAYS_INLINE static T *CreateInternal(Arena *arena, std::false_type, Args &&...args) { + if (arena == nullptr) { + return new T(std::forward(args)...); + } else { + auto destructor = internal::ObjectDestructor::value, T>::destructor; + return new (arena->AllocateInternal(sizeof(T), alignof(T), destructor, RTTI_TYPE_ID(T))) + T(std::forward(args)...); + } + } + + // These implement Own(), which registers an object for deletion (destructor + // call and operator delete()). The second parameter has type 'true_type' if T + // is a subtype of Message and 'false_type' otherwise. Collapsing + // all template instantiations to one for generic Message reduces code size, + // using the virtual destructor instead. + template + PROTOBUF_ALWAYS_INLINE void OwnInternal(T *object, std::true_type) { + if (object != NULL) { + impl_.AddCleanup(object, &internal::arena_delete_object); + } + } + template + PROTOBUF_ALWAYS_INLINE void OwnInternal(T *object, std::false_type) { + if (object != NULL) { + impl_.AddCleanup(object, &internal::arena_delete_object); + } + } + + // Implementation for GetArena(). Only message objects with + // InternalArenaConstructable_ tags can be associated with an arena, and such + // objects must implement a GetArena() method. + template ::value, int>::type = 0> + PROTOBUF_ALWAYS_INLINE static Arena *GetArenaInternal(const T *value) { + return InternalHelper::GetArena(value); + } + template ::value && has_get_arena::value, int>::type = 0> + PROTOBUF_ALWAYS_INLINE static Arena *GetArenaInternal(const T *value) { + return value->GetArena(); + } + template ::value && !has_get_arena::value, int>::type = 0> + PROTOBUF_ALWAYS_INLINE static Arena *GetArenaInternal(const T *value) { + (void)value; + return nullptr; + } + + template + PROTOBUF_ALWAYS_INLINE static Arena *GetOwningArena(const T *value) { + return GetOwningArenaInternal(value, std::is_convertible()); + } + + // Implementation for GetOwningArena(). All and only message objects have + // GetOwningArena() method. + template + PROTOBUF_ALWAYS_INLINE static Arena *GetOwningArenaInternal(const T *value, std::true_type) { + return InternalHelper::GetOwningArena(value); + } + template + PROTOBUF_ALWAYS_INLINE static Arena *GetOwningArenaInternal(const T * /* value */, std::false_type) { + return nullptr; + } + + // For friends of arena. + void *AllocateAligned(size_t n, size_t align = 8) { + if (align <= 8) { + return AllocateAlignedNoHook(internal::AlignUpTo8(n)); + } else { + // We are wasting space by over allocating align - 8 bytes. Compared + // to a dedicated function that takes current alignment in consideration. + // Such a scheme would only waste (align - 8)/2 bytes on average, but + // requires a dedicated function in the outline arena allocation + // functions. Possibly re-evaluate tradeoffs later. + return internal::AlignTo(AllocateAlignedNoHook(n + align - 8), align); + } + } + + void *AllocateAlignedWithHook(size_t n, size_t align, const std::type_info *type) { + if (align <= 8) { + return AllocateAlignedWithHook(internal::AlignUpTo8(n), type); + } else { + // We are wasting space by over allocating align - 8 bytes. Compared + // to a dedicated function that takes current alignment in consideration. + // Such a schemee would only waste (align - 8)/2 bytes on average, but + // requires a dedicated function in the outline arena allocation + // functions. Possibly re-evaluate tradeoffs later. + return internal::AlignTo(AllocateAlignedWithHook(n + align - 8, type), align); + } + } + + void *AllocateAlignedNoHook(size_t n); + void *AllocateAlignedWithHook(size_t n, const std::type_info *type); + std::pair AllocateAlignedWithCleanup(size_t n, + const std::type_info *type); + + template + friend class internal::GenericTypeHandler; + friend struct internal::ArenaStringPtr; // For AllocateAligned. + friend class internal::InlinedStringField; // For AllocateAligned. + friend class internal::LazyField; // For CreateMaybeMessage. + friend class internal::EpsCopyInputStream; // For parser performance + friend class MessageLite; + template + friend class Map; }; // Defined above for supporting environments without RTTI. #undef RTTI_TYPE_ID -} // namespace protobuf -} // namespace google - +} // namespace protobuf +} // namespace google +} // namespace duckdb #include #endif // GOOGLE_PROTOBUF_ARENA_H__ diff --git a/third_party/google/protobuf/arena_impl.h b/third_party/google/protobuf/arena_impl.h index 2ffac319b41..724d54c7e92 100644 --- a/third_party/google/protobuf/arena_impl.h +++ b/third_party/google/protobuf/arena_impl.h @@ -46,135 +46,148 @@ #include - +namespace duckdb { namespace google { namespace protobuf { namespace internal { inline constexpr size_t AlignUpTo8(size_t n) { - // Align n to next multiple of 8 (from Hacker's Delight, Chapter 3.) - return (n + 7) & static_cast(-8); + // Align n to next multiple of 8 (from Hacker's Delight, Chapter 3.) + return (n + 7) & static_cast(-8); } using LifecycleIdAtomic = uint64_t; // MetricsCollector collects stats for a particular arena. class PROTOBUF_EXPORT ArenaMetricsCollector { - public: - ArenaMetricsCollector(bool record_allocs) : record_allocs_(record_allocs) {} - - // Invoked when the arena is about to be destroyed. This method will - // typically finalize any metric collection and delete the collector. - // space_allocated is the space used by the arena. - virtual void OnDestroy(uint64_t space_allocated) = 0; - - // OnReset() is called when the associated arena is reset. - // space_allocated is the space used by the arena just before the reset. - virtual void OnReset(uint64_t space_allocated) = 0; - - // OnAlloc is called when an allocation happens. - // type_info is promised to be static - its lifetime extends to - // match program's lifetime (It is given by typeid operator). - // Note: typeid(void) will be passed as allocated_type every time we - // intentionally want to avoid monitoring an allocation. (i.e. internal - // allocations for managing the arena) - virtual void OnAlloc(const std::type_info* allocated_type, - uint64_t alloc_size) = 0; - - // Does OnAlloc() need to be called? If false, metric collection overhead - // will be reduced since we will not do extra work per allocation. - bool RecordAllocs() { return record_allocs_; } - - protected: - // This class is destructed by the call to OnDestroy(). - ~ArenaMetricsCollector() = default; - const bool record_allocs_; +public: + ArenaMetricsCollector(bool record_allocs) : record_allocs_(record_allocs) { + } + + // Invoked when the arena is about to be destroyed. This method will + // typically finalize any metric collection and delete the collector. + // space_allocated is the space used by the arena. + virtual void OnDestroy(uint64_t space_allocated) = 0; + + // OnReset() is called when the associated arena is reset. + // space_allocated is the space used by the arena just before the reset. + virtual void OnReset(uint64_t space_allocated) = 0; + + // OnAlloc is called when an allocation happens. + // type_info is promised to be static - its lifetime extends to + // match program's lifetime (It is given by typeid operator). + // Note: typeid(void) will be passed as allocated_type every time we + // intentionally want to avoid monitoring an allocation. (i.e. internal + // allocations for managing the arena) + virtual void OnAlloc(const std::type_info *allocated_type, uint64_t alloc_size) = 0; + + // Does OnAlloc() need to be called? If false, metric collection overhead + // will be reduced since we will not do extra work per allocation. + bool RecordAllocs() { + return record_allocs_; + } + +protected: + // This class is destructed by the call to OnDestroy(). + ~ArenaMetricsCollector() = default; + const bool record_allocs_; }; struct AllocationPolicy { - static constexpr size_t kDefaultStartBlockSize = 256; - static constexpr size_t kDefaultMaxBlockSize = 8192; - - size_t start_block_size = kDefaultStartBlockSize; - size_t max_block_size = kDefaultMaxBlockSize; - void* (*block_alloc)(size_t) = nullptr; - void (*block_dealloc)(void*, size_t) = nullptr; - ArenaMetricsCollector* metrics_collector = nullptr; - - bool IsDefault() const { - return start_block_size == kDefaultMaxBlockSize && - max_block_size == kDefaultMaxBlockSize && block_alloc == nullptr && - block_dealloc == nullptr && metrics_collector == nullptr; - } + static constexpr size_t kDefaultStartBlockSize = 256; + static constexpr size_t kDefaultMaxBlockSize = 8192; + + size_t start_block_size = kDefaultStartBlockSize; + size_t max_block_size = kDefaultMaxBlockSize; + void *(*block_alloc)(size_t) = nullptr; + void (*block_dealloc)(void *, size_t) = nullptr; + ArenaMetricsCollector *metrics_collector = nullptr; + + bool IsDefault() const { + return start_block_size == kDefaultMaxBlockSize && max_block_size == kDefaultMaxBlockSize && + block_alloc == nullptr && block_dealloc == nullptr && metrics_collector == nullptr; + } }; // Tagged pointer to an AllocationPolicy. class TaggedAllocationPolicyPtr { - public: - constexpr TaggedAllocationPolicyPtr() : policy_(0) {} - - explicit TaggedAllocationPolicyPtr(AllocationPolicy* policy) - : policy_(reinterpret_cast(policy)) {} - - void set_policy(AllocationPolicy* policy) { - auto bits = policy_ & kTagsMask; - policy_ = reinterpret_cast(policy) | bits; - } - - AllocationPolicy* get() { - return reinterpret_cast(policy_ & kPtrMask); - } - const AllocationPolicy* get() const { - return reinterpret_cast(policy_ & kPtrMask); - } - - AllocationPolicy& operator*() { return *get(); } - const AllocationPolicy& operator*() const { return *get(); } - - AllocationPolicy* operator->() { return get(); } - const AllocationPolicy* operator->() const { return get(); } - - bool is_user_owned_initial_block() const { - return static_cast(get_mask()); - } - void set_is_user_owned_initial_block(bool v) { - set_mask(v); - } - - bool should_record_allocs() const { - return static_cast(get_mask()); - } - void set_should_record_allocs(bool v) { set_mask(v); } - - uintptr_t get_raw() const { return policy_; } - - inline void RecordAlloc(const std::type_info* allocated_type, - size_t n) const { - get()->metrics_collector->OnAlloc(allocated_type, n); - } - - private: - enum : uintptr_t { - kUserOwnedInitialBlock = 1, - kRecordAllocs = 2, - }; - - static constexpr uintptr_t kTagsMask = 7; - static constexpr uintptr_t kPtrMask = ~kTagsMask; - - template - uintptr_t get_mask() const { - return policy_ & kMask; - } - template - void set_mask(bool v) { - if (v) { - policy_ |= kMask; - } else { - policy_ &= ~kMask; - } - } - uintptr_t policy_; +public: + constexpr TaggedAllocationPolicyPtr() : policy_(0) { + } + + explicit TaggedAllocationPolicyPtr(AllocationPolicy *policy) : policy_(reinterpret_cast(policy)) { + } + + void set_policy(AllocationPolicy *policy) { + auto bits = policy_ & kTagsMask; + policy_ = reinterpret_cast(policy) | bits; + } + + AllocationPolicy *get() { + return reinterpret_cast(policy_ & kPtrMask); + } + const AllocationPolicy *get() const { + return reinterpret_cast(policy_ & kPtrMask); + } + + AllocationPolicy &operator*() { + return *get(); + } + const AllocationPolicy &operator*() const { + return *get(); + } + + AllocationPolicy *operator->() { + return get(); + } + const AllocationPolicy *operator->() const { + return get(); + } + + bool is_user_owned_initial_block() const { + return static_cast(get_mask()); + } + void set_is_user_owned_initial_block(bool v) { + set_mask(v); + } + + bool should_record_allocs() const { + return static_cast(get_mask()); + } + void set_should_record_allocs(bool v) { + set_mask(v); + } + + uintptr_t get_raw() const { + return policy_; + } + + inline void RecordAlloc(const std::type_info *allocated_type, size_t n) const { + get()->metrics_collector->OnAlloc(allocated_type, n); + } + +private: + enum : uintptr_t { + kUserOwnedInitialBlock = 1, + kRecordAllocs = 2, + }; + + static constexpr uintptr_t kTagsMask = 7; + static constexpr uintptr_t kPtrMask = ~kTagsMask; + + template + uintptr_t get_mask() const { + return policy_ & kMask; + } + template + void set_mask(bool v) { + if (v) { + policy_ |= kMask; + } else { + policy_ &= ~kMask; + } + } + uintptr_t policy_; }; // A simple arena allocator. Calls to allocate functions must be properly @@ -189,150 +202,156 @@ class TaggedAllocationPolicyPtr { // contains the information on block growth policy and backing memory allocation // used. class PROTOBUF_EXPORT SerialArena { - public: - struct Memory { - void* ptr; - size_t size; - }; - - // Node contains the ptr of the object to be cleaned up and the associated - // cleanup function ptr. - struct CleanupNode { - void* elem; // Pointer to the object to be cleaned up. - void (*cleanup)(void*); // Function pointer to the destructor or deleter. - }; - - void CleanupList(); - uint64_t SpaceAllocated() const { - return space_allocated_.load(std::memory_order_relaxed); - } - uint64_t SpaceUsed() const; - - bool HasSpace(size_t n) { return n <= static_cast(limit_ - ptr_); } - - void* AllocateAligned(size_t n, const AllocationPolicy* policy) { - GOOGLE_DCHECK_EQ(internal::AlignUpTo8(n), n); // Must be already aligned. - GOOGLE_DCHECK_GE(limit_, ptr_); - if (PROTOBUF_PREDICT_FALSE(!HasSpace(n))) { - return AllocateAlignedFallback(n, policy); - } - return AllocateFromExisting(n); - } - - private: - void* AllocateFromExisting(size_t n) { - void* ret = ptr_; - ptr_ += n; +public: + struct Memory { + void *ptr; + size_t size; + }; + + // Node contains the ptr of the object to be cleaned up and the associated + // cleanup function ptr. + struct CleanupNode { + void *elem; // Pointer to the object to be cleaned up. + void (*cleanup)(void *); // Function pointer to the destructor or deleter. + }; + + void CleanupList(); + uint64_t SpaceAllocated() const { + return space_allocated_.load(std::memory_order_relaxed); + } + uint64_t SpaceUsed() const; + + bool HasSpace(size_t n) { + return n <= static_cast(limit_ - ptr_); + } + + void *AllocateAligned(size_t n, const AllocationPolicy *policy) { + GOOGLE_DCHECK_EQ(internal::AlignUpTo8(n), n); // Must be already aligned. + GOOGLE_DCHECK_GE(limit_, ptr_); + if (PROTOBUF_PREDICT_FALSE(!HasSpace(n))) { + return AllocateAlignedFallback(n, policy); + } + return AllocateFromExisting(n); + } + +private: + void *AllocateFromExisting(size_t n) { + void *ret = ptr_; + ptr_ += n; #ifdef ADDRESS_SANITIZER - ASAN_UNPOISON_MEMORY_REGION(ret, n); -#endif // ADDRESS_SANITIZER - return ret; - } - - public: - // Allocate space if the current region provides enough space. - bool MaybeAllocateAligned(size_t n, void** out) { - GOOGLE_DCHECK_EQ(internal::AlignUpTo8(n), n); // Must be already aligned. - GOOGLE_DCHECK_GE(limit_, ptr_); - if (PROTOBUF_PREDICT_FALSE(!HasSpace(n))) return false; - *out = AllocateFromExisting(n); - return true; - } - - std::pair AllocateAlignedWithCleanup( - size_t n, const AllocationPolicy* policy) { - GOOGLE_DCHECK_EQ(internal::AlignUpTo8(n), n); // Must be already aligned. - if (PROTOBUF_PREDICT_FALSE(!HasSpace(n + kCleanupSize))) { - return AllocateAlignedWithCleanupFallback(n, policy); - } - return AllocateFromExistingWithCleanupFallback(n); - } - - private: - std::pair AllocateFromExistingWithCleanupFallback( - size_t n) { - void* ret = ptr_; - ptr_ += n; - limit_ -= kCleanupSize; + ASAN_UNPOISON_MEMORY_REGION(ret, n); +#endif // ADDRESS_SANITIZER + return ret; + } + +public: + // Allocate space if the current region provides enough space. + bool MaybeAllocateAligned(size_t n, void **out) { + GOOGLE_DCHECK_EQ(internal::AlignUpTo8(n), n); // Must be already aligned. + GOOGLE_DCHECK_GE(limit_, ptr_); + if (PROTOBUF_PREDICT_FALSE(!HasSpace(n))) + return false; + *out = AllocateFromExisting(n); + return true; + } + + std::pair AllocateAlignedWithCleanup(size_t n, const AllocationPolicy *policy) { + GOOGLE_DCHECK_EQ(internal::AlignUpTo8(n), n); // Must be already aligned. + if (PROTOBUF_PREDICT_FALSE(!HasSpace(n + kCleanupSize))) { + return AllocateAlignedWithCleanupFallback(n, policy); + } + return AllocateFromExistingWithCleanupFallback(n); + } + +private: + std::pair AllocateFromExistingWithCleanupFallback(size_t n) { + void *ret = ptr_; + ptr_ += n; + limit_ -= kCleanupSize; #ifdef ADDRESS_SANITIZER - ASAN_UNPOISON_MEMORY_REGION(ret, n); - ASAN_UNPOISON_MEMORY_REGION(limit_, kCleanupSize); -#endif // ADDRESS_SANITIZER - return CreatePair(ret, reinterpret_cast(limit_)); - } - - public: - void AddCleanup(void* elem, void (*cleanup)(void*), - const AllocationPolicy* policy) { - auto res = AllocateAlignedWithCleanup(0, policy); - res.second->elem = elem; - res.second->cleanup = cleanup; - } - - void* owner() const { return owner_; } - SerialArena* next() const { return next_; } - void set_next(SerialArena* next) { next_ = next; } - - private: - friend class ThreadSafeArena; - friend class ArenaBenchmark; - - // Creates a new SerialArena inside mem using the remaining memory as for - // future allocations. - static SerialArena* New(SerialArena::Memory mem, void* owner); - // Free SerialArena returning the memory passed in to New - template - Memory Free(Deallocator deallocator); - - // Blocks are variable length malloc-ed objects. The following structure - // describes the common header for all blocks. - struct Block { - Block(Block* next, size_t size) : next(next), size(size), start(nullptr) {} - - char* Pointer(size_t n) { - GOOGLE_DCHECK(n <= size); - return reinterpret_cast(this) + n; - } - - Block* const next; - const size_t size; - CleanupNode* start; - // data follows - }; - - void* owner_; // &ThreadCache of this thread; - Block* head_; // Head of linked list of blocks. - SerialArena* next_; // Next SerialArena in this linked list. - size_t space_used_ = 0; // Necessary for metrics. - std::atomic space_allocated_; - - // Next pointer to allocate from. Always 8-byte aligned. Points inside - // head_ (and head_->pos will always be non-canonical). We keep these - // here to reduce indirection. - char* ptr_; - char* limit_; - - // Constructor is private as only New() should be used. - inline SerialArena(Block* b, void* owner); - void* AllocateAlignedFallback(size_t n, const AllocationPolicy* policy); - std::pair AllocateAlignedWithCleanupFallback( - size_t n, const AllocationPolicy* policy); - void AllocateNewBlock(size_t n, const AllocationPolicy* policy); - - std::pair CreatePair(void* ptr, CleanupNode* node) { - return {ptr, node}; - } - - public: - static constexpr size_t kBlockHeaderSize = AlignUpTo8(sizeof(Block)); - static constexpr size_t kCleanupSize = AlignUpTo8(sizeof(CleanupNode)); + ASAN_UNPOISON_MEMORY_REGION(ret, n); + ASAN_UNPOISON_MEMORY_REGION(limit_, kCleanupSize); +#endif // ADDRESS_SANITIZER + return CreatePair(ret, reinterpret_cast(limit_)); + } + +public: + void AddCleanup(void *elem, void (*cleanup)(void *), const AllocationPolicy *policy) { + auto res = AllocateAlignedWithCleanup(0, policy); + res.second->elem = elem; + res.second->cleanup = cleanup; + } + + void *owner() const { + return owner_; + } + SerialArena *next() const { + return next_; + } + void set_next(SerialArena *next) { + next_ = next; + } + +private: + friend class ThreadSafeArena; + friend class ArenaBenchmark; + + // Creates a new SerialArena inside mem using the remaining memory as for + // future allocations. + static SerialArena *New(SerialArena::Memory mem, void *owner); + // Free SerialArena returning the memory passed in to New + template + Memory Free(Deallocator deallocator); + + // Blocks are variable length malloc-ed objects. The following structure + // describes the common header for all blocks. + struct Block { + Block(Block *next, size_t size) : next(next), size(size), start(nullptr) { + } + + char *Pointer(size_t n) { + GOOGLE_DCHECK(n <= size); + return reinterpret_cast(this) + n; + } + + Block *const next; + const size_t size; + CleanupNode *start; + // data follows + }; + + void *owner_; // &ThreadCache of this thread; + Block *head_; // Head of linked list of blocks. + SerialArena *next_; // Next SerialArena in this linked list. + size_t space_used_ = 0; // Necessary for metrics. + std::atomic space_allocated_; + + // Next pointer to allocate from. Always 8-byte aligned. Points inside + // head_ (and head_->pos will always be non-canonical). We keep these + // here to reduce indirection. + char *ptr_; + char *limit_; + + // Constructor is private as only New() should be used. + inline SerialArena(Block *b, void *owner); + void *AllocateAlignedFallback(size_t n, const AllocationPolicy *policy); + std::pair AllocateAlignedWithCleanupFallback(size_t n, const AllocationPolicy *policy); + void AllocateNewBlock(size_t n, const AllocationPolicy *policy); + + std::pair CreatePair(void *ptr, CleanupNode *node) { + return {ptr, node}; + } + +public: + static constexpr size_t kBlockHeaderSize = AlignUpTo8(sizeof(Block)); + static constexpr size_t kCleanupSize = AlignUpTo8(sizeof(CleanupNode)); }; // Tag type used to invoke the constructor of message-owned arena. // Only message-owned arenas use this constructor for creation. // Such constructors are internal implementation details of the library. struct MessageOwned { - explicit MessageOwned() = default; + explicit MessageOwned() = default; }; // This class provides the core Arena memory allocation library. Different @@ -342,221 +361,221 @@ struct MessageOwned { // the memory allocation part from the cruft of the API users expect we can // use #ifdef the select the best implementation based on hardware / OS. class PROTOBUF_EXPORT ThreadSafeArena { - public: - ThreadSafeArena() { Init(); } - - // Constructor solely used by message-owned arena. - ThreadSafeArena(internal::MessageOwned) : tag_and_id_(kMessageOwnedArena) { - Init(); - } - - ThreadSafeArena(char* mem, size_t size) { InitializeFrom(mem, size); } - - explicit ThreadSafeArena(void* mem, size_t size, - const AllocationPolicy& policy) { - InitializeWithPolicy(mem, size, policy); - } - - // Destructor deletes all owned heap allocated objects, and destructs objects - // that have non-trivial destructors, except for proto2 message objects whose - // destructors can be skipped. Also, frees all blocks except the initial block - // if it was passed in. - ~ThreadSafeArena(); - - uint64_t Reset(); - - uint64_t SpaceAllocated() const; - uint64_t SpaceUsed() const; - - void* AllocateAligned(size_t n, const std::type_info* type) { - SerialArena* arena; - if (PROTOBUF_PREDICT_TRUE(!alloc_policy_.should_record_allocs() && - GetSerialArenaFast(&arena))) { - return arena->AllocateAligned(n, AllocPolicy()); - } else { - return AllocateAlignedFallback(n, type); - } - } - - // This function allocates n bytes if the common happy case is true and - // returns true. Otherwise does nothing and returns false. This strange - // semantics is necessary to allow callers to program functions that only - // have fallback function calls in tail position. This substantially improves - // code for the happy path. - PROTOBUF_NDEBUG_INLINE bool MaybeAllocateAligned(size_t n, void** out) { - SerialArena* a; - if (PROTOBUF_PREDICT_TRUE(!alloc_policy_.should_record_allocs() && - GetSerialArenaFromThreadCache(&a))) { - return a->MaybeAllocateAligned(n, out); - } - return false; - } - - std::pair AllocateAlignedWithCleanup( - size_t n, const std::type_info* type); - - // Add object pointer and cleanup function pointer to the list. - void AddCleanup(void* elem, void (*cleanup)(void*)); - - // Checks whether this arena is message-owned. - PROTOBUF_ALWAYS_INLINE bool IsMessageOwned() const { - return tag_and_id_ & kMessageOwnedArena; - } - - private: - // Unique for each arena. Changes on Reset(). - uint64_t tag_and_id_ = 0; - // The LSB of tag_and_id_ indicates if the arena is message-owned. - enum : uint64_t { kMessageOwnedArena = 1 }; - - TaggedAllocationPolicyPtr alloc_policy_; // Tagged pointer to AllocPolicy. - - // Pointer to a linked list of SerialArena. - std::atomic threads_; - std::atomic hint_; // Fast thread-local block access - - const AllocationPolicy* AllocPolicy() const { return alloc_policy_.get(); } - void InitializeFrom(void* mem, size_t size); - void InitializeWithPolicy(void* mem, size_t size, AllocationPolicy policy); - void* AllocateAlignedFallback(size_t n, const std::type_info* type); - std::pair - AllocateAlignedWithCleanupFallback(size_t n, const std::type_info* type); - - void Init(); - void SetInitialBlock(void* mem, size_t size); - - // Delete or Destruct all objects owned by the arena. - void CleanupList(); - - inline uint64_t LifeCycleId() const { - return tag_and_id_ & ~kMessageOwnedArena; - } - - inline void CacheSerialArena(SerialArena* serial) { - thread_cache().last_serial_arena = serial; - thread_cache().last_lifecycle_id_seen = tag_and_id_; - // TODO(haberman): evaluate whether we would gain efficiency by getting rid - // of hint_. It's the only write we do to ThreadSafeArena in the allocation - // path, which will dirty the cache line. - - hint_.store(serial, std::memory_order_release); - } - - PROTOBUF_NDEBUG_INLINE bool GetSerialArenaFast(SerialArena** arena) { - if (GetSerialArenaFromThreadCache(arena)) return true; - - // Check whether we own the last accessed SerialArena on this arena. This - // fast path optimizes the case where a single thread uses multiple arenas. - ThreadCache* tc = &thread_cache(); - SerialArena* serial = hint_.load(std::memory_order_acquire); - if (PROTOBUF_PREDICT_TRUE(serial != NULL && serial->owner() == tc)) { - *arena = serial; - return true; - } - return false; - } - - PROTOBUF_NDEBUG_INLINE bool GetSerialArenaFromThreadCache( - SerialArena** arena) { - // If this thread already owns a block in this arena then try to use that. - // This fast path optimizes the case where multiple threads allocate from - // the same arena. - ThreadCache* tc = &thread_cache(); - if (PROTOBUF_PREDICT_TRUE(tc->last_lifecycle_id_seen == tag_and_id_)) { - *arena = tc->last_serial_arena; - return true; - } - return false; - } - SerialArena* GetSerialArenaFallback(void* me); - - template - void PerSerialArena(Functor fn) { - // By omitting an Acquire barrier we ensure that any user code that doesn't - // properly synchronize Reset() or the destructor will throw a TSAN warning. - SerialArena* serial = threads_.load(std::memory_order_relaxed); - - for (; serial; serial = serial->next()) fn(serial); - } - - // Releases all memory except the first block which it returns. The first - // block might be owned by the user and thus need some extra checks before - // deleting. - SerialArena::Memory Free(size_t* space_allocated); +public: + ThreadSafeArena() { + Init(); + } + + // Constructor solely used by message-owned arena. + ThreadSafeArena(internal::MessageOwned) : tag_and_id_(kMessageOwnedArena) { + Init(); + } + + ThreadSafeArena(char *mem, size_t size) { + InitializeFrom(mem, size); + } + + explicit ThreadSafeArena(void *mem, size_t size, const AllocationPolicy &policy) { + InitializeWithPolicy(mem, size, policy); + } + + // Destructor deletes all owned heap allocated objects, and destructs objects + // that have non-trivial destructors, except for proto2 message objects whose + // destructors can be skipped. Also, frees all blocks except the initial block + // if it was passed in. + ~ThreadSafeArena(); + + uint64_t Reset(); + + uint64_t SpaceAllocated() const; + uint64_t SpaceUsed() const; + + void *AllocateAligned(size_t n, const std::type_info *type) { + SerialArena *arena; + if (PROTOBUF_PREDICT_TRUE(!alloc_policy_.should_record_allocs() && GetSerialArenaFast(&arena))) { + return arena->AllocateAligned(n, AllocPolicy()); + } else { + return AllocateAlignedFallback(n, type); + } + } + + // This function allocates n bytes if the common happy case is true and + // returns true. Otherwise does nothing and returns false. This strange + // semantics is necessary to allow callers to program functions that only + // have fallback function calls in tail position. This substantially improves + // code for the happy path. + PROTOBUF_NDEBUG_INLINE bool MaybeAllocateAligned(size_t n, void **out) { + SerialArena *a; + if (PROTOBUF_PREDICT_TRUE(!alloc_policy_.should_record_allocs() && GetSerialArenaFromThreadCache(&a))) { + return a->MaybeAllocateAligned(n, out); + } + return false; + } + + std::pair AllocateAlignedWithCleanup(size_t n, const std::type_info *type); + + // Add object pointer and cleanup function pointer to the list. + void AddCleanup(void *elem, void (*cleanup)(void *)); + + // Checks whether this arena is message-owned. + PROTOBUF_ALWAYS_INLINE bool IsMessageOwned() const { + return tag_and_id_ & kMessageOwnedArena; + } + +private: + // Unique for each arena. Changes on Reset(). + uint64_t tag_and_id_ = 0; + // The LSB of tag_and_id_ indicates if the arena is message-owned. + enum : uint64_t { kMessageOwnedArena = 1 }; + + TaggedAllocationPolicyPtr alloc_policy_; // Tagged pointer to AllocPolicy. + + // Pointer to a linked list of SerialArena. + std::atomic threads_; + std::atomic hint_; // Fast thread-local block access + + const AllocationPolicy *AllocPolicy() const { + return alloc_policy_.get(); + } + void InitializeFrom(void *mem, size_t size); + void InitializeWithPolicy(void *mem, size_t size, AllocationPolicy policy); + void *AllocateAlignedFallback(size_t n, const std::type_info *type); + std::pair AllocateAlignedWithCleanupFallback(size_t n, + const std::type_info *type); + + void Init(); + void SetInitialBlock(void *mem, size_t size); + + // Delete or Destruct all objects owned by the arena. + void CleanupList(); + + inline uint64_t LifeCycleId() const { + return tag_and_id_ & ~kMessageOwnedArena; + } + + inline void CacheSerialArena(SerialArena *serial) { + thread_cache().last_serial_arena = serial; + thread_cache().last_lifecycle_id_seen = tag_and_id_; + // TODO(haberman): evaluate whether we would gain efficiency by getting rid + // of hint_. It's the only write we do to ThreadSafeArena in the allocation + // path, which will dirty the cache line. + + hint_.store(serial, std::memory_order_release); + } + + PROTOBUF_NDEBUG_INLINE bool GetSerialArenaFast(SerialArena **arena) { + if (GetSerialArenaFromThreadCache(arena)) + return true; + + // Check whether we own the last accessed SerialArena on this arena. This + // fast path optimizes the case where a single thread uses multiple arenas. + ThreadCache *tc = &thread_cache(); + SerialArena *serial = hint_.load(std::memory_order_acquire); + if (PROTOBUF_PREDICT_TRUE(serial != NULL && serial->owner() == tc)) { + *arena = serial; + return true; + } + return false; + } + + PROTOBUF_NDEBUG_INLINE bool GetSerialArenaFromThreadCache(SerialArena **arena) { + // If this thread already owns a block in this arena then try to use that. + // This fast path optimizes the case where multiple threads allocate from + // the same arena. + ThreadCache *tc = &thread_cache(); + if (PROTOBUF_PREDICT_TRUE(tc->last_lifecycle_id_seen == tag_and_id_)) { + *arena = tc->last_serial_arena; + return true; + } + return false; + } + SerialArena *GetSerialArenaFallback(void *me); + + template + void PerSerialArena(Functor fn) { + // By omitting an Acquire barrier we ensure that any user code that doesn't + // properly synchronize Reset() or the destructor will throw a TSAN warning. + SerialArena *serial = threads_.load(std::memory_order_relaxed); + + for (; serial; serial = serial->next()) + fn(serial); + } + + // Releases all memory except the first block which it returns. The first + // block might be owned by the user and thus need some extra checks before + // deleting. + SerialArena::Memory Free(size_t *space_allocated); #ifdef _MSC_VER #pragma warning(disable : 4324) #endif - struct alignas(64) ThreadCache { + struct alignas(64) ThreadCache { #if defined(GOOGLE_PROTOBUF_NO_THREADLOCAL) - // If we are using the ThreadLocalStorage class to store the ThreadCache, - // then the ThreadCache's default constructor has to be responsible for - // initializing it. - ThreadCache() - : next_lifecycle_id(0), - last_lifecycle_id_seen(-1), - last_serial_arena(NULL) {} + // If we are using the ThreadLocalStorage class to store the ThreadCache, + // then the ThreadCache's default constructor has to be responsible for + // initializing it. + ThreadCache() : next_lifecycle_id(0), last_lifecycle_id_seen(-1), last_serial_arena(NULL) { + } #endif - // Number of per-thread lifecycle IDs to reserve. Must be power of two. - // To reduce contention on a global atomic, each thread reserves a batch of - // IDs. The following number is calculated based on a stress test with - // ~6500 threads all frequently allocating a new arena. - static constexpr size_t kPerThreadIds = 256; - // Next lifecycle ID available to this thread. We need to reserve a new - // batch, if `next_lifecycle_id & (kPerThreadIds - 1) == 0`. - uint64_t next_lifecycle_id; - // The ThreadCache is considered valid as long as this matches the - // lifecycle_id of the arena being used. - uint64_t last_lifecycle_id_seen; - SerialArena* last_serial_arena; - }; - - // Lifecycle_id can be highly contended variable in a situation of lots of - // arena creation. Make sure that other global variables are not sharing the - // cacheline. + // Number of per-thread lifecycle IDs to reserve. Must be power of two. + // To reduce contention on a global atomic, each thread reserves a batch of + // IDs. The following number is calculated based on a stress test with + // ~6500 threads all frequently allocating a new arena. + static constexpr size_t kPerThreadIds = 256; + // Next lifecycle ID available to this thread. We need to reserve a new + // batch, if `next_lifecycle_id & (kPerThreadIds - 1) == 0`. + uint64_t next_lifecycle_id; + // The ThreadCache is considered valid as long as this matches the + // lifecycle_id of the arena being used. + uint64_t last_lifecycle_id_seen; + SerialArena *last_serial_arena; + }; + + // Lifecycle_id can be highly contended variable in a situation of lots of + // arena creation. Make sure that other global variables are not sharing the + // cacheline. #ifdef _MSC_VER #pragma warning(disable : 4324) #endif - struct alignas(64) CacheAlignedLifecycleIdGenerator { - std::atomic id; - }; - static CacheAlignedLifecycleIdGenerator lifecycle_id_generator_; + struct alignas(64) CacheAlignedLifecycleIdGenerator { + std::atomic id; + }; + static CacheAlignedLifecycleIdGenerator lifecycle_id_generator_; #if defined(GOOGLE_PROTOBUF_NO_THREADLOCAL) - // iOS does not support __thread keyword so we use a custom thread local - // storage class we implemented. - static ThreadCache& thread_cache(); + // iOS does not support __thread keyword so we use a custom thread local + // storage class we implemented. + static ThreadCache &thread_cache(); #elif defined(PROTOBUF_USE_DLLS) - // Thread local variables cannot be exposed through DLL interface but we can - // wrap them in static functions. - static ThreadCache& thread_cache(); + // Thread local variables cannot be exposed through DLL interface but we can + // wrap them in static functions. + static ThreadCache &thread_cache(); #else - static PROTOBUF_THREAD_LOCAL ThreadCache thread_cache_; - static ThreadCache& thread_cache() { return thread_cache_; } + static PROTOBUF_THREAD_LOCAL ThreadCache thread_cache_; + static ThreadCache &thread_cache() { + return thread_cache_; + } #endif - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ThreadSafeArena); - // All protos have pointers back to the arena hence Arena must have - // pointer stability. - ThreadSafeArena(ThreadSafeArena&&) = delete; - ThreadSafeArena& operator=(ThreadSafeArena&&) = delete; - - public: - // kBlockHeaderSize is sizeof(Block), aligned up to the nearest multiple of 8 - // to protect the invariant that pos is always at a multiple of 8. - static constexpr size_t kBlockHeaderSize = SerialArena::kBlockHeaderSize; - static constexpr size_t kSerialArenaSize = - (sizeof(SerialArena) + 7) & static_cast(-8); - static_assert(kBlockHeaderSize % 8 == 0, - "kBlockHeaderSize must be a multiple of 8."); - static_assert(kSerialArenaSize % 8 == 0, - "kSerialArenaSize must be a multiple of 8."); + GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ThreadSafeArena); + // All protos have pointers back to the arena hence Arena must have + // pointer stability. + ThreadSafeArena(ThreadSafeArena &&) = delete; + ThreadSafeArena &operator=(ThreadSafeArena &&) = delete; + +public: + // kBlockHeaderSize is sizeof(Block), aligned up to the nearest multiple of 8 + // to protect the invariant that pos is always at a multiple of 8. + static constexpr size_t kBlockHeaderSize = SerialArena::kBlockHeaderSize; + static constexpr size_t kSerialArenaSize = (sizeof(SerialArena) + 7) & static_cast(-8); + static_assert(kBlockHeaderSize % 8 == 0, "kBlockHeaderSize must be a multiple of 8."); + static_assert(kSerialArenaSize % 8 == 0, "kSerialArenaSize must be a multiple of 8."); }; -} // namespace internal -} // namespace protobuf -} // namespace google - +} // namespace internal +} // namespace protobuf +} // namespace google +} // namespace duckdb #include #endif // GOOGLE_PROTOBUF_ARENA_IMPL_H__ diff --git a/third_party/google/protobuf/arenastring.cc b/third_party/google/protobuf/arenastring.cc index 169f52729d6..d3fe6703e7d 100644 --- a/third_party/google/protobuf/arenastring.cc +++ b/third_party/google/protobuf/arenastring.cc @@ -43,244 +43,232 @@ #include // clang-format on +namespace duckdb { namespace google { namespace protobuf { namespace internal { -const std::string& LazyString::Init() const { - static WrappedMutex mu{GOOGLE_PROTOBUF_LINKER_INITIALIZED}; - mu.Lock(); - const std::string* res = inited_.load(std::memory_order_acquire); - if (res == nullptr) { - auto init_value = init_value_; - res = ::new (static_cast(string_buf_)) - std::string(init_value.ptr, init_value.size); - inited_.store(res, std::memory_order_release); - } - mu.Unlock(); - return *res; +const std::string &LazyString::Init() const { + static WrappedMutex mu {GOOGLE_PROTOBUF_LINKER_INITIALIZED}; + mu.Lock(); + const std::string *res = inited_.load(std::memory_order_acquire); + if (res == nullptr) { + auto init_value = init_value_; + res = ::new (static_cast(string_buf_)) std::string(init_value.ptr, init_value.size); + inited_.store(res, std::memory_order_release); + } + mu.Unlock(); + return *res; } - -std::string* ArenaStringPtr::SetAndReturnNewString() { - std::string* new_string = new std::string(); - tagged_ptr_.Set(new_string); - return new_string; +std::string *ArenaStringPtr::SetAndReturnNewString() { + std::string *new_string = new std::string(); + tagged_ptr_.Set(new_string); + return new_string; } -void ArenaStringPtr::DestroyNoArenaSlowPath() { delete UnsafeMutablePointer(); } +void ArenaStringPtr::DestroyNoArenaSlowPath() { + delete UnsafeMutablePointer(); +} -void ArenaStringPtr::Set(const std::string* default_value, - ConstStringParam value, ::google::protobuf::Arena* arena) { - if (IsDefault(default_value)) { - tagged_ptr_.Set(Arena::Create(arena, value)); - } else { - UnsafeMutablePointer()->assign(value.data(), value.length()); - } +void ArenaStringPtr::Set(const std::string *default_value, ConstStringParam value, + duckdb::google::protobuf::Arena *arena) { + if (IsDefault(default_value)) { + tagged_ptr_.Set(Arena::Create(arena, value)); + } else { + UnsafeMutablePointer()->assign(value.data(), value.length()); + } } -void ArenaStringPtr::Set(const std::string* default_value, std::string&& value, - ::google::protobuf::Arena* arena) { - if (IsDefault(default_value)) { - if (arena == nullptr) { - tagged_ptr_.Set(new std::string(std::move(value))); - } else { - tagged_ptr_.Set(Arena::Create(arena, std::move(value))); - } - } else if (IsDonatedString()) { - std::string* current = tagged_ptr_.Get(); - auto* s = new (current) std::string(std::move(value)); - arena->OwnDestructor(s); - tagged_ptr_.Set(s); - } else /* !IsDonatedString() */ { - *UnsafeMutablePointer() = std::move(value); - } +void ArenaStringPtr::Set(const std::string *default_value, std::string &&value, + duckdb::google::protobuf::Arena *arena) { + if (IsDefault(default_value)) { + if (arena == nullptr) { + tagged_ptr_.Set(new std::string(std::move(value))); + } else { + tagged_ptr_.Set(Arena::Create(arena, std::move(value))); + } + } else if (IsDonatedString()) { + std::string *current = tagged_ptr_.Get(); + auto *s = new (current) std::string(std::move(value)); + arena->OwnDestructor(s); + tagged_ptr_.Set(s); + } else /* !IsDonatedString() */ { + *UnsafeMutablePointer() = std::move(value); + } } -void ArenaStringPtr::Set(EmptyDefault, ConstStringParam value, - ::google::protobuf::Arena* arena) { - Set(&GetEmptyStringAlreadyInited(), value, arena); +void ArenaStringPtr::Set(EmptyDefault, ConstStringParam value, duckdb::google::protobuf::Arena *arena) { + Set(&GetEmptyStringAlreadyInited(), value, arena); } -void ArenaStringPtr::Set(EmptyDefault, std::string&& value, - ::google::protobuf::Arena* arena) { - Set(&GetEmptyStringAlreadyInited(), std::move(value), arena); +void ArenaStringPtr::Set(EmptyDefault, std::string &&value, duckdb::google::protobuf::Arena *arena) { + Set(&GetEmptyStringAlreadyInited(), std::move(value), arena); } -void ArenaStringPtr::Set(NonEmptyDefault, ConstStringParam value, - ::google::protobuf::Arena* arena) { - Set(nullptr, value, arena); +void ArenaStringPtr::Set(NonEmptyDefault, ConstStringParam value, duckdb::google::protobuf::Arena *arena) { + Set(nullptr, value, arena); } -void ArenaStringPtr::Set(NonEmptyDefault, std::string&& value, - ::google::protobuf::Arena* arena) { - Set(nullptr, std::move(value), arena); +void ArenaStringPtr::Set(NonEmptyDefault, std::string &&value, duckdb::google::protobuf::Arena *arena) { + Set(nullptr, std::move(value), arena); } -std::string* ArenaStringPtr::Mutable(EmptyDefault, ::google::protobuf::Arena* arena) { - if (!IsDonatedString() && !IsDefault(&GetEmptyStringAlreadyInited())) { - return UnsafeMutablePointer(); - } else { - return MutableSlow(arena); - } +std::string *ArenaStringPtr::Mutable(EmptyDefault, duckdb::google::protobuf::Arena *arena) { + if (!IsDonatedString() && !IsDefault(&GetEmptyStringAlreadyInited())) { + return UnsafeMutablePointer(); + } else { + return MutableSlow(arena); + } } -std::string* ArenaStringPtr::Mutable(const LazyString& default_value, - ::google::protobuf::Arena* arena) { - if (!IsDonatedString() && !IsDefault(nullptr)) { - return UnsafeMutablePointer(); - } else { - return MutableSlow(arena, default_value); - } +std::string *ArenaStringPtr::Mutable(const LazyString &default_value, duckdb::google::protobuf::Arena *arena) { + if (!IsDonatedString() && !IsDefault(nullptr)) { + return UnsafeMutablePointer(); + } else { + return MutableSlow(arena, default_value); + } } -std::string* ArenaStringPtr::MutableNoCopy(const std::string* default_value, - ::google::protobuf::Arena* arena) { - if (!IsDonatedString() && !IsDefault(default_value)) { - return UnsafeMutablePointer(); - } else { - GOOGLE_DCHECK(IsDefault(default_value)); - // Allocate empty. The contents are not relevant. - std::string* new_string = Arena::Create(arena); - tagged_ptr_.Set(new_string); - return new_string; - } +std::string *ArenaStringPtr::MutableNoCopy(const std::string *default_value, duckdb::google::protobuf::Arena *arena) { + if (!IsDonatedString() && !IsDefault(default_value)) { + return UnsafeMutablePointer(); + } else { + GOOGLE_DCHECK(IsDefault(default_value)); + // Allocate empty. The contents are not relevant. + std::string *new_string = Arena::Create(arena); + tagged_ptr_.Set(new_string); + return new_string; + } } template -std::string* ArenaStringPtr::MutableSlow(::google::protobuf::Arena* arena, - const Lazy&... lazy_default) { - const std::string* const default_value = - sizeof...(Lazy) == 0 ? &GetEmptyStringAlreadyInited() : nullptr; - GOOGLE_DCHECK(IsDefault(default_value)); - std::string* new_string = - Arena::Create(arena, lazy_default.get()...); - tagged_ptr_.Set(new_string); - return new_string; +std::string *ArenaStringPtr::MutableSlow(duckdb::google::protobuf::Arena *arena, const Lazy &...lazy_default) { + const std::string *const default_value = sizeof...(Lazy) == 0 ? &GetEmptyStringAlreadyInited() : nullptr; + GOOGLE_DCHECK(IsDefault(default_value)); + std::string *new_string = Arena::Create(arena, lazy_default.get()...); + tagged_ptr_.Set(new_string); + return new_string; } -std::string* ArenaStringPtr::Release(const std::string* default_value, - ::google::protobuf::Arena* arena) { - if (IsDefault(default_value)) { - return nullptr; - } else { - return ReleaseNonDefault(default_value, arena); - } +std::string *ArenaStringPtr::Release(const std::string *default_value, duckdb::google::protobuf::Arena *arena) { + if (IsDefault(default_value)) { + return nullptr; + } else { + return ReleaseNonDefault(default_value, arena); + } } -std::string* ArenaStringPtr::ReleaseNonDefault(const std::string* default_value, - ::google::protobuf::Arena* arena) { - GOOGLE_DCHECK(!IsDefault(default_value)); - - if (!IsDonatedString()) { - std::string* released; - if (arena != nullptr) { - released = new std::string; - released->swap(*UnsafeMutablePointer()); - } else { - released = UnsafeMutablePointer(); - } - tagged_ptr_.Set(const_cast(default_value)); - return released; - } else /* IsDonatedString() */ { - GOOGLE_DCHECK(arena != nullptr); - std::string* released = new std::string(Get()); - tagged_ptr_.Set(const_cast(default_value)); - return released; - } +std::string *ArenaStringPtr::ReleaseNonDefault(const std::string *default_value, + duckdb::google::protobuf::Arena *arena) { + GOOGLE_DCHECK(!IsDefault(default_value)); + + if (!IsDonatedString()) { + std::string *released; + if (arena != nullptr) { + released = new std::string; + released->swap(*UnsafeMutablePointer()); + } else { + released = UnsafeMutablePointer(); + } + tagged_ptr_.Set(const_cast(default_value)); + return released; + } else /* IsDonatedString() */ { + GOOGLE_DCHECK(arena != nullptr); + std::string *released = new std::string(Get()); + tagged_ptr_.Set(const_cast(default_value)); + return released; + } } -void ArenaStringPtr::SetAllocated(const std::string* default_value, - std::string* value, ::google::protobuf::Arena* arena) { - // Release what we have first. - if (arena == nullptr && !IsDefault(default_value)) { - delete UnsafeMutablePointer(); - } - if (value == nullptr) { - tagged_ptr_.Set(const_cast(default_value)); - } else { +void ArenaStringPtr::SetAllocated(const std::string *default_value, std::string *value, + duckdb::google::protobuf::Arena *arena) { + // Release what we have first. + if (arena == nullptr && !IsDefault(default_value)) { + delete UnsafeMutablePointer(); + } + if (value == nullptr) { + tagged_ptr_.Set(const_cast(default_value)); + } else { #ifdef NDEBUG - tagged_ptr_.Set(value); - if (arena != nullptr) { - arena->Own(value); - } + tagged_ptr_.Set(value); + if (arena != nullptr) { + arena->Own(value); + } #else - // On debug builds, copy the string so the address differs. delete will - // fail if value was a stack-allocated temporary/etc., which would have - // failed when arena ran its cleanup list. - std::string* new_value = Arena::Create(arena, *value); - delete value; - tagged_ptr_.Set(new_value); + // On debug builds, copy the string so the address differs. delete will + // fail if value was a stack-allocated temporary/etc., which would have + // failed when arena ran its cleanup list. + std::string *new_value = Arena::Create(arena, *value); + delete value; + tagged_ptr_.Set(new_value); #endif - } + } } -void ArenaStringPtr::Destroy(const std::string* default_value, - ::google::protobuf::Arena* arena) { - if (arena == nullptr) { - GOOGLE_DCHECK(!IsDonatedString()); - if (!IsDefault(default_value)) { - delete UnsafeMutablePointer(); - } - } +void ArenaStringPtr::Destroy(const std::string *default_value, duckdb::google::protobuf::Arena *arena) { + if (arena == nullptr) { + GOOGLE_DCHECK(!IsDonatedString()); + if (!IsDefault(default_value)) { + delete UnsafeMutablePointer(); + } + } } -void ArenaStringPtr::Destroy(EmptyDefault, ::google::protobuf::Arena* arena) { - Destroy(&GetEmptyStringAlreadyInited(), arena); +void ArenaStringPtr::Destroy(EmptyDefault, duckdb::google::protobuf::Arena *arena) { + Destroy(&GetEmptyStringAlreadyInited(), arena); } -void ArenaStringPtr::Destroy(NonEmptyDefault, ::google::protobuf::Arena* arena) { - Destroy(nullptr, arena); +void ArenaStringPtr::Destroy(NonEmptyDefault, duckdb::google::protobuf::Arena *arena) { + Destroy(nullptr, arena); } void ArenaStringPtr::ClearToEmpty() { - if (IsDefault(&GetEmptyStringAlreadyInited())) { - // Already set to default -- do nothing. - } else { - // Unconditionally mask away the tag. - // - // UpdateDonatedString uses assign when capacity is larger than the new - // value, which is trivially true in the donated string case. - // const_cast(PtrValue())->clear(); - tagged_ptr_.Get()->clear(); - } + if (IsDefault(&GetEmptyStringAlreadyInited())) { + // Already set to default -- do nothing. + } else { + // Unconditionally mask away the tag. + // + // UpdateDonatedString uses assign when capacity is larger than the new + // value, which is trivially true in the donated string case. + // const_cast(PtrValue())->clear(); + tagged_ptr_.Get()->clear(); + } } -void ArenaStringPtr::ClearToDefault(const LazyString& default_value, - ::google::protobuf::Arena* arena) { - (void)arena; - if (IsDefault(nullptr)) { - // Already set to default -- do nothing. - } else if (!IsDonatedString()) { - UnsafeMutablePointer()->assign(default_value.get()); - } +void ArenaStringPtr::ClearToDefault(const LazyString &default_value, duckdb::google::protobuf::Arena *arena) { + (void)arena; + if (IsDefault(nullptr)) { + // Already set to default -- do nothing. + } else if (!IsDonatedString()) { + UnsafeMutablePointer()->assign(default_value.get()); + } } -inline void SetStrWithHeapBuffer(std::string* str, ArenaStringPtr* s) { - TaggedPtr res; - res.Set(str); - s->UnsafeSetTaggedPointer(res); +inline void SetStrWithHeapBuffer(std::string *str, ArenaStringPtr *s) { + TaggedPtr res; + res.Set(str); + s->UnsafeSetTaggedPointer(res); } -const char* EpsCopyInputStream::ReadArenaString(const char* ptr, - ArenaStringPtr* s, - Arena* arena) { - GOOGLE_DCHECK(arena != nullptr); +const char *EpsCopyInputStream::ReadArenaString(const char *ptr, ArenaStringPtr *s, Arena *arena) { + GOOGLE_DCHECK(arena != nullptr); - int size = ReadSize(&ptr); - if (!ptr) return nullptr; + int size = ReadSize(&ptr); + if (!ptr) + return nullptr; - auto* str = Arena::Create(arena); - ptr = ReadString(ptr, size, str); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + auto *str = Arena::Create(arena); + ptr = ReadString(ptr, size, str); + GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - SetStrWithHeapBuffer(str, s); + SetStrWithHeapBuffer(str, s); - return ptr; + return ptr; } -} // namespace internal -} // namespace protobuf -} // namespace google - +} // namespace internal +} // namespace protobuf +} // namespace google +} // namespace duckdb #include diff --git a/third_party/google/protobuf/arenastring.h b/third_party/google/protobuf/arenastring.h index 38c36378cd3..fd1704a879f 100644 --- a/third_party/google/protobuf/arenastring.h +++ b/third_party/google/protobuf/arenastring.h @@ -46,7 +46,7 @@ #error "You cannot SWIG proto headers" #endif - +namespace duckdb { namespace google { namespace protobuf { namespace internal { @@ -59,64 +59,77 @@ class SwapFieldHelper; // Lazy string instance to support string fields with non-empty default. // These are initialized on the first call to .get(). class PROTOBUF_EXPORT LazyString { - public: - // We explicitly make LazyString an aggregate so that MSVC can do constant - // initialization on it without marking it `constexpr`. - // We do not want to use `constexpr` because it makes it harder to have extern - // storage for it and causes library bloat. - struct InitValue { - const char* ptr; - size_t size; - }; - // We keep a union of the initialization value and the std::string to save on - // space. We don't need the string array after Init() is done. - union { - mutable InitValue init_value_; - alignas(std::string) mutable char string_buf_[sizeof(std::string)]; - }; - mutable std::atomic inited_; - - const std::string& get() const { - // This check generates less code than a call-once invocation. - auto* res = inited_.load(std::memory_order_acquire); - if (PROTOBUF_PREDICT_FALSE(res == nullptr)) return Init(); - return *res; - } - - private: - // Initialize the string in `string_buf_`, update `inited_` and return it. - // We return it here to avoid having to read it again in the inlined code. - const std::string& Init() const; +public: + // We explicitly make LazyString an aggregate so that MSVC can do constant + // initialization on it without marking it `constexpr`. + // We do not want to use `constexpr` because it makes it harder to have extern + // storage for it and causes library bloat. + struct InitValue { + const char *ptr; + size_t size; + }; + // We keep a union of the initialization value and the std::string to save on + // space. We don't need the string array after Init() is done. + union { + mutable InitValue init_value_; + alignas(std::string) mutable char string_buf_[sizeof(std::string)]; + }; + mutable std::atomic inited_; + + const std::string &get() const { + // This check generates less code than a call-once invocation. + auto *res = inited_.load(std::memory_order_acquire); + if (PROTOBUF_PREDICT_FALSE(res == nullptr)) + return Init(); + return *res; + } + +private: + // Initialize the string in `string_buf_`, update `inited_` and return it. + // We return it here to avoid having to read it again in the inlined code. + const std::string &Init() const; }; template class TaggedPtr { - public: - TaggedPtr() = default; - explicit constexpr TaggedPtr(const ExplicitlyConstructed* ptr) - : ptr_(const_cast*>(ptr)) {} - - void SetTagged(T* p) { - Set(p); - ptr_ = reinterpret_cast(as_int() | 1); - } - void Set(T* p) { ptr_ = p; } - T* Get() const { return reinterpret_cast(as_int() & -2); } - bool IsTagged() const { return as_int() & 1; } - - // Returned value is only safe to dereference if IsTagged() == false. - // It is safe to compare. - T* UnsafeGet() const { return static_cast(ptr_); } - - bool IsNull() { return ptr_ == nullptr; } - - private: - uintptr_t as_int() const { return reinterpret_cast(ptr_); } - void* ptr_; +public: + TaggedPtr() = default; + explicit constexpr TaggedPtr(const ExplicitlyConstructed *ptr) + : ptr_(const_cast *>(ptr)) { + } + + void SetTagged(T *p) { + Set(p); + ptr_ = reinterpret_cast(as_int() | 1); + } + void Set(T *p) { + ptr_ = p; + } + T *Get() const { + return reinterpret_cast(as_int() & -2); + } + bool IsTagged() const { + return as_int() & 1; + } + + // Returned value is only safe to dereference if IsTagged() == false. + // It is safe to compare. + T *UnsafeGet() const { + return static_cast(ptr_); + } + + bool IsNull() { + return ptr_ == nullptr; + } + +private: + uintptr_t as_int() const { + return reinterpret_cast(ptr_); + } + void *ptr_; }; -static_assert(std::is_trivial>::value, - "TaggedPtr must be trivial"); +static_assert(std::is_trivial>::value, "TaggedPtr must be trivial"); // This class encapsulates a pointer to a std::string with or without a donated // buffer, tagged by bottom bit. It is a high-level wrapper that almost directly @@ -175,246 +188,234 @@ static_assert(std::is_trivial>::value, // single "cmp %reg, GLOBAL" in the resulting machine code. (Note that this also // requires the String tag to be 0 so we can avoid the mask before comparing.) struct PROTOBUF_EXPORT ArenaStringPtr { - ArenaStringPtr() = default; - explicit constexpr ArenaStringPtr( - const ExplicitlyConstructed* default_value) - : tagged_ptr_(default_value) {} - - // Some methods below are overloaded on a `default_value` and on tags. - // The tagged overloads help reduce code size in the callers in generated - // code, while the `default_value` overloads are useful from reflection. - // By-value empty struct arguments are elided in the ABI. - struct EmptyDefault {}; - struct NonEmptyDefault {}; - - void Set(const std::string* default_value, ConstStringParam value, - ::google::protobuf::Arena* arena); - void Set(const std::string* default_value, std::string&& value, - ::google::protobuf::Arena* arena); - void Set(EmptyDefault, ConstStringParam value, ::google::protobuf::Arena* arena); - void Set(EmptyDefault, std::string&& value, ::google::protobuf::Arena* arena); - void Set(NonEmptyDefault, ConstStringParam value, ::google::protobuf::Arena* arena); - void Set(NonEmptyDefault, std::string&& value, ::google::protobuf::Arena* arena); - template - void Set(FirstParam p1, const char* str, ::google::protobuf::Arena* arena) { - Set(p1, ConstStringParam(str), arena); - } - template - void Set(FirstParam p1, const char* str, size_t size, - ::google::protobuf::Arena* arena) { - ConstStringParam sp{str, size}; // for string_view and `const string &` - Set(p1, sp, arena); - } - template - void Set(FirstParam p1, - std::reference_wrapper const_string_ref, - ::google::protobuf::Arena* arena) { - Set(p1, const_string_ref.get(), arena); - } - - template - void SetBytes(FirstParam p1, SecondParam&& p2, ::google::protobuf::Arena* arena) { - Set(p1, static_cast(p2), arena); - } - template - void SetBytes(FirstParam p1, const void* str, size_t size, - ::google::protobuf::Arena* arena) { - // must work whether ConstStringParam is string_view or `const string &` - ConstStringParam sp{static_cast(str), size}; - Set(p1, sp, arena); - } - - // Basic accessors. - PROTOBUF_NDEBUG_INLINE const std::string& Get() const { - // Unconditionally mask away the tag. - return *tagged_ptr_.Get(); - } - PROTOBUF_NDEBUG_INLINE const std::string* GetPointer() const { - // Unconditionally mask away the tag. - return tagged_ptr_.Get(); - } - - // For fields with an empty default value. - std::string* Mutable(EmptyDefault, ::google::protobuf::Arena* arena); - // For fields with a non-empty default value. - std::string* Mutable(const LazyString& default_value, ::google::protobuf::Arena* arena); - - // Release returns a std::string* instance that is heap-allocated and is not - // Own()'d by any arena. If the field is not set, this returns nullptr. The - // caller retains ownership. Clears this field back to nullptr state. Used to - // implement release_() methods on generated classes. - PROTOBUF_NODISCARD std::string* Release(const std::string* default_value, - ::google::protobuf::Arena* arena); - PROTOBUF_NODISCARD std::string* ReleaseNonDefault( - const std::string* default_value, ::google::protobuf::Arena* arena); - - // Takes a std::string that is heap-allocated, and takes ownership. The - // std::string's destructor is registered with the arena. Used to implement - // set_allocated_ in generated classes. - void SetAllocated(const std::string* default_value, std::string* value, - ::google::protobuf::Arena* arena); - - // Swaps internal pointers. Arena-safety semantics: this is guarded by the - // logic in Swap()/UnsafeArenaSwap() at the message level, so this method is - // 'unsafe' if called directly. - inline PROTOBUF_NDEBUG_INLINE static void InternalSwap( - const std::string* default_value, ArenaStringPtr* rhs, Arena* rhs_arena, - ArenaStringPtr* lhs, Arena* lhs_arena); - - // Frees storage (if not on an arena). - void Destroy(const std::string* default_value, ::google::protobuf::Arena* arena); - void Destroy(EmptyDefault, ::google::protobuf::Arena* arena); - void Destroy(NonEmptyDefault, ::google::protobuf::Arena* arena); - - // Clears content, but keeps allocated std::string, to avoid the overhead of - // heap operations. After this returns, the content (as seen by the user) will - // always be the empty std::string. Assumes that |default_value| is an empty - // std::string. - void ClearToEmpty(); - - // Clears content, assuming that the current value is not the empty - // string default. - void ClearNonDefaultToEmpty(); - - // Clears content, but keeps allocated std::string if arena != nullptr, to - // avoid the overhead of heap operations. After this returns, the content - // (as seen by the user) will always be equal to |default_value|. - void ClearToDefault(const LazyString& default_value, ::google::protobuf::Arena* arena); - - // Called from generated code / reflection runtime only. Resets value to point - // to a default string pointer, with the semantics that this - // ArenaStringPtr does not own the pointed-to memory. Disregards initial value - // of ptr_ (so this is the *ONLY* safe method to call after construction or - // when reinitializing after becoming the active field in a oneof union). - inline void UnsafeSetDefault(const std::string* default_value); - - // Returns a mutable pointer, but doesn't initialize the string to the - // default value. - std::string* MutableNoArenaNoDefault(const std::string* default_value); - - // Get a mutable pointer with unspecified contents. - // Similar to `MutableNoArenaNoDefault`, but also handles the arena case. - // If the value was donated, the contents are discarded. - std::string* MutableNoCopy(const std::string* default_value, - ::google::protobuf::Arena* arena); - - // Destroy the string. Assumes `arena == nullptr`. - void DestroyNoArena(const std::string* default_value); - - // Internal setter used only at parse time to directly set a donated string - // value. - void UnsafeSetTaggedPointer(TaggedPtr value) { - tagged_ptr_ = value; - } - // Generated code only! An optimization, in certain cases the generated - // code is certain we can obtain a std::string with no default checks and - // tag tests. - std::string* UnsafeMutablePointer() PROTOBUF_RETURNS_NONNULL; - - inline bool IsDefault(const std::string* default_value) const { - // Relies on the fact that kPtrTagString == 0, so if IsString(), ptr_ is the - // actual std::string pointer (and if !IsString(), ptr_ will never be equal - // to any aligned |default_value| pointer). The key is that we want to avoid - // masking in the fastpath const-pointer Get() case for non-arena code. - return tagged_ptr_.UnsafeGet() == default_value; - } - - private: - TaggedPtr tagged_ptr_; - - bool IsDonatedString() const { return false; } - - // Swaps tagged pointer without debug hardening. This is to allow python - // protobuf to maintain pointer stability even in DEBUG builds. - inline PROTOBUF_NDEBUG_INLINE static void UnsafeShallowSwap( - ArenaStringPtr* rhs, ArenaStringPtr* lhs) { - std::swap(lhs->tagged_ptr_, rhs->tagged_ptr_); - } - - friend class ::google::protobuf::internal::SwapFieldHelper; - - // Slow paths. - - // MutableSlow requires that !IsString() || IsDefault - // Variadic to support 0 args for EmptyDefault and 1 arg for LazyString. - template - std::string* MutableSlow(::google::protobuf::Arena* arena, const Lazy&... lazy_default); - - // Sets value to a newly allocated string and returns it - std::string* SetAndReturnNewString(); - - // Destroys the non-default string value out-of-line - void DestroyNoArenaSlowPath(); - + ArenaStringPtr() = default; + explicit constexpr ArenaStringPtr(const ExplicitlyConstructed *default_value) + : tagged_ptr_(default_value) { + } + + // Some methods below are overloaded on a `default_value` and on tags. + // The tagged overloads help reduce code size in the callers in generated + // code, while the `default_value` overloads are useful from reflection. + // By-value empty struct arguments are elided in the ABI. + struct EmptyDefault {}; + struct NonEmptyDefault {}; + + void Set(const std::string *default_value, ConstStringParam value, duckdb::google::protobuf::Arena *arena); + void Set(const std::string *default_value, std::string &&value, duckdb::google::protobuf::Arena *arena); + void Set(EmptyDefault, ConstStringParam value, duckdb::google::protobuf::Arena *arena); + void Set(EmptyDefault, std::string &&value, duckdb::google::protobuf::Arena *arena); + void Set(NonEmptyDefault, ConstStringParam value, duckdb::google::protobuf::Arena *arena); + void Set(NonEmptyDefault, std::string &&value, duckdb::google::protobuf::Arena *arena); + template + void Set(FirstParam p1, const char *str, duckdb::google::protobuf::Arena *arena) { + Set(p1, ConstStringParam(str), arena); + } + template + void Set(FirstParam p1, const char *str, size_t size, duckdb::google::protobuf::Arena *arena) { + ConstStringParam sp {str, size}; // for string_view and `const string &` + Set(p1, sp, arena); + } + template + void Set(FirstParam p1, std::reference_wrapper const_string_ref, duckdb::google::protobuf::Arena *arena) { + Set(p1, const_string_ref.get(), arena); + } + + template + void SetBytes(FirstParam p1, SecondParam &&p2, duckdb::google::protobuf::Arena *arena) { + Set(p1, static_cast(p2), arena); + } + template + void SetBytes(FirstParam p1, const void *str, size_t size, duckdb::google::protobuf::Arena *arena) { + // must work whether ConstStringParam is string_view or `const string &` + ConstStringParam sp {static_cast(str), size}; + Set(p1, sp, arena); + } + + // Basic accessors. + PROTOBUF_NDEBUG_INLINE const std::string &Get() const { + // Unconditionally mask away the tag. + return *tagged_ptr_.Get(); + } + PROTOBUF_NDEBUG_INLINE const std::string *GetPointer() const { + // Unconditionally mask away the tag. + return tagged_ptr_.Get(); + } + + // For fields with an empty default value. + std::string *Mutable(EmptyDefault, duckdb::google::protobuf::Arena *arena); + // For fields with a non-empty default value. + std::string *Mutable(const LazyString &default_value, duckdb::google::protobuf::Arena *arena); + + // Release returns a std::string* instance that is heap-allocated and is not + // Own()'d by any arena. If the field is not set, this returns nullptr. The + // caller retains ownership. Clears this field back to nullptr state. Used to + // implement release_() methods on generated classes. + PROTOBUF_NODISCARD std::string *Release(const std::string *default_value, duckdb::google::protobuf::Arena *arena); + PROTOBUF_NODISCARD std::string *ReleaseNonDefault(const std::string *default_value, + duckdb::google::protobuf::Arena *arena); + + // Takes a std::string that is heap-allocated, and takes ownership. The + // std::string's destructor is registered with the arena. Used to implement + // set_allocated_ in generated classes. + void SetAllocated(const std::string *default_value, std::string *value, duckdb::google::protobuf::Arena *arena); + + // Swaps internal pointers. Arena-safety semantics: this is guarded by the + // logic in Swap()/UnsafeArenaSwap() at the message level, so this method is + // 'unsafe' if called directly. + inline PROTOBUF_NDEBUG_INLINE static void InternalSwap(const std::string *default_value, ArenaStringPtr *rhs, + Arena *rhs_arena, ArenaStringPtr *lhs, Arena *lhs_arena); + + // Frees storage (if not on an arena). + void Destroy(const std::string *default_value, duckdb::google::protobuf::Arena *arena); + void Destroy(EmptyDefault, duckdb::google::protobuf::Arena *arena); + void Destroy(NonEmptyDefault, duckdb::google::protobuf::Arena *arena); + + // Clears content, but keeps allocated std::string, to avoid the overhead of + // heap operations. After this returns, the content (as seen by the user) will + // always be the empty std::string. Assumes that |default_value| is an empty + // std::string. + void ClearToEmpty(); + + // Clears content, assuming that the current value is not the empty + // string default. + void ClearNonDefaultToEmpty(); + + // Clears content, but keeps allocated std::string if arena != nullptr, to + // avoid the overhead of heap operations. After this returns, the content + // (as seen by the user) will always be equal to |default_value|. + void ClearToDefault(const LazyString &default_value, duckdb::google::protobuf::Arena *arena); + + // Called from generated code / reflection runtime only. Resets value to point + // to a default string pointer, with the semantics that this + // ArenaStringPtr does not own the pointed-to memory. Disregards initial value + // of ptr_ (so this is the *ONLY* safe method to call after construction or + // when reinitializing after becoming the active field in a oneof union). + inline void UnsafeSetDefault(const std::string *default_value); + + // Returns a mutable pointer, but doesn't initialize the string to the + // default value. + std::string *MutableNoArenaNoDefault(const std::string *default_value); + + // Get a mutable pointer with unspecified contents. + // Similar to `MutableNoArenaNoDefault`, but also handles the arena case. + // If the value was donated, the contents are discarded. + std::string *MutableNoCopy(const std::string *default_value, duckdb::google::protobuf::Arena *arena); + + // Destroy the string. Assumes `arena == nullptr`. + void DestroyNoArena(const std::string *default_value); + + // Internal setter used only at parse time to directly set a donated string + // value. + void UnsafeSetTaggedPointer(TaggedPtr value) { + tagged_ptr_ = value; + } + // Generated code only! An optimization, in certain cases the generated + // code is certain we can obtain a std::string with no default checks and + // tag tests. + std::string *UnsafeMutablePointer() PROTOBUF_RETURNS_NONNULL; + + inline bool IsDefault(const std::string *default_value) const { + // Relies on the fact that kPtrTagString == 0, so if IsString(), ptr_ is the + // actual std::string pointer (and if !IsString(), ptr_ will never be equal + // to any aligned |default_value| pointer). The key is that we want to avoid + // masking in the fastpath const-pointer Get() case for non-arena code. + return tagged_ptr_.UnsafeGet() == default_value; + } + +private: + TaggedPtr tagged_ptr_; + + bool IsDonatedString() const { + return false; + } + + // Swaps tagged pointer without debug hardening. This is to allow python + // protobuf to maintain pointer stability even in DEBUG builds. + inline PROTOBUF_NDEBUG_INLINE static void UnsafeShallowSwap(ArenaStringPtr *rhs, ArenaStringPtr *lhs) { + std::swap(lhs->tagged_ptr_, rhs->tagged_ptr_); + } + + friend class duckdb::google::protobuf::internal::SwapFieldHelper; + + // Slow paths. + + // MutableSlow requires that !IsString() || IsDefault + // Variadic to support 0 args for EmptyDefault and 1 arg for LazyString. + template + std::string *MutableSlow(duckdb::google::protobuf::Arena *arena, const Lazy &...lazy_default); + + // Sets value to a newly allocated string and returns it + std::string *SetAndReturnNewString(); + + // Destroys the non-default string value out-of-line + void DestroyNoArenaSlowPath(); }; -inline void ArenaStringPtr::UnsafeSetDefault(const std::string* value) { - tagged_ptr_.Set(const_cast(value)); +inline void ArenaStringPtr::UnsafeSetDefault(const std::string *value) { + tagged_ptr_.Set(const_cast(value)); } // Make sure rhs_arena allocated rhs, and lhs_arena allocated lhs. -inline PROTOBUF_NDEBUG_INLINE void ArenaStringPtr::InternalSwap( // - const std::string* default_value, // - ArenaStringPtr* rhs, Arena* rhs_arena, // - ArenaStringPtr* lhs, Arena* lhs_arena) { - // Silence unused variable warnings in release buildls. - (void)default_value; - (void)rhs_arena; - (void)lhs_arena; - std::swap(lhs->tagged_ptr_, rhs->tagged_ptr_); +inline PROTOBUF_NDEBUG_INLINE void ArenaStringPtr::InternalSwap( // + const std::string *default_value, // + ArenaStringPtr *rhs, Arena *rhs_arena, // + ArenaStringPtr *lhs, Arena *lhs_arena) { + // Silence unused variable warnings in release buildls. + (void)default_value; + (void)rhs_arena; + (void)lhs_arena; + std::swap(lhs->tagged_ptr_, rhs->tagged_ptr_); #ifdef PROTOBUF_FORCE_COPY_IN_SWAP - auto force_realloc = [default_value](ArenaStringPtr* p, Arena* arena) { - if (p->IsDefault(default_value)) return; - std::string* old_value = p->tagged_ptr_.Get(); - std::string* new_value = - p->IsDonatedString() - ? Arena::Create(arena, *old_value) - : Arena::Create(arena, std::move(*old_value)); - if (arena == nullptr) delete old_value; - p->tagged_ptr_.Set(new_value); - }; - // Because, at this point, tagged_ptr_ has been swapped, arena should also be - // swapped. - force_realloc(lhs, rhs_arena); - force_realloc(rhs, lhs_arena); -#endif // PROTOBUF_FORCE_COPY_IN_SWAP + auto force_realloc = [default_value](ArenaStringPtr *p, Arena *arena) { + if (p->IsDefault(default_value)) + return; + std::string *old_value = p->tagged_ptr_.Get(); + std::string *new_value = p->IsDonatedString() ? Arena::Create(arena, *old_value) + : Arena::Create(arena, std::move(*old_value)); + if (arena == nullptr) + delete old_value; + p->tagged_ptr_.Set(new_value); + }; + // Because, at this point, tagged_ptr_ has been swapped, arena should also be + // swapped. + force_realloc(lhs, rhs_arena); + force_realloc(rhs, lhs_arena); +#endif // PROTOBUF_FORCE_COPY_IN_SWAP } inline void ArenaStringPtr::ClearNonDefaultToEmpty() { - // Unconditionally mask away the tag. - tagged_ptr_.Get()->clear(); + // Unconditionally mask away the tag. + tagged_ptr_.Get()->clear(); } -inline std::string* ArenaStringPtr::MutableNoArenaNoDefault( - const std::string* default_value) { - // VERY IMPORTANT for performance and code size: this will reduce to a member - // variable load, a pointer check (against |default_value|, in practice a - // static global) and a branch to the slowpath (which calls operator new and - // the ctor). DO NOT add any tagged-pointer operations here. - if (IsDefault(default_value)) { - return SetAndReturnNewString(); - } else { - return UnsafeMutablePointer(); - } +inline std::string *ArenaStringPtr::MutableNoArenaNoDefault(const std::string *default_value) { + // VERY IMPORTANT for performance and code size: this will reduce to a member + // variable load, a pointer check (against |default_value|, in practice a + // static global) and a branch to the slowpath (which calls operator new and + // the ctor). DO NOT add any tagged-pointer operations here. + if (IsDefault(default_value)) { + return SetAndReturnNewString(); + } else { + return UnsafeMutablePointer(); + } } -inline void ArenaStringPtr::DestroyNoArena(const std::string* default_value) { - if (!IsDefault(default_value)) { - DestroyNoArenaSlowPath(); - } +inline void ArenaStringPtr::DestroyNoArena(const std::string *default_value) { + if (!IsDefault(default_value)) { + DestroyNoArenaSlowPath(); + } } -inline std::string* ArenaStringPtr::UnsafeMutablePointer() { - GOOGLE_DCHECK(!tagged_ptr_.IsTagged()); - GOOGLE_DCHECK(tagged_ptr_.UnsafeGet() != nullptr); - return tagged_ptr_.UnsafeGet(); +inline std::string *ArenaStringPtr::UnsafeMutablePointer() { + GOOGLE_DCHECK(!tagged_ptr_.IsTagged()); + GOOGLE_DCHECK(tagged_ptr_.UnsafeGet() != nullptr); + return tagged_ptr_.UnsafeGet(); } - -} // namespace internal -} // namespace protobuf -} // namespace google - +} // namespace internal +} // namespace protobuf +} // namespace google +} // namespace duckdb #include #endif // GOOGLE_PROTOBUF_ARENASTRING_H__ diff --git a/third_party/google/protobuf/descriptor.cc b/third_party/google/protobuf/descriptor.cc index 6e0341ee770..26506198cd8 100644 --- a/third_party/google/protobuf/descriptor.cc +++ b/third_party/google/protobuf/descriptor.cc @@ -73,6 +73,7 @@ #include +namespace duckdb{ namespace google { namespace protobuf { @@ -8021,5 +8022,5 @@ void LazyDescriptor::Once(const ServiceDescriptor* service) { } // namespace protobuf } // namespace google - +} // namespace duckdb #include diff --git a/third_party/google/protobuf/descriptor.h b/third_party/google/protobuf/descriptor.h index e74e355b5a3..4ed1f7eadf2 100644 --- a/third_party/google/protobuf/descriptor.h +++ b/third_party/google/protobuf/descriptor.h @@ -77,7 +77,7 @@ #define PROTOBUF_EXPORT #endif - +namespace duckdb { namespace google { namespace protobuf { @@ -133,49 +133,46 @@ class CommandLineInterface; namespace cpp { // Defined in helpers.h class Formatter; -} // namespace cpp -} // namespace compiler +} // namespace cpp +} // namespace compiler namespace descriptor_unittest { class DescriptorTest; -} // namespace descriptor_unittest +} // namespace descriptor_unittest // Defined in printer.h namespace io { class Printer; -} // namespace io +} // namespace io // NB, all indices are zero-based. struct SourceLocation { - int start_line; - int end_line; - int start_column; - int end_column; - - // Doc comments found at the source location. - // See the comments in SourceCodeInfo.Location (descriptor.proto) for details. - std::string leading_comments; - std::string trailing_comments; - std::vector leading_detached_comments; + int start_line; + int end_line; + int start_column; + int end_column; + + // Doc comments found at the source location. + // See the comments in SourceCodeInfo.Location (descriptor.proto) for details. + std::string leading_comments; + std::string trailing_comments; + std::vector leading_detached_comments; }; // Options when generating machine-parsable output from a descriptor with // DebugString(). struct DebugStringOptions { - // include original user comments as recorded in SourceLocation entries. N.B. - // that this must be |false| by default: several other pieces of code (for - // example, the C++ code generation for fields in the proto compiler) rely on - // DebugString() output being unobstructed by user comments. - bool include_comments; - // If true, elide the braced body in the debug string. - bool elide_group_body; - bool elide_oneof_body; - - DebugStringOptions() - : include_comments(false), - elide_group_body(false), - elide_oneof_body(false) { - } + // include original user comments as recorded in SourceLocation entries. N.B. + // that this must be |false| by default: several other pieces of code (for + // example, the C++ code generation for fields in the proto compiler) rely on + // DebugString() output being unobstructed by user comments. + bool include_comments; + // If true, elide the braced body in the debug string. + bool elide_group_body; + bool elide_oneof_body; + + DebugStringOptions() : include_comments(false), elide_group_body(false), elide_oneof_body(false) { + } }; // A class to handle the simplest cases of a lazily linked descriptor @@ -185,49 +182,49 @@ struct DebugStringOptions { namespace internal { class PROTOBUF_EXPORT LazyDescriptor { - public: - // Init function to be called at init time of a descriptor containing - // a LazyDescriptor. - void Init() { - descriptor_ = nullptr; - once_ = nullptr; - } - - // Sets the value of the descriptor if it is known during the descriptor - // building process. Not thread safe, should only be called during the - // descriptor build process. Should not be called after SetLazy has been - // called. - void Set(const Descriptor* descriptor); - - // Sets the information needed to lazily cross link the descriptor at a later - // time, SetLazy is not thread safe, should be called only once at descriptor - // build time if the symbol wasn't found and building of the file containing - // that type is delayed because lazily_build_dependencies_ is set on the pool. - // Should not be called after Set() has been called. - void SetLazy(StringPiece name, const FileDescriptor* file); - - // Returns the current value of the descriptor, thread-safe. If SetLazy(...) - // has been called, will do a one-time cross link of the type specified, - // building the descriptor file that contains the type if necessary. - inline const Descriptor* Get(const ServiceDescriptor* service) { - Once(service); - return descriptor_; - } - - private: - void Once(const ServiceDescriptor* service); - - union { - const Descriptor* descriptor_; - const char* lazy_name_; - }; - internal::once_flag* once_; +public: + // Init function to be called at init time of a descriptor containing + // a LazyDescriptor. + void Init() { + descriptor_ = nullptr; + once_ = nullptr; + } + + // Sets the value of the descriptor if it is known during the descriptor + // building process. Not thread safe, should only be called during the + // descriptor build process. Should not be called after SetLazy has been + // called. + void Set(const Descriptor *descriptor); + + // Sets the information needed to lazily cross link the descriptor at a later + // time, SetLazy is not thread safe, should be called only once at descriptor + // build time if the symbol wasn't found and building of the file containing + // that type is delayed because lazily_build_dependencies_ is set on the pool. + // Should not be called after Set() has been called. + void SetLazy(StringPiece name, const FileDescriptor *file); + + // Returns the current value of the descriptor, thread-safe. If SetLazy(...) + // has been called, will do a one-time cross link of the type specified, + // building the descriptor file that contains the type if necessary. + inline const Descriptor *Get(const ServiceDescriptor *service) { + Once(service); + return descriptor_; + } + +private: + void Once(const ServiceDescriptor *service); + + union { + const Descriptor *descriptor_; + const char *lazy_name_; + }; + internal::once_flag *once_; }; class PROTOBUF_EXPORT SymbolBase { - private: - friend class google::protobuf::Symbol; - uint8_t symbol_type_; +private: + friend class google::protobuf::Symbol; + uint8_t symbol_type_; }; // Some types have more than one SymbolBase because they have multiple @@ -237,7 +234,7 @@ class PROTOBUF_EXPORT SymbolBase { template class PROTOBUF_EXPORT SymbolBaseN : public SymbolBase {}; -} // namespace internal +} // namespace internal // Describes a type of protocol message, or a particular group within a // message. To obtain the Descriptor for a given message object, call @@ -245,360 +242,355 @@ class PROTOBUF_EXPORT SymbolBaseN : public SymbolBase {}; // static method called descriptor() which returns the type's descriptor. // Use DescriptorPool to construct your own descriptors. class PROTOBUF_EXPORT Descriptor : private internal::SymbolBase { - public: - typedef DescriptorProto Proto; - - // The name of the message type, not including its scope. - const std::string& name() const; - - // The fully-qualified name of the message type, scope delimited by - // periods. For example, message type "Foo" which is declared in package - // "bar" has full name "bar.Foo". If a type "Baz" is nested within - // Foo, Baz's full_name is "bar.Foo.Baz". To get only the part that - // comes after the last '.', use name(). - const std::string& full_name() const; - - // Index of this descriptor within the file or containing type's message - // type array. - int index() const; - - // The .proto file in which this message type was defined. Never nullptr. - const FileDescriptor* file() const; - - // If this Descriptor describes a nested type, this returns the type - // in which it is nested. Otherwise, returns nullptr. - const Descriptor* containing_type() const; - - // Get options for this message type. These are specified in the .proto file - // by placing lines like "option foo = 1234;" in the message definition. - // Allowed options are defined by MessageOptions in descriptor.proto, and any - // available extensions of that message. - const MessageOptions& options() const; - - // Write the contents of this Descriptor into the given DescriptorProto. - // The target DescriptorProto must be clear before calling this; if it - // isn't, the result may be garbage. - void CopyTo(DescriptorProto* proto) const; - - // Write the contents of this descriptor in a human-readable form. Output - // will be suitable for re-parsing. - std::string DebugString() const; - - // Similar to DebugString(), but additionally takes options (e.g., - // include original user comments in output). - std::string DebugStringWithOptions(const DebugStringOptions& options) const; - - // Returns true if this is a placeholder for an unknown type. This will - // only be the case if this descriptor comes from a DescriptorPool - // with AllowUnknownDependencies() set. - bool is_placeholder() const; - - enum WellKnownType { - WELLKNOWNTYPE_UNSPECIFIED, // Not a well-known type. - - // Wrapper types. - WELLKNOWNTYPE_DOUBLEVALUE, // google.protobuf.DoubleValue - WELLKNOWNTYPE_FLOATVALUE, // google.protobuf.FloatValue - WELLKNOWNTYPE_INT64VALUE, // google.protobuf.Int64Value - WELLKNOWNTYPE_UINT64VALUE, // google.protobuf.UInt64Value - WELLKNOWNTYPE_INT32VALUE, // google.protobuf.Int32Value - WELLKNOWNTYPE_UINT32VALUE, // google.protobuf.UInt32Value - WELLKNOWNTYPE_STRINGVALUE, // google.protobuf.StringValue - WELLKNOWNTYPE_BYTESVALUE, // google.protobuf.BytesValue - WELLKNOWNTYPE_BOOLVALUE, // google.protobuf.BoolValue - - // Other well known types. - WELLKNOWNTYPE_ANY, // google.protobuf.Any - WELLKNOWNTYPE_FIELDMASK, // google.protobuf.FieldMask - WELLKNOWNTYPE_DURATION, // google.protobuf.Duration - WELLKNOWNTYPE_TIMESTAMP, // google.protobuf.Timestamp - WELLKNOWNTYPE_VALUE, // google.protobuf.Value - WELLKNOWNTYPE_LISTVALUE, // google.protobuf.ListValue - WELLKNOWNTYPE_STRUCT, // google.protobuf.Struct - - // New well-known types may be added in the future. - // Please make sure any switch() statements have a 'default' case. - __WELLKNOWNTYPE__DO_NOT_USE__ADD_DEFAULT_INSTEAD__, - }; - - WellKnownType well_known_type() const; - - // Field stuff ----------------------------------------------------- - - // The number of fields in this message type. - int field_count() const; - // Gets a field by index, where 0 <= index < field_count(). - // These are returned in the order they were defined in the .proto file. - const FieldDescriptor* field(int index) const; - - // Looks up a field by declared tag number. Returns nullptr if no such field - // exists. - const FieldDescriptor* FindFieldByNumber(int number) const; - // Looks up a field by name. Returns nullptr if no such field exists. - const FieldDescriptor* FindFieldByName(ConstStringParam name) const; - - // Looks up a field by lowercased name (as returned by lowercase_name()). - // This lookup may be ambiguous if multiple field names differ only by case, - // in which case the field returned is chosen arbitrarily from the matches. - const FieldDescriptor* FindFieldByLowercaseName( - ConstStringParam lowercase_name) const; - - // Looks up a field by camel-case name (as returned by camelcase_name()). - // This lookup may be ambiguous if multiple field names differ in a way that - // leads them to have identical camel-case names, in which case the field - // returned is chosen arbitrarily from the matches. - const FieldDescriptor* FindFieldByCamelcaseName( - ConstStringParam camelcase_name) const; - - // The number of oneofs in this message type. - int oneof_decl_count() const; - // The number of oneofs in this message type, excluding synthetic oneofs. - // Real oneofs always come first, so iterating up to real_oneof_decl_cout() - // will yield all real oneofs. - int real_oneof_decl_count() const; - // Get a oneof by index, where 0 <= index < oneof_decl_count(). - // These are returned in the order they were defined in the .proto file. - const OneofDescriptor* oneof_decl(int index) const; - - // Looks up a oneof by name. Returns nullptr if no such oneof exists. - const OneofDescriptor* FindOneofByName(ConstStringParam name) const; - - // Nested type stuff ----------------------------------------------- - - // The number of nested types in this message type. - int nested_type_count() const; - // Gets a nested type by index, where 0 <= index < nested_type_count(). - // These are returned in the order they were defined in the .proto file. - const Descriptor* nested_type(int index) const; - - // Looks up a nested type by name. Returns nullptr if no such nested type - // exists. - const Descriptor* FindNestedTypeByName(ConstStringParam name) const; - - // Enum stuff ------------------------------------------------------ - - // The number of enum types in this message type. - int enum_type_count() const; - // Gets an enum type by index, where 0 <= index < enum_type_count(). - // These are returned in the order they were defined in the .proto file. - const EnumDescriptor* enum_type(int index) const; - - // Looks up an enum type by name. Returns nullptr if no such enum type - // exists. - const EnumDescriptor* FindEnumTypeByName(ConstStringParam name) const; - - // Looks up an enum value by name, among all enum types in this message. - // Returns nullptr if no such value exists. - const EnumValueDescriptor* FindEnumValueByName(ConstStringParam name) const; - - // Extensions ------------------------------------------------------ - - // A range of field numbers which are designated for third-party - // extensions. - struct ExtensionRange { - typedef DescriptorProto_ExtensionRange Proto; - - typedef ExtensionRangeOptions OptionsType; - - // See Descriptor::CopyTo(). - void CopyTo(DescriptorProto_ExtensionRange* proto) const; - - int start; // inclusive - int end; // exclusive - - const ExtensionRangeOptions* options_; - }; - - // The number of extension ranges in this message type. - int extension_range_count() const; - // Gets an extension range by index, where 0 <= index < - // extension_range_count(). These are returned in the order they were defined - // in the .proto file. - const ExtensionRange* extension_range(int index) const; - - // Returns true if the number is in one of the extension ranges. - bool IsExtensionNumber(int number) const; - - // Returns nullptr if no extension range contains the given number. - const ExtensionRange* FindExtensionRangeContainingNumber(int number) const; - - // The number of extensions defined nested within this message type's scope. - // See doc: - // https://developers.google.com/protocol-buffers/docs/proto#nested-extensions - // - // Note that the extensions may be extending *other* messages. - // - // For example: - // message M1 { - // extensions 1 to max; - // } - // - // message M2 { - // extend M1 { - // optional int32 foo = 1; - // } - // } - // - // In this case, - // DescriptorPool::generated_pool() - // ->FindMessageTypeByName("M2") - // ->extension(0) - // will return "foo", even though "foo" is an extension of M1. - // To find all known extensions of a given message, instead use - // DescriptorPool::FindAllExtensions. - int extension_count() const; - // Get an extension by index, where 0 <= index < extension_count(). - // These are returned in the order they were defined in the .proto file. - const FieldDescriptor* extension(int index) const; - - // Looks up a named extension (which extends some *other* message type) - // defined within this message type's scope. - const FieldDescriptor* FindExtensionByName(ConstStringParam name) const; - - // Similar to FindFieldByLowercaseName(), but finds extensions defined within - // this message type's scope. - const FieldDescriptor* FindExtensionByLowercaseName( - ConstStringParam name) const; - - // Similar to FindFieldByCamelcaseName(), but finds extensions defined within - // this message type's scope. - const FieldDescriptor* FindExtensionByCamelcaseName( - ConstStringParam name) const; - - // Reserved fields ------------------------------------------------- - - // A range of reserved field numbers. - struct ReservedRange { - int start; // inclusive - int end; // exclusive - }; - - // The number of reserved ranges in this message type. - int reserved_range_count() const; - // Gets an reserved range by index, where 0 <= index < - // reserved_range_count(). These are returned in the order they were defined - // in the .proto file. - const ReservedRange* reserved_range(int index) const; - - // Returns true if the number is in one of the reserved ranges. - bool IsReservedNumber(int number) const; - - // Returns nullptr if no reserved range contains the given number. - const ReservedRange* FindReservedRangeContainingNumber(int number) const; - - // The number of reserved field names in this message type. - int reserved_name_count() const; - - // Gets a reserved name by index, where 0 <= index < reserved_name_count(). - const std::string& reserved_name(int index) const; - - // Returns true if the field name is reserved. - bool IsReservedName(ConstStringParam name) const; - - // Source Location --------------------------------------------------- - - // Updates |*out_location| to the source location of the complete - // extent of this message declaration. Returns false and leaves - // |*out_location| unchanged iff location information was not available. - bool GetSourceLocation(SourceLocation* out_location) const; - - // Maps -------------------------------------------------------------- - - // Returns the FieldDescriptor for the "key" field. If this isn't a map entry - // field, returns nullptr. - const FieldDescriptor* map_key() const; - - // Returns the FieldDescriptor for the "value" field. If this isn't a map - // entry field, returns nullptr. - const FieldDescriptor* map_value() const; - - private: - friend class Symbol; - typedef MessageOptions OptionsType; - - // Allows tests to test CopyTo(proto, true). - friend class descriptor_unittest::DescriptorTest; - - // Allows access to GetLocationPath for annotations. - friend class io::Printer; - friend class compiler::cpp::Formatter; - - // Fill the json_name field of FieldDescriptorProto. - void CopyJsonNameTo(DescriptorProto* proto) const; - - // Internal version of DebugString; controls the level of indenting for - // correct depth. Takes |options| to control debug-string options, and - // |include_opening_clause| to indicate whether the "message ... " part of the - // clause has already been generated (this varies depending on context). - void DebugString(int depth, std::string* contents, - const DebugStringOptions& options, - bool include_opening_clause) const; - - // Walks up the descriptor tree to generate the source location path - // to this descriptor from the file root. - void GetLocationPath(std::vector* output) const; - - // True if this is a placeholder for an unknown type. - bool is_placeholder_ : 1; - // True if this is a placeholder and the type name wasn't fully-qualified. - bool is_unqualified_placeholder_ : 1; - // Well known type. Stored like this to conserve space. - uint8_t well_known_type_ : 5; - - // This points to the last field _number_ that is part of the sequence - // starting at 1, where - // `desc->field(i)->number() == i + 1` - // A value of `0` means no field matches. That is, there are no fields or the - // first field is not field `1`. - // Uses 16-bit to avoid extra padding. Unlikely to have more than 2^16 - // sequentially numbered fields in a message. - uint16_t sequential_field_limit_; - - int field_count_; - - // all_names_ = [name, full_name] - const std::string* all_names_; - const FileDescriptor* file_; - const Descriptor* containing_type_; - const MessageOptions* options_; - - // These arrays are separated from their sizes to minimize padding on 64-bit. - FieldDescriptor* fields_; - OneofDescriptor* oneof_decls_; - Descriptor* nested_types_; - EnumDescriptor* enum_types_; - ExtensionRange* extension_ranges_; - FieldDescriptor* extensions_; - ReservedRange* reserved_ranges_; - const std::string** reserved_names_; - - int oneof_decl_count_; - int real_oneof_decl_count_; - int nested_type_count_; - int enum_type_count_; - int extension_range_count_; - int extension_count_; - int reserved_range_count_; - int reserved_name_count_; - - // IMPORTANT: If you add a new field, make sure to search for all instances - // of Allocate() and AllocateArray() in descriptor.cc - // and update them to initialize the field. - - // Must be constructed using DescriptorPool. - Descriptor() {} - friend class DescriptorBuilder; - friend class DescriptorPool; - friend class EnumDescriptor; - friend class FieldDescriptor; - friend class FileDescriptorTables; - friend class OneofDescriptor; - friend class MethodDescriptor; - friend class FileDescriptor; - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Descriptor); +public: + typedef DescriptorProto Proto; + + // The name of the message type, not including its scope. + const std::string &name() const; + + // The fully-qualified name of the message type, scope delimited by + // periods. For example, message type "Foo" which is declared in package + // "bar" has full name "bar.Foo". If a type "Baz" is nested within + // Foo, Baz's full_name is "bar.Foo.Baz". To get only the part that + // comes after the last '.', use name(). + const std::string &full_name() const; + + // Index of this descriptor within the file or containing type's message + // type array. + int index() const; + + // The .proto file in which this message type was defined. Never nullptr. + const FileDescriptor *file() const; + + // If this Descriptor describes a nested type, this returns the type + // in which it is nested. Otherwise, returns nullptr. + const Descriptor *containing_type() const; + + // Get options for this message type. These are specified in the .proto file + // by placing lines like "option foo = 1234;" in the message definition. + // Allowed options are defined by MessageOptions in descriptor.proto, and any + // available extensions of that message. + const MessageOptions &options() const; + + // Write the contents of this Descriptor into the given DescriptorProto. + // The target DescriptorProto must be clear before calling this; if it + // isn't, the result may be garbage. + void CopyTo(DescriptorProto *proto) const; + + // Write the contents of this descriptor in a human-readable form. Output + // will be suitable for re-parsing. + std::string DebugString() const; + + // Similar to DebugString(), but additionally takes options (e.g., + // include original user comments in output). + std::string DebugStringWithOptions(const DebugStringOptions &options) const; + + // Returns true if this is a placeholder for an unknown type. This will + // only be the case if this descriptor comes from a DescriptorPool + // with AllowUnknownDependencies() set. + bool is_placeholder() const; + + enum WellKnownType { + WELLKNOWNTYPE_UNSPECIFIED, // Not a well-known type. + + // Wrapper types. + WELLKNOWNTYPE_DOUBLEVALUE, // google.protobuf.DoubleValue + WELLKNOWNTYPE_FLOATVALUE, // google.protobuf.FloatValue + WELLKNOWNTYPE_INT64VALUE, // google.protobuf.Int64Value + WELLKNOWNTYPE_UINT64VALUE, // google.protobuf.UInt64Value + WELLKNOWNTYPE_INT32VALUE, // google.protobuf.Int32Value + WELLKNOWNTYPE_UINT32VALUE, // google.protobuf.UInt32Value + WELLKNOWNTYPE_STRINGVALUE, // google.protobuf.StringValue + WELLKNOWNTYPE_BYTESVALUE, // google.protobuf.BytesValue + WELLKNOWNTYPE_BOOLVALUE, // google.protobuf.BoolValue + + // Other well known types. + WELLKNOWNTYPE_ANY, // google.protobuf.Any + WELLKNOWNTYPE_FIELDMASK, // google.protobuf.FieldMask + WELLKNOWNTYPE_DURATION, // google.protobuf.Duration + WELLKNOWNTYPE_TIMESTAMP, // google.protobuf.Timestamp + WELLKNOWNTYPE_VALUE, // google.protobuf.Value + WELLKNOWNTYPE_LISTVALUE, // google.protobuf.ListValue + WELLKNOWNTYPE_STRUCT, // google.protobuf.Struct + + // New well-known types may be added in the future. + // Please make sure any switch() statements have a 'default' case. + __WELLKNOWNTYPE__DO_NOT_USE__ADD_DEFAULT_INSTEAD__, + }; + + WellKnownType well_known_type() const; + + // Field stuff ----------------------------------------------------- + + // The number of fields in this message type. + int field_count() const; + // Gets a field by index, where 0 <= index < field_count(). + // These are returned in the order they were defined in the .proto file. + const FieldDescriptor *field(int index) const; + + // Looks up a field by declared tag number. Returns nullptr if no such field + // exists. + const FieldDescriptor *FindFieldByNumber(int number) const; + // Looks up a field by name. Returns nullptr if no such field exists. + const FieldDescriptor *FindFieldByName(ConstStringParam name) const; + + // Looks up a field by lowercased name (as returned by lowercase_name()). + // This lookup may be ambiguous if multiple field names differ only by case, + // in which case the field returned is chosen arbitrarily from the matches. + const FieldDescriptor *FindFieldByLowercaseName(ConstStringParam lowercase_name) const; + + // Looks up a field by camel-case name (as returned by camelcase_name()). + // This lookup may be ambiguous if multiple field names differ in a way that + // leads them to have identical camel-case names, in which case the field + // returned is chosen arbitrarily from the matches. + const FieldDescriptor *FindFieldByCamelcaseName(ConstStringParam camelcase_name) const; + + // The number of oneofs in this message type. + int oneof_decl_count() const; + // The number of oneofs in this message type, excluding synthetic oneofs. + // Real oneofs always come first, so iterating up to real_oneof_decl_cout() + // will yield all real oneofs. + int real_oneof_decl_count() const; + // Get a oneof by index, where 0 <= index < oneof_decl_count(). + // These are returned in the order they were defined in the .proto file. + const OneofDescriptor *oneof_decl(int index) const; + + // Looks up a oneof by name. Returns nullptr if no such oneof exists. + const OneofDescriptor *FindOneofByName(ConstStringParam name) const; + + // Nested type stuff ----------------------------------------------- + + // The number of nested types in this message type. + int nested_type_count() const; + // Gets a nested type by index, where 0 <= index < nested_type_count(). + // These are returned in the order they were defined in the .proto file. + const Descriptor *nested_type(int index) const; + + // Looks up a nested type by name. Returns nullptr if no such nested type + // exists. + const Descriptor *FindNestedTypeByName(ConstStringParam name) const; + + // Enum stuff ------------------------------------------------------ + + // The number of enum types in this message type. + int enum_type_count() const; + // Gets an enum type by index, where 0 <= index < enum_type_count(). + // These are returned in the order they were defined in the .proto file. + const EnumDescriptor *enum_type(int index) const; + + // Looks up an enum type by name. Returns nullptr if no such enum type + // exists. + const EnumDescriptor *FindEnumTypeByName(ConstStringParam name) const; + + // Looks up an enum value by name, among all enum types in this message. + // Returns nullptr if no such value exists. + const EnumValueDescriptor *FindEnumValueByName(ConstStringParam name) const; + + // Extensions ------------------------------------------------------ + + // A range of field numbers which are designated for third-party + // extensions. + struct ExtensionRange { + typedef DescriptorProto_ExtensionRange Proto; + + typedef ExtensionRangeOptions OptionsType; + + // See Descriptor::CopyTo(). + void CopyTo(DescriptorProto_ExtensionRange *proto) const; + + int start; // inclusive + int end; // exclusive + + const ExtensionRangeOptions *options_; + }; + + // The number of extension ranges in this message type. + int extension_range_count() const; + // Gets an extension range by index, where 0 <= index < + // extension_range_count(). These are returned in the order they were defined + // in the .proto file. + const ExtensionRange *extension_range(int index) const; + + // Returns true if the number is in one of the extension ranges. + bool IsExtensionNumber(int number) const; + + // Returns nullptr if no extension range contains the given number. + const ExtensionRange *FindExtensionRangeContainingNumber(int number) const; + + // The number of extensions defined nested within this message type's scope. + // See doc: + // https://developers.google.com/protocol-buffers/docs/proto#nested-extensions + // + // Note that the extensions may be extending *other* messages. + // + // For example: + // message M1 { + // extensions 1 to max; + // } + // + // message M2 { + // extend M1 { + // optional int32 foo = 1; + // } + // } + // + // In this case, + // DescriptorPool::generated_pool() + // ->FindMessageTypeByName("M2") + // ->extension(0) + // will return "foo", even though "foo" is an extension of M1. + // To find all known extensions of a given message, instead use + // DescriptorPool::FindAllExtensions. + int extension_count() const; + // Get an extension by index, where 0 <= index < extension_count(). + // These are returned in the order they were defined in the .proto file. + const FieldDescriptor *extension(int index) const; + + // Looks up a named extension (which extends some *other* message type) + // defined within this message type's scope. + const FieldDescriptor *FindExtensionByName(ConstStringParam name) const; + + // Similar to FindFieldByLowercaseName(), but finds extensions defined within + // this message type's scope. + const FieldDescriptor *FindExtensionByLowercaseName(ConstStringParam name) const; + + // Similar to FindFieldByCamelcaseName(), but finds extensions defined within + // this message type's scope. + const FieldDescriptor *FindExtensionByCamelcaseName(ConstStringParam name) const; + + // Reserved fields ------------------------------------------------- + + // A range of reserved field numbers. + struct ReservedRange { + int start; // inclusive + int end; // exclusive + }; + + // The number of reserved ranges in this message type. + int reserved_range_count() const; + // Gets an reserved range by index, where 0 <= index < + // reserved_range_count(). These are returned in the order they were defined + // in the .proto file. + const ReservedRange *reserved_range(int index) const; + + // Returns true if the number is in one of the reserved ranges. + bool IsReservedNumber(int number) const; + + // Returns nullptr if no reserved range contains the given number. + const ReservedRange *FindReservedRangeContainingNumber(int number) const; + + // The number of reserved field names in this message type. + int reserved_name_count() const; + + // Gets a reserved name by index, where 0 <= index < reserved_name_count(). + const std::string &reserved_name(int index) const; + + // Returns true if the field name is reserved. + bool IsReservedName(ConstStringParam name) const; + + // Source Location --------------------------------------------------- + + // Updates |*out_location| to the source location of the complete + // extent of this message declaration. Returns false and leaves + // |*out_location| unchanged iff location information was not available. + bool GetSourceLocation(SourceLocation *out_location) const; + + // Maps -------------------------------------------------------------- + + // Returns the FieldDescriptor for the "key" field. If this isn't a map entry + // field, returns nullptr. + const FieldDescriptor *map_key() const; + + // Returns the FieldDescriptor for the "value" field. If this isn't a map + // entry field, returns nullptr. + const FieldDescriptor *map_value() const; + +private: + friend class Symbol; + typedef MessageOptions OptionsType; + + // Allows tests to test CopyTo(proto, true). + friend class descriptor_unittest::DescriptorTest; + + // Allows access to GetLocationPath for annotations. + friend class io::Printer; + friend class compiler::cpp::Formatter; + + // Fill the json_name field of FieldDescriptorProto. + void CopyJsonNameTo(DescriptorProto *proto) const; + + // Internal version of DebugString; controls the level of indenting for + // correct depth. Takes |options| to control debug-string options, and + // |include_opening_clause| to indicate whether the "message ... " part of the + // clause has already been generated (this varies depending on context). + void DebugString(int depth, std::string *contents, const DebugStringOptions &options, + bool include_opening_clause) const; + + // Walks up the descriptor tree to generate the source location path + // to this descriptor from the file root. + void GetLocationPath(std::vector *output) const; + + // True if this is a placeholder for an unknown type. + bool is_placeholder_ : 1; + // True if this is a placeholder and the type name wasn't fully-qualified. + bool is_unqualified_placeholder_ : 1; + // Well known type. Stored like this to conserve space. + uint8_t well_known_type_ : 5; + + // This points to the last field _number_ that is part of the sequence + // starting at 1, where + // `desc->field(i)->number() == i + 1` + // A value of `0` means no field matches. That is, there are no fields or the + // first field is not field `1`. + // Uses 16-bit to avoid extra padding. Unlikely to have more than 2^16 + // sequentially numbered fields in a message. + uint16_t sequential_field_limit_; + + int field_count_; + + // all_names_ = [name, full_name] + const std::string *all_names_; + const FileDescriptor *file_; + const Descriptor *containing_type_; + const MessageOptions *options_; + + // These arrays are separated from their sizes to minimize padding on 64-bit. + FieldDescriptor *fields_; + OneofDescriptor *oneof_decls_; + Descriptor *nested_types_; + EnumDescriptor *enum_types_; + ExtensionRange *extension_ranges_; + FieldDescriptor *extensions_; + ReservedRange *reserved_ranges_; + const std::string **reserved_names_; + + int oneof_decl_count_; + int real_oneof_decl_count_; + int nested_type_count_; + int enum_type_count_; + int extension_range_count_; + int extension_count_; + int reserved_range_count_; + int reserved_name_count_; + + // IMPORTANT: If you add a new field, make sure to search for all instances + // of Allocate() and AllocateArray() in descriptor.cc + // and update them to initialize the field. + + // Must be constructed using DescriptorPool. + Descriptor() { + } + friend class DescriptorBuilder; + friend class DescriptorPool; + friend class EnumDescriptor; + friend class FieldDescriptor; + friend class FileDescriptorTables; + friend class OneofDescriptor; + friend class MethodDescriptor; + friend class FileDescriptor; + GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Descriptor); }; - // Describes a single field of a message. To get the descriptor for a given // field, first get the Descriptor for the message in which it is defined, // then call Descriptor::FindFieldByName(). To get a FieldDescriptor for @@ -610,617 +602,622 @@ class PROTOBUF_EXPORT Descriptor : private internal::SymbolBase { // DescriptorPool::FindExtensionByPrintableName(). // Use DescriptorPool to construct your own descriptors. class PROTOBUF_EXPORT FieldDescriptor : private internal::SymbolBase { - public: - typedef FieldDescriptorProto Proto; - - // Identifies a field type. 0 is reserved for errors. The order is weird - // for historical reasons. Types 12 and up are new in proto2. - enum Type { - TYPE_DOUBLE = 1, // double, exactly eight bytes on the wire. - TYPE_FLOAT = 2, // float, exactly four bytes on the wire. - TYPE_INT64 = 3, // int64, varint on the wire. Negative numbers - // take 10 bytes. Use TYPE_SINT64 if negative - // values are likely. - TYPE_UINT64 = 4, // uint64, varint on the wire. - TYPE_INT32 = 5, // int32, varint on the wire. Negative numbers - // take 10 bytes. Use TYPE_SINT32 if negative - // values are likely. - TYPE_FIXED64 = 6, // uint64, exactly eight bytes on the wire. - TYPE_FIXED32 = 7, // uint32, exactly four bytes on the wire. - TYPE_BOOL = 8, // bool, varint on the wire. - TYPE_STRING = 9, // UTF-8 text. - TYPE_GROUP = 10, // Tag-delimited message. Deprecated. - TYPE_MESSAGE = 11, // Length-delimited message. - - TYPE_BYTES = 12, // Arbitrary byte array. - TYPE_UINT32 = 13, // uint32, varint on the wire - TYPE_ENUM = 14, // Enum, varint on the wire - TYPE_SFIXED32 = 15, // int32, exactly four bytes on the wire - TYPE_SFIXED64 = 16, // int64, exactly eight bytes on the wire - TYPE_SINT32 = 17, // int32, ZigZag-encoded varint on the wire - TYPE_SINT64 = 18, // int64, ZigZag-encoded varint on the wire - - MAX_TYPE = 18, // Constant useful for defining lookup tables - // indexed by Type. - }; - - // Specifies the C++ data type used to represent the field. There is a - // fixed mapping from Type to CppType where each Type maps to exactly one - // CppType. 0 is reserved for errors. - enum CppType { - CPPTYPE_INT32 = 1, // TYPE_INT32, TYPE_SINT32, TYPE_SFIXED32 - CPPTYPE_INT64 = 2, // TYPE_INT64, TYPE_SINT64, TYPE_SFIXED64 - CPPTYPE_UINT32 = 3, // TYPE_UINT32, TYPE_FIXED32 - CPPTYPE_UINT64 = 4, // TYPE_UINT64, TYPE_FIXED64 - CPPTYPE_DOUBLE = 5, // TYPE_DOUBLE - CPPTYPE_FLOAT = 6, // TYPE_FLOAT - CPPTYPE_BOOL = 7, // TYPE_BOOL - CPPTYPE_ENUM = 8, // TYPE_ENUM - CPPTYPE_STRING = 9, // TYPE_STRING, TYPE_BYTES - CPPTYPE_MESSAGE = 10, // TYPE_MESSAGE, TYPE_GROUP - - MAX_CPPTYPE = 10, // Constant useful for defining lookup tables - // indexed by CppType. - }; - - // Identifies whether the field is optional, required, or repeated. 0 is - // reserved for errors. - enum Label { - LABEL_OPTIONAL = 1, // optional - LABEL_REQUIRED = 2, // required - LABEL_REPEATED = 3, // repeated - - MAX_LABEL = 3, // Constant useful for defining lookup tables - // indexed by Label. - }; - - // Valid field numbers are positive integers up to kMaxNumber. - static const int kMaxNumber = (1 << 29) - 1; - - // First field number reserved for the protocol buffer library implementation. - // Users may not declare fields that use reserved numbers. - static const int kFirstReservedNumber = 19000; - // Last field number reserved for the protocol buffer library implementation. - // Users may not declare fields that use reserved numbers. - static const int kLastReservedNumber = 19999; - - const std::string& name() const; // Name of this field within the message. - const std::string& full_name() const; // Fully-qualified name of the field. - const std::string& json_name() const; // JSON name of this field. - const FileDescriptor* file() const; // File in which this field was defined. - bool is_extension() const; // Is this an extension field? - int number() const; // Declared tag number. - - // Same as name() except converted to lower-case. This (and especially the - // FindFieldByLowercaseName() method) can be useful when parsing formats - // which prefer to use lowercase naming style. (Although, technically - // field names should be lowercased anyway according to the protobuf style - // guide, so this only makes a difference when dealing with old .proto files - // which do not follow the guide.) - const std::string& lowercase_name() const; - - // Same as name() except converted to camel-case. In this conversion, any - // time an underscore appears in the name, it is removed and the next - // letter is capitalized. Furthermore, the first letter of the name is - // lower-cased. Examples: - // FooBar -> fooBar - // foo_bar -> fooBar - // fooBar -> fooBar - // This (and especially the FindFieldByCamelcaseName() method) can be useful - // when parsing formats which prefer to use camel-case naming style. - const std::string& camelcase_name() const; - - Type type() const; // Declared type of this field. - const char* type_name() const; // Name of the declared type. - CppType cpp_type() const; // C++ type of this field. - const char* cpp_type_name() const; // Name of the C++ type. - Label label() const; // optional/required/repeated - - bool is_required() const; // shorthand for label() == LABEL_REQUIRED - bool is_optional() const; // shorthand for label() == LABEL_OPTIONAL - bool is_repeated() const; // shorthand for label() == LABEL_REPEATED - bool is_packable() const; // shorthand for is_repeated() && - // IsTypePackable(type()) - bool is_packed() const; // shorthand for is_packable() && - // options().packed() - bool is_map() const; // shorthand for type() == TYPE_MESSAGE && - // message_type()->options().map_entry() - - // Returns true if this field was syntactically written with "optional" in the - // .proto file. Excludes singular proto3 fields that do not have a label. - bool has_optional_keyword() const; - - // Returns true if this field tracks presence, ie. does the field - // distinguish between "unset" and "present with default value." - // This includes required, optional, and oneof fields. It excludes maps, - // repeated fields, and singular proto3 fields without "optional". - // - // For fields where has_presence() == true, the return value of - // Reflection::HasField() is semantically meaningful. - bool has_presence() const; - - // Index of this field within the message's field array, or the file or - // extension scope's extensions array. - int index() const; - - // Does this field have an explicitly-declared default value? - bool has_default_value() const; - - // Whether the user has specified the json_name field option in the .proto - // file. - bool has_json_name() const; - - // Get the field default value if cpp_type() == CPPTYPE_INT32. If no - // explicit default was defined, the default is 0. - int32_t default_value_int32_t() const; - int32_t default_value_int32() const { return default_value_int32_t(); } - // Get the field default value if cpp_type() == CPPTYPE_INT64. If no - // explicit default was defined, the default is 0. - int64_t default_value_int64_t() const; - int64_t default_value_int64() const { return default_value_int64_t(); } - // Get the field default value if cpp_type() == CPPTYPE_UINT32. If no - // explicit default was defined, the default is 0. - uint32_t default_value_uint32_t() const; - uint32_t default_value_uint32() const { return default_value_uint32_t(); } - // Get the field default value if cpp_type() == CPPTYPE_UINT64. If no - // explicit default was defined, the default is 0. - uint64_t default_value_uint64_t() const; - uint64_t default_value_uint64() const { return default_value_uint64_t(); } - // Get the field default value if cpp_type() == CPPTYPE_FLOAT. If no - // explicit default was defined, the default is 0.0. - float default_value_float() const; - // Get the field default value if cpp_type() == CPPTYPE_DOUBLE. If no - // explicit default was defined, the default is 0.0. - double default_value_double() const; - // Get the field default value if cpp_type() == CPPTYPE_BOOL. If no - // explicit default was defined, the default is false. - bool default_value_bool() const; - // Get the field default value if cpp_type() == CPPTYPE_ENUM. If no - // explicit default was defined, the default is the first value defined - // in the enum type (all enum types are required to have at least one value). - // This never returns nullptr. - const EnumValueDescriptor* default_value_enum() const; - // Get the field default value if cpp_type() == CPPTYPE_STRING. If no - // explicit default was defined, the default is the empty string. - const std::string& default_value_string() const; - - // The Descriptor for the message of which this is a field. For extensions, - // this is the extended type. Never nullptr. - const Descriptor* containing_type() const; - - // If the field is a member of a oneof, this is the one, otherwise this is - // nullptr. - const OneofDescriptor* containing_oneof() const; - - // If the field is a member of a non-synthetic oneof, returns the descriptor - // for the oneof, otherwise returns nullptr. - const OneofDescriptor* real_containing_oneof() const; - - // If the field is a member of a oneof, returns the index in that oneof. - int index_in_oneof() const; - - // An extension may be declared within the scope of another message. If this - // field is an extension (is_extension() is true), then extension_scope() - // returns that message, or nullptr if the extension was declared at global - // scope. If this is not an extension, extension_scope() is undefined (may - // assert-fail). - const Descriptor* extension_scope() const; - - // If type is TYPE_MESSAGE or TYPE_GROUP, returns a descriptor for the - // message or the group type. Otherwise, returns null. - const Descriptor* message_type() const; - // If type is TYPE_ENUM, returns a descriptor for the enum. Otherwise, - // returns null. - const EnumDescriptor* enum_type() const; - - // Get the FieldOptions for this field. This includes things listed in - // square brackets after the field definition. E.g., the field: - // optional string text = 1 [ctype=CORD]; - // has the "ctype" option set. Allowed options are defined by FieldOptions in - // descriptor.proto, and any available extensions of that message. - const FieldOptions& options() const; - - // See Descriptor::CopyTo(). - void CopyTo(FieldDescriptorProto* proto) const; - - // See Descriptor::DebugString(). - std::string DebugString() const; - - // See Descriptor::DebugStringWithOptions(). - std::string DebugStringWithOptions(const DebugStringOptions& options) const; - - // Helper method to get the CppType for a particular Type. - static CppType TypeToCppType(Type type); - - // Helper method to get the name of a Type. - static const char* TypeName(Type type); - - // Helper method to get the name of a CppType. - static const char* CppTypeName(CppType cpp_type); - - // Return true iff [packed = true] is valid for fields of this type. - static inline bool IsTypePackable(Type field_type); - - // Returns full_name() except if the field is a MessageSet extension, - // in which case it returns the full_name() of the containing message type - // for backwards compatibility with proto1. - // - // A MessageSet extension is defined as an optional message extension - // whose containing type has the message_set_wire_format option set. - // This should be true of extensions of google.protobuf.bridge.MessageSet; - // by convention, such extensions are named "message_set_extension". - // - // The opposite operation (looking up an extension's FieldDescriptor given - // its printable name) can be accomplished with - // message->file()->pool()->FindExtensionByPrintableName(message, name) - // where the extension extends "message". - const std::string& PrintableNameForExtension() const; - - // Source Location --------------------------------------------------- - - // Updates |*out_location| to the source location of the complete - // extent of this field declaration. Returns false and leaves - // |*out_location| unchanged iff location information was not available. - bool GetSourceLocation(SourceLocation* out_location) const; - - private: - friend class Symbol; - typedef FieldOptions OptionsType; - - // Allows access to GetLocationPath for annotations. - friend class io::Printer; - friend class compiler::cpp::Formatter; - friend class Reflection; - - // Fill the json_name field of FieldDescriptorProto. - void CopyJsonNameTo(FieldDescriptorProto* proto) const; - - // See Descriptor::DebugString(). - void DebugString(int depth, std::string* contents, - const DebugStringOptions& options) const; - - // formats the default value appropriately and returns it as a string. - // Must have a default value to call this. If quote_string_type is true, then - // types of CPPTYPE_STRING whill be surrounded by quotes and CEscaped. - std::string DefaultValueAsString(bool quote_string_type) const; - - // Helper function that returns the field type name for DebugString. - std::string FieldTypeNameDebugString() const; - - // Walks up the descriptor tree to generate the source location path - // to this descriptor from the file root. - void GetLocationPath(std::vector* output) const; - - // Returns true if this is a map message type. - bool is_map_message_type() const; - - bool has_default_value_ : 1; - bool proto3_optional_ : 1; - // Whether the user has specified the json_name field option in the .proto - // file. - bool has_json_name_ : 1; - bool is_extension_ : 1; - bool is_oneof_ : 1; - - // Actually a `Label` but stored as uint8_t to save space. - uint8_t label_ : 2; - - // Actually a `Type`, but stored as uint8_t to save space. - mutable uint8_t type_; - - // Logically: - // all_names_ = [name, full_name, lower, camel, json] - // However: - // duplicates will be omitted, so lower/camel/json might be in the same - // position. - // We store the true offset for each name here, and the bit width must be - // large enough to account for the worst case where all names are present. - uint8_t lowercase_name_index_ : 2; - uint8_t camelcase_name_index_ : 2; - uint8_t json_name_index_ : 3; - // Sadly, `number_` located here to reduce padding. Unrelated to all_names_ - // and its indices above. - int number_; - const std::string* all_names_; - const FileDescriptor* file_; - - internal::once_flag* type_once_; - static void TypeOnceInit(const FieldDescriptor* to_init); - void InternalTypeOnceInit() const; - const Descriptor* containing_type_; - union { - const OneofDescriptor* containing_oneof; - const Descriptor* extension_scope; - } scope_; - union { - mutable const Descriptor* message_type; - mutable const EnumDescriptor* enum_type; - const char* lazy_type_name; - } type_descriptor_; - const FieldOptions* options_; - // IMPORTANT: If you add a new field, make sure to search for all instances - // of Allocate() and AllocateArray() in - // descriptor.cc and update them to initialize the field. - - union { - int32_t default_value_int32_t_; - int64_t default_value_int64_t_; - uint32_t default_value_uint32_t_; - uint64_t default_value_uint64_t_; - float default_value_float_; - double default_value_double_; - bool default_value_bool_; - - mutable const EnumValueDescriptor* default_value_enum_; - const char* lazy_default_value_enum_name_; - const std::string* default_value_string_; - mutable std::atomic default_generated_instance_; - }; - - static const CppType kTypeToCppTypeMap[MAX_TYPE + 1]; - - static const char* const kTypeToName[MAX_TYPE + 1]; - - static const char* const kCppTypeToName[MAX_CPPTYPE + 1]; - - static const char* const kLabelToName[MAX_LABEL + 1]; - - // Must be constructed using DescriptorPool. - FieldDescriptor() {} - friend class DescriptorBuilder; - friend class FileDescriptor; - friend class Descriptor; - friend class OneofDescriptor; - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldDescriptor); +public: + typedef FieldDescriptorProto Proto; + + // Identifies a field type. 0 is reserved for errors. The order is weird + // for historical reasons. Types 12 and up are new in proto2. + enum Type { + TYPE_DOUBLE = 1, // double, exactly eight bytes on the wire. + TYPE_FLOAT = 2, // float, exactly four bytes on the wire. + TYPE_INT64 = 3, // int64, varint on the wire. Negative numbers + // take 10 bytes. Use TYPE_SINT64 if negative + // values are likely. + TYPE_UINT64 = 4, // uint64, varint on the wire. + TYPE_INT32 = 5, // int32, varint on the wire. Negative numbers + // take 10 bytes. Use TYPE_SINT32 if negative + // values are likely. + TYPE_FIXED64 = 6, // uint64, exactly eight bytes on the wire. + TYPE_FIXED32 = 7, // uint32, exactly four bytes on the wire. + TYPE_BOOL = 8, // bool, varint on the wire. + TYPE_STRING = 9, // UTF-8 text. + TYPE_GROUP = 10, // Tag-delimited message. Deprecated. + TYPE_MESSAGE = 11, // Length-delimited message. + + TYPE_BYTES = 12, // Arbitrary byte array. + TYPE_UINT32 = 13, // uint32, varint on the wire + TYPE_ENUM = 14, // Enum, varint on the wire + TYPE_SFIXED32 = 15, // int32, exactly four bytes on the wire + TYPE_SFIXED64 = 16, // int64, exactly eight bytes on the wire + TYPE_SINT32 = 17, // int32, ZigZag-encoded varint on the wire + TYPE_SINT64 = 18, // int64, ZigZag-encoded varint on the wire + + MAX_TYPE = 18, // Constant useful for defining lookup tables + // indexed by Type. + }; + + // Specifies the C++ data type used to represent the field. There is a + // fixed mapping from Type to CppType where each Type maps to exactly one + // CppType. 0 is reserved for errors. + enum CppType { + CPPTYPE_INT32 = 1, // TYPE_INT32, TYPE_SINT32, TYPE_SFIXED32 + CPPTYPE_INT64 = 2, // TYPE_INT64, TYPE_SINT64, TYPE_SFIXED64 + CPPTYPE_UINT32 = 3, // TYPE_UINT32, TYPE_FIXED32 + CPPTYPE_UINT64 = 4, // TYPE_UINT64, TYPE_FIXED64 + CPPTYPE_DOUBLE = 5, // TYPE_DOUBLE + CPPTYPE_FLOAT = 6, // TYPE_FLOAT + CPPTYPE_BOOL = 7, // TYPE_BOOL + CPPTYPE_ENUM = 8, // TYPE_ENUM + CPPTYPE_STRING = 9, // TYPE_STRING, TYPE_BYTES + CPPTYPE_MESSAGE = 10, // TYPE_MESSAGE, TYPE_GROUP + + MAX_CPPTYPE = 10, // Constant useful for defining lookup tables + // indexed by CppType. + }; + + // Identifies whether the field is optional, required, or repeated. 0 is + // reserved for errors. + enum Label { + LABEL_OPTIONAL = 1, // optional + LABEL_REQUIRED = 2, // required + LABEL_REPEATED = 3, // repeated + + MAX_LABEL = 3, // Constant useful for defining lookup tables + // indexed by Label. + }; + + // Valid field numbers are positive integers up to kMaxNumber. + static const int kMaxNumber = (1 << 29) - 1; + + // First field number reserved for the protocol buffer library implementation. + // Users may not declare fields that use reserved numbers. + static const int kFirstReservedNumber = 19000; + // Last field number reserved for the protocol buffer library implementation. + // Users may not declare fields that use reserved numbers. + static const int kLastReservedNumber = 19999; + + const std::string &name() const; // Name of this field within the message. + const std::string &full_name() const; // Fully-qualified name of the field. + const std::string &json_name() const; // JSON name of this field. + const FileDescriptor *file() const; // File in which this field was defined. + bool is_extension() const; // Is this an extension field? + int number() const; // Declared tag number. + + // Same as name() except converted to lower-case. This (and especially the + // FindFieldByLowercaseName() method) can be useful when parsing formats + // which prefer to use lowercase naming style. (Although, technically + // field names should be lowercased anyway according to the protobuf style + // guide, so this only makes a difference when dealing with old .proto files + // which do not follow the guide.) + const std::string &lowercase_name() const; + + // Same as name() except converted to camel-case. In this conversion, any + // time an underscore appears in the name, it is removed and the next + // letter is capitalized. Furthermore, the first letter of the name is + // lower-cased. Examples: + // FooBar -> fooBar + // foo_bar -> fooBar + // fooBar -> fooBar + // This (and especially the FindFieldByCamelcaseName() method) can be useful + // when parsing formats which prefer to use camel-case naming style. + const std::string &camelcase_name() const; + + Type type() const; // Declared type of this field. + const char *type_name() const; // Name of the declared type. + CppType cpp_type() const; // C++ type of this field. + const char *cpp_type_name() const; // Name of the C++ type. + Label label() const; // optional/required/repeated + + bool is_required() const; // shorthand for label() == LABEL_REQUIRED + bool is_optional() const; // shorthand for label() == LABEL_OPTIONAL + bool is_repeated() const; // shorthand for label() == LABEL_REPEATED + bool is_packable() const; // shorthand for is_repeated() && + // IsTypePackable(type()) + bool is_packed() const; // shorthand for is_packable() && + // options().packed() + bool is_map() const; // shorthand for type() == TYPE_MESSAGE && + // message_type()->options().map_entry() + + // Returns true if this field was syntactically written with "optional" in the + // .proto file. Excludes singular proto3 fields that do not have a label. + bool has_optional_keyword() const; + + // Returns true if this field tracks presence, ie. does the field + // distinguish between "unset" and "present with default value." + // This includes required, optional, and oneof fields. It excludes maps, + // repeated fields, and singular proto3 fields without "optional". + // + // For fields where has_presence() == true, the return value of + // Reflection::HasField() is semantically meaningful. + bool has_presence() const; + + // Index of this field within the message's field array, or the file or + // extension scope's extensions array. + int index() const; + + // Does this field have an explicitly-declared default value? + bool has_default_value() const; + + // Whether the user has specified the json_name field option in the .proto + // file. + bool has_json_name() const; + + // Get the field default value if cpp_type() == CPPTYPE_INT32. If no + // explicit default was defined, the default is 0. + int32_t default_value_int32_t() const; + int32_t default_value_int32() const { + return default_value_int32_t(); + } + // Get the field default value if cpp_type() == CPPTYPE_INT64. If no + // explicit default was defined, the default is 0. + int64_t default_value_int64_t() const; + int64_t default_value_int64() const { + return default_value_int64_t(); + } + // Get the field default value if cpp_type() == CPPTYPE_UINT32. If no + // explicit default was defined, the default is 0. + uint32_t default_value_uint32_t() const; + uint32_t default_value_uint32() const { + return default_value_uint32_t(); + } + // Get the field default value if cpp_type() == CPPTYPE_UINT64. If no + // explicit default was defined, the default is 0. + uint64_t default_value_uint64_t() const; + uint64_t default_value_uint64() const { + return default_value_uint64_t(); + } + // Get the field default value if cpp_type() == CPPTYPE_FLOAT. If no + // explicit default was defined, the default is 0.0. + float default_value_float() const; + // Get the field default value if cpp_type() == CPPTYPE_DOUBLE. If no + // explicit default was defined, the default is 0.0. + double default_value_double() const; + // Get the field default value if cpp_type() == CPPTYPE_BOOL. If no + // explicit default was defined, the default is false. + bool default_value_bool() const; + // Get the field default value if cpp_type() == CPPTYPE_ENUM. If no + // explicit default was defined, the default is the first value defined + // in the enum type (all enum types are required to have at least one value). + // This never returns nullptr. + const EnumValueDescriptor *default_value_enum() const; + // Get the field default value if cpp_type() == CPPTYPE_STRING. If no + // explicit default was defined, the default is the empty string. + const std::string &default_value_string() const; + + // The Descriptor for the message of which this is a field. For extensions, + // this is the extended type. Never nullptr. + const Descriptor *containing_type() const; + + // If the field is a member of a oneof, this is the one, otherwise this is + // nullptr. + const OneofDescriptor *containing_oneof() const; + + // If the field is a member of a non-synthetic oneof, returns the descriptor + // for the oneof, otherwise returns nullptr. + const OneofDescriptor *real_containing_oneof() const; + + // If the field is a member of a oneof, returns the index in that oneof. + int index_in_oneof() const; + + // An extension may be declared within the scope of another message. If this + // field is an extension (is_extension() is true), then extension_scope() + // returns that message, or nullptr if the extension was declared at global + // scope. If this is not an extension, extension_scope() is undefined (may + // assert-fail). + const Descriptor *extension_scope() const; + + // If type is TYPE_MESSAGE or TYPE_GROUP, returns a descriptor for the + // message or the group type. Otherwise, returns null. + const Descriptor *message_type() const; + // If type is TYPE_ENUM, returns a descriptor for the enum. Otherwise, + // returns null. + const EnumDescriptor *enum_type() const; + + // Get the FieldOptions for this field. This includes things listed in + // square brackets after the field definition. E.g., the field: + // optional string text = 1 [ctype=CORD]; + // has the "ctype" option set. Allowed options are defined by FieldOptions in + // descriptor.proto, and any available extensions of that message. + const FieldOptions &options() const; + + // See Descriptor::CopyTo(). + void CopyTo(FieldDescriptorProto *proto) const; + + // See Descriptor::DebugString(). + std::string DebugString() const; + + // See Descriptor::DebugStringWithOptions(). + std::string DebugStringWithOptions(const DebugStringOptions &options) const; + + // Helper method to get the CppType for a particular Type. + static CppType TypeToCppType(Type type); + + // Helper method to get the name of a Type. + static const char *TypeName(Type type); + + // Helper method to get the name of a CppType. + static const char *CppTypeName(CppType cpp_type); + + // Return true iff [packed = true] is valid for fields of this type. + static inline bool IsTypePackable(Type field_type); + + // Returns full_name() except if the field is a MessageSet extension, + // in which case it returns the full_name() of the containing message type + // for backwards compatibility with proto1. + // + // A MessageSet extension is defined as an optional message extension + // whose containing type has the message_set_wire_format option set. + // This should be true of extensions of google.protobuf.bridge.MessageSet; + // by convention, such extensions are named "message_set_extension". + // + // The opposite operation (looking up an extension's FieldDescriptor given + // its printable name) can be accomplished with + // message->file()->pool()->FindExtensionByPrintableName(message, name) + // where the extension extends "message". + const std::string &PrintableNameForExtension() const; + + // Source Location --------------------------------------------------- + + // Updates |*out_location| to the source location of the complete + // extent of this field declaration. Returns false and leaves + // |*out_location| unchanged iff location information was not available. + bool GetSourceLocation(SourceLocation *out_location) const; + +private: + friend class Symbol; + typedef FieldOptions OptionsType; + + // Allows access to GetLocationPath for annotations. + friend class io::Printer; + friend class compiler::cpp::Formatter; + friend class Reflection; + + // Fill the json_name field of FieldDescriptorProto. + void CopyJsonNameTo(FieldDescriptorProto *proto) const; + + // See Descriptor::DebugString(). + void DebugString(int depth, std::string *contents, const DebugStringOptions &options) const; + + // formats the default value appropriately and returns it as a string. + // Must have a default value to call this. If quote_string_type is true, then + // types of CPPTYPE_STRING whill be surrounded by quotes and CEscaped. + std::string DefaultValueAsString(bool quote_string_type) const; + + // Helper function that returns the field type name for DebugString. + std::string FieldTypeNameDebugString() const; + + // Walks up the descriptor tree to generate the source location path + // to this descriptor from the file root. + void GetLocationPath(std::vector *output) const; + + // Returns true if this is a map message type. + bool is_map_message_type() const; + + bool has_default_value_ : 1; + bool proto3_optional_ : 1; + // Whether the user has specified the json_name field option in the .proto + // file. + bool has_json_name_ : 1; + bool is_extension_ : 1; + bool is_oneof_ : 1; + + // Actually a `Label` but stored as uint8_t to save space. + uint8_t label_ : 2; + + // Actually a `Type`, but stored as uint8_t to save space. + mutable uint8_t type_; + + // Logically: + // all_names_ = [name, full_name, lower, camel, json] + // However: + // duplicates will be omitted, so lower/camel/json might be in the same + // position. + // We store the true offset for each name here, and the bit width must be + // large enough to account for the worst case where all names are present. + uint8_t lowercase_name_index_ : 2; + uint8_t camelcase_name_index_ : 2; + uint8_t json_name_index_ : 3; + // Sadly, `number_` located here to reduce padding. Unrelated to all_names_ + // and its indices above. + int number_; + const std::string *all_names_; + const FileDescriptor *file_; + + internal::once_flag *type_once_; + static void TypeOnceInit(const FieldDescriptor *to_init); + void InternalTypeOnceInit() const; + const Descriptor *containing_type_; + union { + const OneofDescriptor *containing_oneof; + const Descriptor *extension_scope; + } scope_; + union { + mutable const Descriptor *message_type; + mutable const EnumDescriptor *enum_type; + const char *lazy_type_name; + } type_descriptor_; + const FieldOptions *options_; + // IMPORTANT: If you add a new field, make sure to search for all instances + // of Allocate() and AllocateArray() in + // descriptor.cc and update them to initialize the field. + + union { + int32_t default_value_int32_t_; + int64_t default_value_int64_t_; + uint32_t default_value_uint32_t_; + uint64_t default_value_uint64_t_; + float default_value_float_; + double default_value_double_; + bool default_value_bool_; + + mutable const EnumValueDescriptor *default_value_enum_; + const char *lazy_default_value_enum_name_; + const std::string *default_value_string_; + mutable std::atomic default_generated_instance_; + }; + + static const CppType kTypeToCppTypeMap[MAX_TYPE + 1]; + + static const char *const kTypeToName[MAX_TYPE + 1]; + + static const char *const kCppTypeToName[MAX_CPPTYPE + 1]; + + static const char *const kLabelToName[MAX_LABEL + 1]; + + // Must be constructed using DescriptorPool. + FieldDescriptor() { + } + friend class DescriptorBuilder; + friend class FileDescriptor; + friend class Descriptor; + friend class OneofDescriptor; + GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldDescriptor); }; - // Describes a oneof defined in a message type. class PROTOBUF_EXPORT OneofDescriptor : private internal::SymbolBase { - public: - typedef OneofDescriptorProto Proto; +public: + typedef OneofDescriptorProto Proto; - const std::string& name() const; // Name of this oneof. - const std::string& full_name() const; // Fully-qualified name of the oneof. + const std::string &name() const; // Name of this oneof. + const std::string &full_name() const; // Fully-qualified name of the oneof. - // Index of this oneof within the message's oneof array. - int index() const; + // Index of this oneof within the message's oneof array. + int index() const; - // Returns whether this oneof was inserted by the compiler to wrap a proto3 - // optional field. If this returns true, code generators should *not* emit it. - bool is_synthetic() const; + // Returns whether this oneof was inserted by the compiler to wrap a proto3 + // optional field. If this returns true, code generators should *not* emit it. + bool is_synthetic() const; - // The .proto file in which this oneof was defined. Never nullptr. - const FileDescriptor* file() const; - // The Descriptor for the message containing this oneof. - const Descriptor* containing_type() const; + // The .proto file in which this oneof was defined. Never nullptr. + const FileDescriptor *file() const; + // The Descriptor for the message containing this oneof. + const Descriptor *containing_type() const; - // The number of (non-extension) fields which are members of this oneof. - int field_count() const; - // Get a member of this oneof, in the order in which they were declared in the - // .proto file. Does not include extensions. - const FieldDescriptor* field(int index) const; + // The number of (non-extension) fields which are members of this oneof. + int field_count() const; + // Get a member of this oneof, in the order in which they were declared in the + // .proto file. Does not include extensions. + const FieldDescriptor *field(int index) const; - const OneofOptions& options() const; + const OneofOptions &options() const; - // See Descriptor::CopyTo(). - void CopyTo(OneofDescriptorProto* proto) const; + // See Descriptor::CopyTo(). + void CopyTo(OneofDescriptorProto *proto) const; - // See Descriptor::DebugString(). - std::string DebugString() const; + // See Descriptor::DebugString(). + std::string DebugString() const; - // See Descriptor::DebugStringWithOptions(). - std::string DebugStringWithOptions(const DebugStringOptions& options) const; + // See Descriptor::DebugStringWithOptions(). + std::string DebugStringWithOptions(const DebugStringOptions &options) const; - // Source Location --------------------------------------------------- + // Source Location --------------------------------------------------- - // Updates |*out_location| to the source location of the complete - // extent of this oneof declaration. Returns false and leaves - // |*out_location| unchanged iff location information was not available. - bool GetSourceLocation(SourceLocation* out_location) const; + // Updates |*out_location| to the source location of the complete + // extent of this oneof declaration. Returns false and leaves + // |*out_location| unchanged iff location information was not available. + bool GetSourceLocation(SourceLocation *out_location) const; - private: - friend class Symbol; - typedef OneofOptions OptionsType; +private: + friend class Symbol; + typedef OneofOptions OptionsType; - // Allows access to GetLocationPath for annotations. - friend class io::Printer; - friend class compiler::cpp::Formatter; + // Allows access to GetLocationPath for annotations. + friend class io::Printer; + friend class compiler::cpp::Formatter; - // See Descriptor::DebugString(). - void DebugString(int depth, std::string* contents, - const DebugStringOptions& options) const; + // See Descriptor::DebugString(). + void DebugString(int depth, std::string *contents, const DebugStringOptions &options) const; - // Walks up the descriptor tree to generate the source location path - // to this descriptor from the file root. - void GetLocationPath(std::vector* output) const; + // Walks up the descriptor tree to generate the source location path + // to this descriptor from the file root. + void GetLocationPath(std::vector *output) const; - int field_count_; + int field_count_; - // all_names_ = [name, full_name] - const std::string* all_names_; - const Descriptor* containing_type_; - const OneofOptions* options_; - const FieldDescriptor* fields_; + // all_names_ = [name, full_name] + const std::string *all_names_; + const Descriptor *containing_type_; + const OneofOptions *options_; + const FieldDescriptor *fields_; - // IMPORTANT: If you add a new field, make sure to search for all instances - // of Allocate() and AllocateArray() - // in descriptor.cc and update them to initialize the field. + // IMPORTANT: If you add a new field, make sure to search for all instances + // of Allocate() and AllocateArray() + // in descriptor.cc and update them to initialize the field. - // Must be constructed using DescriptorPool. - OneofDescriptor() {} - friend class DescriptorBuilder; - friend class Descriptor; - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(OneofDescriptor); + // Must be constructed using DescriptorPool. + OneofDescriptor() { + } + friend class DescriptorBuilder; + friend class Descriptor; + GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(OneofDescriptor); }; // Describes an enum type defined in a .proto file. To get the EnumDescriptor // for a generated enum type, call TypeName_descriptor(). Use DescriptorPool // to construct your own descriptors. class PROTOBUF_EXPORT EnumDescriptor : private internal::SymbolBase { - public: - typedef EnumDescriptorProto Proto; +public: + typedef EnumDescriptorProto Proto; + + // The name of this enum type in the containing scope. + const std::string &name() const; + + // The fully-qualified name of the enum type, scope delimited by periods. + const std::string &full_name() const; + + // Index of this enum within the file or containing message's enum array. + int index() const; + + // The .proto file in which this enum type was defined. Never nullptr. + const FileDescriptor *file() const; - // The name of this enum type in the containing scope. - const std::string& name() const; + // The number of values for this EnumDescriptor. Guaranteed to be greater + // than zero. + int value_count() const; + // Gets a value by index, where 0 <= index < value_count(). + // These are returned in the order they were defined in the .proto file. + const EnumValueDescriptor *value(int index) const; - // The fully-qualified name of the enum type, scope delimited by periods. - const std::string& full_name() const; - - // Index of this enum within the file or containing message's enum array. - int index() const; - - // The .proto file in which this enum type was defined. Never nullptr. - const FileDescriptor* file() const; - - // The number of values for this EnumDescriptor. Guaranteed to be greater - // than zero. - int value_count() const; - // Gets a value by index, where 0 <= index < value_count(). - // These are returned in the order they were defined in the .proto file. - const EnumValueDescriptor* value(int index) const; - - // Looks up a value by name. Returns nullptr if no such value exists. - const EnumValueDescriptor* FindValueByName(ConstStringParam name) const; - // Looks up a value by number. Returns nullptr if no such value exists. If - // multiple values have this number, the first one defined is returned. - const EnumValueDescriptor* FindValueByNumber(int number) const; - - // If this enum type is nested in a message type, this is that message type. - // Otherwise, nullptr. - const Descriptor* containing_type() const; - - // Get options for this enum type. These are specified in the .proto file by - // placing lines like "option foo = 1234;" in the enum definition. Allowed - // options are defined by EnumOptions in descriptor.proto, and any available - // extensions of that message. - const EnumOptions& options() const; - - // See Descriptor::CopyTo(). - void CopyTo(EnumDescriptorProto* proto) const; - - // See Descriptor::DebugString(). - std::string DebugString() const; - - // See Descriptor::DebugStringWithOptions(). - std::string DebugStringWithOptions(const DebugStringOptions& options) const; - - // Returns true if this is a placeholder for an unknown enum. This will - // only be the case if this descriptor comes from a DescriptorPool - // with AllowUnknownDependencies() set. - bool is_placeholder() const; - - // Reserved fields ------------------------------------------------- - - // A range of reserved field numbers. - struct ReservedRange { - int start; // inclusive - int end; // inclusive - }; - - // The number of reserved ranges in this message type. - int reserved_range_count() const; - // Gets an reserved range by index, where 0 <= index < - // reserved_range_count(). These are returned in the order they were defined - // in the .proto file. - const EnumDescriptor::ReservedRange* reserved_range(int index) const; - - // Returns true if the number is in one of the reserved ranges. - bool IsReservedNumber(int number) const; - - // Returns nullptr if no reserved range contains the given number. - const EnumDescriptor::ReservedRange* FindReservedRangeContainingNumber( - int number) const; - - // The number of reserved field names in this message type. - int reserved_name_count() const; - - // Gets a reserved name by index, where 0 <= index < reserved_name_count(). - const std::string& reserved_name(int index) const; - - // Returns true if the field name is reserved. - bool IsReservedName(ConstStringParam name) const; - - // Source Location --------------------------------------------------- - - // Updates |*out_location| to the source location of the complete - // extent of this enum declaration. Returns false and leaves - // |*out_location| unchanged iff location information was not available. - bool GetSourceLocation(SourceLocation* out_location) const; - - private: - friend class Symbol; - typedef EnumOptions OptionsType; - - // Allows access to GetLocationPath for annotations. - friend class io::Printer; - friend class compiler::cpp::Formatter; - - // Allow access to FindValueByNumberCreatingIfUnknown. - friend class descriptor_unittest::DescriptorTest; - - // Looks up a value by number. If the value does not exist, dynamically - // creates a new EnumValueDescriptor for that value, assuming that it was - // unknown. If a new descriptor is created, this is done in a thread-safe way, - // and future calls will return the same value descriptor pointer. - // - // This is private but is used by Reflection (which is friended below) to - // return a valid EnumValueDescriptor from GetEnum() when this feature is - // enabled. - const EnumValueDescriptor* FindValueByNumberCreatingIfUnknown( - int number) const; - - // See Descriptor::DebugString(). - void DebugString(int depth, std::string* contents, - const DebugStringOptions& options) const; - - // Walks up the descriptor tree to generate the source location path - // to this descriptor from the file root. - void GetLocationPath(std::vector* output) const; - - // True if this is a placeholder for an unknown type. - bool is_placeholder_ : 1; - // True if this is a placeholder and the type name wasn't fully-qualified. - bool is_unqualified_placeholder_ : 1; - - // This points to the last value _index_ that is part of the sequence starting - // with the first label, where - // `enum->value(i)->number() == enum->value(0)->number() + i` - // We measure relative to the first label to adapt to enum labels starting at - // 0 or 1. - // Uses 16-bit to avoid extra padding. Unlikely to have more than 2^15 - // sequentially numbered labels in an enum. - int16_t sequential_value_limit_; - - int value_count_; - - // all_names_ = [name, full_name] - const std::string* all_names_; - const FileDescriptor* file_; - const Descriptor* containing_type_; - const EnumOptions* options_; - EnumValueDescriptor* values_; - - int reserved_range_count_; - int reserved_name_count_; - EnumDescriptor::ReservedRange* reserved_ranges_; - const std::string** reserved_names_; - - // IMPORTANT: If you add a new field, make sure to search for all instances - // of Allocate() and AllocateArray() in - // descriptor.cc and update them to initialize the field. - - // Must be constructed using DescriptorPool. - EnumDescriptor() {} - friend class DescriptorBuilder; - friend class Descriptor; - friend class FieldDescriptor; - friend class FileDescriptorTables; - friend class EnumValueDescriptor; - friend class FileDescriptor; - friend class DescriptorPool; - friend class Reflection; - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(EnumDescriptor); + // Looks up a value by name. Returns nullptr if no such value exists. + const EnumValueDescriptor *FindValueByName(ConstStringParam name) const; + // Looks up a value by number. Returns nullptr if no such value exists. If + // multiple values have this number, the first one defined is returned. + const EnumValueDescriptor *FindValueByNumber(int number) const; + + // If this enum type is nested in a message type, this is that message type. + // Otherwise, nullptr. + const Descriptor *containing_type() const; + + // Get options for this enum type. These are specified in the .proto file by + // placing lines like "option foo = 1234;" in the enum definition. Allowed + // options are defined by EnumOptions in descriptor.proto, and any available + // extensions of that message. + const EnumOptions &options() const; + + // See Descriptor::CopyTo(). + void CopyTo(EnumDescriptorProto *proto) const; + + // See Descriptor::DebugString(). + std::string DebugString() const; + + // See Descriptor::DebugStringWithOptions(). + std::string DebugStringWithOptions(const DebugStringOptions &options) const; + + // Returns true if this is a placeholder for an unknown enum. This will + // only be the case if this descriptor comes from a DescriptorPool + // with AllowUnknownDependencies() set. + bool is_placeholder() const; + + // Reserved fields ------------------------------------------------- + + // A range of reserved field numbers. + struct ReservedRange { + int start; // inclusive + int end; // inclusive + }; + + // The number of reserved ranges in this message type. + int reserved_range_count() const; + // Gets an reserved range by index, where 0 <= index < + // reserved_range_count(). These are returned in the order they were defined + // in the .proto file. + const EnumDescriptor::ReservedRange *reserved_range(int index) const; + + // Returns true if the number is in one of the reserved ranges. + bool IsReservedNumber(int number) const; + + // Returns nullptr if no reserved range contains the given number. + const EnumDescriptor::ReservedRange *FindReservedRangeContainingNumber(int number) const; + + // The number of reserved field names in this message type. + int reserved_name_count() const; + + // Gets a reserved name by index, where 0 <= index < reserved_name_count(). + const std::string &reserved_name(int index) const; + + // Returns true if the field name is reserved. + bool IsReservedName(ConstStringParam name) const; + + // Source Location --------------------------------------------------- + + // Updates |*out_location| to the source location of the complete + // extent of this enum declaration. Returns false and leaves + // |*out_location| unchanged iff location information was not available. + bool GetSourceLocation(SourceLocation *out_location) const; + +private: + friend class Symbol; + typedef EnumOptions OptionsType; + + // Allows access to GetLocationPath for annotations. + friend class io::Printer; + friend class compiler::cpp::Formatter; + + // Allow access to FindValueByNumberCreatingIfUnknown. + friend class descriptor_unittest::DescriptorTest; + + // Looks up a value by number. If the value does not exist, dynamically + // creates a new EnumValueDescriptor for that value, assuming that it was + // unknown. If a new descriptor is created, this is done in a thread-safe way, + // and future calls will return the same value descriptor pointer. + // + // This is private but is used by Reflection (which is friended below) to + // return a valid EnumValueDescriptor from GetEnum() when this feature is + // enabled. + const EnumValueDescriptor *FindValueByNumberCreatingIfUnknown(int number) const; + + // See Descriptor::DebugString(). + void DebugString(int depth, std::string *contents, const DebugStringOptions &options) const; + + // Walks up the descriptor tree to generate the source location path + // to this descriptor from the file root. + void GetLocationPath(std::vector *output) const; + + // True if this is a placeholder for an unknown type. + bool is_placeholder_ : 1; + // True if this is a placeholder and the type name wasn't fully-qualified. + bool is_unqualified_placeholder_ : 1; + + // This points to the last value _index_ that is part of the sequence starting + // with the first label, where + // `enum->value(i)->number() == enum->value(0)->number() + i` + // We measure relative to the first label to adapt to enum labels starting at + // 0 or 1. + // Uses 16-bit to avoid extra padding. Unlikely to have more than 2^15 + // sequentially numbered labels in an enum. + int16_t sequential_value_limit_; + + int value_count_; + + // all_names_ = [name, full_name] + const std::string *all_names_; + const FileDescriptor *file_; + const Descriptor *containing_type_; + const EnumOptions *options_; + EnumValueDescriptor *values_; + + int reserved_range_count_; + int reserved_name_count_; + EnumDescriptor::ReservedRange *reserved_ranges_; + const std::string **reserved_names_; + + // IMPORTANT: If you add a new field, make sure to search for all instances + // of Allocate() and AllocateArray() in + // descriptor.cc and update them to initialize the field. + + // Must be constructed using DescriptorPool. + EnumDescriptor() { + } + friend class DescriptorBuilder; + friend class Descriptor; + friend class FieldDescriptor; + friend class FileDescriptorTables; + friend class EnumValueDescriptor; + friend class FileDescriptor; + friend class DescriptorPool; + friend class Reflection; + GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(EnumDescriptor); }; // Describes an individual enum constant of a particular type. To get the @@ -1228,460 +1225,454 @@ class PROTOBUF_EXPORT EnumDescriptor : private internal::SymbolBase { // for its type, then use EnumDescriptor::FindValueByName() or // EnumDescriptor::FindValueByNumber(). Use DescriptorPool to construct // your own descriptors. -class PROTOBUF_EXPORT EnumValueDescriptor : private internal::SymbolBaseN<0>, - private internal::SymbolBaseN<1> { - public: - typedef EnumValueDescriptorProto Proto; - - const std::string& name() const; // Name of this enum constant. - int index() const; // Index within the enums's Descriptor. - int number() const; // Numeric value of this enum constant. - - // The full_name of an enum value is a sibling symbol of the enum type. - // e.g. the full name of FieldDescriptorProto::TYPE_INT32 is actually - // "google.protobuf.FieldDescriptorProto.TYPE_INT32", NOT - // "google.protobuf.FieldDescriptorProto.Type.TYPE_INT32". This is to conform - // with C++ scoping rules for enums. - const std::string& full_name() const; - - // The .proto file in which this value was defined. Never nullptr. - const FileDescriptor* file() const; - // The type of this value. Never nullptr. - const EnumDescriptor* type() const; - - // Get options for this enum value. These are specified in the .proto file by - // adding text like "[foo = 1234]" after an enum value definition. Allowed - // options are defined by EnumValueOptions in descriptor.proto, and any - // available extensions of that message. - const EnumValueOptions& options() const; - - // See Descriptor::CopyTo(). - void CopyTo(EnumValueDescriptorProto* proto) const; - - // See Descriptor::DebugString(). - std::string DebugString() const; - - // See Descriptor::DebugStringWithOptions(). - std::string DebugStringWithOptions(const DebugStringOptions& options) const; - - // Source Location --------------------------------------------------- - - // Updates |*out_location| to the source location of the complete - // extent of this enum value declaration. Returns false and leaves - // |*out_location| unchanged iff location information was not available. - bool GetSourceLocation(SourceLocation* out_location) const; - - private: - friend class Symbol; - typedef EnumValueOptions OptionsType; - - // Allows access to GetLocationPath for annotations. - friend class io::Printer; - friend class compiler::cpp::Formatter; - - // See Descriptor::DebugString(). - void DebugString(int depth, std::string* contents, - const DebugStringOptions& options) const; - - // Walks up the descriptor tree to generate the source location path - // to this descriptor from the file root. - void GetLocationPath(std::vector* output) const; - - int number_; - // all_names_ = [name, full_name] - const std::string* all_names_; - const EnumDescriptor* type_; - const EnumValueOptions* options_; - // IMPORTANT: If you add a new field, make sure to search for all instances - // of Allocate() and AllocateArray() - // in descriptor.cc and update them to initialize the field. - - // Must be constructed using DescriptorPool. - EnumValueDescriptor() {} - friend class DescriptorBuilder; - friend class EnumDescriptor; - friend class DescriptorPool; - friend class FileDescriptorTables; - friend class Reflection; - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(EnumValueDescriptor); +class PROTOBUF_EXPORT EnumValueDescriptor : private internal::SymbolBaseN<0>, private internal::SymbolBaseN<1> { +public: + typedef EnumValueDescriptorProto Proto; + + const std::string &name() const; // Name of this enum constant. + int index() const; // Index within the enums's Descriptor. + int number() const; // Numeric value of this enum constant. + + // The full_name of an enum value is a sibling symbol of the enum type. + // e.g. the full name of FieldDescriptorProto::TYPE_INT32 is actually + // "google.protobuf.FieldDescriptorProto.TYPE_INT32", NOT + // "google.protobuf.FieldDescriptorProto.Type.TYPE_INT32". This is to conform + // with C++ scoping rules for enums. + const std::string &full_name() const; + + // The .proto file in which this value was defined. Never nullptr. + const FileDescriptor *file() const; + // The type of this value. Never nullptr. + const EnumDescriptor *type() const; + + // Get options for this enum value. These are specified in the .proto file by + // adding text like "[foo = 1234]" after an enum value definition. Allowed + // options are defined by EnumValueOptions in descriptor.proto, and any + // available extensions of that message. + const EnumValueOptions &options() const; + + // See Descriptor::CopyTo(). + void CopyTo(EnumValueDescriptorProto *proto) const; + + // See Descriptor::DebugString(). + std::string DebugString() const; + + // See Descriptor::DebugStringWithOptions(). + std::string DebugStringWithOptions(const DebugStringOptions &options) const; + + // Source Location --------------------------------------------------- + + // Updates |*out_location| to the source location of the complete + // extent of this enum value declaration. Returns false and leaves + // |*out_location| unchanged iff location information was not available. + bool GetSourceLocation(SourceLocation *out_location) const; + +private: + friend class Symbol; + typedef EnumValueOptions OptionsType; + + // Allows access to GetLocationPath for annotations. + friend class io::Printer; + friend class compiler::cpp::Formatter; + + // See Descriptor::DebugString(). + void DebugString(int depth, std::string *contents, const DebugStringOptions &options) const; + + // Walks up the descriptor tree to generate the source location path + // to this descriptor from the file root. + void GetLocationPath(std::vector *output) const; + + int number_; + // all_names_ = [name, full_name] + const std::string *all_names_; + const EnumDescriptor *type_; + const EnumValueOptions *options_; + // IMPORTANT: If you add a new field, make sure to search for all instances + // of Allocate() and AllocateArray() + // in descriptor.cc and update them to initialize the field. + + // Must be constructed using DescriptorPool. + EnumValueDescriptor() { + } + friend class DescriptorBuilder; + friend class EnumDescriptor; + friend class DescriptorPool; + friend class FileDescriptorTables; + friend class Reflection; + GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(EnumValueDescriptor); }; // Describes an RPC service. Use DescriptorPool to construct your own // descriptors. class PROTOBUF_EXPORT ServiceDescriptor : private internal::SymbolBase { - public: - typedef ServiceDescriptorProto Proto; - - // The name of the service, not including its containing scope. - const std::string& name() const; - // The fully-qualified name of the service, scope delimited by periods. - const std::string& full_name() const; - // Index of this service within the file's services array. - int index() const; - - // The .proto file in which this service was defined. Never nullptr. - const FileDescriptor* file() const; - - // Get options for this service type. These are specified in the .proto file - // by placing lines like "option foo = 1234;" in the service definition. - // Allowed options are defined by ServiceOptions in descriptor.proto, and any - // available extensions of that message. - const ServiceOptions& options() const; - - // The number of methods this service defines. - int method_count() const; - // Gets a MethodDescriptor by index, where 0 <= index < method_count(). - // These are returned in the order they were defined in the .proto file. - const MethodDescriptor* method(int index) const; - - // Look up a MethodDescriptor by name. - const MethodDescriptor* FindMethodByName(ConstStringParam name) const; - // See Descriptor::CopyTo(). - void CopyTo(ServiceDescriptorProto* proto) const; - - // See Descriptor::DebugString(). - std::string DebugString() const; - - // See Descriptor::DebugStringWithOptions(). - std::string DebugStringWithOptions(const DebugStringOptions& options) const; - - // Source Location --------------------------------------------------- - - // Updates |*out_location| to the source location of the complete - // extent of this service declaration. Returns false and leaves - // |*out_location| unchanged iff location information was not available. - bool GetSourceLocation(SourceLocation* out_location) const; - - private: - friend class Symbol; - typedef ServiceOptions OptionsType; - - // Allows access to GetLocationPath for annotations. - friend class io::Printer; - friend class compiler::cpp::Formatter; - - // See Descriptor::DebugString(). - void DebugString(std::string* contents, - const DebugStringOptions& options) const; - - // Walks up the descriptor tree to generate the source location path - // to this descriptor from the file root. - void GetLocationPath(std::vector* output) const; - - // all_names_ = [name, full_name] - const std::string* all_names_; - const FileDescriptor* file_; - const ServiceOptions* options_; - MethodDescriptor* methods_; - int method_count_; - // IMPORTANT: If you add a new field, make sure to search for all instances - // of Allocate() and AllocateArray() in - // descriptor.cc and update them to initialize the field. - - // Must be constructed using DescriptorPool. - ServiceDescriptor() {} - friend class DescriptorBuilder; - friend class FileDescriptor; - friend class MethodDescriptor; - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ServiceDescriptor); +public: + typedef ServiceDescriptorProto Proto; + + // The name of the service, not including its containing scope. + const std::string &name() const; + // The fully-qualified name of the service, scope delimited by periods. + const std::string &full_name() const; + // Index of this service within the file's services array. + int index() const; + + // The .proto file in which this service was defined. Never nullptr. + const FileDescriptor *file() const; + + // Get options for this service type. These are specified in the .proto file + // by placing lines like "option foo = 1234;" in the service definition. + // Allowed options are defined by ServiceOptions in descriptor.proto, and any + // available extensions of that message. + const ServiceOptions &options() const; + + // The number of methods this service defines. + int method_count() const; + // Gets a MethodDescriptor by index, where 0 <= index < method_count(). + // These are returned in the order they were defined in the .proto file. + const MethodDescriptor *method(int index) const; + + // Look up a MethodDescriptor by name. + const MethodDescriptor *FindMethodByName(ConstStringParam name) const; + // See Descriptor::CopyTo(). + void CopyTo(ServiceDescriptorProto *proto) const; + + // See Descriptor::DebugString(). + std::string DebugString() const; + + // See Descriptor::DebugStringWithOptions(). + std::string DebugStringWithOptions(const DebugStringOptions &options) const; + + // Source Location --------------------------------------------------- + + // Updates |*out_location| to the source location of the complete + // extent of this service declaration. Returns false and leaves + // |*out_location| unchanged iff location information was not available. + bool GetSourceLocation(SourceLocation *out_location) const; + +private: + friend class Symbol; + typedef ServiceOptions OptionsType; + + // Allows access to GetLocationPath for annotations. + friend class io::Printer; + friend class compiler::cpp::Formatter; + + // See Descriptor::DebugString(). + void DebugString(std::string *contents, const DebugStringOptions &options) const; + + // Walks up the descriptor tree to generate the source location path + // to this descriptor from the file root. + void GetLocationPath(std::vector *output) const; + + // all_names_ = [name, full_name] + const std::string *all_names_; + const FileDescriptor *file_; + const ServiceOptions *options_; + MethodDescriptor *methods_; + int method_count_; + // IMPORTANT: If you add a new field, make sure to search for all instances + // of Allocate() and AllocateArray() in + // descriptor.cc and update them to initialize the field. + + // Must be constructed using DescriptorPool. + ServiceDescriptor() { + } + friend class DescriptorBuilder; + friend class FileDescriptor; + friend class MethodDescriptor; + GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ServiceDescriptor); }; - // Describes an individual service method. To obtain a MethodDescriptor given // a service, first get its ServiceDescriptor, then call // ServiceDescriptor::FindMethodByName(). Use DescriptorPool to construct your // own descriptors. class PROTOBUF_EXPORT MethodDescriptor : private internal::SymbolBase { - public: - typedef MethodDescriptorProto Proto; - - // Name of this method, not including containing scope. - const std::string& name() const; - // The fully-qualified name of the method, scope delimited by periods. - const std::string& full_name() const; - // Index within the service's Descriptor. - int index() const; - - // The .proto file in which this method was defined. Never nullptr. - const FileDescriptor* file() const; - // Gets the service to which this method belongs. Never nullptr. - const ServiceDescriptor* service() const; - - // Gets the type of protocol message which this method accepts as input. - const Descriptor* input_type() const; - // Gets the type of protocol message which this message produces as output. - const Descriptor* output_type() const; - - // Gets whether the client streams multiple requests. - bool client_streaming() const; - // Gets whether the server streams multiple responses. - bool server_streaming() const; - - // Get options for this method. These are specified in the .proto file by - // placing lines like "option foo = 1234;" in curly-braces after a method - // declaration. Allowed options are defined by MethodOptions in - // descriptor.proto, and any available extensions of that message. - const MethodOptions& options() const; - - // See Descriptor::CopyTo(). - void CopyTo(MethodDescriptorProto* proto) const; - - // See Descriptor::DebugString(). - std::string DebugString() const; - - // See Descriptor::DebugStringWithOptions(). - std::string DebugStringWithOptions(const DebugStringOptions& options) const; - - // Source Location --------------------------------------------------- - - // Updates |*out_location| to the source location of the complete - // extent of this method declaration. Returns false and leaves - // |*out_location| unchanged iff location information was not available. - bool GetSourceLocation(SourceLocation* out_location) const; - - private: - friend class Symbol; - typedef MethodOptions OptionsType; - - // Allows access to GetLocationPath for annotations. - friend class io::Printer; - friend class compiler::cpp::Formatter; - - // See Descriptor::DebugString(). - void DebugString(int depth, std::string* contents, - const DebugStringOptions& options) const; - - // Walks up the descriptor tree to generate the source location path - // to this descriptor from the file root. - void GetLocationPath(std::vector* output) const; - - bool client_streaming_; - bool server_streaming_; - // all_names_ = [name, full_name] - const std::string* all_names_; - const ServiceDescriptor* service_; - mutable internal::LazyDescriptor input_type_; - mutable internal::LazyDescriptor output_type_; - const MethodOptions* options_; - // IMPORTANT: If you add a new field, make sure to search for all instances - // of Allocate() and AllocateArray() in - // descriptor.cc and update them to initialize the field. - - // Must be constructed using DescriptorPool. - MethodDescriptor() {} - friend class DescriptorBuilder; - friend class ServiceDescriptor; - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MethodDescriptor); +public: + typedef MethodDescriptorProto Proto; + + // Name of this method, not including containing scope. + const std::string &name() const; + // The fully-qualified name of the method, scope delimited by periods. + const std::string &full_name() const; + // Index within the service's Descriptor. + int index() const; + + // The .proto file in which this method was defined. Never nullptr. + const FileDescriptor *file() const; + // Gets the service to which this method belongs. Never nullptr. + const ServiceDescriptor *service() const; + + // Gets the type of protocol message which this method accepts as input. + const Descriptor *input_type() const; + // Gets the type of protocol message which this message produces as output. + const Descriptor *output_type() const; + + // Gets whether the client streams multiple requests. + bool client_streaming() const; + // Gets whether the server streams multiple responses. + bool server_streaming() const; + + // Get options for this method. These are specified in the .proto file by + // placing lines like "option foo = 1234;" in curly-braces after a method + // declaration. Allowed options are defined by MethodOptions in + // descriptor.proto, and any available extensions of that message. + const MethodOptions &options() const; + + // See Descriptor::CopyTo(). + void CopyTo(MethodDescriptorProto *proto) const; + + // See Descriptor::DebugString(). + std::string DebugString() const; + + // See Descriptor::DebugStringWithOptions(). + std::string DebugStringWithOptions(const DebugStringOptions &options) const; + + // Source Location --------------------------------------------------- + + // Updates |*out_location| to the source location of the complete + // extent of this method declaration. Returns false and leaves + // |*out_location| unchanged iff location information was not available. + bool GetSourceLocation(SourceLocation *out_location) const; + +private: + friend class Symbol; + typedef MethodOptions OptionsType; + + // Allows access to GetLocationPath for annotations. + friend class io::Printer; + friend class compiler::cpp::Formatter; + + // See Descriptor::DebugString(). + void DebugString(int depth, std::string *contents, const DebugStringOptions &options) const; + + // Walks up the descriptor tree to generate the source location path + // to this descriptor from the file root. + void GetLocationPath(std::vector *output) const; + + bool client_streaming_; + bool server_streaming_; + // all_names_ = [name, full_name] + const std::string *all_names_; + const ServiceDescriptor *service_; + mutable internal::LazyDescriptor input_type_; + mutable internal::LazyDescriptor output_type_; + const MethodOptions *options_; + // IMPORTANT: If you add a new field, make sure to search for all instances + // of Allocate() and AllocateArray() in + // descriptor.cc and update them to initialize the field. + + // Must be constructed using DescriptorPool. + MethodDescriptor() { + } + friend class DescriptorBuilder; + friend class ServiceDescriptor; + GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MethodDescriptor); }; - // Describes a whole .proto file. To get the FileDescriptor for a compiled-in // file, get the descriptor for something defined in that file and call // descriptor->file(). Use DescriptorPool to construct your own descriptors. class PROTOBUF_EXPORT FileDescriptor { - public: - typedef FileDescriptorProto Proto; - - // The filename, relative to the source tree. - // e.g. "foo/bar/baz.proto" - const std::string& name() const; - - // The package, e.g. "google.protobuf.compiler". - const std::string& package() const; - - // The DescriptorPool in which this FileDescriptor and all its contents were - // allocated. Never nullptr. - const DescriptorPool* pool() const; - - // The number of files imported by this one. - int dependency_count() const; - // Gets an imported file by index, where 0 <= index < dependency_count(). - // These are returned in the order they were defined in the .proto file. - const FileDescriptor* dependency(int index) const; - - // The number of files public imported by this one. - // The public dependency list is a subset of the dependency list. - int public_dependency_count() const; - // Gets a public imported file by index, where 0 <= index < - // public_dependency_count(). - // These are returned in the order they were defined in the .proto file. - const FileDescriptor* public_dependency(int index) const; - - // The number of files that are imported for weak fields. - // The weak dependency list is a subset of the dependency list. - int weak_dependency_count() const; - // Gets a weak imported file by index, where 0 <= index < - // weak_dependency_count(). - // These are returned in the order they were defined in the .proto file. - const FileDescriptor* weak_dependency(int index) const; - - // Number of top-level message types defined in this file. (This does not - // include nested types.) - int message_type_count() const; - // Gets a top-level message type, where 0 <= index < message_type_count(). - // These are returned in the order they were defined in the .proto file. - const Descriptor* message_type(int index) const; - - // Number of top-level enum types defined in this file. (This does not - // include nested types.) - int enum_type_count() const; - // Gets a top-level enum type, where 0 <= index < enum_type_count(). - // These are returned in the order they were defined in the .proto file. - const EnumDescriptor* enum_type(int index) const; - - // Number of services defined in this file. - int service_count() const; - // Gets a service, where 0 <= index < service_count(). - // These are returned in the order they were defined in the .proto file. - const ServiceDescriptor* service(int index) const; - - // Number of extensions defined at file scope. (This does not include - // extensions nested within message types.) - int extension_count() const; - // Gets an extension's descriptor, where 0 <= index < extension_count(). - // These are returned in the order they were defined in the .proto file. - const FieldDescriptor* extension(int index) const; - - // Get options for this file. These are specified in the .proto file by - // placing lines like "option foo = 1234;" at the top level, outside of any - // other definitions. Allowed options are defined by FileOptions in - // descriptor.proto, and any available extensions of that message. - const FileOptions& options() const; - - // Syntax of this file. - enum Syntax { - SYNTAX_UNKNOWN = 0, - SYNTAX_PROTO2 = 2, - SYNTAX_PROTO3 = 3, - }; - Syntax syntax() const; - static const char* SyntaxName(Syntax syntax); - - // Find a top-level message type by name (not full_name). Returns nullptr if - // not found. - const Descriptor* FindMessageTypeByName(ConstStringParam name) const; - // Find a top-level enum type by name. Returns nullptr if not found. - const EnumDescriptor* FindEnumTypeByName(ConstStringParam name) const; - // Find an enum value defined in any top-level enum by name. Returns nullptr - // if not found. - const EnumValueDescriptor* FindEnumValueByName(ConstStringParam name) const; - // Find a service definition by name. Returns nullptr if not found. - const ServiceDescriptor* FindServiceByName(ConstStringParam name) const; - // Find a top-level extension definition by name. Returns nullptr if not - // found. - const FieldDescriptor* FindExtensionByName(ConstStringParam name) const; - // Similar to FindExtensionByName(), but searches by lowercased-name. See - // Descriptor::FindFieldByLowercaseName(). - const FieldDescriptor* FindExtensionByLowercaseName( - ConstStringParam name) const; - // Similar to FindExtensionByName(), but searches by camelcased-name. See - // Descriptor::FindFieldByCamelcaseName(). - const FieldDescriptor* FindExtensionByCamelcaseName( - ConstStringParam name) const; - - // See Descriptor::CopyTo(). - // Notes: - // - This method does NOT copy source code information since it is relatively - // large and rarely needed. See CopySourceCodeInfoTo() below. - void CopyTo(FileDescriptorProto* proto) const; - // Write the source code information of this FileDescriptor into the given - // FileDescriptorProto. See CopyTo() above. - void CopySourceCodeInfoTo(FileDescriptorProto* proto) const; - // Fill the json_name field of FieldDescriptorProto for all fields. Can only - // be called after CopyTo(). - void CopyJsonNameTo(FileDescriptorProto* proto) const; - - // See Descriptor::DebugString(). - std::string DebugString() const; - - // See Descriptor::DebugStringWithOptions(). - std::string DebugStringWithOptions(const DebugStringOptions& options) const; - - // Returns true if this is a placeholder for an unknown file. This will - // only be the case if this descriptor comes from a DescriptorPool - // with AllowUnknownDependencies() set. - bool is_placeholder() const; - - // Updates |*out_location| to the source location of the complete extent of - // this file declaration (namely, the empty path). - bool GetSourceLocation(SourceLocation* out_location) const; - - // Updates |*out_location| to the source location of the complete - // extent of the declaration or declaration-part denoted by |path|. - // Returns false and leaves |*out_location| unchanged iff location - // information was not available. (See SourceCodeInfo for - // description of path encoding.) - bool GetSourceLocation(const std::vector& path, - SourceLocation* out_location) const; - - private: - typedef FileOptions OptionsType; - - const std::string* name_; - const std::string* package_; - const DescriptorPool* pool_; - - // Data required to do lazy initialization. - struct PROTOBUF_EXPORT LazyInitData { +public: + typedef FileDescriptorProto Proto; + + // The filename, relative to the source tree. + // e.g. "foo/bar/baz.proto" + const std::string &name() const; + + // The package, e.g. "google.protobuf.compiler". + const std::string &package() const; + + // The DescriptorPool in which this FileDescriptor and all its contents were + // allocated. Never nullptr. + const DescriptorPool *pool() const; + + // The number of files imported by this one. + int dependency_count() const; + // Gets an imported file by index, where 0 <= index < dependency_count(). + // These are returned in the order they were defined in the .proto file. + const FileDescriptor *dependency(int index) const; + + // The number of files public imported by this one. + // The public dependency list is a subset of the dependency list. + int public_dependency_count() const; + // Gets a public imported file by index, where 0 <= index < + // public_dependency_count(). + // These are returned in the order they were defined in the .proto file. + const FileDescriptor *public_dependency(int index) const; + + // The number of files that are imported for weak fields. + // The weak dependency list is a subset of the dependency list. + int weak_dependency_count() const; + // Gets a weak imported file by index, where 0 <= index < + // weak_dependency_count(). + // These are returned in the order they were defined in the .proto file. + const FileDescriptor *weak_dependency(int index) const; + + // Number of top-level message types defined in this file. (This does not + // include nested types.) + int message_type_count() const; + // Gets a top-level message type, where 0 <= index < message_type_count(). + // These are returned in the order they were defined in the .proto file. + const Descriptor *message_type(int index) const; + + // Number of top-level enum types defined in this file. (This does not + // include nested types.) + int enum_type_count() const; + // Gets a top-level enum type, where 0 <= index < enum_type_count(). + // These are returned in the order they were defined in the .proto file. + const EnumDescriptor *enum_type(int index) const; + + // Number of services defined in this file. + int service_count() const; + // Gets a service, where 0 <= index < service_count(). + // These are returned in the order they were defined in the .proto file. + const ServiceDescriptor *service(int index) const; + + // Number of extensions defined at file scope. (This does not include + // extensions nested within message types.) + int extension_count() const; + // Gets an extension's descriptor, where 0 <= index < extension_count(). + // These are returned in the order they were defined in the .proto file. + const FieldDescriptor *extension(int index) const; + + // Get options for this file. These are specified in the .proto file by + // placing lines like "option foo = 1234;" at the top level, outside of any + // other definitions. Allowed options are defined by FileOptions in + // descriptor.proto, and any available extensions of that message. + const FileOptions &options() const; + + // Syntax of this file. + enum Syntax { + SYNTAX_UNKNOWN = 0, + SYNTAX_PROTO2 = 2, + SYNTAX_PROTO3 = 3, + }; + Syntax syntax() const; + static const char *SyntaxName(Syntax syntax); + + // Find a top-level message type by name (not full_name). Returns nullptr if + // not found. + const Descriptor *FindMessageTypeByName(ConstStringParam name) const; + // Find a top-level enum type by name. Returns nullptr if not found. + const EnumDescriptor *FindEnumTypeByName(ConstStringParam name) const; + // Find an enum value defined in any top-level enum by name. Returns nullptr + // if not found. + const EnumValueDescriptor *FindEnumValueByName(ConstStringParam name) const; + // Find a service definition by name. Returns nullptr if not found. + const ServiceDescriptor *FindServiceByName(ConstStringParam name) const; + // Find a top-level extension definition by name. Returns nullptr if not + // found. + const FieldDescriptor *FindExtensionByName(ConstStringParam name) const; + // Similar to FindExtensionByName(), but searches by lowercased-name. See + // Descriptor::FindFieldByLowercaseName(). + const FieldDescriptor *FindExtensionByLowercaseName(ConstStringParam name) const; + // Similar to FindExtensionByName(), but searches by camelcased-name. See + // Descriptor::FindFieldByCamelcaseName(). + const FieldDescriptor *FindExtensionByCamelcaseName(ConstStringParam name) const; + + // See Descriptor::CopyTo(). + // Notes: + // - This method does NOT copy source code information since it is relatively + // large and rarely needed. See CopySourceCodeInfoTo() below. + void CopyTo(FileDescriptorProto *proto) const; + // Write the source code information of this FileDescriptor into the given + // FileDescriptorProto. See CopyTo() above. + void CopySourceCodeInfoTo(FileDescriptorProto *proto) const; + // Fill the json_name field of FieldDescriptorProto for all fields. Can only + // be called after CopyTo(). + void CopyJsonNameTo(FileDescriptorProto *proto) const; + + // See Descriptor::DebugString(). + std::string DebugString() const; + + // See Descriptor::DebugStringWithOptions(). + std::string DebugStringWithOptions(const DebugStringOptions &options) const; + + // Returns true if this is a placeholder for an unknown file. This will + // only be the case if this descriptor comes from a DescriptorPool + // with AllowUnknownDependencies() set. + bool is_placeholder() const; + + // Updates |*out_location| to the source location of the complete extent of + // this file declaration (namely, the empty path). + bool GetSourceLocation(SourceLocation *out_location) const; + + // Updates |*out_location| to the source location of the complete + // extent of the declaration or declaration-part denoted by |path|. + // Returns false and leaves |*out_location| unchanged iff location + // information was not available. (See SourceCodeInfo for + // description of path encoding.) + bool GetSourceLocation(const std::vector &path, SourceLocation *out_location) const; + +private: + typedef FileOptions OptionsType; + + const std::string *name_; + const std::string *package_; + const DescriptorPool *pool_; + + // Data required to do lazy initialization. + struct PROTOBUF_EXPORT LazyInitData { #ifndef SWIG - internal::once_flag once; + internal::once_flag once; #endif - const char** dependencies_names; - }; - - LazyInitData* dependencies_once_; - static void DependenciesOnceInit(const FileDescriptor* to_init); - void InternalDependenciesOnceInit() const; - - // These are arranged to minimize padding on 64-bit. - int dependency_count_; - int public_dependency_count_; - int weak_dependency_count_; - int message_type_count_; - int enum_type_count_; - int service_count_; - - bool is_placeholder_; - // Indicates the FileDescriptor is completed building. Used to verify - // that type accessor functions that can possibly build a dependent file - // aren't called during the process of building the file. - bool finished_building_; - // Actually a `Syntax` but stored as uint8_t to save space. - uint8_t syntax_; - // This one is here to fill the padding. - int extension_count_; - - mutable const FileDescriptor** dependencies_; - int* public_dependencies_; - int* weak_dependencies_; - Descriptor* message_types_; - EnumDescriptor* enum_types_; - ServiceDescriptor* services_; - FieldDescriptor* extensions_; - const FileOptions* options_; - - const FileDescriptorTables* tables_; - const SourceCodeInfo* source_code_info_; - - // IMPORTANT: If you add a new field, make sure to search for all instances - // of Allocate() and AllocateArray() in - // descriptor.cc and update them to initialize the field. - - FileDescriptor() {} - friend class DescriptorBuilder; - friend class DescriptorPool; - friend class Descriptor; - friend class FieldDescriptor; - friend class internal::LazyDescriptor; - friend class OneofDescriptor; - friend class EnumDescriptor; - friend class EnumValueDescriptor; - friend class MethodDescriptor; - friend class ServiceDescriptor; - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FileDescriptor); + const char **dependencies_names; + }; + + LazyInitData *dependencies_once_; + static void DependenciesOnceInit(const FileDescriptor *to_init); + void InternalDependenciesOnceInit() const; + + // These are arranged to minimize padding on 64-bit. + int dependency_count_; + int public_dependency_count_; + int weak_dependency_count_; + int message_type_count_; + int enum_type_count_; + int service_count_; + + bool is_placeholder_; + // Indicates the FileDescriptor is completed building. Used to verify + // that type accessor functions that can possibly build a dependent file + // aren't called during the process of building the file. + bool finished_building_; + // Actually a `Syntax` but stored as uint8_t to save space. + uint8_t syntax_; + // This one is here to fill the padding. + int extension_count_; + + mutable const FileDescriptor **dependencies_; + int *public_dependencies_; + int *weak_dependencies_; + Descriptor *message_types_; + EnumDescriptor *enum_types_; + ServiceDescriptor *services_; + FieldDescriptor *extensions_; + const FileOptions *options_; + + const FileDescriptorTables *tables_; + const SourceCodeInfo *source_code_info_; + + // IMPORTANT: If you add a new field, make sure to search for all instances + // of Allocate() and AllocateArray() in + // descriptor.cc and update them to initialize the field. + + FileDescriptor() { + } + friend class DescriptorBuilder; + friend class DescriptorPool; + friend class Descriptor; + friend class FieldDescriptor; + friend class internal::LazyDescriptor; + friend class OneofDescriptor; + friend class EnumDescriptor; + friend class EnumValueDescriptor; + friend class MethodDescriptor; + friend class ServiceDescriptor; + GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FileDescriptor); }; - // =================================================================== // Used to construct descriptors. @@ -1709,351 +1700,337 @@ class PROTOBUF_EXPORT FileDescriptor { // You can also search for descriptors within a DescriptorPool by name, and // extensions by number. class PROTOBUF_EXPORT DescriptorPool { - public: - // Create a normal, empty DescriptorPool. - DescriptorPool(); - - // Constructs a DescriptorPool that, when it can't find something among the - // descriptors already in the pool, looks for it in the given - // DescriptorDatabase. - // Notes: - // - If a DescriptorPool is constructed this way, its BuildFile*() methods - // must not be called (they will assert-fail). The only way to populate - // the pool with descriptors is to call the Find*By*() methods. - // - The Find*By*() methods may block the calling thread if the - // DescriptorDatabase blocks. This in turn means that parsing messages - // may block if they need to look up extensions. - // - The Find*By*() methods will use mutexes for thread-safety, thus making - // them slower even when they don't have to fall back to the database. - // In fact, even the Find*By*() methods of descriptor objects owned by - // this pool will be slower, since they will have to obtain locks too. - // - An ErrorCollector may optionally be given to collect validation errors - // in files loaded from the database. If not given, errors will be printed - // to GOOGLE_LOG(ERROR). Remember that files are built on-demand, so this - // ErrorCollector may be called from any thread that calls one of the - // Find*By*() methods. - // - The DescriptorDatabase must not be mutated during the lifetime of - // the DescriptorPool. Even if the client takes care to avoid data races, - // changes to the content of the DescriptorDatabase may not be reflected - // in subsequent lookups in the DescriptorPool. - class ErrorCollector; - explicit DescriptorPool(DescriptorDatabase* fallback_database, - ErrorCollector* error_collector = nullptr); - - ~DescriptorPool(); - - // Get a pointer to the generated pool. Generated protocol message classes - // which are compiled into the binary will allocate their descriptors in - // this pool. Do not add your own descriptors to this pool. - static const DescriptorPool* generated_pool(); - - - // Find a FileDescriptor in the pool by file name. Returns nullptr if not - // found. - const FileDescriptor* FindFileByName(ConstStringParam name) const; - - // Find the FileDescriptor in the pool which defines the given symbol. - // If any of the Find*ByName() methods below would succeed, then this is - // equivalent to calling that method and calling the result's file() method. - // Otherwise this returns nullptr. - const FileDescriptor* FindFileContainingSymbol( - ConstStringParam symbol_name) const; - - // Looking up descriptors ------------------------------------------ - // These find descriptors by fully-qualified name. These will find both - // top-level descriptors and nested descriptors. They return nullptr if not - // found. - - const Descriptor* FindMessageTypeByName(ConstStringParam name) const; - const FieldDescriptor* FindFieldByName(ConstStringParam name) const; - const FieldDescriptor* FindExtensionByName(ConstStringParam name) const; - const OneofDescriptor* FindOneofByName(ConstStringParam name) const; - const EnumDescriptor* FindEnumTypeByName(ConstStringParam name) const; - const EnumValueDescriptor* FindEnumValueByName(ConstStringParam name) const; - const ServiceDescriptor* FindServiceByName(ConstStringParam name) const; - const MethodDescriptor* FindMethodByName(ConstStringParam name) const; - - // Finds an extension of the given type by number. The extendee must be - // a member of this DescriptorPool or one of its underlays. - const FieldDescriptor* FindExtensionByNumber(const Descriptor* extendee, - int number) const; - - // Finds an extension of the given type by its printable name. - // See comments above PrintableNameForExtension() for the definition of - // "printable name". The extendee must be a member of this DescriptorPool - // or one of its underlays. Returns nullptr if there is no known message - // extension with the given printable name. - const FieldDescriptor* FindExtensionByPrintableName( - const Descriptor* extendee, ConstStringParam printable_name) const; - - // Finds extensions of extendee. The extensions will be appended to - // out in an undefined order. Only extensions defined directly in - // this DescriptorPool or one of its underlays are guaranteed to be - // found: extensions defined in the fallback database might not be found - // depending on the database implementation. - void FindAllExtensions(const Descriptor* extendee, - std::vector* out) const; - - // Building descriptors -------------------------------------------- - - // When converting a FileDescriptorProto to a FileDescriptor, various - // errors might be detected in the input. The caller may handle these - // programmatically by implementing an ErrorCollector. - class PROTOBUF_EXPORT ErrorCollector { - public: - inline ErrorCollector() {} - virtual ~ErrorCollector(); - - // These constants specify what exact part of the construct is broken. - // This is useful e.g. for mapping the error back to an exact location - // in a .proto file. - enum ErrorLocation { - NAME, // the symbol name, or the package name for files - NUMBER, // field or extension range number - TYPE, // field type - EXTENDEE, // field extendee - DEFAULT_VALUE, // field default value - INPUT_TYPE, // method input type - OUTPUT_TYPE, // method output type - OPTION_NAME, // name in assignment - OPTION_VALUE, // value in option assignment - IMPORT, // import error - OTHER // some other problem - }; - - // Reports an error in the FileDescriptorProto. Use this function if the - // problem occurred should interrupt building the FileDescriptorProto. - virtual void AddError( - const std::string& filename, // File name in which the error occurred. - const std::string& element_name, // Full name of the erroneous element. - const Message* descriptor, // Descriptor of the erroneous element. - ErrorLocation location, // One of the location constants, above. - const std::string& message // Human-readable error message. - ) = 0; - - // Reports a warning in the FileDescriptorProto. Use this function if the - // problem occurred should NOT interrupt building the FileDescriptorProto. - virtual void AddWarning( - const std::string& /*filename*/, // File name in which the error - // occurred. - const std::string& /*element_name*/, // Full name of the erroneous - // element. - const Message* /*descriptor*/, // Descriptor of the erroneous element. - ErrorLocation /*location*/, // One of the location constants, above. - const std::string& /*message*/ // Human-readable error message. - ) {} - - private: - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ErrorCollector); - }; - - // Convert the FileDescriptorProto to real descriptors and place them in - // this DescriptorPool. All dependencies of the file must already be in - // the pool. Returns the resulting FileDescriptor, or nullptr if there were - // problems with the input (e.g. the message was invalid, or dependencies - // were missing). Details about the errors are written to GOOGLE_LOG(ERROR). - const FileDescriptor* BuildFile(const FileDescriptorProto& proto); - - // Same as BuildFile() except errors are sent to the given ErrorCollector. - const FileDescriptor* BuildFileCollectingErrors( - const FileDescriptorProto& proto, ErrorCollector* error_collector); - - // By default, it is an error if a FileDescriptorProto contains references - // to types or other files that are not found in the DescriptorPool (or its - // backing DescriptorDatabase, if any). If you call - // AllowUnknownDependencies(), however, then unknown types and files - // will be replaced by placeholder descriptors (which can be identified by - // the is_placeholder() method). This can allow you to - // perform some useful operations with a .proto file even if you do not - // have access to other .proto files on which it depends. However, some - // heuristics must be used to fill in the gaps in information, and these - // can lead to descriptors which are inaccurate. For example, the - // DescriptorPool may be forced to guess whether an unknown type is a message - // or an enum, as well as what package it resides in. Furthermore, - // placeholder types will not be discoverable via FindMessageTypeByName() - // and similar methods, which could confuse some descriptor-based algorithms. - // Generally, the results of this option should be handled with extreme care. - void AllowUnknownDependencies() { allow_unknown_ = true; } - - // By default, weak imports are allowed to be missing, in which case we will - // use a placeholder for the dependency and convert the field to be an Empty - // message field. If you call EnforceWeakDependencies(true), however, the - // DescriptorPool will report a import not found error. - void EnforceWeakDependencies(bool enforce) { enforce_weak_ = enforce; } - - // Internal stuff -------------------------------------------------- - // These methods MUST NOT be called from outside the proto2 library. - // These methods may contain hidden pitfalls and may be removed in a - // future library version. - - // Create a DescriptorPool which is overlaid on top of some other pool. - // If you search for a descriptor in the overlay and it is not found, the - // underlay will be searched as a backup. If the underlay has its own - // underlay, that will be searched next, and so on. This also means that - // files built in the overlay will be cross-linked with the underlay's - // descriptors if necessary. The underlay remains property of the caller; - // it must remain valid for the lifetime of the newly-constructed pool. - // - // Example: Say you want to parse a .proto file at runtime in order to use - // its type with a DynamicMessage. Say this .proto file has dependencies, - // but you know that all the dependencies will be things that are already - // compiled into the binary. For ease of use, you'd like to load the types - // right out of generated_pool() rather than have to parse redundant copies - // of all these .protos and runtime. But, you don't want to add the parsed - // types directly into generated_pool(): this is not allowed, and would be - // bad design anyway. So, instead, you could use generated_pool() as an - // underlay for a new DescriptorPool in which you add only the new file. - // - // WARNING: Use of underlays can lead to many subtle gotchas. Instead, - // try to formulate what you want to do in terms of DescriptorDatabases. - explicit DescriptorPool(const DescriptorPool* underlay); - - // Called by generated classes at init time to add their descriptors to - // generated_pool. Do NOT call this in your own code! filename must be a - // permanent string (e.g. a string literal). - static void InternalAddGeneratedFile(const void* encoded_file_descriptor, - int size); - - // Disallow [enforce_utf8 = false] in .proto files. - void DisallowEnforceUtf8() { disallow_enforce_utf8_ = true; } - - - // For internal use only: Gets a non-const pointer to the generated pool. - // This is called at static-initialization time only, so thread-safety is - // not a concern. If both an underlay and a fallback database are present, - // the underlay takes precedence. - static DescriptorPool* internal_generated_pool(); - - // For internal use only: Gets a non-const pointer to the generated - // descriptor database. - // Only used for testing. - static DescriptorDatabase* internal_generated_database(); - - // For internal use only: Changes the behavior of BuildFile() such that it - // allows the file to make reference to message types declared in other files - // which it did not officially declare as dependencies. - void InternalDontEnforceDependencies(); - - // For internal use only: Enables lazy building of dependencies of a file. - // Delay the building of dependencies of a file descriptor until absolutely - // necessary, like when message_type() is called on a field that is defined - // in that dependency's file. This will cause functional issues if a proto - // or one of its dependencies has errors. Should only be enabled for the - // generated_pool_ (because no descriptor build errors are guaranteed by - // the compilation generation process), testing, or if a lack of descriptor - // build errors can be guaranteed for a pool. - void InternalSetLazilyBuildDependencies() { - lazily_build_dependencies_ = true; - // This needs to be set when lazily building dependencies, as it breaks - // dependency checking. - InternalDontEnforceDependencies(); - } - - // For internal use only. - void internal_set_underlay(const DescriptorPool* underlay) { - underlay_ = underlay; - } - - // For internal (unit test) use only: Returns true if a FileDescriptor has - // been constructed for the given file, false otherwise. Useful for testing - // lazy descriptor initialization behavior. - bool InternalIsFileLoaded(ConstStringParam filename) const; - - // Add a file to unused_import_track_files_. DescriptorBuilder will log - // warnings or errors for those files if there is any unused import. - void AddUnusedImportTrackFile(ConstStringParam file_name, - bool is_error = false); - void ClearUnusedImportTrackFiles(); - - private: - friend class Descriptor; - friend class internal::LazyDescriptor; - friend class FieldDescriptor; - friend class EnumDescriptor; - friend class ServiceDescriptor; - friend class MethodDescriptor; - friend class FileDescriptor; - friend class StreamDescriptor; - friend class DescriptorBuilder; - friend class FileDescriptorTables; - - // Return true if the given name is a sub-symbol of any non-package - // descriptor that already exists in the descriptor pool. (The full - // definition of such types is already known.) - bool IsSubSymbolOfBuiltType(StringPiece name) const; - - // Tries to find something in the fallback database and link in the - // corresponding proto file. Returns true if successful, in which case - // the caller should search for the thing again. These are declared - // const because they are called by (semantically) const methods. - bool TryFindFileInFallbackDatabase(StringPiece name) const; - bool TryFindSymbolInFallbackDatabase(StringPiece name) const; - bool TryFindExtensionInFallbackDatabase(const Descriptor* containing_type, - int field_number) const; - - // This internal find extension method only check with its table and underlay - // descriptor_pool's table. It does not check with fallback DB and no - // additional proto file will be build in this method. - const FieldDescriptor* InternalFindExtensionByNumberNoLock( - const Descriptor* extendee, int number) const; - - // Like BuildFile() but called internally when the file has been loaded from - // fallback_database_. Declared const because it is called by (semantically) - // const methods. - const FileDescriptor* BuildFileFromDatabase( - const FileDescriptorProto& proto) const; - - // Helper for when lazily_build_dependencies_ is set, can look up a symbol - // after the file's descriptor is built, and can build the file where that - // symbol is defined if necessary. Will create a placeholder if the type - // doesn't exist in the fallback database, or the file doesn't build - // successfully. - Symbol CrossLinkOnDemandHelper(StringPiece name, - bool expecting_enum) const; - - // Create a placeholder FileDescriptor of the specified name - FileDescriptor* NewPlaceholderFile(StringPiece name) const; - FileDescriptor* NewPlaceholderFileWithMutexHeld(StringPiece name) const; - - enum PlaceholderType { - PLACEHOLDER_MESSAGE, - PLACEHOLDER_ENUM, - PLACEHOLDER_EXTENDABLE_MESSAGE - }; - // Create a placeholder Descriptor of the specified name - Symbol NewPlaceholder(StringPiece name, - PlaceholderType placeholder_type) const; - Symbol NewPlaceholderWithMutexHeld(StringPiece name, - PlaceholderType placeholder_type) const; - - // If fallback_database_ is nullptr, this is nullptr. Otherwise, this is a - // mutex which must be locked while accessing tables_. - internal::WrappedMutex* mutex_; - - // See constructor. - DescriptorDatabase* fallback_database_; - ErrorCollector* default_error_collector_; - const DescriptorPool* underlay_; - - // This class contains a lot of hash maps with complicated types that - // we'd like to keep out of the header. - class Tables; - std::unique_ptr tables_; - - bool enforce_dependencies_; - bool lazily_build_dependencies_; - bool allow_unknown_; - bool enforce_weak_; - bool disallow_enforce_utf8_; - - // Set of files to track for unused imports. The bool value when true means - // unused imports are treated as errors (and as warnings when false). - std::map unused_import_track_files_; - - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(DescriptorPool); +public: + // Create a normal, empty DescriptorPool. + DescriptorPool(); + + // Constructs a DescriptorPool that, when it can't find something among the + // descriptors already in the pool, looks for it in the given + // DescriptorDatabase. + // Notes: + // - If a DescriptorPool is constructed this way, its BuildFile*() methods + // must not be called (they will assert-fail). The only way to populate + // the pool with descriptors is to call the Find*By*() methods. + // - The Find*By*() methods may block the calling thread if the + // DescriptorDatabase blocks. This in turn means that parsing messages + // may block if they need to look up extensions. + // - The Find*By*() methods will use mutexes for thread-safety, thus making + // them slower even when they don't have to fall back to the database. + // In fact, even the Find*By*() methods of descriptor objects owned by + // this pool will be slower, since they will have to obtain locks too. + // - An ErrorCollector may optionally be given to collect validation errors + // in files loaded from the database. If not given, errors will be printed + // to GOOGLE_LOG(ERROR). Remember that files are built on-demand, so this + // ErrorCollector may be called from any thread that calls one of the + // Find*By*() methods. + // - The DescriptorDatabase must not be mutated during the lifetime of + // the DescriptorPool. Even if the client takes care to avoid data races, + // changes to the content of the DescriptorDatabase may not be reflected + // in subsequent lookups in the DescriptorPool. + class ErrorCollector; + explicit DescriptorPool(DescriptorDatabase *fallback_database, ErrorCollector *error_collector = nullptr); + + ~DescriptorPool(); + + // Get a pointer to the generated pool. Generated protocol message classes + // which are compiled into the binary will allocate their descriptors in + // this pool. Do not add your own descriptors to this pool. + static const DescriptorPool *generated_pool(); + + // Find a FileDescriptor in the pool by file name. Returns nullptr if not + // found. + const FileDescriptor *FindFileByName(ConstStringParam name) const; + + // Find the FileDescriptor in the pool which defines the given symbol. + // If any of the Find*ByName() methods below would succeed, then this is + // equivalent to calling that method and calling the result's file() method. + // Otherwise this returns nullptr. + const FileDescriptor *FindFileContainingSymbol(ConstStringParam symbol_name) const; + + // Looking up descriptors ------------------------------------------ + // These find descriptors by fully-qualified name. These will find both + // top-level descriptors and nested descriptors. They return nullptr if not + // found. + + const Descriptor *FindMessageTypeByName(ConstStringParam name) const; + const FieldDescriptor *FindFieldByName(ConstStringParam name) const; + const FieldDescriptor *FindExtensionByName(ConstStringParam name) const; + const OneofDescriptor *FindOneofByName(ConstStringParam name) const; + const EnumDescriptor *FindEnumTypeByName(ConstStringParam name) const; + const EnumValueDescriptor *FindEnumValueByName(ConstStringParam name) const; + const ServiceDescriptor *FindServiceByName(ConstStringParam name) const; + const MethodDescriptor *FindMethodByName(ConstStringParam name) const; + + // Finds an extension of the given type by number. The extendee must be + // a member of this DescriptorPool or one of its underlays. + const FieldDescriptor *FindExtensionByNumber(const Descriptor *extendee, int number) const; + + // Finds an extension of the given type by its printable name. + // See comments above PrintableNameForExtension() for the definition of + // "printable name". The extendee must be a member of this DescriptorPool + // or one of its underlays. Returns nullptr if there is no known message + // extension with the given printable name. + const FieldDescriptor *FindExtensionByPrintableName(const Descriptor *extendee, + ConstStringParam printable_name) const; + + // Finds extensions of extendee. The extensions will be appended to + // out in an undefined order. Only extensions defined directly in + // this DescriptorPool or one of its underlays are guaranteed to be + // found: extensions defined in the fallback database might not be found + // depending on the database implementation. + void FindAllExtensions(const Descriptor *extendee, std::vector *out) const; + + // Building descriptors -------------------------------------------- + + // When converting a FileDescriptorProto to a FileDescriptor, various + // errors might be detected in the input. The caller may handle these + // programmatically by implementing an ErrorCollector. + class PROTOBUF_EXPORT ErrorCollector { + public: + inline ErrorCollector() { + } + virtual ~ErrorCollector(); + + // These constants specify what exact part of the construct is broken. + // This is useful e.g. for mapping the error back to an exact location + // in a .proto file. + enum ErrorLocation { + NAME, // the symbol name, or the package name for files + NUMBER, // field or extension range number + TYPE, // field type + EXTENDEE, // field extendee + DEFAULT_VALUE, // field default value + INPUT_TYPE, // method input type + OUTPUT_TYPE, // method output type + OPTION_NAME, // name in assignment + OPTION_VALUE, // value in option assignment + IMPORT, // import error + OTHER // some other problem + }; + + // Reports an error in the FileDescriptorProto. Use this function if the + // problem occurred should interrupt building the FileDescriptorProto. + virtual void AddError(const std::string &filename, // File name in which the error occurred. + const std::string &element_name, // Full name of the erroneous element. + const Message *descriptor, // Descriptor of the erroneous element. + ErrorLocation location, // One of the location constants, above. + const std::string &message // Human-readable error message. + ) = 0; + + // Reports a warning in the FileDescriptorProto. Use this function if the + // problem occurred should NOT interrupt building the FileDescriptorProto. + virtual void AddWarning(const std::string & /*filename*/, // File name in which the error + // occurred. + const std::string & /*element_name*/, // Full name of the erroneous + // element. + const Message * /*descriptor*/, // Descriptor of the erroneous element. + ErrorLocation /*location*/, // One of the location constants, above. + const std::string & /*message*/ // Human-readable error message. + ) { + } + + private: + GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ErrorCollector); + }; + + // Convert the FileDescriptorProto to real descriptors and place them in + // this DescriptorPool. All dependencies of the file must already be in + // the pool. Returns the resulting FileDescriptor, or nullptr if there were + // problems with the input (e.g. the message was invalid, or dependencies + // were missing). Details about the errors are written to GOOGLE_LOG(ERROR). + const FileDescriptor *BuildFile(const FileDescriptorProto &proto); + + // Same as BuildFile() except errors are sent to the given ErrorCollector. + const FileDescriptor *BuildFileCollectingErrors(const FileDescriptorProto &proto, ErrorCollector *error_collector); + + // By default, it is an error if a FileDescriptorProto contains references + // to types or other files that are not found in the DescriptorPool (or its + // backing DescriptorDatabase, if any). If you call + // AllowUnknownDependencies(), however, then unknown types and files + // will be replaced by placeholder descriptors (which can be identified by + // the is_placeholder() method). This can allow you to + // perform some useful operations with a .proto file even if you do not + // have access to other .proto files on which it depends. However, some + // heuristics must be used to fill in the gaps in information, and these + // can lead to descriptors which are inaccurate. For example, the + // DescriptorPool may be forced to guess whether an unknown type is a message + // or an enum, as well as what package it resides in. Furthermore, + // placeholder types will not be discoverable via FindMessageTypeByName() + // and similar methods, which could confuse some descriptor-based algorithms. + // Generally, the results of this option should be handled with extreme care. + void AllowUnknownDependencies() { + allow_unknown_ = true; + } + + // By default, weak imports are allowed to be missing, in which case we will + // use a placeholder for the dependency and convert the field to be an Empty + // message field. If you call EnforceWeakDependencies(true), however, the + // DescriptorPool will report a import not found error. + void EnforceWeakDependencies(bool enforce) { + enforce_weak_ = enforce; + } + + // Internal stuff -------------------------------------------------- + // These methods MUST NOT be called from outside the proto2 library. + // These methods may contain hidden pitfalls and may be removed in a + // future library version. + + // Create a DescriptorPool which is overlaid on top of some other pool. + // If you search for a descriptor in the overlay and it is not found, the + // underlay will be searched as a backup. If the underlay has its own + // underlay, that will be searched next, and so on. This also means that + // files built in the overlay will be cross-linked with the underlay's + // descriptors if necessary. The underlay remains property of the caller; + // it must remain valid for the lifetime of the newly-constructed pool. + // + // Example: Say you want to parse a .proto file at runtime in order to use + // its type with a DynamicMessage. Say this .proto file has dependencies, + // but you know that all the dependencies will be things that are already + // compiled into the binary. For ease of use, you'd like to load the types + // right out of generated_pool() rather than have to parse redundant copies + // of all these .protos and runtime. But, you don't want to add the parsed + // types directly into generated_pool(): this is not allowed, and would be + // bad design anyway. So, instead, you could use generated_pool() as an + // underlay for a new DescriptorPool in which you add only the new file. + // + // WARNING: Use of underlays can lead to many subtle gotchas. Instead, + // try to formulate what you want to do in terms of DescriptorDatabases. + explicit DescriptorPool(const DescriptorPool *underlay); + + // Called by generated classes at init time to add their descriptors to + // generated_pool. Do NOT call this in your own code! filename must be a + // permanent string (e.g. a string literal). + static void InternalAddGeneratedFile(const void *encoded_file_descriptor, int size); + + // Disallow [enforce_utf8 = false] in .proto files. + void DisallowEnforceUtf8() { + disallow_enforce_utf8_ = true; + } + + // For internal use only: Gets a non-const pointer to the generated pool. + // This is called at static-initialization time only, so thread-safety is + // not a concern. If both an underlay and a fallback database are present, + // the underlay takes precedence. + static DescriptorPool *internal_generated_pool(); + + // For internal use only: Gets a non-const pointer to the generated + // descriptor database. + // Only used for testing. + static DescriptorDatabase *internal_generated_database(); + + // For internal use only: Changes the behavior of BuildFile() such that it + // allows the file to make reference to message types declared in other files + // which it did not officially declare as dependencies. + void InternalDontEnforceDependencies(); + + // For internal use only: Enables lazy building of dependencies of a file. + // Delay the building of dependencies of a file descriptor until absolutely + // necessary, like when message_type() is called on a field that is defined + // in that dependency's file. This will cause functional issues if a proto + // or one of its dependencies has errors. Should only be enabled for the + // generated_pool_ (because no descriptor build errors are guaranteed by + // the compilation generation process), testing, or if a lack of descriptor + // build errors can be guaranteed for a pool. + void InternalSetLazilyBuildDependencies() { + lazily_build_dependencies_ = true; + // This needs to be set when lazily building dependencies, as it breaks + // dependency checking. + InternalDontEnforceDependencies(); + } + + // For internal use only. + void internal_set_underlay(const DescriptorPool *underlay) { + underlay_ = underlay; + } + + // For internal (unit test) use only: Returns true if a FileDescriptor has + // been constructed for the given file, false otherwise. Useful for testing + // lazy descriptor initialization behavior. + bool InternalIsFileLoaded(ConstStringParam filename) const; + + // Add a file to unused_import_track_files_. DescriptorBuilder will log + // warnings or errors for those files if there is any unused import. + void AddUnusedImportTrackFile(ConstStringParam file_name, bool is_error = false); + void ClearUnusedImportTrackFiles(); + +private: + friend class Descriptor; + friend class internal::LazyDescriptor; + friend class FieldDescriptor; + friend class EnumDescriptor; + friend class ServiceDescriptor; + friend class MethodDescriptor; + friend class FileDescriptor; + friend class StreamDescriptor; + friend class DescriptorBuilder; + friend class FileDescriptorTables; + + // Return true if the given name is a sub-symbol of any non-package + // descriptor that already exists in the descriptor pool. (The full + // definition of such types is already known.) + bool IsSubSymbolOfBuiltType(StringPiece name) const; + + // Tries to find something in the fallback database and link in the + // corresponding proto file. Returns true if successful, in which case + // the caller should search for the thing again. These are declared + // const because they are called by (semantically) const methods. + bool TryFindFileInFallbackDatabase(StringPiece name) const; + bool TryFindSymbolInFallbackDatabase(StringPiece name) const; + bool TryFindExtensionInFallbackDatabase(const Descriptor *containing_type, int field_number) const; + + // This internal find extension method only check with its table and underlay + // descriptor_pool's table. It does not check with fallback DB and no + // additional proto file will be build in this method. + const FieldDescriptor *InternalFindExtensionByNumberNoLock(const Descriptor *extendee, int number) const; + + // Like BuildFile() but called internally when the file has been loaded from + // fallback_database_. Declared const because it is called by (semantically) + // const methods. + const FileDescriptor *BuildFileFromDatabase(const FileDescriptorProto &proto) const; + + // Helper for when lazily_build_dependencies_ is set, can look up a symbol + // after the file's descriptor is built, and can build the file where that + // symbol is defined if necessary. Will create a placeholder if the type + // doesn't exist in the fallback database, or the file doesn't build + // successfully. + Symbol CrossLinkOnDemandHelper(StringPiece name, bool expecting_enum) const; + + // Create a placeholder FileDescriptor of the specified name + FileDescriptor *NewPlaceholderFile(StringPiece name) const; + FileDescriptor *NewPlaceholderFileWithMutexHeld(StringPiece name) const; + + enum PlaceholderType { PLACEHOLDER_MESSAGE, PLACEHOLDER_ENUM, PLACEHOLDER_EXTENDABLE_MESSAGE }; + // Create a placeholder Descriptor of the specified name + Symbol NewPlaceholder(StringPiece name, PlaceholderType placeholder_type) const; + Symbol NewPlaceholderWithMutexHeld(StringPiece name, PlaceholderType placeholder_type) const; + + // If fallback_database_ is nullptr, this is nullptr. Otherwise, this is a + // mutex which must be locked while accessing tables_. + internal::WrappedMutex *mutex_; + + // See constructor. + DescriptorDatabase *fallback_database_; + ErrorCollector *default_error_collector_; + const DescriptorPool *underlay_; + + // This class contains a lot of hash maps with complicated types that + // we'd like to keep out of the header. + class Tables; + std::unique_ptr tables_; + + bool enforce_dependencies_; + bool lazily_build_dependencies_; + bool allow_unknown_; + bool enforce_weak_; + bool disallow_enforce_utf8_; + + // Set of files to track for unused imports. The bool value when true means + // unused imports are treated as errors (and as warnings when false). + std::map unused_import_track_files_; + + GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(DescriptorPool); }; - // inline methods ==================================================== // These macros makes this repetitive code more readable. @@ -2062,23 +2039,23 @@ class PROTOBUF_EXPORT DescriptorPool { // Strings fields are stored as pointers but returned as const references. #define PROTOBUF_DEFINE_STRING_ACCESSOR(CLASS, FIELD) \ - inline const std::string& CLASS::FIELD() const { return *FIELD##_; } + inline const std::string &CLASS::FIELD() const { return *FIELD##_; } // Name and full name are stored in a single array to save space. #define PROTOBUF_DEFINE_NAME_ACCESSOR(CLASS) \ - inline const std::string& CLASS::name() const { return all_names_[0]; } \ - inline const std::string& CLASS::full_name() const { return all_names_[1]; } + inline const std::string &CLASS::name() const { return all_names_[0]; } \ + inline const std::string &CLASS::full_name() const { return all_names_[1]; } // Arrays take an index parameter, obviously. #define PROTOBUF_DEFINE_ARRAY_ACCESSOR(CLASS, FIELD, TYPE) \ inline TYPE CLASS::FIELD(int index) const { return FIELD##s_ + index; } #define PROTOBUF_DEFINE_OPTIONS_ACCESSOR(CLASS, TYPE) \ - inline const TYPE& CLASS::options() const { return *options_; } + inline const TYPE &CLASS::options() const { return *options_; } PROTOBUF_DEFINE_NAME_ACCESSOR(Descriptor) -PROTOBUF_DEFINE_ACCESSOR(Descriptor, file, const FileDescriptor*) -PROTOBUF_DEFINE_ACCESSOR(Descriptor, containing_type, const Descriptor*) +PROTOBUF_DEFINE_ACCESSOR(Descriptor, file, const FileDescriptor *) +PROTOBUF_DEFINE_ACCESSOR(Descriptor, containing_type, const Descriptor *) PROTOBUF_DEFINE_ACCESSOR(Descriptor, field_count, int) PROTOBUF_DEFINE_ACCESSOR(Descriptor, oneof_decl_count, int) @@ -2086,30 +2063,28 @@ PROTOBUF_DEFINE_ACCESSOR(Descriptor, real_oneof_decl_count, int) PROTOBUF_DEFINE_ACCESSOR(Descriptor, nested_type_count, int) PROTOBUF_DEFINE_ACCESSOR(Descriptor, enum_type_count, int) -PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, field, const FieldDescriptor*) -PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, oneof_decl, const OneofDescriptor*) -PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, nested_type, const Descriptor*) -PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, enum_type, const EnumDescriptor*) +PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, field, const FieldDescriptor *) +PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, oneof_decl, const OneofDescriptor *) +PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, nested_type, const Descriptor *) +PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, enum_type, const EnumDescriptor *) PROTOBUF_DEFINE_ACCESSOR(Descriptor, extension_range_count, int) PROTOBUF_DEFINE_ACCESSOR(Descriptor, extension_count, int) -PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, extension_range, - const Descriptor::ExtensionRange*) -PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, extension, const FieldDescriptor*) +PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, extension_range, const Descriptor::ExtensionRange *) +PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, extension, const FieldDescriptor *) PROTOBUF_DEFINE_ACCESSOR(Descriptor, reserved_range_count, int) -PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, reserved_range, - const Descriptor::ReservedRange*) +PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, reserved_range, const Descriptor::ReservedRange *) PROTOBUF_DEFINE_ACCESSOR(Descriptor, reserved_name_count, int) PROTOBUF_DEFINE_OPTIONS_ACCESSOR(Descriptor, MessageOptions) PROTOBUF_DEFINE_ACCESSOR(Descriptor, is_placeholder, bool) PROTOBUF_DEFINE_NAME_ACCESSOR(FieldDescriptor) -PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, file, const FileDescriptor*) +PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, file, const FileDescriptor *) PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, number, int) PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, is_extension, bool) -PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, containing_type, const Descriptor*) +PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, containing_type, const Descriptor *) PROTOBUF_DEFINE_OPTIONS_ACCESSOR(FieldDescriptor, FieldOptions) PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, has_default_value, bool) PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, has_json_name, bool) @@ -2123,45 +2098,42 @@ PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_bool, bool) PROTOBUF_DEFINE_STRING_ACCESSOR(FieldDescriptor, default_value_string) PROTOBUF_DEFINE_NAME_ACCESSOR(OneofDescriptor) -PROTOBUF_DEFINE_ACCESSOR(OneofDescriptor, containing_type, const Descriptor*) +PROTOBUF_DEFINE_ACCESSOR(OneofDescriptor, containing_type, const Descriptor *) PROTOBUF_DEFINE_ACCESSOR(OneofDescriptor, field_count, int) -PROTOBUF_DEFINE_ARRAY_ACCESSOR(OneofDescriptor, field, const FieldDescriptor*) +PROTOBUF_DEFINE_ARRAY_ACCESSOR(OneofDescriptor, field, const FieldDescriptor *) PROTOBUF_DEFINE_OPTIONS_ACCESSOR(OneofDescriptor, OneofOptions) PROTOBUF_DEFINE_NAME_ACCESSOR(EnumDescriptor) -PROTOBUF_DEFINE_ACCESSOR(EnumDescriptor, file, const FileDescriptor*) -PROTOBUF_DEFINE_ACCESSOR(EnumDescriptor, containing_type, const Descriptor*) +PROTOBUF_DEFINE_ACCESSOR(EnumDescriptor, file, const FileDescriptor *) +PROTOBUF_DEFINE_ACCESSOR(EnumDescriptor, containing_type, const Descriptor *) PROTOBUF_DEFINE_ACCESSOR(EnumDescriptor, value_count, int) -PROTOBUF_DEFINE_ARRAY_ACCESSOR(EnumDescriptor, value, - const EnumValueDescriptor*) +PROTOBUF_DEFINE_ARRAY_ACCESSOR(EnumDescriptor, value, const EnumValueDescriptor *) PROTOBUF_DEFINE_OPTIONS_ACCESSOR(EnumDescriptor, EnumOptions) PROTOBUF_DEFINE_ACCESSOR(EnumDescriptor, is_placeholder, bool) PROTOBUF_DEFINE_ACCESSOR(EnumDescriptor, reserved_range_count, int) -PROTOBUF_DEFINE_ARRAY_ACCESSOR(EnumDescriptor, reserved_range, - const EnumDescriptor::ReservedRange*) +PROTOBUF_DEFINE_ARRAY_ACCESSOR(EnumDescriptor, reserved_range, const EnumDescriptor::ReservedRange *) PROTOBUF_DEFINE_ACCESSOR(EnumDescriptor, reserved_name_count, int) PROTOBUF_DEFINE_NAME_ACCESSOR(EnumValueDescriptor) PROTOBUF_DEFINE_ACCESSOR(EnumValueDescriptor, number, int) -PROTOBUF_DEFINE_ACCESSOR(EnumValueDescriptor, type, const EnumDescriptor*) +PROTOBUF_DEFINE_ACCESSOR(EnumValueDescriptor, type, const EnumDescriptor *) PROTOBUF_DEFINE_OPTIONS_ACCESSOR(EnumValueDescriptor, EnumValueOptions) PROTOBUF_DEFINE_NAME_ACCESSOR(ServiceDescriptor) -PROTOBUF_DEFINE_ACCESSOR(ServiceDescriptor, file, const FileDescriptor*) +PROTOBUF_DEFINE_ACCESSOR(ServiceDescriptor, file, const FileDescriptor *) PROTOBUF_DEFINE_ACCESSOR(ServiceDescriptor, method_count, int) -PROTOBUF_DEFINE_ARRAY_ACCESSOR(ServiceDescriptor, method, - const MethodDescriptor*) +PROTOBUF_DEFINE_ARRAY_ACCESSOR(ServiceDescriptor, method, const MethodDescriptor *) PROTOBUF_DEFINE_OPTIONS_ACCESSOR(ServiceDescriptor, ServiceOptions) PROTOBUF_DEFINE_NAME_ACCESSOR(MethodDescriptor) -PROTOBUF_DEFINE_ACCESSOR(MethodDescriptor, service, const ServiceDescriptor*) +PROTOBUF_DEFINE_ACCESSOR(MethodDescriptor, service, const ServiceDescriptor *) PROTOBUF_DEFINE_OPTIONS_ACCESSOR(MethodDescriptor, MethodOptions) PROTOBUF_DEFINE_ACCESSOR(MethodDescriptor, client_streaming, bool) PROTOBUF_DEFINE_ACCESSOR(MethodDescriptor, server_streaming, bool) PROTOBUF_DEFINE_STRING_ACCESSOR(FileDescriptor, name) PROTOBUF_DEFINE_STRING_ACCESSOR(FileDescriptor, package) -PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, pool, const DescriptorPool*) +PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, pool, const DescriptorPool *) PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, dependency_count, int) PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, public_dependency_count, int) PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, weak_dependency_count, int) @@ -2172,12 +2144,10 @@ PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, extension_count, int) PROTOBUF_DEFINE_OPTIONS_ACCESSOR(FileDescriptor, FileOptions) PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, is_placeholder, bool) -PROTOBUF_DEFINE_ARRAY_ACCESSOR(FileDescriptor, message_type, const Descriptor*) -PROTOBUF_DEFINE_ARRAY_ACCESSOR(FileDescriptor, enum_type, const EnumDescriptor*) -PROTOBUF_DEFINE_ARRAY_ACCESSOR(FileDescriptor, service, - const ServiceDescriptor*) -PROTOBUF_DEFINE_ARRAY_ACCESSOR(FileDescriptor, extension, - const FieldDescriptor*) +PROTOBUF_DEFINE_ARRAY_ACCESSOR(FileDescriptor, message_type, const Descriptor *) +PROTOBUF_DEFINE_ARRAY_ACCESSOR(FileDescriptor, enum_type, const EnumDescriptor *) +PROTOBUF_DEFINE_ARRAY_ACCESSOR(FileDescriptor, service, const ServiceDescriptor *) +PROTOBUF_DEFINE_ARRAY_ACCESSOR(FileDescriptor, extension, const FieldDescriptor *) #undef PROTOBUF_DEFINE_ACCESSOR #undef PROTOBUF_DEFINE_STRING_ACCESSOR @@ -2186,232 +2156,228 @@ PROTOBUF_DEFINE_ARRAY_ACCESSOR(FileDescriptor, extension, // A few accessors differ from the macros... inline Descriptor::WellKnownType Descriptor::well_known_type() const { - return static_cast(well_known_type_); + return static_cast(well_known_type_); } inline bool Descriptor::IsExtensionNumber(int number) const { - return FindExtensionRangeContainingNumber(number) != nullptr; + return FindExtensionRangeContainingNumber(number) != nullptr; } inline bool Descriptor::IsReservedNumber(int number) const { - return FindReservedRangeContainingNumber(number) != nullptr; + return FindReservedRangeContainingNumber(number) != nullptr; } inline bool Descriptor::IsReservedName(ConstStringParam name) const { - for (int i = 0; i < reserved_name_count(); i++) { - if (name == static_cast(reserved_name(i))) { - return true; - } - } - return false; + for (int i = 0; i < reserved_name_count(); i++) { + if (name == static_cast(reserved_name(i))) { + return true; + } + } + return false; } // Can't use PROTOBUF_DEFINE_ARRAY_ACCESSOR because reserved_names_ is actually // an array of pointers rather than the usual array of objects. -inline const std::string& Descriptor::reserved_name(int index) const { - return *reserved_names_[index]; +inline const std::string &Descriptor::reserved_name(int index) const { + return *reserved_names_[index]; } inline bool EnumDescriptor::IsReservedNumber(int number) const { - return FindReservedRangeContainingNumber(number) != nullptr; + return FindReservedRangeContainingNumber(number) != nullptr; } inline bool EnumDescriptor::IsReservedName(ConstStringParam name) const { - for (int i = 0; i < reserved_name_count(); i++) { - if (name == static_cast(reserved_name(i))) { - return true; - } - } - return false; + for (int i = 0; i < reserved_name_count(); i++) { + if (name == static_cast(reserved_name(i))) { + return true; + } + } + return false; } // Can't use PROTOBUF_DEFINE_ARRAY_ACCESSOR because reserved_names_ is actually // an array of pointers rather than the usual array of objects. -inline const std::string& EnumDescriptor::reserved_name(int index) const { - return *reserved_names_[index]; +inline const std::string &EnumDescriptor::reserved_name(int index) const { + return *reserved_names_[index]; } -inline const std::string& FieldDescriptor::lowercase_name() const { - return all_names_[lowercase_name_index_]; +inline const std::string &FieldDescriptor::lowercase_name() const { + return all_names_[lowercase_name_index_]; } -inline const std::string& FieldDescriptor::camelcase_name() const { - return all_names_[camelcase_name_index_]; +inline const std::string &FieldDescriptor::camelcase_name() const { + return all_names_[camelcase_name_index_]; } -inline const std::string& FieldDescriptor::json_name() const { - return all_names_[json_name_index_]; +inline const std::string &FieldDescriptor::json_name() const { + return all_names_[json_name_index_]; } -inline const OneofDescriptor* FieldDescriptor::containing_oneof() const { - return is_oneof_ ? scope_.containing_oneof : nullptr; +inline const OneofDescriptor *FieldDescriptor::containing_oneof() const { + return is_oneof_ ? scope_.containing_oneof : nullptr; } inline int FieldDescriptor::index_in_oneof() const { - GOOGLE_DCHECK(is_oneof_); - return static_cast(this - scope_.containing_oneof->field(0)); + GOOGLE_DCHECK(is_oneof_); + return static_cast(this - scope_.containing_oneof->field(0)); } -inline const Descriptor* FieldDescriptor::extension_scope() const { - GOOGLE_CHECK(is_extension_); - return scope_.extension_scope; +inline const Descriptor *FieldDescriptor::extension_scope() const { + GOOGLE_CHECK(is_extension_); + return scope_.extension_scope; } inline FieldDescriptor::Label FieldDescriptor::label() const { - return static_cast