Replace PROTOBUF_FORCE_COPY_IN_SWAP macro with constexpr function for easier

maintenance.
Also, added helper function to deduplicate the "can use fast swap" logic.
No semantic change expected.

PiperOrigin-RevId: 671111042
pull/18072/head
Protobuf Team Bot 6 months ago committed by Copybara-Service
parent 5d341470da
commit c890e87bc9
  1. 11
      src/google/protobuf/arena.h
  2. 28
      src/google/protobuf/arenastring.h
  3. 6
      src/google/protobuf/compiler/cpp/message.cc
  4. 6
      src/google/protobuf/compiler/java/java_features.pb.h
  5. 24
      src/google/protobuf/compiler/plugin.pb.h
  6. 6
      src/google/protobuf/cpp_features.pb.h
  7. 198
      src/google/protobuf/descriptor.pb.h
  8. 6
      src/google/protobuf/extension_set.cc
  9. 24
      src/google/protobuf/generated_message_reflection.cc
  10. 21
      src/google/protobuf/lazy_repeated_field.cc
  11. 4
      src/google/protobuf/port.h
  12. 4
      src/google/protobuf/port_def.inc
  13. 1
      src/google/protobuf/port_undef.inc
  14. 22
      src/google/protobuf/reflection_ops.cc
  15. 6
      src/google/protobuf/repeated_field.h
  16. 13
      src/google/protobuf/repeated_ptr_field.h

@ -87,6 +87,17 @@ template <typename T>
void arena_delete_object(void* object) {
delete reinterpret_cast<T*>(object);
}
inline bool CanUseInternalSwap(Arena* lhs, Arena* rhs) {
if (DebugHardenForceCopyInSwap()) {
// We force copy in swap when we are not using an arena.
// If we did with an arena we would grow arena usage too much.
return lhs != nullptr && lhs == rhs;
} else {
return lhs == rhs;
}
}
} // namespace internal
// ArenaOptions provides optional additional parameters to arena construction

@ -503,22 +503,22 @@ inline PROTOBUF_NDEBUG_INLINE void ArenaStringPtr::InternalSwap(
// Silence unused variable warnings in release buildls.
(void)arena;
std::swap(lhs->tagged_ptr_, rhs->tagged_ptr_);
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
for (auto* p : {lhs, rhs}) {
if (p->IsDefault()) continue;
std::string* old_value = p->tagged_ptr_.Get();
std::string* new_value =
p->IsFixedSizeArena()
? Arena::Create<std::string>(arena, *old_value)
: Arena::Create<std::string>(arena, std::move(*old_value));
if (arena == nullptr) {
delete old_value;
p->tagged_ptr_.SetAllocated(new_value);
} else {
p->tagged_ptr_.SetMutableArena(new_value);
if (internal::DebugHardenForceCopyInSwap()) {
for (auto* p : {lhs, rhs}) {
if (p->IsDefault()) continue;
std::string* old_value = p->tagged_ptr_.Get();
std::string* new_value =
p->IsFixedSizeArena()
? Arena::Create<std::string>(arena, *old_value)
: Arena::Create<std::string>(arena, std::move(*old_value));
if (arena == nullptr) {
delete old_value;
p->tagged_ptr_.SetAllocated(new_value);
} else {
p->tagged_ptr_.SetMutableArena(new_value);
}
}
}
#endif // PROTOBUF_FORCE_COPY_IN_SWAP
}
inline void ArenaStringPtr::ClearNonDefaultToEmpty() {

@ -2126,11 +2126,7 @@ void MessageGenerator::GenerateClassDefinition(io::Printer* p) {
friend void swap($classname$& a, $classname$& b) { a.Swap(&b); }
inline void Swap($classname$* other) {
if (other == this) return;
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() != nullptr && GetArena() == other->GetArena()) {
#else // PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() == other->GetArena()) {
#endif // !PROTOBUF_FORCE_COPY_IN_SWAP
if ($pbi$::CanUseInternalSwap(GetArena(), other->GetArena())) {
InternalSwap(other);
} else {
$pbi$::GenericSwap(this, other);

@ -159,11 +159,7 @@ class PROTOC_EXPORT JavaFeatures final
friend void swap(JavaFeatures& a, JavaFeatures& b) { a.Swap(&b); }
inline void Swap(JavaFeatures* other) {
if (other == this) return;
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() != nullptr && GetArena() == other->GetArena()) {
#else // PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() == other->GetArena()) {
#endif // !PROTOBUF_FORCE_COPY_IN_SWAP
if (::google::protobuf::internal::CanUseInternalSwap(GetArena(), other->GetArena())) {
InternalSwap(other);
} else {
::google::protobuf::internal::GenericSwap(this, other);

@ -176,11 +176,7 @@ class PROTOC_EXPORT Version final
friend void swap(Version& a, Version& b) { a.Swap(&b); }
inline void Swap(Version* other) {
if (other == this) return;
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() != nullptr && GetArena() == other->GetArena()) {
#else // PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() == other->GetArena()) {
#endif // !PROTOBUF_FORCE_COPY_IN_SWAP
if (::google::protobuf::internal::CanUseInternalSwap(GetArena(), other->GetArena())) {
InternalSwap(other);
} else {
::google::protobuf::internal::GenericSwap(this, other);
@ -415,11 +411,7 @@ class PROTOC_EXPORT CodeGeneratorResponse_File final
friend void swap(CodeGeneratorResponse_File& a, CodeGeneratorResponse_File& b) { a.Swap(&b); }
inline void Swap(CodeGeneratorResponse_File* other) {
if (other == this) return;
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() != nullptr && GetArena() == other->GetArena()) {
#else // PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() == other->GetArena()) {
#endif // !PROTOBUF_FORCE_COPY_IN_SWAP
if (::google::protobuf::internal::CanUseInternalSwap(GetArena(), other->GetArena())) {
InternalSwap(other);
} else {
::google::protobuf::internal::GenericSwap(this, other);
@ -670,11 +662,7 @@ class PROTOC_EXPORT CodeGeneratorResponse final
friend void swap(CodeGeneratorResponse& a, CodeGeneratorResponse& b) { a.Swap(&b); }
inline void Swap(CodeGeneratorResponse* other) {
if (other == this) return;
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() != nullptr && GetArena() == other->GetArena()) {
#else // PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() == other->GetArena()) {
#endif // !PROTOBUF_FORCE_COPY_IN_SWAP
if (::google::protobuf::internal::CanUseInternalSwap(GetArena(), other->GetArena())) {
InternalSwap(other);
} else {
::google::protobuf::internal::GenericSwap(this, other);
@ -949,11 +937,7 @@ class PROTOC_EXPORT CodeGeneratorRequest final
friend void swap(CodeGeneratorRequest& a, CodeGeneratorRequest& b) { a.Swap(&b); }
inline void Swap(CodeGeneratorRequest* other) {
if (other == this) return;
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() != nullptr && GetArena() == other->GetArena()) {
#else // PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() == other->GetArena()) {
#endif // !PROTOBUF_FORCE_COPY_IN_SWAP
if (::google::protobuf::internal::CanUseInternalSwap(GetArena(), other->GetArena())) {
InternalSwap(other);
} else {
::google::protobuf::internal::GenericSwap(this, other);

@ -160,11 +160,7 @@ class PROTOBUF_EXPORT CppFeatures final
friend void swap(CppFeatures& a, CppFeatures& b) { a.Swap(&b); }
inline void Swap(CppFeatures* other) {
if (other == this) return;
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() != nullptr && GetArena() == other->GetArena()) {
#else // PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() == other->GetArena()) {
#endif // !PROTOBUF_FORCE_COPY_IN_SWAP
if (::google::protobuf::internal::CanUseInternalSwap(GetArena(), other->GetArena())) {
InternalSwap(other);
} else {
::google::protobuf::internal::GenericSwap(this, other);

@ -762,11 +762,7 @@ class PROTOBUF_EXPORT UninterpretedOption_NamePart final
friend void swap(UninterpretedOption_NamePart& a, UninterpretedOption_NamePart& b) { a.Swap(&b); }
inline void Swap(UninterpretedOption_NamePart* other) {
if (other == this) return;
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() != nullptr && GetArena() == other->GetArena()) {
#else // PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() == other->GetArena()) {
#endif // !PROTOBUF_FORCE_COPY_IN_SWAP
if (::google::protobuf::internal::CanUseInternalSwap(GetArena(), other->GetArena())) {
InternalSwap(other);
} else {
::google::protobuf::internal::GenericSwap(this, other);
@ -980,11 +976,7 @@ class PROTOBUF_EXPORT SourceCodeInfo_Location final
friend void swap(SourceCodeInfo_Location& a, SourceCodeInfo_Location& b) { a.Swap(&b); }
inline void Swap(SourceCodeInfo_Location* other) {
if (other == this) return;
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() != nullptr && GetArena() == other->GetArena()) {
#else // PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() == other->GetArena()) {
#endif // !PROTOBUF_FORCE_COPY_IN_SWAP
if (::google::protobuf::internal::CanUseInternalSwap(GetArena(), other->GetArena())) {
InternalSwap(other);
} else {
::google::protobuf::internal::GenericSwap(this, other);
@ -1265,11 +1257,7 @@ class PROTOBUF_EXPORT GeneratedCodeInfo_Annotation final
friend void swap(GeneratedCodeInfo_Annotation& a, GeneratedCodeInfo_Annotation& b) { a.Swap(&b); }
inline void Swap(GeneratedCodeInfo_Annotation* other) {
if (other == this) return;
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() != nullptr && GetArena() == other->GetArena()) {
#else // PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() == other->GetArena()) {
#endif // !PROTOBUF_FORCE_COPY_IN_SWAP
if (::google::protobuf::internal::CanUseInternalSwap(GetArena(), other->GetArena())) {
InternalSwap(other);
} else {
::google::protobuf::internal::GenericSwap(this, other);
@ -1545,11 +1533,7 @@ class PROTOBUF_EXPORT FieldOptions_FeatureSupport final
friend void swap(FieldOptions_FeatureSupport& a, FieldOptions_FeatureSupport& b) { a.Swap(&b); }
inline void Swap(FieldOptions_FeatureSupport* other) {
if (other == this) return;
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() != nullptr && GetArena() == other->GetArena()) {
#else // PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() == other->GetArena()) {
#endif // !PROTOBUF_FORCE_COPY_IN_SWAP
if (::google::protobuf::internal::CanUseInternalSwap(GetArena(), other->GetArena())) {
InternalSwap(other);
} else {
::google::protobuf::internal::GenericSwap(this, other);
@ -1784,11 +1768,7 @@ class PROTOBUF_EXPORT FieldOptions_EditionDefault final
friend void swap(FieldOptions_EditionDefault& a, FieldOptions_EditionDefault& b) { a.Swap(&b); }
inline void Swap(FieldOptions_EditionDefault* other) {
if (other == this) return;
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() != nullptr && GetArena() == other->GetArena()) {
#else // PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() == other->GetArena()) {
#endif // !PROTOBUF_FORCE_COPY_IN_SWAP
if (::google::protobuf::internal::CanUseInternalSwap(GetArena(), other->GetArena())) {
InternalSwap(other);
} else {
::google::protobuf::internal::GenericSwap(this, other);
@ -1997,11 +1977,7 @@ class PROTOBUF_EXPORT FeatureSet final
friend void swap(FeatureSet& a, FeatureSet& b) { a.Swap(&b); }
inline void Swap(FeatureSet* other) {
if (other == this) return;
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() != nullptr && GetArena() == other->GetArena()) {
#else // PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() == other->GetArena()) {
#endif // !PROTOBUF_FORCE_COPY_IN_SWAP
if (::google::protobuf::internal::CanUseInternalSwap(GetArena(), other->GetArena())) {
InternalSwap(other);
} else {
::google::protobuf::internal::GenericSwap(this, other);
@ -2562,11 +2538,7 @@ class PROTOBUF_EXPORT ExtensionRangeOptions_Declaration final
friend void swap(ExtensionRangeOptions_Declaration& a, ExtensionRangeOptions_Declaration& b) { a.Swap(&b); }
inline void Swap(ExtensionRangeOptions_Declaration* other) {
if (other == this) return;
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() != nullptr && GetArena() == other->GetArena()) {
#else // PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() == other->GetArena()) {
#endif // !PROTOBUF_FORCE_COPY_IN_SWAP
if (::google::protobuf::internal::CanUseInternalSwap(GetArena(), other->GetArena())) {
InternalSwap(other);
} else {
::google::protobuf::internal::GenericSwap(this, other);
@ -2820,11 +2792,7 @@ class PROTOBUF_EXPORT EnumDescriptorProto_EnumReservedRange final
friend void swap(EnumDescriptorProto_EnumReservedRange& a, EnumDescriptorProto_EnumReservedRange& b) { a.Swap(&b); }
inline void Swap(EnumDescriptorProto_EnumReservedRange* other) {
if (other == this) return;
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() != nullptr && GetArena() == other->GetArena()) {
#else // PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() == other->GetArena()) {
#endif // !PROTOBUF_FORCE_COPY_IN_SWAP
if (::google::protobuf::internal::CanUseInternalSwap(GetArena(), other->GetArena())) {
InternalSwap(other);
} else {
::google::protobuf::internal::GenericSwap(this, other);
@ -3027,11 +2995,7 @@ class PROTOBUF_EXPORT DescriptorProto_ReservedRange final
friend void swap(DescriptorProto_ReservedRange& a, DescriptorProto_ReservedRange& b) { a.Swap(&b); }
inline void Swap(DescriptorProto_ReservedRange* other) {
if (other == this) return;
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() != nullptr && GetArena() == other->GetArena()) {
#else // PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() == other->GetArena()) {
#endif // !PROTOBUF_FORCE_COPY_IN_SWAP
if (::google::protobuf::internal::CanUseInternalSwap(GetArena(), other->GetArena())) {
InternalSwap(other);
} else {
::google::protobuf::internal::GenericSwap(this, other);
@ -3234,11 +3198,7 @@ class PROTOBUF_EXPORT UninterpretedOption final
friend void swap(UninterpretedOption& a, UninterpretedOption& b) { a.Swap(&b); }
inline void Swap(UninterpretedOption* other) {
if (other == this) return;
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() != nullptr && GetArena() == other->GetArena()) {
#else // PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() == other->GetArena()) {
#endif // !PROTOBUF_FORCE_COPY_IN_SWAP
if (::google::protobuf::internal::CanUseInternalSwap(GetArena(), other->GetArena())) {
InternalSwap(other);
} else {
::google::protobuf::internal::GenericSwap(this, other);
@ -3536,11 +3496,7 @@ class PROTOBUF_EXPORT SourceCodeInfo final
friend void swap(SourceCodeInfo& a, SourceCodeInfo& b) { a.Swap(&b); }
inline void Swap(SourceCodeInfo* other) {
if (other == this) return;
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() != nullptr && GetArena() == other->GetArena()) {
#else // PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() == other->GetArena()) {
#endif // !PROTOBUF_FORCE_COPY_IN_SWAP
if (::google::protobuf::internal::CanUseInternalSwap(GetArena(), other->GetArena())) {
InternalSwap(other);
} else {
::google::protobuf::internal::GenericSwap(this, other);
@ -3736,11 +3692,7 @@ class PROTOBUF_EXPORT GeneratedCodeInfo final
friend void swap(GeneratedCodeInfo& a, GeneratedCodeInfo& b) { a.Swap(&b); }
inline void Swap(GeneratedCodeInfo* other) {
if (other == this) return;
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() != nullptr && GetArena() == other->GetArena()) {
#else // PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() == other->GetArena()) {
#endif // !PROTOBUF_FORCE_COPY_IN_SWAP
if (::google::protobuf::internal::CanUseInternalSwap(GetArena(), other->GetArena())) {
InternalSwap(other);
} else {
::google::protobuf::internal::GenericSwap(this, other);
@ -3936,11 +3888,7 @@ class PROTOBUF_EXPORT FeatureSetDefaults_FeatureSetEditionDefault final
friend void swap(FeatureSetDefaults_FeatureSetEditionDefault& a, FeatureSetDefaults_FeatureSetEditionDefault& b) { a.Swap(&b); }
inline void Swap(FeatureSetDefaults_FeatureSetEditionDefault* other) {
if (other == this) return;
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() != nullptr && GetArena() == other->GetArena()) {
#else // PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() == other->GetArena()) {
#endif // !PROTOBUF_FORCE_COPY_IN_SWAP
if (::google::protobuf::internal::CanUseInternalSwap(GetArena(), other->GetArena())) {
InternalSwap(other);
} else {
::google::protobuf::internal::GenericSwap(this, other);
@ -4169,11 +4117,7 @@ class PROTOBUF_EXPORT ServiceOptions final
friend void swap(ServiceOptions& a, ServiceOptions& b) { a.Swap(&b); }
inline void Swap(ServiceOptions* other) {
if (other == this) return;
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() != nullptr && GetArena() == other->GetArena()) {
#else // PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() == other->GetArena()) {
#endif // !PROTOBUF_FORCE_COPY_IN_SWAP
if (::google::protobuf::internal::CanUseInternalSwap(GetArena(), other->GetArena())) {
InternalSwap(other);
} else {
::google::protobuf::internal::GenericSwap(this, other);
@ -4584,11 +4528,7 @@ class PROTOBUF_EXPORT OneofOptions final
friend void swap(OneofOptions& a, OneofOptions& b) { a.Swap(&b); }
inline void Swap(OneofOptions* other) {
if (other == this) return;
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() != nullptr && GetArena() == other->GetArena()) {
#else // PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() == other->GetArena()) {
#endif // !PROTOBUF_FORCE_COPY_IN_SWAP
if (::google::protobuf::internal::CanUseInternalSwap(GetArena(), other->GetArena())) {
InternalSwap(other);
} else {
::google::protobuf::internal::GenericSwap(this, other);
@ -4986,11 +4926,7 @@ class PROTOBUF_EXPORT MethodOptions final
friend void swap(MethodOptions& a, MethodOptions& b) { a.Swap(&b); }
inline void Swap(MethodOptions* other) {
if (other == this) return;
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() != nullptr && GetArena() == other->GetArena()) {
#else // PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() == other->GetArena()) {
#endif // !PROTOBUF_FORCE_COPY_IN_SWAP
if (::google::protobuf::internal::CanUseInternalSwap(GetArena(), other->GetArena())) {
InternalSwap(other);
} else {
::google::protobuf::internal::GenericSwap(this, other);
@ -5434,11 +5370,7 @@ class PROTOBUF_EXPORT MessageOptions final
friend void swap(MessageOptions& a, MessageOptions& b) { a.Swap(&b); }
inline void Swap(MessageOptions* other) {
if (other == this) return;
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() != nullptr && GetArena() == other->GetArena()) {
#else // PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() == other->GetArena()) {
#endif // !PROTOBUF_FORCE_COPY_IN_SWAP
if (::google::protobuf::internal::CanUseInternalSwap(GetArena(), other->GetArena())) {
InternalSwap(other);
} else {
::google::protobuf::internal::GenericSwap(this, other);
@ -5901,11 +5833,7 @@ class PROTOBUF_EXPORT FileOptions final
friend void swap(FileOptions& a, FileOptions& b) { a.Swap(&b); }
inline void Swap(FileOptions* other) {
if (other == this) return;
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() != nullptr && GetArena() == other->GetArena()) {
#else // PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() == other->GetArena()) {
#endif // !PROTOBUF_FORCE_COPY_IN_SWAP
if (::google::protobuf::internal::CanUseInternalSwap(GetArena(), other->GetArena())) {
InternalSwap(other);
} else {
::google::protobuf::internal::GenericSwap(this, other);
@ -6630,11 +6558,7 @@ class PROTOBUF_EXPORT FieldOptions final
friend void swap(FieldOptions& a, FieldOptions& b) { a.Swap(&b); }
inline void Swap(FieldOptions* other) {
if (other == this) return;
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() != nullptr && GetArena() == other->GetArena()) {
#else // PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() == other->GetArena()) {
#endif // !PROTOBUF_FORCE_COPY_IN_SWAP
if (::google::protobuf::internal::CanUseInternalSwap(GetArena(), other->GetArena())) {
InternalSwap(other);
} else {
::google::protobuf::internal::GenericSwap(this, other);
@ -7295,11 +7219,7 @@ class PROTOBUF_EXPORT FeatureSetDefaults final
friend void swap(FeatureSetDefaults& a, FeatureSetDefaults& b) { a.Swap(&b); }
inline void Swap(FeatureSetDefaults* other) {
if (other == this) return;
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() != nullptr && GetArena() == other->GetArena()) {
#else // PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() == other->GetArena()) {
#endif // !PROTOBUF_FORCE_COPY_IN_SWAP
if (::google::protobuf::internal::CanUseInternalSwap(GetArena(), other->GetArena())) {
InternalSwap(other);
} else {
::google::protobuf::internal::GenericSwap(this, other);
@ -7527,11 +7447,7 @@ class PROTOBUF_EXPORT ExtensionRangeOptions final
friend void swap(ExtensionRangeOptions& a, ExtensionRangeOptions& b) { a.Swap(&b); }
inline void Swap(ExtensionRangeOptions* other) {
if (other == this) return;
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() != nullptr && GetArena() == other->GetArena()) {
#else // PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() == other->GetArena()) {
#endif // !PROTOBUF_FORCE_COPY_IN_SWAP
if (::google::protobuf::internal::CanUseInternalSwap(GetArena(), other->GetArena())) {
InternalSwap(other);
} else {
::google::protobuf::internal::GenericSwap(this, other);
@ -7981,11 +7897,7 @@ class PROTOBUF_EXPORT EnumValueOptions final
friend void swap(EnumValueOptions& a, EnumValueOptions& b) { a.Swap(&b); }
inline void Swap(EnumValueOptions* other) {
if (other == this) return;
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() != nullptr && GetArena() == other->GetArena()) {
#else // PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() == other->GetArena()) {
#endif // !PROTOBUF_FORCE_COPY_IN_SWAP
if (::google::protobuf::internal::CanUseInternalSwap(GetArena(), other->GetArena())) {
InternalSwap(other);
} else {
::google::protobuf::internal::GenericSwap(this, other);
@ -8426,11 +8338,7 @@ class PROTOBUF_EXPORT EnumOptions final
friend void swap(EnumOptions& a, EnumOptions& b) { a.Swap(&b); }
inline void Swap(EnumOptions* other) {
if (other == this) return;
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() != nullptr && GetArena() == other->GetArena()) {
#else // PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() == other->GetArena()) {
#endif // !PROTOBUF_FORCE_COPY_IN_SWAP
if (::google::protobuf::internal::CanUseInternalSwap(GetArena(), other->GetArena())) {
InternalSwap(other);
} else {
::google::protobuf::internal::GenericSwap(this, other);
@ -8867,11 +8775,7 @@ class PROTOBUF_EXPORT OneofDescriptorProto final
friend void swap(OneofDescriptorProto& a, OneofDescriptorProto& b) { a.Swap(&b); }
inline void Swap(OneofDescriptorProto* other) {
if (other == this) return;
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() != nullptr && GetArena() == other->GetArena()) {
#else // PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() == other->GetArena()) {
#endif // !PROTOBUF_FORCE_COPY_IN_SWAP
if (::google::protobuf::internal::CanUseInternalSwap(GetArena(), other->GetArena())) {
InternalSwap(other);
} else {
::google::protobuf::internal::GenericSwap(this, other);
@ -9089,11 +8993,7 @@ class PROTOBUF_EXPORT MethodDescriptorProto final
friend void swap(MethodDescriptorProto& a, MethodDescriptorProto& b) { a.Swap(&b); }
inline void Swap(MethodDescriptorProto* other) {
if (other == this) return;
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() != nullptr && GetArena() == other->GetArena()) {
#else // PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() == other->GetArena()) {
#endif // !PROTOBUF_FORCE_COPY_IN_SWAP
if (::google::protobuf::internal::CanUseInternalSwap(GetArena(), other->GetArena())) {
InternalSwap(other);
} else {
::google::protobuf::internal::GenericSwap(this, other);
@ -9375,11 +9275,7 @@ class PROTOBUF_EXPORT FieldDescriptorProto final
friend void swap(FieldDescriptorProto& a, FieldDescriptorProto& b) { a.Swap(&b); }
inline void Swap(FieldDescriptorProto* other) {
if (other == this) return;
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() != nullptr && GetArena() == other->GetArena()) {
#else // PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() == other->GetArena()) {
#endif // !PROTOBUF_FORCE_COPY_IN_SWAP
if (::google::protobuf::internal::CanUseInternalSwap(GetArena(), other->GetArena())) {
InternalSwap(other);
} else {
::google::protobuf::internal::GenericSwap(this, other);
@ -9793,11 +9689,7 @@ class PROTOBUF_EXPORT EnumValueDescriptorProto final
friend void swap(EnumValueDescriptorProto& a, EnumValueDescriptorProto& b) { a.Swap(&b); }
inline void Swap(EnumValueDescriptorProto* other) {
if (other == this) return;
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() != nullptr && GetArena() == other->GetArena()) {
#else // PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() == other->GetArena()) {
#endif // !PROTOBUF_FORCE_COPY_IN_SWAP
if (::google::protobuf::internal::CanUseInternalSwap(GetArena(), other->GetArena())) {
InternalSwap(other);
} else {
::google::protobuf::internal::GenericSwap(this, other);
@ -10028,11 +9920,7 @@ class PROTOBUF_EXPORT DescriptorProto_ExtensionRange final
friend void swap(DescriptorProto_ExtensionRange& a, DescriptorProto_ExtensionRange& b) { a.Swap(&b); }
inline void Swap(DescriptorProto_ExtensionRange* other) {
if (other == this) return;
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() != nullptr && GetArena() == other->GetArena()) {
#else // PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() == other->GetArena()) {
#endif // !PROTOBUF_FORCE_COPY_IN_SWAP
if (::google::protobuf::internal::CanUseInternalSwap(GetArena(), other->GetArena())) {
InternalSwap(other);
} else {
::google::protobuf::internal::GenericSwap(this, other);
@ -10257,11 +10145,7 @@ class PROTOBUF_EXPORT ServiceDescriptorProto final
friend void swap(ServiceDescriptorProto& a, ServiceDescriptorProto& b) { a.Swap(&b); }
inline void Swap(ServiceDescriptorProto* other) {
if (other == this) return;
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() != nullptr && GetArena() == other->GetArena()) {
#else // PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() == other->GetArena()) {
#endif // !PROTOBUF_FORCE_COPY_IN_SWAP
if (::google::protobuf::internal::CanUseInternalSwap(GetArena(), other->GetArena())) {
InternalSwap(other);
} else {
::google::protobuf::internal::GenericSwap(this, other);
@ -10498,11 +10382,7 @@ class PROTOBUF_EXPORT EnumDescriptorProto final
friend void swap(EnumDescriptorProto& a, EnumDescriptorProto& b) { a.Swap(&b); }
inline void Swap(EnumDescriptorProto* other) {
if (other == this) return;
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() != nullptr && GetArena() == other->GetArena()) {
#else // PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() == other->GetArena()) {
#endif // !PROTOBUF_FORCE_COPY_IN_SWAP
if (::google::protobuf::internal::CanUseInternalSwap(GetArena(), other->GetArena())) {
InternalSwap(other);
} else {
::google::protobuf::internal::GenericSwap(this, other);
@ -10783,11 +10663,7 @@ class PROTOBUF_EXPORT DescriptorProto final
friend void swap(DescriptorProto& a, DescriptorProto& b) { a.Swap(&b); }
inline void Swap(DescriptorProto* other) {
if (other == this) return;
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() != nullptr && GetArena() == other->GetArena()) {
#else // PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() == other->GetArena()) {
#endif // !PROTOBUF_FORCE_COPY_IN_SWAP
if (::google::protobuf::internal::CanUseInternalSwap(GetArena(), other->GetArena())) {
InternalSwap(other);
} else {
::google::protobuf::internal::GenericSwap(this, other);
@ -11164,11 +11040,7 @@ class PROTOBUF_EXPORT FileDescriptorProto final
friend void swap(FileDescriptorProto& a, FileDescriptorProto& b) { a.Swap(&b); }
inline void Swap(FileDescriptorProto* other) {
if (other == this) return;
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() != nullptr && GetArena() == other->GetArena()) {
#else // PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() == other->GetArena()) {
#endif // !PROTOBUF_FORCE_COPY_IN_SWAP
if (::google::protobuf::internal::CanUseInternalSwap(GetArena(), other->GetArena())) {
InternalSwap(other);
} else {
::google::protobuf::internal::GenericSwap(this, other);
@ -11594,11 +11466,7 @@ class PROTOBUF_EXPORT FileDescriptorSet final
friend void swap(FileDescriptorSet& a, FileDescriptorSet& b) { a.Swap(&b); }
inline void Swap(FileDescriptorSet* other) {
if (other == this) return;
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() != nullptr && GetArena() == other->GetArena()) {
#else // PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() == other->GetArena()) {
#endif // !PROTOBUF_FORCE_COPY_IN_SWAP
if (::google::protobuf::internal::CanUseInternalSwap(GetArena(), other->GetArena())) {
InternalSwap(other);
} else {
::google::protobuf::internal::GenericSwap(this, other);

@ -1097,11 +1097,7 @@ void ExtensionSet::InternalExtensionMergeFrom(const MessageLite* extendee,
}
void ExtensionSet::Swap(const MessageLite* extendee, ExtensionSet* other) {
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
if (arena_ != nullptr && arena_ == other->arena_) {
#else // PROTOBUF_FORCE_COPY_IN_SWAP
if (arena_ == other->arena_) {
#endif // !PROTOBUF_FORCE_COPY_IN_SWAP
if (internal::CanUseInternalSwap(arena_, other->arena_)) {
InternalSwap(other);
} else {
// TODO: We maybe able to optimize a case where we are

@ -779,11 +779,7 @@ void SwapFieldHelper::SwapMessage(const Reflection* r, Message* lhs,
if (*lhs_sub == *rhs_sub) return;
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
if (lhs_arena != nullptr && lhs_arena == rhs_arena) {
#else // PROTOBUF_FORCE_COPY_IN_SWAP
if (lhs_arena == rhs_arena) {
#endif // !PROTOBUF_FORCE_COPY_IN_SWAP
if (internal::CanUseInternalSwap(lhs_arena, rhs_arena)) {
std::swap(*lhs_sub, *rhs_sub);
return;
}
@ -1084,11 +1080,7 @@ void Reflection::Swap(Message* lhs, Message* rhs) const {
// Check that both messages are in the same arena (or both on the heap). We
// need to copy all data if not, due to ownership semantics.
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
if (lhs_arena == nullptr || lhs_arena != rhs_arena) {
#else // PROTOBUF_FORCE_COPY_IN_SWAP
if (lhs_arena != rhs_arena) {
#endif // !PROTOBUF_FORCE_COPY_IN_SWAP
if (!internal::CanUseInternalSwap(lhs_arena, rhs_arena)) {
// One of the two is guaranteed to have an arena. Switch things around
// to guarantee that lhs has an arena.
Arena* arena = lhs_arena;
@ -1100,12 +1092,12 @@ void Reflection::Swap(Message* lhs, Message* rhs) const {
Message* temp = lhs->New(arena);
temp->MergeFrom(*rhs);
rhs->CopyFrom(*lhs);
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
lhs->CopyFrom(*temp);
if (arena == nullptr) delete temp;
#else // PROTOBUF_FORCE_COPY_IN_SWAP
Swap(lhs, temp);
#endif // !PROTOBUF_FORCE_COPY_IN_SWAP
if (internal::DebugHardenForceCopyInSwap()) {
lhs->CopyFrom(*temp);
if (arena == nullptr) delete temp;
} else {
Swap(lhs, temp);
}
return;
}

@ -27,11 +27,12 @@
#include "google/protobuf/io/zero_copy_stream_impl_lite.h"
#include "google/protobuf/message_lite.h"
#include "google/protobuf/parse_context.h"
#include "google/protobuf/port.h"
#include "google/protobuf/repeated_ptr_field.h"
// Must be included last.
// clang-format off
#include "google/protobuf/port_def.inc"
#include "google/protobuf/repeated_ptr_field.h"
// clang-format on
namespace google {
@ -222,13 +223,13 @@ void LazyRepeatedPtrField::Swap(LazyRepeatedPtrField* lhs, Arena* lhs_arena,
if (cleanup_old) old_unparsed.Destroy();
};
static auto take_ownership = [](LazyRepeatedPtrField* f, Arena* arena) {
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
reallocate(f, arena, true);
#else
arena->Own(reinterpret_cast<RepeatedPtrField<MessageLite>*>(
f->raw_.load(std::memory_order_relaxed).mutable_value()));
f->unparsed_.TransferHeapOwnershipToArena(arena);
#endif
if (internal::DebugHardenForceCopyInSwap()) {
reallocate(f, arena, true);
} else {
arena->Own(reinterpret_cast<RepeatedPtrField<MessageLite>*>(
f->raw_.load(std::memory_order_relaxed).mutable_value()));
f->unparsed_.TransferHeapOwnershipToArena(arena);
}
};
using std::swap; // Enable ADL with fallback
@ -239,12 +240,10 @@ void LazyRepeatedPtrField::Swap(LazyRepeatedPtrField* lhs, Arena* lhs_arena,
// arena is actually on the opposite message. Now we straighten out our
// ownership by forcing reallocations/ownership changes as needed.
if (lhs_arena == rhs_arena) {
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
if (lhs_arena == nullptr) {
if (internal::DebugHardenForceCopyInSwap() && lhs_arena == nullptr) {
reallocate(lhs, lhs_arena, true);
reallocate(rhs, rhs_arena, true);
}
#endif
} else {
if (lhs_arena == nullptr) {
take_ownership(rhs, rhs_arena);

@ -276,6 +276,10 @@ constexpr bool DebugHardenForceCopyInRelease() {
return false;
}
constexpr bool DebugHardenForceCopyInSwap() {
return false;
}
// Returns true if pointers are 8B aligned, leaving least significant 3 bits
// available.
inline constexpr bool PtrIsAtLeast8BAligned() { return alignof(void*) >= 8; }

@ -398,10 +398,6 @@ static_assert(PROTOBUF_ABSL_MIN(20230125, 3),
#define PROTOBUF_RESTRICT
#endif
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
#error PROTOBUF_FORCE_COPY_IN_SWAP was previously defined
#endif
#ifdef PROTOBUF_FORCE_COPY_IN_MOVE
#error PROTOBUF_FORCE_COPY_IN_MOVE was previously defined
#endif

@ -41,7 +41,6 @@
#undef PROTOC_EXPORT
#undef PROTOBUF_NODISCARD
#undef PROTOBUF_RESTRICT
#undef PROTOBUF_FORCE_COPY_IN_SWAP
#undef PROTOBUF_FORCE_COPY_IN_MOVE
#undef PROTOBUF_FUZZ_MESSAGE_SPACE_USED_LONG
#undef PROTOBUF_FORCE_COPY_DEFAULT_STRING

@ -419,10 +419,10 @@ void ReflectionOps::FindInitializationErrors(const Message& message,
}
void GenericSwap(Message* lhs, Message* rhs) {
#ifndef PROTOBUF_FORCE_COPY_IN_SWAP
ABSL_DCHECK(lhs->GetArena() != rhs->GetArena());
ABSL_DCHECK(lhs->GetArena() != nullptr || rhs->GetArena() != nullptr);
#endif // !PROTOBUF_FORCE_COPY_IN_SWAP
if (!internal::DebugHardenForceCopyInSwap()) {
ABSL_DCHECK(lhs->GetArena() != rhs->GetArena());
ABSL_DCHECK(lhs->GetArena() != nullptr || rhs->GetArena() != nullptr);
}
// At least one of these must have an arena, so make `rhs` point to it.
Arena* arena = rhs->GetArena();
if (arena == nullptr) {
@ -436,13 +436,13 @@ void GenericSwap(Message* lhs, Message* rhs) {
tmp->CheckTypeAndMergeFrom(*lhs);
lhs->Clear();
lhs->CheckTypeAndMergeFrom(*rhs);
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
rhs->Clear();
rhs->CheckTypeAndMergeFrom(*tmp);
if (arena == nullptr) delete tmp;
#else // PROTOBUF_FORCE_COPY_IN_SWAP
rhs->GetReflection()->Swap(tmp, rhs);
#endif // !PROTOBUF_FORCE_COPY_IN_SWAP
if (internal::DebugHardenForceCopyInSwap()) {
rhs->Clear();
rhs->CheckTypeAndMergeFrom(*tmp);
if (arena == nullptr) delete tmp;
} else {
rhs->GetReflection()->Swap(tmp, rhs);
}
}
} // namespace internal

@ -1074,11 +1074,7 @@ void RepeatedField<Element>::Swap(RepeatedField* other) {
if (this == other) return;
Arena* arena = GetArena();
Arena* other_arena = other->GetArena();
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
if (arena != nullptr && arena == other_arena) {
#else // PROTOBUF_FORCE_COPY_IN_SWAP
if (arena == other_arena) {
#endif // !PROTOBUF_FORCE_COPY_IN_SWAP
if (internal::CanUseInternalSwap(arena, other_arena)) {
InternalSwap(other);
} else {
RepeatedField<Element> temp(other_arena);

@ -355,12 +355,7 @@ class PROTOBUF_EXPORT RepeatedPtrFieldBase {
template <typename TypeHandler>
PROTOBUF_NDEBUG_INLINE void Swap(RepeatedPtrFieldBase* other) {
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() != nullptr && GetArena() == other->GetArena())
#else // PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() == other->GetArena())
#endif // !PROTOBUF_FORCE_COPY_IN_SWAP
{
if (internal::CanUseInternalSwap(GetArena(), other->GetArena())) {
InternalSwap(other);
} else {
SwapFallback<TypeHandler>(other);
@ -512,11 +507,7 @@ class PROTOBUF_EXPORT RepeatedPtrFieldBase {
template <typename TypeHandler>
PROTOBUF_NOINLINE void SwapFallback(RepeatedPtrFieldBase* other) {
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
ABSL_DCHECK(GetArena() == nullptr || other->GetArena() != GetArena());
#else // PROTOBUF_FORCE_COPY_IN_SWAP
ABSL_DCHECK(other->GetArena() != GetArena());
#endif // !PROTOBUF_FORCE_COPY_IN_SWAP
ABSL_DCHECK(!internal::CanUseInternalSwap(GetArena(), other->GetArena()));
// Copy semantics in this case. We try to improve efficiency by placing the
// temporary on |other|'s arena so that messages are copied twice rather

Loading…
Cancel
Save