Skip to content
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
42 changes: 42 additions & 0 deletions libcudacxx/include/cuda/__memory_resource/shared_resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,48 @@ struct shared_resource : ::cuda::mr::__copy_default_queries<_Resource>
__left.swap(__right);
}

//! @brief Returns a reference to the stored resource.
//! @return A reference to the stored resource.
[[nodiscard]] _Resource& get() noexcept
{
return __control_block->__resource;
}

//! @brief Returns a const reference to the stored resource.
//! @return A const reference to the stored resource.
[[nodiscard]] const _Resource& get() const noexcept
{
return __control_block->__resource;
}

//! @brief Returns a pointer to the stored resource.
//! @return A pointer to the stored resource.
[[nodiscard]] _Resource* operator->() noexcept
{
return &__control_block->__resource;
}

//! @brief Returns a const pointer to the stored resource.
//! @return A const pointer to the stored resource.
[[nodiscard]] const _Resource* operator->() const noexcept
{
return &__control_block->__resource;
}

//! @brief Returns a reference to the stored resource.
//! @return A reference to the stored resource.
[[nodiscard]] _Resource& operator*() noexcept
{
return __control_block->__resource;
}

//! @brief Returns a const reference to the stored resource.
//! @return A const reference to the stored resource.
[[nodiscard]] const _Resource& operator*() const noexcept
{
return __control_block->__resource;
}

//! @brief Allocate memory of size at least \p __bytes using the stored resource.
//! @param __bytes The size in bytes of the allocation.
//! @param __alignment The requested alignment of the allocation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,42 @@ TEMPLATE_TEST_CASE_METHOD(test_fixture, "shared_resource", "[container][resource
// Reset the counters:
this->counts = Counts();

SECTION("get, operator->, and operator*")
{
Counts expected{};
CHECK(this->counts == expected);
{
cuda::mr::shared_resource mr{cuda::std::in_place_type<TestResource>, 42, this};
++expected.object_count;
CHECK(this->counts == expected);

// Test get()
TestResource& ref = mr.get();
CHECK(ref.data == 42);

// Test operator->
CHECK(mr->data == 42);

// Test operator*
TestResource& deref = *mr;
CHECK(deref.data == 42);

// Test const versions
const auto& cmr = mr;
const TestResource& cref = cmr.get();
CHECK(cref.data == 42);
CHECK(cmr->data == 42);
const TestResource& cderef = *cmr;
CHECK(cderef.data == 42);
}

--expected.object_count;
CHECK(this->counts == expected);
}

// Reset the counters:
this->counts = Counts();

SECTION("allocate_sync and deallocate_sync")
{
Counts expected{};
Expand Down
Loading