From 1682c6048380f7b2b523ce45218bb91454b58e11 Mon Sep 17 00:00:00 2001 From: Antonio Sanchez Date: Mon, 27 Oct 2025 16:04:41 -0700 Subject: [PATCH] Allow user to configure if `free` is allowed at runtime. Fixes #2983. --- Eigen/src/Core/util/Memory.h | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/Eigen/src/Core/util/Memory.h b/Eigen/src/Core/util/Memory.h index d1f2bf3ed..f8d3c42ec 100644 --- a/Eigen/src/Core/util/Memory.h +++ b/Eigen/src/Core/util/Memory.h @@ -91,6 +91,9 @@ namespace internal { EIGEN_DEVICE_FUNC inline void check_that_malloc_is_allowed() { eigen_assert(false && "heap allocation is forbidden (EIGEN_NO_MALLOC is defined)"); } +EIGEN_DEVICE_FUNC inline void check_that_free_is_allowed() { + eigen_assert(false && "heap deallocation is forbidden (EIGEN_NO_MALLOC is defined)"); +} #elif defined EIGEN_RUNTIME_NO_MALLOC EIGEN_DEVICE_FUNC inline bool is_malloc_allowed_impl(bool update, bool new_value = false) { EIGEN_MALLOC_CHECK_THREAD_LOCAL static bool value = true; @@ -101,10 +104,22 @@ EIGEN_DEVICE_FUNC inline bool is_malloc_allowed() { return is_malloc_allowed_imp EIGEN_DEVICE_FUNC inline bool set_is_malloc_allowed(bool new_value) { return is_malloc_allowed_impl(true, new_value); } EIGEN_DEVICE_FUNC inline void check_that_malloc_is_allowed() { eigen_assert(is_malloc_allowed() && - "heap allocation is forbidden (EIGEN_RUNTIME_NO_MALLOC is defined and g_is_malloc_allowed is false)"); + "heap allocation is forbidden (EIGEN_RUNTIME_NO_MALLOC is defined and set_is_malloc_allowed is false)"); +} +EIGEN_DEVICE_FUNC inline bool is_free_allowed_impl(bool update, bool new_value = false) { + EIGEN_MALLOC_CHECK_THREAD_LOCAL static bool value = true; + if (update == 1) value = new_value; + return value; +} +EIGEN_DEVICE_FUNC inline bool is_free_allowed() { return is_free_allowed_impl(false); } +EIGEN_DEVICE_FUNC inline bool set_is_free_allowed(bool new_value) { return is_free_allowed_impl(true, new_value); } +EIGEN_DEVICE_FUNC inline void check_that_free_is_allowed() { + eigen_assert(is_malloc_allowed() && + "heap deallocation is forbidden (EIGEN_RUNTIME_NO_MALLOC is defined and set_is_free_allowed is false)"); } #else EIGEN_DEVICE_FUNC inline void check_that_malloc_is_allowed() {} +EIGEN_DEVICE_FUNC inline void check_that_free_is_allowed() {} #endif EIGEN_DEVICE_FUNC inline void throw_std_bad_alloc() { @@ -161,7 +176,7 @@ EIGEN_DEVICE_FUNC inline void handmade_aligned_free(void* ptr) { std::size_t offset = static_cast(*(static_cast(ptr) - 1)) + 1; void* original = static_cast(static_cast(ptr) - offset); - check_that_malloc_is_allowed(); + check_that_free_is_allowed(); EIGEN_USING_STD(free) free(original); } @@ -227,7 +242,7 @@ EIGEN_DEVICE_FUNC inline void aligned_free(void* ptr) { #if (EIGEN_DEFAULT_ALIGN_BYTES == 0) || EIGEN_MALLOC_ALREADY_ALIGNED if (ptr != nullptr) { - check_that_malloc_is_allowed(); + check_that_free_is_allowed(); EIGEN_USING_STD(free) free(ptr); } @@ -299,7 +314,7 @@ EIGEN_DEVICE_FUNC inline void conditional_aligned_free(void* ptr) { template <> EIGEN_DEVICE_FUNC inline void conditional_aligned_free(void* ptr) { if (ptr != nullptr) { - check_that_malloc_is_allowed(); + check_that_free_is_allowed(); EIGEN_USING_STD(free) free(ptr); } -- GitLab