Skip to content

Commit

Permalink
Breaking change: Make RepeatedField::GetArena non-const in order to s…
Browse files Browse the repository at this point in the history
…upport split RepeatedFields.

PiperOrigin-RevId: 503205991
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Jan 19, 2023
1 parent 58f6216 commit 514c9a8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
16 changes: 8 additions & 8 deletions src/google/protobuf/arena.h
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ class PROTOBUF_EXPORT PROTOBUF_ALIGNAS(8) Arena final {
// time. Note that we can often devirtualize calls to `value->GetArena()` so
// usually calling this method is unnecessary.
template <typename T>
PROTOBUF_ALWAYS_INLINE static Arena* GetArena(const T* value) {
PROTOBUF_ALWAYS_INLINE static Arena* GetArena(T* value) {
return GetArenaInternal(value);
}

Expand Down Expand Up @@ -432,31 +432,31 @@ class PROTOBUF_EXPORT PROTOBUF_ALIGNAS(8) Arena final {

static void InternalSwap(T* a, T* b) { a->InternalSwap(b); }

static Arena* GetArenaForAllocation(const T* p) {
static Arena* GetArenaForAllocation(T* p) {
return GetArenaForAllocation(Rank0{}, p);
}

static Arena* GetArena(const T* p) {
static Arena* GetArena(T* p) {
// Rather than replicate probing for `GetArena` with fallback to nullptr,
// we borrow the implementation of `GetArenaForAllocation` but skip
// `Rank0` which probes for `GetArenaForAllocation`.
return GetArenaForAllocation(Rank1{}, p);
}

template <typename U>
static auto GetArenaForAllocation(Rank0, const U* p)
static auto GetArenaForAllocation(Rank0, U* p)
-> EnableIfArena<decltype(p->GetArenaForAllocation())> {
return p->GetArenaForAllocation();
}

template <typename U>
static auto GetArenaForAllocation(Rank1, const U* p)
static auto GetArenaForAllocation(Rank1, U* p)
-> EnableIfArena<decltype(p->GetArena())> {
return p->GetArena();
}

template <typename U>
static Arena* GetArenaForAllocation(Rank2, const U*) {
static Arena* GetArenaForAllocation(Rank2, U*) {
return nullptr;
}

Expand Down Expand Up @@ -506,7 +506,7 @@ class PROTOBUF_EXPORT PROTOBUF_ALIGNAS(8) Arena final {
// Provides access to protected GetArenaForAllocation to generated messages.
// For internal use only.
template <typename T>
static Arena* InternalGetArenaForAllocation(const T* p) {
static Arena* InternalGetArenaForAllocation(T* p) {
return InternalHelper<T>::GetArenaForAllocation(p);
}

Expand Down Expand Up @@ -650,7 +650,7 @@ class PROTOBUF_EXPORT PROTOBUF_ALIGNAS(8) Arena final {
// InternalArenaConstructable_ tags can be associated with an arena, and such
// objects must implement a GetArena() method.
template <typename T>
PROTOBUF_ALWAYS_INLINE static Arena* GetArenaInternal(const T* value) {
PROTOBUF_ALWAYS_INLINE static Arena* GetArenaInternal(T* value) {
return InternalHelper<T>::GetArena(value);
}

Expand Down
4 changes: 3 additions & 1 deletion src/google/protobuf/repeated_field.h
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,9 @@ class RepeatedField final {
iterator erase(const_iterator first, const_iterator last);

// Gets the Arena on which this RepeatedField stores its elements.
inline Arena* GetArena() const { return GetOwningArena(); }
// Note: this can be inaccurate for split default fields so we make this
// function non-const.
inline Arena* GetArena() { return GetOwningArena(); }

// For internal use only.
//
Expand Down

0 comments on commit 514c9a8

Please sign in to comment.