aboutsummaryrefslogtreecommitdiffhomepage
path: root/Eigen/src/Core/util/Memory.h
diff options
context:
space:
mode:
authorGravatar Benoit Jacob <jacob.benoit.1@gmail.com>2011-03-06 20:59:25 -0500
committerGravatar Benoit Jacob <jacob.benoit.1@gmail.com>2011-03-06 20:59:25 -0500
commitbfcad536e85cb189936eef3dd8e62597281ab32b (patch)
treea7e3d56cafbfeb652fda1f0d870967a097ac4398 /Eigen/src/Core/util/Memory.h
parentb464fc19bcaa50051fac345ea2f4ccdf52c3370e (diff)
* bug #206: correctly forward computationOptions and work towards avoiding mallocs after preallocation, with unit test.
* added EIGEN_RUNTIME_NO_MALLOC and new set_is_malloc_allowed() function to implement that test
Diffstat (limited to 'Eigen/src/Core/util/Memory.h')
-rw-r--r--Eigen/src/Core/util/Memory.h32
1 files changed, 26 insertions, 6 deletions
diff --git a/Eigen/src/Core/util/Memory.h b/Eigen/src/Core/util/Memory.h
index 8881cefad..8ccf0e603 100644
--- a/Eigen/src/Core/util/Memory.h
+++ b/Eigen/src/Core/util/Memory.h
@@ -167,14 +167,36 @@ inline void* generic_aligned_realloc(void* ptr, size_t size, size_t old_size)
*** Implementation of portable aligned versions of malloc/free/realloc ***
*****************************************************************************/
+#ifdef EIGEN_NO_MALLOC
+inline void check_that_malloc_is_allowed()
+{
+ eigen_assert(false && "heap allocation is forbidden (EIGEN_NO_MALLOC is defined)");
+}
+#elif defined EIGEN_RUNTIME_NO_MALLOC
+inline bool is_malloc_allowed_impl(bool update, bool new_value = false)
+{
+ static bool value = true;
+ if (update == 1)
+ value = new_value;
+ return value;
+}
+inline bool is_malloc_allowed() { return is_malloc_allowed_impl(false); }
+inline bool set_is_malloc_allowed(bool new_value) { return is_malloc_allowed_impl(true, new_value); }
+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)");
+}
+#else
+inline void check_that_malloc_is_allowed()
+{}
+#endif
+
/** \internal Allocates \a size bytes. The returned pointer is guaranteed to have 16 bytes alignment.
* On allocation error, the returned pointer is null, and if exceptions are enabled then a std::bad_alloc is thrown.
*/
inline void* aligned_malloc(size_t size)
{
- #ifdef EIGEN_NO_MALLOC
- eigen_assert(false && "heap allocation is forbidden (EIGEN_NO_MALLOC is defined)");
- #endif
+ check_that_malloc_is_allowed();
void *result;
#if !EIGEN_ALIGN
@@ -268,9 +290,7 @@ template<bool Align> inline void* conditional_aligned_malloc(size_t size)
template<> inline void* conditional_aligned_malloc<false>(size_t size)
{
- #ifdef EIGEN_NO_MALLOC
- eigen_assert(false && "heap allocation is forbidden (EIGEN_NO_MALLOC is defined)");
- #endif
+ check_that_malloc_is_allowed();
void *result = std::malloc(size);
#ifdef EIGEN_EXCEPTIONS