Skip to content

Commit

Permalink
add stack_buffer_size method
Browse files Browse the repository at this point in the history
  • Loading branch information
KRM7 committed Feb 10, 2024
1 parent ac1cc1d commit e767c39
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/small_unique_ptr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,13 @@ class small_unique_ptr : private detail::small_unique_ptr_base<T>
return small_unique_ptr::small_unique_ptr_base::is_stack_allocated();
}

[[nodiscard]]
static constexpr std::size_t stack_buffer_size() noexcept
{
if constexpr (detail::is_always_heap_allocated_v<T>) return 0;
else return detail::buffer_size_v<T>;
}

[[nodiscard]]
constexpr pointer get() const noexcept
{
Expand Down
20 changes: 19 additions & 1 deletion test/small_unique_ptr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,23 @@ TEST_CASE("object_size", "[small_unique_ptr]")

STATIC_REQUIRE(alignof(small_unique_ptr<SmallIntrusive>) == detail::small_ptr_size);
STATIC_REQUIRE(alignof(small_unique_ptr<LargeIntrusive>) == alignof(void*));
}

TEST_CASE("stack_buffer_size", "[small_unique_ptr]")
{
STATIC_REQUIRE(small_unique_ptr<SmallPOD>::stack_buffer_size() == sizeof(SmallPOD));
STATIC_REQUIRE(small_unique_ptr<LargePOD>::stack_buffer_size() == 0);

STATIC_REQUIRE(small_unique_ptr<LargeDerived>::stack_buffer_size() == 0);
STATIC_REQUIRE(small_unique_ptr<LargeIntrusive>::stack_buffer_size() == 0);

STATIC_REQUIRE(detail::buffer_size_v<BaseIntrusive> > detail::buffer_size_v<Base>);
STATIC_REQUIRE(small_unique_ptr<SmallIntrusive>::stack_buffer_size() > small_unique_ptr<SmallDerived>::stack_buffer_size());
}

TEST_CASE("stack_buffer_size_archdep", "[small_unique_ptr][!mayfail]")
{
REQUIRE(small_unique_ptr<SmallDerived>::stack_buffer_size() == 48);
REQUIRE(small_unique_ptr<SmallIntrusive>::stack_buffer_size() == 56);
}

TEMPLATE_TEST_CASE("construction", "[small_unique_ptr]", SmallPOD, LargePOD, Base, SmallDerived, LargeDerived, BaseIntrusive, SmallIntrusive, LargeIntrusive)
Expand Down Expand Up @@ -171,6 +186,9 @@ TEST_CASE("is_stack_allocated", "[small_unique_ptr]")
small_unique_ptr<LargePOD> p6 = make_unique_small<LargePOD>();
REQUIRE(p5.is_stack_allocated());
REQUIRE(!p6.is_stack_allocated());

small_unique_ptr<SmallPOD> np(nullptr);
REQUIRE(!np.is_stack_allocated());
}

TEST_CASE("comparisons", "[small_unique_ptr]")
Expand Down

0 comments on commit e767c39

Please sign in to comment.