Skip to content

Commit

Permalink
Change AllocateInternal to allow us to easy specialize for std::strin…
Browse files Browse the repository at this point in the history
…g or absl::Cord

PiperOrigin-RevId: 504441955
  • Loading branch information
martijnvels authored and Copybara-Service committed Jan 25, 2023
1 parent 9416dca commit 233aa47
Showing 1 changed file with 9 additions and 16 deletions.
25 changes: 9 additions & 16 deletions src/google/protobuf/arena.h
Original file line number Diff line number Diff line change
Expand Up @@ -274,11 +274,7 @@ class PROTOBUF_EXPORT PROTOBUF_ALIGNAS(8) Arena final {
if (PROTOBUF_PREDICT_FALSE(arena == nullptr)) {
return new T(std::forward<Args>(args)...);
}
auto destructor =
internal::ObjectDestructor<std::is_trivially_destructible<T>::value,
T>::destructor;
return new (arena->AllocateInternal(sizeof(T), alignof(T), destructor))
T(std::forward<Args>(args)...);
return new (arena->AllocateInternal<T>()) T(std::forward<Args>(args)...);
}

// API to delete any objects not on an arena. This can be used to safely
Expand Down Expand Up @@ -565,13 +561,13 @@ class PROTOBUF_EXPORT PROTOBUF_ALIGNAS(8) Arena final {
}
}

PROTOBUF_NDEBUG_INLINE void* AllocateInternal(size_t size, size_t align,
void (*destructor)(void*)) {
// Monitor allocation if needed.
if (destructor == nullptr) {
return AllocateAligned(size, align);
template <typename T, bool trivial = std::is_trivially_destructible<T>::value>
PROTOBUF_NDEBUG_INLINE void* AllocateInternal() {
if (trivial) {
return AllocateAligned(sizeof(T), alignof(T));
} else {
return AllocateAlignedWithCleanup(size, align, destructor);
constexpr auto dtor = &internal::cleanup::arena_destruct_object<T>;
return AllocateAlignedWithCleanup(sizeof(T), alignof(T), dtor);
}
}

Expand Down Expand Up @@ -604,11 +600,8 @@ class PROTOBUF_EXPORT PROTOBUF_ALIGNAS(8) Arena final {
template <typename T, typename... Args>
PROTOBUF_NDEBUG_INLINE T* DoCreateMessage(Args&&... args) {
return InternalHelper<T>::Construct(
AllocateInternal(sizeof(T), alignof(T),
internal::ObjectDestructor<
InternalHelper<T>::is_destructor_skippable::value,
T>::destructor),
this, std::forward<Args>(args)...);
AllocateInternal<T, is_destructor_skippable<T>::value>(), this,
std::forward<Args>(args)...);
}

// CreateInArenaStorage is used to implement map field. Without it,
Expand Down

0 comments on commit 233aa47

Please sign in to comment.