diff options
author | 2009-06-04 09:11:35 +0200 | |
---|---|---|
committer | 2009-06-04 09:11:35 +0200 | |
commit | 5f04f8eb6b934405c994c257dbe1e175fe0c9094 (patch) | |
tree | 24bb58c197dab23ac9d3734f688a3da7d26ada40 | |
parent | 6530c8f5b49a7c28f4bcae84a11e01645ff1c797 (diff) |
Fixes #9. Thanks to the (unknown) bug contributor.
-rw-r--r-- | Eigen/src/Core/util/Memory.h | 19 |
1 files changed, 14 insertions, 5 deletions
diff --git a/Eigen/src/Core/util/Memory.h b/Eigen/src/Core/util/Memory.h index c90da71ed..d1d52d88d 100644 --- a/Eigen/src/Core/util/Memory.h +++ b/Eigen/src/Core/util/Memory.h @@ -116,20 +116,29 @@ template<> inline void* ei_conditional_aligned_malloc<false>(size_t size) return result; } +/** \internal in-place allocate the elements of an array. + * The \a size parameter tells on how many objects to call the constructor of T. + */ +template<typename T> inline T* ei_alloc_elements_of_array(T *ptr, size_t size) +{ + for (size_t i=0; i < size; ++i) ::new (ptr + i) T; + return ptr; +} + /** allocates \a size objects of type T. The returned pointer is guaranteed to have 16 bytes alignment. * On allocation error, the returned pointer is undefined, but if exceptions are enabled then a std::bad_alloc is thrown. * The default constructor of T is called. */ template<typename T> inline T* ei_aligned_new(size_t size) { - void *void_result = ei_aligned_malloc(sizeof(T)*size); - return ::new(void_result) T[size]; + T *result = reinterpret_cast<T*>(ei_aligned_malloc(sizeof(T)*size)); + return ei_alloc_elements_of_array(result, size); } template<typename T, bool Align> inline T* ei_conditional_aligned_new(size_t size) { - void *void_result = ei_conditional_aligned_malloc<Align>(sizeof(T)*size); - return ::new(void_result) T[size]; + T *result = reinterpret_cast<T*>(ei_conditional_aligned_malloc<Align>(sizeof(T)*size)); + return ei_alloc_elements_of_array(result, size); } /** \internal free memory allocated with ei_aligned_malloc @@ -225,7 +234,7 @@ inline static int ei_alignmentOffset(const Scalar* ptr, int maxOffset) #define ei_aligned_stack_free(PTR,SIZE) ei_aligned_free(PTR) #endif -#define ei_aligned_stack_new(TYPE,SIZE) ::new(ei_aligned_stack_alloc(sizeof(TYPE)*SIZE)) TYPE[SIZE] +#define ei_aligned_stack_new(TYPE,SIZE) ei_alloc_elements_of_array(reinterpret_cast<TYPE*>(ei_aligned_stack_alloc(sizeof(TYPE)*SIZE)), SIZE) #define ei_aligned_stack_delete(TYPE,PTR,SIZE) do {ei_delete_elements_of_array<TYPE>(PTR, SIZE); \ ei_aligned_stack_free(PTR,sizeof(TYPE)*SIZE);} while(0) |