From 55b603e4573711ff33a293ed9706c33e92b834fc Mon Sep 17 00:00:00 2001 From: Benoit Jacob Date: Mon, 15 Dec 2008 20:14:59 +0000 Subject: [PATCH] * fix a bug I introduced in WithAlignedOperatorNew * add an important comment --- Eigen/src/Core/util/Memory.h | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/Eigen/src/Core/util/Memory.h b/Eigen/src/Core/util/Memory.h index ab2b87bc5..7dbc8dbd3 100644 --- a/Eigen/src/Core/util/Memory.h +++ b/Eigen/src/Core/util/Memory.h @@ -52,12 +52,21 @@ template struct ei_aligned_array T array[Size]; }; -/** \internal allocates \a size * sizeof(\a T) bytes with 16 bytes alignment */ +struct ei_byte_forcing_aligned_malloc +{ + unsigned char c; // sizeof must be 1. +}; + +template struct ei_force_aligned_malloc { enum { ret = 0 }; }; +template<> struct ei_force_aligned_malloc { enum { ret = 1 }; }; + +/** \internal allocates \a size * sizeof(\a T) bytes with 16 bytes alignment. + * */ template inline T* ei_aligned_malloc(size_t size) { #ifdef EIGEN_VECTORIZE - if(ei_packet_traits::size>1) + if(ei_packet_traits::size>1 || ei_force_aligned_malloc::ret) { #ifdef _MSC_VER return static_cast(_aligned_malloc(size*sizeof(T), 16)); @@ -71,7 +80,11 @@ inline T* ei_aligned_malloc(size_t size) } else #endif - return new T[size]; + return new T[size]; // here we really want a new, not a malloc. Justification: if the user uses Eigen on + // some fancy scalar type such as multiple-precision numbers, and this type has a custom operator new, + // then we want to honor this operator new! Anyway this type won't have vectorization so the vectorizing path + // is irrelevant here. Yes, we should say somewhere in the docs that if the user uses a custom scalar type then + // he can't have both vectorization and a custom operator new on his scalar type. } /** \internal free memory allocated with ei_aligned_malloc */ @@ -79,7 +92,7 @@ template inline void ei_aligned_free(T* ptr) { #ifdef EIGEN_VECTORIZE - if (ei_packet_traits::size>1) + if (ei_packet_traits::size>1 || ei_force_aligned_malloc::ret) #ifdef _MSC_VER _aligned_free(ptr); #else @@ -167,16 +180,16 @@ struct WithAlignedOperatorNew void *operator new(size_t size) throw() { - return ei_aligned_malloc(size); + return ei_aligned_malloc(size); } void *operator new[](size_t size) throw() { - return ei_aligned_malloc(size); + return ei_aligned_malloc(size); } - void operator delete(void * ptr) { ei_aligned_free(static_cast(ptr)); } - void operator delete[](void * ptr) { ei_aligned_free(static_cast(ptr)); } + void operator delete(void * ptr) { ei_aligned_free(static_cast(ptr)); } + void operator delete[](void * ptr) { ei_aligned_free(static_cast(ptr)); } #endif };