From e767c39d8ce8f5c148d01cc00e576d37ddd5bdd2 Mon Sep 17 00:00:00 2001 From: KRM7 <70973547+KRM7@users.noreply.github.com> Date: Sat, 10 Feb 2024 15:53:25 +0100 Subject: [PATCH] add stack_buffer_size method --- src/small_unique_ptr.hpp | 7 +++++++ test/small_unique_ptr.cpp | 20 +++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/small_unique_ptr.hpp b/src/small_unique_ptr.hpp index cea0f29..f31cb09 100644 --- a/src/small_unique_ptr.hpp +++ b/src/small_unique_ptr.hpp @@ -351,6 +351,13 @@ class small_unique_ptr : private detail::small_unique_ptr_base 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) return 0; + else return detail::buffer_size_v; + } + [[nodiscard]] constexpr pointer get() const noexcept { diff --git a/test/small_unique_ptr.cpp b/test/small_unique_ptr.cpp index 71485be..724d750 100644 --- a/test/small_unique_ptr.cpp +++ b/test/small_unique_ptr.cpp @@ -108,8 +108,23 @@ TEST_CASE("object_size", "[small_unique_ptr]") STATIC_REQUIRE(alignof(small_unique_ptr) == detail::small_ptr_size); STATIC_REQUIRE(alignof(small_unique_ptr) == alignof(void*)); +} + +TEST_CASE("stack_buffer_size", "[small_unique_ptr]") +{ + STATIC_REQUIRE(small_unique_ptr::stack_buffer_size() == sizeof(SmallPOD)); + STATIC_REQUIRE(small_unique_ptr::stack_buffer_size() == 0); + + STATIC_REQUIRE(small_unique_ptr::stack_buffer_size() == 0); + STATIC_REQUIRE(small_unique_ptr::stack_buffer_size() == 0); - STATIC_REQUIRE(detail::buffer_size_v > detail::buffer_size_v); + STATIC_REQUIRE(small_unique_ptr::stack_buffer_size() > small_unique_ptr::stack_buffer_size()); +} + +TEST_CASE("stack_buffer_size_archdep", "[small_unique_ptr][!mayfail]") +{ + REQUIRE(small_unique_ptr::stack_buffer_size() == 48); + REQUIRE(small_unique_ptr::stack_buffer_size() == 56); } TEMPLATE_TEST_CASE("construction", "[small_unique_ptr]", SmallPOD, LargePOD, Base, SmallDerived, LargeDerived, BaseIntrusive, SmallIntrusive, LargeIntrusive) @@ -171,6 +186,9 @@ TEST_CASE("is_stack_allocated", "[small_unique_ptr]") small_unique_ptr p6 = make_unique_small(); REQUIRE(p5.is_stack_allocated()); REQUIRE(!p6.is_stack_allocated()); + + small_unique_ptr np(nullptr); + REQUIRE(!np.is_stack_allocated()); } TEST_CASE("comparisons", "[small_unique_ptr]")