|
|
|
@ -251,10 +251,12 @@ class PROTOBUF_EXPORT RepeatedPtrFieldBase { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Must be called from destructor.
|
|
|
|
|
//
|
|
|
|
|
// Pre-condition: NeedsDestroy() returns true.
|
|
|
|
|
template <typename TypeHandler> |
|
|
|
|
void Destroy() { |
|
|
|
|
ABSL_DCHECK(NeedsDestroy()); |
|
|
|
|
using H = CommonHandler<TypeHandler>; |
|
|
|
|
if (arena_ != nullptr) return; |
|
|
|
|
int n = allocated_size(); |
|
|
|
|
void** elems = elements(); |
|
|
|
|
for (int i = 0; i < n; i++) { |
|
|
|
@ -267,7 +269,10 @@ class PROTOBUF_EXPORT RepeatedPtrFieldBase { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool NeedsDestroy() const { |
|
|
|
|
return tagged_rep_or_elem_ != nullptr && arena_ == nullptr; |
|
|
|
|
// TODO: arena check is redundant once all `RepeatedPtrField`s
|
|
|
|
|
// with non-null arena are owned by the arena.
|
|
|
|
|
return tagged_rep_or_elem_ != nullptr && |
|
|
|
|
PROTOBUF_PREDICT_FALSE(arena_ == nullptr); |
|
|
|
|
} |
|
|
|
|
void DestroyProtos(); // implemented in the cc file
|
|
|
|
|
|
|
|
|
@ -630,7 +635,9 @@ class PROTOBUF_EXPORT RepeatedPtrFieldBase { |
|
|
|
|
} |
|
|
|
|
this->CopyFrom<TypeHandler>(*other); |
|
|
|
|
other->InternalSwap(&temp); |
|
|
|
|
temp.Destroy<TypeHandler>(); // Frees rep_ if `other` had no arena.
|
|
|
|
|
if (temp.NeedsDestroy()) { |
|
|
|
|
temp.Destroy<TypeHandler>(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Gets the Arena on which this RepeatedPtrField stores its elements.
|
|
|
|
@ -1374,12 +1381,13 @@ inline RepeatedPtrField<Element>::RepeatedPtrField(Iter begin, Iter end) { |
|
|
|
|
template <typename Element> |
|
|
|
|
RepeatedPtrField<Element>::~RepeatedPtrField() { |
|
|
|
|
StaticValidityCheck(); |
|
|
|
|
if (!NeedsDestroy()) return; |
|
|
|
|
#ifdef __cpp_if_constexpr |
|
|
|
|
if constexpr (std::is_base_of<MessageLite, Element>::value) { |
|
|
|
|
#else |
|
|
|
|
if (std::is_base_of<MessageLite, Element>::value) { |
|
|
|
|
#endif |
|
|
|
|
if (NeedsDestroy()) DestroyProtos(); |
|
|
|
|
DestroyProtos(); |
|
|
|
|
} else { |
|
|
|
|
Destroy<TypeHandler>(); |
|
|
|
|
} |
|
|
|
|