Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add aliasing constructor to Shared #602

Merged
1 commit merged into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions common/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,27 @@ class ABSL_ATTRIBUTE_TRIVIAL_ABI Shared final {
// NOLINTNEXTLINE(google-explicit-constructor)
explicit Shared(SharedView<U> other);

// An aliasing constructor. The resulting `Shared` shares ownership
// information with `alias`, but holds an unmanaged pointer to `T`.
//
// Usage:
// Shared<Object> object;
// Shared<Member> member = Shared<Member>(object, &object->member);
template <typename U>
Shared(const Shared<U>& alias, T* ptr)
: value_(ptr), refcount_(alias.refcount_) {
common_internal::StrongRef(refcount_);
}

// An aliasing constructor. The resulting `Shared` shares ownership
// information with `alias`, but holds an unmanaged pointer to `T`.
template <typename U>
Shared(Shared<U>&& alias, T* ptr) noexcept
: value_(ptr), refcount_(alias.refcount_) {
alias.value_ = nullptr;
alias.refcount_ = nullptr;
}

~Shared() { common_internal::StrongUnref(refcount_); }

Shared& operator=(const Shared& other) {
Expand Down Expand Up @@ -245,6 +266,10 @@ class ABSL_ATTRIBUTE_TRIVIAL_ABI SharedView final {
SharedView(const Shared<U>& other ABSL_ATTRIBUTE_LIFETIME_BOUND) noexcept
: value_(other.value_), refcount_(other.refcount_) {}

template <typename U>
SharedView(SharedView<U> alias, T* ptr)
: value_(ptr), refcount_(alias.refcount_) {}

template <
typename U,
typename = std::enable_if_t<std::conjunction_v<
Expand Down
52 changes: 52 additions & 0 deletions common/memory_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ class Object {
}
}

int member = 0;

private:
bool* deleted_;
};
Expand Down Expand Up @@ -260,6 +262,56 @@ TEST_P(MemoryManagerTest, Shared) {
Finish();
}

TEST_P(MemoryManagerTest, SharedAliasCopy) {
bool deleted = false;
{
auto object = memory_manager().MakeShared<Object>(deleted);
EXPECT_TRUE(object);
EXPECT_FALSE(deleted);
{
auto member = Shared<int>(object, &object->member);
EXPECT_TRUE(object);
EXPECT_FALSE(deleted);
EXPECT_TRUE(member);
}
EXPECT_TRUE(object);
EXPECT_FALSE(deleted);
}
switch (memory_management()) {
case MemoryManagement::kPooling:
EXPECT_FALSE(deleted);
break;
case MemoryManagement::kReferenceCounting:
EXPECT_TRUE(deleted);
break;
}
Finish();
}

TEST_P(MemoryManagerTest, SharedAliasMove) {
bool deleted = false;
{
auto object = memory_manager().MakeShared<Object>(deleted);
EXPECT_TRUE(object);
EXPECT_FALSE(deleted);
{
auto member = Shared<int>(std::move(object), &object->member);
EXPECT_FALSE(object);
EXPECT_FALSE(deleted);
EXPECT_TRUE(member);
}
switch (memory_management()) {
case MemoryManagement::kPooling:
EXPECT_FALSE(deleted);
break;
case MemoryManagement::kReferenceCounting:
EXPECT_TRUE(deleted);
break;
}
}
Finish();
}

TEST_P(MemoryManagerTest, SharedCopyConstruct) {
bool deleted = false;
{
Expand Down