* introduce macros to replace inheritance for operator new overloading

(former solution still available and tested)
  This plays much better with classes that already have base classes --
  don't force the user to mess with multiple inheritance, which gave
  much trouble with MSVC.
* Expand the unaligned assert dox page
* Minor fixes in the lazy evaluation dox page
This commit is contained in:
Benoit Jacob
2009-01-06 03:16:50 +00:00
parent e71de20f16
commit 1c29d70312
12 changed files with 150 additions and 67 deletions

View File

@@ -135,31 +135,7 @@ class Matrix
public:
enum { NeedsToAlign = (Options&Matrix_AutoAlign) == Matrix_AutoAlign
&& SizeAtCompileTime!=Dynamic && ((sizeof(Scalar)*SizeAtCompileTime)%16)==0 };
typedef typename ei_meta_if<NeedsToAlign, ei_byte_forcing_aligned_malloc, char>::ret ByteAlignedAsNeeded;
void *operator new(size_t size) throw()
{
return ei_aligned_malloc<ByteAlignedAsNeeded>(size);
}
void *operator new(size_t, void *ptr) throw()
{
return ptr;
}
void *operator new[](size_t size) throw()
{
return ei_aligned_malloc<ByteAlignedAsNeeded>(size);
}
void *operator new[](size_t, void *ptr) throw()
{
return ptr;
}
void operator delete(void * ptr) { ei_aligned_free(static_cast<ByteAlignedAsNeeded *>(ptr), 0); }
void operator delete[](void * ptr) { ei_aligned_free(static_cast<ByteAlignedAsNeeded *>(ptr), 0); }
public:
EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign)
EIGEN_STRONG_INLINE int rows() const { return m_storage.rows(); }
EIGEN_STRONG_INLINE int cols() const { return m_storage.cols(); }

View File

@@ -144,6 +144,33 @@ inline static int ei_alignmentOffset(const Scalar* ptr, int maxOffset)
#define ei_aligned_stack_free(PTR,TYPE,SIZE) ei_aligned_free(PTR,SIZE)
#endif
#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF__INTERNAL(NeedsToAlign, TYPENAME) \
typedef TYPENAME Eigen::ei_meta_if<(NeedsToAlign), \
Eigen::ei_byte_forcing_aligned_malloc, \
char \
>::ret Eigen_ByteAlignedAsNeeded; \
void *operator new(size_t size) throw() { \
return ei_aligned_malloc<Eigen_ByteAlignedAsNeeded>(size); \
} \
void *operator new(size_t, void *ptr) throw() { \
return ptr; \
} \
void *operator new[](size_t size) throw() { \
return ei_aligned_malloc<Eigen_ByteAlignedAsNeeded>(size); \
} \
void *operator new[](size_t, void *ptr) throw() { \
return ptr; \
} \
void operator delete(void * ptr) { ei_aligned_free(static_cast<Eigen_ByteAlignedAsNeeded *>(ptr), 0); } \
void operator delete[](void * ptr) { ei_aligned_free(static_cast<Eigen_ByteAlignedAsNeeded *>(ptr), 0); }
#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW \
EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF__INTERNAL(true, )
#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign)\
EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF__INTERNAL(NeedsToAlign, typename)
#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE(Type,Size)\
EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(((Size)!=Eigen::Dynamic) && ((sizeof(Type)*(Size))%16==0))
/** \class WithAlignedOperatorNew
*
* \brief Enforces instances of inherited classes to be 16 bytes aligned when allocated with operator new
@@ -185,27 +212,9 @@ inline static int ei_alignmentOffset(const Scalar* ptr, int maxOffset)
*/
struct WithAlignedOperatorNew
{
void *operator new(size_t size) throw()
{
return ei_aligned_malloc<ei_byte_forcing_aligned_malloc>(size);
}
void *operator new[](size_t size) throw()
{
return ei_aligned_malloc<ei_byte_forcing_aligned_malloc>(size);
}
void operator delete(void * ptr) { ei_aligned_free(static_cast<ei_byte_forcing_aligned_malloc *>(ptr), 0); }
void operator delete[](void * ptr) { ei_aligned_free(static_cast<ei_byte_forcing_aligned_malloc *>(ptr), 0); }
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
};
template<typename T, int SizeAtCompileTime,
bool NeedsToAlign = (SizeAtCompileTime!=Dynamic) && ((sizeof(T)*SizeAtCompileTime)%16==0)>
struct ei_with_aligned_operator_new : public WithAlignedOperatorNew {};
template<typename T, int SizeAtCompileTime>
struct ei_with_aligned_operator_new<T,SizeAtCompileTime,false> {};
/** \class ei_new_allocator
*
* \brief stl compatible allocator to use with with fixed-size vector and matrix types