From 6acc02eb1f8f0c6a7b07a7f6dfff981ef2365b8b Mon Sep 17 00:00:00 2001 From: Miguel Company Date: Wed, 24 Dec 2025 11:39:41 +0100 Subject: [PATCH 1/3] Add new macro for assertion with return value. Signed-off-by: Miguel Company --- include/foonathan/memory/detail/assert.hpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/include/foonathan/memory/detail/assert.hpp b/include/foonathan/memory/detail/assert.hpp index 7317503..895d4b9 100644 --- a/include/foonathan/memory/detail/assert.hpp +++ b/include/foonathan/memory/detail/assert.hpp @@ -37,6 +37,12 @@ namespace foonathan __FILE__, __LINE__, __func__), \ true)) +#define FOONATHAN_MEMORY_ASSERT_RETURN(Expr, Ret) \ + ((Expr) ? (Ret) \ + : (detail::handle_failed_assert("Assertion \"" #Expr "\" failed", __FILE__, \ + __LINE__, __func__), \ + (Ret))) + #define FOONATHAN_MEMORY_UNREACHABLE(Msg) \ detail::handle_failed_assert("Unreachable code reached: " Msg, __FILE__, __LINE__, __func__) @@ -45,6 +51,7 @@ namespace foonathan #elif !defined(FOONATHAN_MEMORY_ASSERT) #define FOONATHAN_MEMORY_ASSERT(Expr) #define FOONATHAN_MEMORY_ASSERT_MSG(Expr, Msg) +#define FOONATHAN_MEMORY_ASSERT_RETURN(Expr, Ret) (Ret) #define FOONATHAN_MEMORY_UNREACHABLE(Msg) std::abort() #define FOONATHAN_MEMORY_WARNING(Msg) #endif From 9b58607b037d372036636b513002168c76383936 Mon Sep 17 00:00:00 2001 From: Miguel Company Date: Wed, 24 Dec 2025 11:49:59 +0100 Subject: [PATCH 2/3] Use new macro in constexpr method. Signed-off-by: Miguel Company --- include/foonathan/memory/detail/align.hpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/include/foonathan/memory/detail/align.hpp b/include/foonathan/memory/detail/align.hpp index 8fcf426..ea4e1d2 100644 --- a/include/foonathan/memory/detail/align.hpp +++ b/include/foonathan/memory/detail/align.hpp @@ -24,8 +24,9 @@ namespace foonathan constexpr std::size_t round_up_to_multiple_of_alignment(std::size_t size, std::size_t alignment) noexcept { - FOONATHAN_MEMORY_ASSERT(is_valid_alignment(alignment)); - return (size + alignment - 1) & ~(alignment - 1); + return FOONATHAN_MEMORY_ASSERT_RETURN( + is_valid_alignment(alignment), + (size + alignment - 1) & ~(alignment - 1)); } // returns the offset needed to align ptr for given alignment From 1de8562aa2b8241ca3d562ddc6d1e9a5cfc07e94 Mon Sep 17 00:00:00 2001 From: Miguel Company Date: Wed, 24 Dec 2025 13:47:21 +0100 Subject: [PATCH 3/3] Avoid using `__func__` Signed-off-by: Miguel Company --- include/foonathan/memory/detail/assert.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/foonathan/memory/detail/assert.hpp b/include/foonathan/memory/detail/assert.hpp index 895d4b9..4786d78 100644 --- a/include/foonathan/memory/detail/assert.hpp +++ b/include/foonathan/memory/detail/assert.hpp @@ -40,7 +40,7 @@ namespace foonathan #define FOONATHAN_MEMORY_ASSERT_RETURN(Expr, Ret) \ ((Expr) ? (Ret) \ : (detail::handle_failed_assert("Assertion \"" #Expr "\" failed", __FILE__, \ - __LINE__, __func__), \ + __LINE__, "some constexpr"), \ (Ret))) #define FOONATHAN_MEMORY_UNREACHABLE(Msg) \