Skip to content

Commit

Permalink
Use GetArena() instead of GetOwningArena() #5.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 573864426
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Oct 16, 2023
1 parent 5e2f4fa commit 919c909
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 115 deletions.
13 changes: 10 additions & 3 deletions src/google/protobuf/arena.h
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ class PROTOBUF_EXPORT PROTOBUF_ALIGNAS(8) Arena final {
// again.
template <typename T>
PROTOBUF_ALWAYS_INLINE static void Destroy(T* obj) {
if (InternalGetOwningArena(obj) == nullptr) delete obj;
if (InternalGetArena(obj) == nullptr) delete obj;
}

// Allocates memory with the specific size and alignment.
Expand Down Expand Up @@ -389,7 +389,8 @@ class PROTOBUF_EXPORT PROTOBUF_ALIGNAS(8) Arena final {
// mutually exclusive fashion, we use implicit conversions to base classes
// to force an explicit ranking for our preferences. The lowest ranked
// version that compiles will be accepted.
struct Rank1 {};
struct Rank2 {};
struct Rank1 : Rank2 {};
struct Rank0 : Rank1 {};

static Arena* GetOwningArena(const T* p) {
Expand All @@ -403,7 +404,13 @@ class PROTOBUF_EXPORT PROTOBUF_ALIGNAS(8) Arena final {
}

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

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

Expand Down
10 changes: 0 additions & 10 deletions src/google/protobuf/arena_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -561,16 +561,6 @@ TEST(ArenaTest, UnsafeArenaSwap) {
TestUtil::ExpectAllFieldsSet(*message2);
}

TEST(ArenaTest, GetOwningArena) {
Arena arena;
auto* m1 = Arena::CreateMessage<TestAllTypes>(&arena);
EXPECT_EQ(Arena::InternalGetOwningArena(m1), &arena);
EXPECT_EQ(&arena, Arena::InternalGetOwningArena(
m1->mutable_repeated_foreign_message()));
EXPECT_EQ(&arena,
Arena::InternalGetOwningArena(m1->mutable_repeated_int32()));
}

TEST(ArenaTest, SwapBetweenArenasUsingReflection) {
Arena arena1;
TestAllTypes* arena1_message = Arena::CreateMessage<TestAllTypes>(&arena1);
Expand Down
24 changes: 10 additions & 14 deletions src/google/protobuf/compiler/cpp/field_generators/message_field.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ std::vector<Sub> Vars(const FieldDescriptor* field, const Options& opts,
? default_ptr
: absl::Substitute("reinterpret_cast<const $0*>($1)",
base, default_ptr)},
{"base_cast",
absl::Substitute("reinterpret_cast<$0*>",
!is_foreign && !weak ? qualified_type : base)},
{"base_cast", !is_foreign && !weak
? ""
: absl::Substitute("reinterpret_cast<$0*>", base)},
Sub{"weak_cast",
!weak ? "" : absl::Substitute("reinterpret_cast<$0*>", base)}
.ConditionalFunctionCall(),
Expand Down Expand Up @@ -431,19 +431,15 @@ void SingularMessage::GenerateSwappingCode(io::Printer* p) const {
}

void SingularMessage::GenerateDestructorCode(io::Printer* p) const {
if (opts_->opensource_runtime) {
// TODO Remove this when we don't need to destruct default
// instances. In google3 a default instance will never get deleted so we
// don't need to worry about that but in opensource protobuf default
// instances are deleted in shutdown process and we need to take special
// care when handling them.
p->Emit("if (this != internal_default_instance()) ");
}
if (should_split()) {
p->Emit("delete $cached_split_ptr$->$name$_;\n");
return;
p->Emit(R"cc(
delete $cached_split_ptr$->$name$_;
)cc");
} else {
p->Emit(R"cc(
delete $field_$;
)cc");
}
p->Emit("delete $field_$;\n");
}

using internal::cpp::HasHasbit;
Expand Down
4 changes: 2 additions & 2 deletions src/google/protobuf/compiler/plugin.pb.cc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/google/protobuf/compiler/plugin.pb.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 20 additions & 20 deletions src/google/protobuf/descriptor.pb.cc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 919c909

Please sign in to comment.