From 7b1f7938bc89b2346582464dba2767891509d550 Mon Sep 17 00:00:00 2001 From: Yuriy Chernyshov Date: Mon, 12 Jul 2021 21:47:03 +0300 Subject: [PATCH] Support arena allocation of google::protobuf::AnyMetadata (#8758) --- src/google/protobuf/any.cc | 11 ++++++----- src/google/protobuf/any.h | 15 ++++++++------- src/google/protobuf/any.pb.h | 4 ++-- src/google/protobuf/any_lite.cc | 7 ++++--- src/google/protobuf/compiler/cpp/cpp_message.cc | 8 ++++---- 5 files changed, 24 insertions(+), 21 deletions(-) diff --git a/src/google/protobuf/any.cc b/src/google/protobuf/any.cc index 029ba0d9299..955dd80b5be 100644 --- a/src/google/protobuf/any.cc +++ b/src/google/protobuf/any.cc @@ -41,18 +41,19 @@ namespace google { namespace protobuf { namespace internal { -bool AnyMetadata::PackFrom(const Message& message) { - return PackFrom(message, kTypeGoogleApisComPrefix); +bool AnyMetadata::PackFrom(Arena* arena, const Message& message) { + return PackFrom(arena, message, kTypeGoogleApisComPrefix); } -bool AnyMetadata::PackFrom(const Message& message, +bool AnyMetadata::PackFrom(Arena* arena, + const Message& message, StringPiece type_url_prefix) { type_url_->Set( &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyString(), GetTypeUrl(message.GetDescriptor()->full_name(), type_url_prefix), - nullptr); + arena); return message.SerializeToString( - value_->Mutable(ArenaStringPtr::EmptyDefault{}, nullptr)); + value_->Mutable(ArenaStringPtr::EmptyDefault{}, arena)); } bool AnyMetadata::UnpackTo(Message* message) const { diff --git a/src/google/protobuf/any.h b/src/google/protobuf/any.h index 684fbf15843..3ec8294e2e2 100644 --- a/src/google/protobuf/any.h +++ b/src/google/protobuf/any.h @@ -67,12 +67,12 @@ class PROTOBUF_EXPORT AnyMetadata { // The resulted type URL will be "type.googleapis.com/". // Returns false if serializing the message failed. template - bool PackFrom(const T& message) { - return InternalPackFrom(message, kTypeGoogleApisComPrefix, + bool PackFrom(Arena* arena, const T& message) { + return InternalPackFrom(arena, message, kTypeGoogleApisComPrefix, T::FullMessageName()); } - bool PackFrom(const Message& message); + 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 @@ -82,11 +82,11 @@ class PROTOBUF_EXPORT AnyMetadata { // URL: "type.googleapis.com/". // Returns false if serializing the message failed. template - bool PackFrom(const T& message, StringPiece type_url_prefix) { - return InternalPackFrom(message, type_url_prefix, T::FullMessageName()); + bool PackFrom(Arena* arena, const T& message, StringPiece type_url_prefix) { + return InternalPackFrom(arena, message, type_url_prefix, T::FullMessageName()); } - bool PackFrom(const Message& message, StringPiece type_url_prefix); + 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 @@ -108,7 +108,8 @@ class PROTOBUF_EXPORT AnyMetadata { } private: - bool InternalPackFrom(const MessageLite& message, + bool InternalPackFrom(Arena* arena, + const MessageLite& message, StringPiece type_url_prefix, StringPiece type_name); bool InternalUnpackTo(StringPiece type_name, diff --git a/src/google/protobuf/any.pb.h b/src/google/protobuf/any.pb.h index 494b2511f74..b4ab93c1db0 100644 --- a/src/google/protobuf/any.pb.h +++ b/src/google/protobuf/any.pb.h @@ -114,11 +114,11 @@ class PROTOBUF_EXPORT Any final : // implements Any ----------------------------------------------- bool PackFrom(const ::PROTOBUF_NAMESPACE_ID::Message& message) { - return _any_metadata_.PackFrom(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(message, 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); diff --git a/src/google/protobuf/any_lite.cc b/src/google/protobuf/any_lite.cc index 48e0faccf7f..1fa06c732d3 100644 --- a/src/google/protobuf/any_lite.cc +++ b/src/google/protobuf/any_lite.cc @@ -53,13 +53,14 @@ const char kAnyFullTypeName[] = "google.protobuf.Any"; const char kTypeGoogleApisComPrefix[] = "type.googleapis.com/"; const char kTypeGoogleProdComPrefix[] = "type.googleprod.com/"; -bool AnyMetadata::InternalPackFrom(const MessageLite& message, +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), nullptr); + GetTypeUrl(type_name, type_url_prefix), arena); return message.SerializeToString( - value_->Mutable(ArenaStringPtr::EmptyDefault{}, nullptr)); + value_->Mutable(ArenaStringPtr::EmptyDefault{}, arena)); } bool AnyMetadata::InternalUnpackTo(StringPiece type_name, diff --git a/src/google/protobuf/compiler/cpp/cpp_message.cc b/src/google/protobuf/compiler/cpp/cpp_message.cc index 3ebe9f0365b..79ed8aaa27c 100644 --- a/src/google/protobuf/compiler/cpp/cpp_message.cc +++ b/src/google/protobuf/compiler/cpp/cpp_message.cc @@ -1259,12 +1259,12 @@ void MessageGenerator::GenerateClassDefinition(io::Printer* printer) { if (HasDescriptorMethods(descriptor_->file(), options_)) { format( "bool PackFrom(const ::$proto_ns$::Message& message) {\n" - " return _any_metadata_.PackFrom(message);\n" + " return _any_metadata_.PackFrom(GetArena(), message);\n" "}\n" "bool PackFrom(const ::$proto_ns$::Message& message,\n" " ::PROTOBUF_NAMESPACE_ID::ConstStringParam " "type_url_prefix) {\n" - " return _any_metadata_.PackFrom(message, type_url_prefix);\n" + " return _any_metadata_.PackFrom(GetArena(), message, type_url_prefix);\n" "}\n" "bool UnpackTo(::$proto_ns$::Message* message) const {\n" " return _any_metadata_.UnpackTo(message);\n" @@ -1277,7 +1277,7 @@ void MessageGenerator::GenerateClassDefinition(io::Printer* printer) { "!std::is_convertible" "::value>::type>\n" "bool PackFrom(const T& message) {\n" - " return _any_metadata_.PackFrom(message);\n" + " return _any_metadata_.PackFrom(GetArena(), message);\n" "}\n" "template " @@ -1285,7 +1285,7 @@ void MessageGenerator::GenerateClassDefinition(io::Printer* printer) { "bool PackFrom(const T& message,\n" " ::PROTOBUF_NAMESPACE_ID::ConstStringParam " "type_url_prefix) {\n" - " return _any_metadata_.PackFrom(message, type_url_prefix);" + " return _any_metadata_.PackFrom(GetArena(), message, type_url_prefix);" "}\n" "template "